blob: dd1685106fac48376ba8edc9b6691c79bce5d76c [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 Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000117 * All user-defined global variables are stored in dictionary "globvardict".
118 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120static dict_T globvardict;
121static dictitem_T globvars_var;
122#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
124/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125 * Old Vim variables such as "v:version" are also available without the "v:".
126 * Also in functions. We need a special hashtable for them.
127 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000128static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200130/* When using exists() don't auto-load a script. */
131static int no_autoload = FALSE;
132
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134 * When recursively copying lists and dicts we need to remember which ones we
135 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000136 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 */
138static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139#define COPYID_INC 2
140#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141
142/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000143 * Array to hold the hashtab with variables local to each sourced script.
144 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000146typedef struct
147{
148 dictitem_T sv_var;
149 dict_T sv_dict;
150} scriptvar_T;
151
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200152static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
153#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
154#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156static int echo_attr = 0; /* attributes used for ":echo" */
157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000158/* Values for trans_function_name() argument: */
159#define TFN_INT 1 /* internal function name OK */
160#define TFN_QUIET 2 /* no error messages */
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
373/*
374 * The v: variables are stored in dictionary "vimvardict".
375 * "vimvars_var" is the variable that is used for the "l:" scope.
376 */
377static dict_T vimvardict;
378static dictitem_T vimvars_var;
379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
403static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
404static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
405static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
406static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
407static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
408static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
409static void item_lock __ARGS((typval_T *tv, int deep, int lock));
410static int tv_islocked __ARGS((typval_T *tv));
411
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
413static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000418static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
419static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000420
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000421static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000426static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static listitem_T *listitem_alloc __ARGS((void));
428static void listitem_free __ARGS((listitem_T *item));
429static void listitem_remove __ARGS((list_T *l, listitem_T *item));
430static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100431static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
432static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
433static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000435static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000438static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
440static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
441static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000442static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000444static char_u *list2string __ARGS((typval_T *tv, int copyID));
445static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000446static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000447static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
448static void set_ref_in_list __ARGS((list_T *l, int copyID));
449static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200450static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000451static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
453static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000454static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000456static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000458static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
459static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
462static int string2float __ARGS((char_u *text, float_T *value));
463#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
465static int find_internal_func __ARGS((char_u *name));
466static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
467static 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 +0200468static 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 +0000469static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000470static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000471
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000472#ifdef FEAT_FLOAT
473static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200474static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100477static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000478static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200483static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200485static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000486#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000487static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000498#ifdef FEAT_FLOAT
499static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
500#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000501static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000504static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000506#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000507static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000508static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
510#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000513#ifdef FEAT_FLOAT
514static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200515static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000516#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
520static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200531#ifdef FEAT_FLOAT
532static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
533#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000536static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#ifdef FEAT_FLOAT
543static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200545static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000547static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000556static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000558static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000564static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000572static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000573static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000574static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000575static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200578static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000579static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000587static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000601static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100606static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static 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 Moolenaard6e256c2011-12-14 15:32:50 +0100645static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000646static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000647#ifdef FEAT_FLOAT
648static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000651static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000652static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000654static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000655static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000667#ifdef FEAT_FLOAT
668static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
669#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000671static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000673static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000680static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000681static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000682static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000683static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200685static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000686static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000688static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000690#ifdef FEAT_FLOAT
691static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200692static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000695static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000696static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000699#ifdef FEAT_FLOAT
700static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
702#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000703static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200704static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705#ifdef HAVE_STRFTIME
706static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
707#endif
708static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200714static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200715static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000721static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200722static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000724static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000725static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000726static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000727static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000728static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000729static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000730static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200731#ifdef FEAT_FLOAT
732static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
734#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000738#ifdef FEAT_FLOAT
739static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
740#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200742static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200743static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000753static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000756static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100757static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000759static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000760static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static int get_env_len __ARGS((char_u **arg));
762static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000763static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000764static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
765#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
766#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
767 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000768static 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 +0000769static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000770static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
772static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static typval_T *alloc_tv __ARGS((void));
774static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static void init_tv __ARGS((typval_T *varp));
776static long get_tv_number __ARGS((typval_T *varp));
777static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000778static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static char_u *get_tv_string __ARGS((typval_T *varp));
780static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000781static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000783static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
785static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
786static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000787static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
788static 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 +0000789static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
790static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000791static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200792static int var_check_func_name __ARGS((char_u *name, int new_var));
793static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000794static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000795static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
797static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
798static int eval_fname_script __ARGS((char_u *p));
799static int eval_fname_sid __ARGS((char_u *p));
800static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static ufunc_T *find_func __ARGS((char_u *name));
802static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000803static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804#ifdef FEAT_PROFILE
805static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000806static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
807static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
808static int
809# ifdef __BORLANDC__
810 _RTLENTRYF
811# endif
812 prof_total_cmp __ARGS((const void *s1, const void *s2));
813static int
814# ifdef __BORLANDC__
815 _RTLENTRYF
816# endif
817 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000819static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000820static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000821static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void func_free __ARGS((ufunc_T *fp));
823static void func_unref __ARGS((char_u *name));
824static void func_ref __ARGS((char_u *name));
825static 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 +0000826static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
827static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
830static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000831static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000832static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200835
836#ifdef EBCDIC
837static int compare_func_name __ARGS((const void *s1, const void *s2));
838static void sortFunctions __ARGS(());
839#endif
840
841
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000842/* Character used as separated in autoload function/variable names. */
843#define AUTOLOAD_CHAR '#'
844
Bram Moolenaar33570922005-01-25 22:26:29 +0000845/*
846 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000847 */
848 void
849eval_init()
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 int i;
852 struct vimvar *p;
853
854 init_var_dict(&globvardict, &globvars_var);
855 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200856 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000857 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000858 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000859
860 for (i = 0; i < VV_LEN; ++i)
861 {
862 p = &vimvars[i];
863 STRCPY(p->vv_di.di_key, p->vv_name);
864 if (p->vv_flags & VV_RO)
865 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
866 else if (p->vv_flags & VV_RO_SBX)
867 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
868 else
869 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000870
871 /* add to v: scope dict, unless the value is not always available */
872 if (p->vv_type != VAR_UNKNOWN)
873 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 /* add to compat scope dict */
876 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000878 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100882 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200914 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915
916 /* global variables */
917 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000918
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000919 /* autoloaded script names */
920 ga_clear_strings(&ga_loaded);
921
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200922 /* script-local variables */
923 for (i = 1; i <= ga_scripts.ga_len; ++i)
924 {
925 vars_clear(&SCRIPT_VARS(i));
926 vim_free(SCRIPT_SV(i));
927 }
928 ga_clear(&ga_scripts);
929
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000930 /* unreferenced lists and dicts */
931 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000932
933 /* functions */
934 free_all_functions();
935 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000936}
937#endif
938
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * Return the name of the executed function.
941 */
942 char_u *
943func_name(cookie)
944 void *cookie;
945{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000946 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947}
948
949/*
950 * Return the address holding the next breakpoint line for a funccall cookie.
951 */
952 linenr_T *
953func_breakpoint(cookie)
954 void *cookie;
955{
Bram Moolenaar33570922005-01-25 22:26:29 +0000956 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959/*
960 * Return the address holding the debug tick for a funccall cookie.
961 */
962 int *
963func_dbg_tick(cookie)
964 void *cookie;
965{
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
969/*
970 * Return the nesting level for a funccall cookie.
971 */
972 int
973func_level(cookie)
974 void *cookie;
975{
Bram Moolenaar33570922005-01-25 22:26:29 +0000976 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000980funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000982/* pointer to list of previously used funccal, still around because some
983 * item in it is still being used. */
984funccall_T *previous_funccal = NULL;
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986/*
987 * Return TRUE when a function was ended by a ":return" command.
988 */
989 int
990current_func_returned()
991{
992 return current_funccal->returned;
993}
994
995
996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 * Set an internal variable to a string value. Creates the variable if it does
998 * not already exist.
999 */
1000 void
1001set_internal_string_var(name, value)
1002 char_u *name;
1003 char_u *value;
1004{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001006 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008 val = vim_strsave(value);
1009 if (val != NULL)
1010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001012 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001015 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 }
1018}
1019
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001021static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001022static char_u *redir_endp = NULL;
1023static char_u *redir_varname = NULL;
1024
1025/*
1026 * Start recording command output to a variable
1027 * Returns OK if successfully completed the setup. FAIL otherwise.
1028 */
1029 int
1030var_redir_start(name, append)
1031 char_u *name;
1032 int append; /* append to an existing variable */
1033{
1034 int save_emsg;
1035 int err;
1036 typval_T tv;
1037
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001038 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001039 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 {
1041 EMSG(_(e_invarg));
1042 return FAIL;
1043 }
1044
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001045 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 redir_varname = vim_strsave(name);
1047 if (redir_varname == NULL)
1048 return FAIL;
1049
1050 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1051 if (redir_lval == NULL)
1052 {
1053 var_redir_stop();
1054 return FAIL;
1055 }
1056
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001057 /* The output is stored in growarray "redir_ga" until redirection ends. */
1058 ga_init2(&redir_ga, (int)sizeof(char), 500);
1059
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001061 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1062 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1064 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001065 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp != NULL && *redir_endp != NUL)
1067 /* Trailing characters are present after the variable name */
1068 EMSG(_(e_trailing));
1069 else
1070 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001071 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 var_redir_stop();
1073 return FAIL;
1074 }
1075
1076 /* check if we can write to the variable: set it to or append an empty
1077 * string */
1078 save_emsg = did_emsg;
1079 did_emsg = FALSE;
1080 tv.v_type = VAR_STRING;
1081 tv.vval.v_string = (char_u *)"";
1082 if (append)
1083 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1084 else
1085 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001086 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001088 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 if (err)
1090 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001091 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 var_redir_stop();
1093 return FAIL;
1094 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095
1096 return OK;
1097}
1098
1099/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100 * Append "value[value_len]" to the variable set by var_redir_start().
1101 * The actual appending is postponed until redirection ends, because the value
1102 * appended may in fact be the string we write to, changing it may cause freed
1103 * memory to be used:
1104 * :redir => foo
1105 * :let foo
1106 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 */
1108 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001111 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114
1115 if (redir_lval == NULL)
1116 return;
1117
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001121 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001123 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 {
1125 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 }
1128 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130}
1131
1132/*
1133 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 */
1136 void
1137var_redir_stop()
1138{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001139 typval_T tv;
1140
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 if (redir_lval != NULL)
1142 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 /* If there was no error: assign the text to the variable. */
1144 if (redir_endp != NULL)
1145 {
1146 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1147 tv.v_type = VAR_STRING;
1148 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001149 /* Call get_lval() again, if it's inside a Dict or List it may
1150 * have changed. */
1151 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1152 FALSE, FALSE, FALSE, FNE_CHECK_START);
1153 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1154 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1155 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 /* free the collected output */
1159 vim_free(redir_ga.ga_data);
1160 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001161
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 vim_free(redir_lval);
1163 redir_lval = NULL;
1164 }
1165 vim_free(redir_varname);
1166 redir_varname = NULL;
1167}
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169# if defined(FEAT_MBYTE) || defined(PROTO)
1170 int
1171eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1172 char_u *enc_from;
1173 char_u *enc_to;
1174 char_u *fname_from;
1175 char_u *fname_to;
1176{
1177 int err = FALSE;
1178
1179 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1180 set_vim_var_string(VV_CC_TO, enc_to, -1);
1181 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1182 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1183 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1184 err = TRUE;
1185 set_vim_var_string(VV_CC_FROM, NULL, -1);
1186 set_vim_var_string(VV_CC_TO, NULL, -1);
1187 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1188 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1189
1190 if (err)
1191 return FAIL;
1192 return OK;
1193}
1194# endif
1195
1196# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1197 int
1198eval_printexpr(fname, args)
1199 char_u *fname;
1200 char_u *args;
1201{
1202 int err = FALSE;
1203
1204 set_vim_var_string(VV_FNAME_IN, fname, -1);
1205 set_vim_var_string(VV_CMDARG, args, -1);
1206 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1207 err = TRUE;
1208 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1209 set_vim_var_string(VV_CMDARG, NULL, -1);
1210
1211 if (err)
1212 {
1213 mch_remove(fname);
1214 return FAIL;
1215 }
1216 return OK;
1217}
1218# endif
1219
1220# if defined(FEAT_DIFF) || defined(PROTO)
1221 void
1222eval_diff(origfile, newfile, outfile)
1223 char_u *origfile;
1224 char_u *newfile;
1225 char_u *outfile;
1226{
1227 int err = FALSE;
1228
1229 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1230 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1231 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1232 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1233 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1234 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1235 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1236}
1237
1238 void
1239eval_patch(origfile, difffile, outfile)
1240 char_u *origfile;
1241 char_u *difffile;
1242 char_u *outfile;
1243{
1244 int err;
1245
1246 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1248 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1249 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1250 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1251 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1252 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1253}
1254# endif
1255
1256/*
1257 * Top level evaluation function, returning a boolean.
1258 * Sets "error" to TRUE if there was an error.
1259 * Return TRUE or FALSE.
1260 */
1261 int
1262eval_to_bool(arg, error, nextcmd, skip)
1263 char_u *arg;
1264 int *error;
1265 char_u **nextcmd;
1266 int skip; /* only parse, don't execute */
1267{
Bram Moolenaar33570922005-01-25 22:26:29 +00001268 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 int retval = FALSE;
1270
1271 if (skip)
1272 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 else
1276 {
1277 *error = FALSE;
1278 if (!skip)
1279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001280 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001281 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 }
1284 if (skip)
1285 --emsg_skip;
1286
1287 return retval;
1288}
1289
1290/*
1291 * Top level evaluation function, returning a string. If "skip" is TRUE,
1292 * only parsing to "nextcmd" is done, without reporting errors. Return
1293 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1294 */
1295 char_u *
1296eval_to_string_skip(arg, nextcmd, skip)
1297 char_u *arg;
1298 char_u **nextcmd;
1299 int skip; /* only parse, don't execute */
1300{
Bram Moolenaar33570922005-01-25 22:26:29 +00001301 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 char_u *retval;
1303
1304 if (skip)
1305 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 retval = NULL;
1308 else
1309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001310 retval = vim_strsave(get_tv_string(&tv));
1311 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 if (skip)
1314 --emsg_skip;
1315
1316 return retval;
1317}
1318
1319/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001320 * Skip over an expression at "*pp".
1321 * Return FAIL for an error, OK otherwise.
1322 */
1323 int
1324skip_expr(pp)
1325 char_u **pp;
1326{
Bram Moolenaar33570922005-01-25 22:26:29 +00001327 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001328
1329 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331}
1332
1333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001335 * When "convert" is TRUE convert a List into a sequence of lines and convert
1336 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Return pointer to allocated memory, or NULL for failure.
1338 */
1339 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 char_u *arg;
1342 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
Bram Moolenaar33570922005-01-25 22:26:29 +00001345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001347 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001348#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 retval = NULL;
1354 else
1355 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001357 {
1358 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001359 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001360 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001361 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001362 if (tv.vval.v_list->lv_len > 0)
1363 ga_append(&ga, NL);
1364 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001365 ga_append(&ga, NUL);
1366 retval = (char_u *)ga.ga_data;
1367 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001368#ifdef FEAT_FLOAT
1369 else if (convert && tv.v_type == VAR_FLOAT)
1370 {
1371 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1372 retval = vim_strsave(numbuf);
1373 }
1374#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001375 else
1376 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379
1380 return retval;
1381}
1382
1383/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384 * Call eval_to_string() without using current local variables and using
1385 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 */
1387 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 char_u *arg;
1390 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393 char_u *retval;
1394 void *save_funccalp;
1395
1396 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 if (use_sandbox)
1398 ++sandbox;
1399 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001400 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001401 if (use_sandbox)
1402 --sandbox;
1403 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 restore_funccal(save_funccalp);
1405 return retval;
1406}
1407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408/*
1409 * Top level evaluation function, returning a number.
1410 * Evaluates "expr" silently.
1411 * Returns -1 for an error.
1412 */
1413 int
1414eval_to_number(expr)
1415 char_u *expr;
1416{
Bram Moolenaar33570922005-01-25 22:26:29 +00001417 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001419 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421 ++emsg_off;
1422
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 retval = -1;
1425 else
1426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001427 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 }
1430 --emsg_off;
1431
1432 return retval;
1433}
1434
Bram Moolenaara40058a2005-07-11 22:42:07 +00001435/*
1436 * Prepare v: variable "idx" to be used.
1437 * Save the current typeval in "save_tv".
1438 * When not used yet add the variable to the v: hashtable.
1439 */
1440 static void
1441prepare_vimvar(idx, save_tv)
1442 int idx;
1443 typval_T *save_tv;
1444{
1445 *save_tv = vimvars[idx].vv_tv;
1446 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1447 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1448}
1449
1450/*
1451 * Restore v: variable "idx" to typeval "save_tv".
1452 * When no longer defined, remove the variable from the v: hashtable.
1453 */
1454 static void
1455restore_vimvar(idx, save_tv)
1456 int idx;
1457 typval_T *save_tv;
1458{
1459 hashitem_T *hi;
1460
Bram Moolenaara40058a2005-07-11 22:42:07 +00001461 vimvars[idx].vv_tv = *save_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 {
1464 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1465 if (HASHITEM_EMPTY(hi))
1466 EMSG2(_(e_intern2), "restore_vimvar()");
1467 else
1468 hash_remove(&vimvarht, hi);
1469 }
1470}
1471
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001472#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001473/*
1474 * Evaluate an expression to a list with suggestions.
1475 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001476 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001477 */
1478 list_T *
1479eval_spell_expr(badword, expr)
1480 char_u *badword;
1481 char_u *expr;
1482{
1483 typval_T save_val;
1484 typval_T rettv;
1485 list_T *list = NULL;
1486 char_u *p = skipwhite(expr);
1487
1488 /* Set "v:val" to the bad word. */
1489 prepare_vimvar(VV_VAL, &save_val);
1490 vimvars[VV_VAL].vv_type = VAR_STRING;
1491 vimvars[VV_VAL].vv_str = badword;
1492 if (p_verbose == 0)
1493 ++emsg_off;
1494
1495 if (eval1(&p, &rettv, TRUE) == OK)
1496 {
1497 if (rettv.v_type != VAR_LIST)
1498 clear_tv(&rettv);
1499 else
1500 list = rettv.vval.v_list;
1501 }
1502
1503 if (p_verbose == 0)
1504 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001505 restore_vimvar(VV_VAL, &save_val);
1506
1507 return list;
1508}
1509
1510/*
1511 * "list" is supposed to contain two items: a word and a number. Return the
1512 * word in "pp" and the number as the return value.
1513 * Return -1 if anything isn't right.
1514 * Used to get the good word and score from the eval_spell_expr() result.
1515 */
1516 int
1517get_spellword(list, pp)
1518 list_T *list;
1519 char_u **pp;
1520{
1521 listitem_T *li;
1522
1523 li = list->lv_first;
1524 if (li == NULL)
1525 return -1;
1526 *pp = get_tv_string(&li->li_tv);
1527
1528 li = li->li_next;
1529 if (li == NULL)
1530 return -1;
1531 return get_tv_number(&li->li_tv);
1532}
1533#endif
1534
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001535/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001536 * Top level evaluation function.
1537 * Returns an allocated typval_T with the result.
1538 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001539 */
1540 typval_T *
1541eval_expr(arg, nextcmd)
1542 char_u *arg;
1543 char_u **nextcmd;
1544{
1545 typval_T *tv;
1546
1547 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001548 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001549 {
1550 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001552 }
1553
1554 return tv;
1555}
1556
1557
Bram Moolenaar4f688582007-07-24 12:34:30 +00001558#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1559 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001562 * Uses argv[argc] for the function arguments. Only Number and String
1563 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001566 int
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 char_u *func;
1569 int argc;
1570 char_u **argv;
1571 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
Bram Moolenaar33570922005-01-25 22:26:29 +00001574 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 long n;
1576 int len;
1577 int i;
1578 int doesrange;
1579 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001582 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
1586 for (i = 0; i < argc; i++)
1587 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 /* Pass a NULL or empty argument as an empty string */
1589 if (argv[i] == NULL || *argv[i] == NUL)
1590 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001591 argvars[i].v_type = VAR_STRING;
1592 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001593 continue;
1594 }
1595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 /* Recognize a number argument, the others must be strings. */
1597 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1598 if (len != 0 && len == (int)STRLEN(argv[i]))
1599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001600 argvars[i].v_type = VAR_NUMBER;
1601 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
1603 else
1604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001605 argvars[i].v_type = VAR_STRING;
1606 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 }
1609
1610 if (safe)
1611 {
1612 save_funccalp = save_funccal();
1613 ++sandbox;
1614 }
1615
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001616 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1617 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 if (safe)
1621 {
1622 --sandbox;
1623 restore_funccal(save_funccalp);
1624 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001625 vim_free(argvars);
1626
1627 if (ret == FAIL)
1628 clear_tv(rettv);
1629
1630 return ret;
1631}
1632
Bram Moolenaar4f688582007-07-24 12:34:30 +00001633# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001635 * Call vimL function "func" and return the result as a string.
1636 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 * Uses argv[argc] for the function arguments.
1638 */
1639 void *
1640call_func_retstr(func, argc, argv, safe)
1641 char_u *func;
1642 int argc;
1643 char_u **argv;
1644 int safe; /* use the sandbox */
1645{
1646 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001647 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648
1649 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1650 return NULL;
1651
1652 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 return retval;
1655}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001656# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657
Bram Moolenaar4f688582007-07-24 12:34:30 +00001658# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001660 * Call vimL function "func" and return the result as a number.
1661 * Returns -1 when calling the function fails.
1662 * Uses argv[argc] for the function arguments.
1663 */
1664 long
1665call_func_retnr(func, argc, argv, safe)
1666 char_u *func;
1667 int argc;
1668 char_u **argv;
1669 int safe; /* use the sandbox */
1670{
1671 typval_T rettv;
1672 long retval;
1673
1674 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1675 return -1;
1676
1677 retval = get_tv_number_chk(&rettv, NULL);
1678 clear_tv(&rettv);
1679 return retval;
1680}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001681# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001682
1683/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001684 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001686 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687 */
1688 void *
1689call_func_retlist(func, argc, argv, safe)
1690 char_u *func;
1691 int argc;
1692 char_u **argv;
1693 int safe; /* use the sandbox */
1694{
1695 typval_T rettv;
1696
1697 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1698 return NULL;
1699
1700 if (rettv.v_type != VAR_LIST)
1701 {
1702 clear_tv(&rettv);
1703 return NULL;
1704 }
1705
1706 return rettv.vval.v_list;
1707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
1709
Bram Moolenaar4f688582007-07-24 12:34:30 +00001710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711/*
1712 * Save the current function call pointer, and set it to NULL.
1713 * Used when executing autocommands and for ":source".
1714 */
1715 void *
1716save_funccal()
1717{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001718 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 current_funccal = NULL;
1721 return (void *)fc;
1722}
1723
1724 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001725restore_funccal(vfc)
1726 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728 funccall_T *fc = (funccall_T *)vfc;
1729
1730 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731}
1732
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733#if defined(FEAT_PROFILE) || defined(PROTO)
1734/*
1735 * Prepare profiling for entering a child or something else that is not
1736 * counted for the script/function itself.
1737 * Should always be called in pair with prof_child_exit().
1738 */
1739 void
1740prof_child_enter(tm)
1741 proftime_T *tm; /* place to store waittime */
1742{
1743 funccall_T *fc = current_funccal;
1744
1745 if (fc != NULL && fc->func->uf_profiling)
1746 profile_start(&fc->prof_child);
1747 script_prof_save(tm);
1748}
1749
1750/*
1751 * Take care of time spent in a child.
1752 * Should always be called after prof_child_enter().
1753 */
1754 void
1755prof_child_exit(tm)
1756 proftime_T *tm; /* where waittime was stored */
1757{
1758 funccall_T *fc = current_funccal;
1759
1760 if (fc != NULL && fc->func->uf_profiling)
1761 {
1762 profile_end(&fc->prof_child);
1763 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1764 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1765 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1766 }
1767 script_prof_restore(tm);
1768}
1769#endif
1770
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#ifdef FEAT_FOLDING
1773/*
1774 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1775 * it in "*cp". Doesn't give error messages.
1776 */
1777 int
1778eval_foldexpr(arg, cp)
1779 char_u *arg;
1780 int *cp;
1781{
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 int retval;
1784 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001785 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1786 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
1788 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001789 if (use_sandbox)
1790 ++sandbox;
1791 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 retval = 0;
1795 else
1796 {
1797 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 if (tv.v_type == VAR_NUMBER)
1799 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001800 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 retval = 0;
1802 else
1803 {
1804 /* If the result is a string, check if there is a non-digit before
1805 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (!VIM_ISDIGIT(*s) && *s != '-')
1808 *cp = *s++;
1809 retval = atol((char *)s);
1810 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
1813 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001814 if (use_sandbox)
1815 --sandbox;
1816 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 return retval;
1819}
1820#endif
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 * ":let" list all variable values
1824 * ":let var1 var2" list variable values
1825 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 * ":let var += expr" assignment command.
1827 * ":let var -= expr" assignment command.
1828 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 */
1831 void
1832ex_let(eap)
1833 exarg_T *eap;
1834{
1835 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 int var_count = 0;
1840 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001841 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001842 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001843 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
Bram Moolenaardb552d602006-03-23 22:59:57 +00001845 argend = skip_var_list(arg, &var_count, &semicolon);
1846 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001848 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1849 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001850 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001853 /*
1854 * ":let" without "=": list variables
1855 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 if (*arg == '[')
1857 EMSG(_(e_invarg));
1858 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_glob_vars(&first);
1865 list_buf_vars(&first);
1866 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001867#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001869#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 list_script_vars(&first);
1871 list_func_vars(&first);
1872 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 eap->nextcmd = check_nextcmd(arg);
1875 }
1876 else
1877 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001878 op[0] = '=';
1879 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001880 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 {
1882 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1883 op[0] = expr[-1]; /* +=, -= or .= */
1884 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (eap->skip)
1888 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (eap->skip)
1891 {
1892 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 --emsg_skip;
1895 }
1896 else if (i != FAIL)
1897 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
1902 }
1903}
1904
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905/*
1906 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1907 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 * When "nextchars" is not NULL it points to a string with characters that
1909 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1910 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 * Returns OK or FAIL;
1912 */
1913 static int
1914ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1915 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001916 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 int copy; /* copy values from "tv", don't move */
1918 int semicolon; /* from skip_var_list() */
1919 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921{
1922 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001925 listitem_T *item;
1926 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927
1928 if (*arg != '[')
1929 {
1930 /*
1931 * ":let var = expr" or ":for var in list"
1932 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 return FAIL;
1935 return OK;
1936 }
1937
1938 /*
1939 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1940 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001941 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 {
1943 EMSG(_(e_listreq));
1944 return FAIL;
1945 }
1946
1947 i = list_len(l);
1948 if (semicolon == 0 && var_count < i)
1949 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001950 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 }
1953 if (var_count - semicolon > i)
1954 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001955 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 return FAIL;
1957 }
1958
1959 item = l->lv_first;
1960 while (*arg != ']')
1961 {
1962 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 item = item->li_next;
1965 if (arg == NULL)
1966 return FAIL;
1967
1968 arg = skipwhite(arg);
1969 if (*arg == ';')
1970 {
1971 /* Put the rest of the list (may be empty) in the var after ';'.
1972 * Create a new list for this. */
1973 l = list_alloc();
1974 if (l == NULL)
1975 return FAIL;
1976 while (item != NULL)
1977 {
1978 list_append_tv(l, &item->li_tv);
1979 item = item->li_next;
1980 }
1981
1982 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001983 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 ltv.vval.v_list = l;
1985 l->lv_refcount = 1;
1986
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1988 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 clear_tv(&ltv);
1990 if (arg == NULL)
1991 return FAIL;
1992 break;
1993 }
1994 else if (*arg != ',' && *arg != ']')
1995 {
1996 EMSG2(_(e_intern2), "ex_let_vars()");
1997 return FAIL;
1998 }
1999 }
2000
2001 return OK;
2002}
2003
2004/*
2005 * Skip over assignable variable "var" or list of variables "[var, var]".
2006 * Used for ":let varvar = expr" and ":for varvar in expr".
2007 * For "[var, var]" increment "*var_count" for each variable.
2008 * for "[var, var; var]" set "semicolon".
2009 * Return NULL for an error.
2010 */
2011 static char_u *
2012skip_var_list(arg, var_count, semicolon)
2013 char_u *arg;
2014 int *var_count;
2015 int *semicolon;
2016{
2017 char_u *p, *s;
2018
2019 if (*arg == '[')
2020 {
2021 /* "[var, var]": find the matching ']'. */
2022 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002023 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 {
2025 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2026 s = skip_var_one(p);
2027 if (s == p)
2028 {
2029 EMSG2(_(e_invarg2), p);
2030 return NULL;
2031 }
2032 ++*var_count;
2033
2034 p = skipwhite(s);
2035 if (*p == ']')
2036 break;
2037 else if (*p == ';')
2038 {
2039 if (*semicolon == 1)
2040 {
2041 EMSG(_("Double ; in list of variables"));
2042 return NULL;
2043 }
2044 *semicolon = 1;
2045 }
2046 else if (*p != ',')
2047 {
2048 EMSG2(_(e_invarg2), p);
2049 return NULL;
2050 }
2051 }
2052 return p + 1;
2053 }
2054 else
2055 return skip_var_one(arg);
2056}
2057
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002059 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002060 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002061 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 static char_u *
2063skip_var_one(arg)
2064 char_u *arg;
2065{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002066 if (*arg == '@' && arg[1] != NUL)
2067 return arg + 2;
2068 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2069 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070}
2071
Bram Moolenaara7043832005-01-21 11:56:39 +00002072/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 * List variables for hashtab "ht" with prefix "prefix".
2074 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002080 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002082{
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 hashitem_T *hi;
2084 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 int todo;
2086
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002087 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002088 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2089 {
2090 if (!HASHITEM_EMPTY(hi))
2091 {
2092 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002093 di = HI2DI(hi);
2094 if (empty || di->di_tv.v_type != VAR_STRING
2095 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002096 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002097 }
2098 }
2099}
2100
2101/*
2102 * List global variables.
2103 */
2104 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105list_glob_vars(first)
2106 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002107{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002108 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002109}
2110
2111/*
2112 * List buffer variables.
2113 */
2114 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115list_buf_vars(first)
2116 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118 char_u numbuf[NUMBUFLEN];
2119
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2121 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122
2123 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2125 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002126}
2127
2128/*
2129 * List window variables.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_win_vars(first)
2133 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002134{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2136 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002137}
2138
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002139#ifdef FEAT_WINDOWS
2140/*
2141 * List tab page variables.
2142 */
2143 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144list_tab_vars(first)
2145 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002146{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2148 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002149}
2150#endif
2151
Bram Moolenaara7043832005-01-21 11:56:39 +00002152/*
2153 * List Vim variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_vim_vars(first)
2157 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160}
2161
2162/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002163 * List script-local variables, if there is a script.
2164 */
2165 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166list_script_vars(first)
2167 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168{
2169 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2171 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002172}
2173
2174/*
2175 * List function variables, if there is a function.
2176 */
2177 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178list_func_vars(first)
2179 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180{
2181 if (current_funccal != NULL)
2182 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002184}
2185
2186/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 * List variables in "arg".
2188 */
2189 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 exarg_T *eap;
2192 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194{
2195 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 char_u *name_start;
2199 char_u *arg_subsc;
2200 char_u *tofree;
2201 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202
2203 while (!ends_excmd(*arg) && !got_int)
2204 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002205 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002207 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002208 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2209 {
2210 emsg_severe = TRUE;
2211 EMSG(_(e_trailing));
2212 break;
2213 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 /* get_name_len() takes care of expanding curly braces */
2218 name_start = name = arg;
2219 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2220 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 /* This is mainly to keep test 49 working: when expanding
2223 * curly braces fails overrule the exception error message. */
2224 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 emsg_severe = TRUE;
2227 EMSG2(_(e_invarg2), arg);
2228 break;
2229 }
2230 error = TRUE;
2231 }
2232 else
2233 {
2234 if (tofree != NULL)
2235 name = tofree;
2236 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 else
2239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 /* handle d.key, l[idx], f(expr) */
2241 arg_subsc = arg;
2242 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 'g': list_glob_vars(first); break;
2251 case 'b': list_buf_vars(first); break;
2252 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002253#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002254 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002256 case 'v': list_vim_vars(first); break;
2257 case 's': list_script_vars(first); break;
2258 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 default:
2260 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 }
2263 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 {
2265 char_u numbuf[NUMBUFLEN];
2266 char_u *tf;
2267 int c;
2268 char_u *s;
2269
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002270 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 c = *arg;
2272 *arg = NUL;
2273 list_one_var_a((char_u *)"",
2274 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002275 tv.v_type,
2276 s == NULL ? (char_u *)"" : s,
2277 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 *arg = c;
2279 vim_free(tf);
2280 }
2281 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
2284 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285
2286 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288
2289 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 }
2291
2292 return arg;
2293}
2294
2295/*
2296 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2297 * Returns a pointer to the char just after the var name.
2298 * Returns NULL if there is an error.
2299 */
2300 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002303 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002305 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307{
2308 int c1;
2309 char_u *name;
2310 char_u *p;
2311 char_u *arg_end = NULL;
2312 int len;
2313 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315
2316 /*
2317 * ":let $VAR = expr": Set environment variable.
2318 */
2319 if (*arg == '$')
2320 {
2321 /* Find the end of the name. */
2322 ++arg;
2323 name = arg;
2324 len = get_env_len(&arg);
2325 if (len == 0)
2326 EMSG2(_(e_invarg2), name - 1);
2327 else
2328 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 if (op != NULL && (*op == '+' || *op == '-'))
2330 EMSG2(_(e_letwrong), op);
2331 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002332 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002334 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 {
2336 c1 = name[len];
2337 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 p = get_tv_string_chk(tv);
2339 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002340 {
2341 int mustfree = FALSE;
2342 char_u *s = vim_getenv(name, &mustfree);
2343
2344 if (s != NULL)
2345 {
2346 p = tofree = concat_str(s, p);
2347 if (mustfree)
2348 vim_free(s);
2349 }
2350 }
2351 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 if (STRICMP(name, "HOME") == 0)
2355 init_homedir();
2356 else if (didset_vim && STRICMP(name, "VIM") == 0)
2357 didset_vim = FALSE;
2358 else if (didset_vimruntime
2359 && STRICMP(name, "VIMRUNTIME") == 0)
2360 didset_vimruntime = FALSE;
2361 arg_end = arg;
2362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 }
2366 }
2367 }
2368
2369 /*
2370 * ":let &option = expr": Set option value.
2371 * ":let &l:option = expr": Set local option value.
2372 * ":let &g:option = expr": Set global option value.
2373 */
2374 else if (*arg == '&')
2375 {
2376 /* Find the end of the name. */
2377 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002378 if (p == NULL || (endchars != NULL
2379 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 EMSG(_(e_letunexp));
2381 else
2382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 long n;
2384 int opt_type;
2385 long numval;
2386 char_u *stringval = NULL;
2387 char_u *s;
2388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 c1 = *p;
2390 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391
2392 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 s = get_tv_string_chk(tv); /* != NULL if number or string */
2394 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 {
2396 opt_type = get_option_value(arg, &numval,
2397 &stringval, opt_flags);
2398 if ((opt_type == 1 && *op == '.')
2399 || (opt_type == 0 && *op != '.'))
2400 EMSG2(_(e_letwrong), op);
2401 else
2402 {
2403 if (opt_type == 1) /* number */
2404 {
2405 if (*op == '+')
2406 n = numval + n;
2407 else
2408 n = numval - n;
2409 }
2410 else if (opt_type == 0 && stringval != NULL) /* string */
2411 {
2412 s = concat_str(stringval, s);
2413 vim_free(stringval);
2414 stringval = s;
2415 }
2416 }
2417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418 if (s != NULL)
2419 {
2420 set_option_value(arg, n, s, opt_flags);
2421 arg_end = p;
2422 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 }
2426 }
2427
2428 /*
2429 * ":let @r = expr": Set register contents.
2430 */
2431 else if (*arg == '@')
2432 {
2433 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 if (op != NULL && (*op == '+' || *op == '-'))
2435 EMSG2(_(e_letwrong), op);
2436 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002437 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 EMSG(_(e_letunexp));
2439 else
2440 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002441 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 char_u *s;
2443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 p = get_tv_string_chk(tv);
2445 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002447 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (s != NULL)
2449 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002450 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 vim_free(s);
2452 }
2453 }
2454 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 arg_end = arg + 1;
2458 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002459 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 }
2461 }
2462
2463 /*
2464 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002467 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002469 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002471 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2475 EMSG(_(e_letunexp));
2476 else
2477 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 arg_end = p;
2480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
2484
2485 else
2486 EMSG2(_(e_invarg2), arg);
2487
2488 return arg_end;
2489}
2490
2491/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2493 */
2494 static int
2495check_changedtick(arg)
2496 char_u *arg;
2497{
2498 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2499 {
2500 EMSG2(_(e_readonlyvar), arg);
2501 return TRUE;
2502 }
2503 return FALSE;
2504}
2505
2506/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 * Get an lval: variable, Dict item or List item that can be assigned a value
2508 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2509 * "name.key", "name.key[expr]" etc.
2510 * Indexing only works if "name" is an existing List or Dictionary.
2511 * "name" points to the start of the name.
2512 * If "rettv" is not NULL it points to the value to be assigned.
2513 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2514 * wrong; must end in space or cmd separator.
2515 *
2516 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002517 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 */
2520 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002521get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 typval_T *rettv;
2524 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 int unlet;
2526 int skip;
2527 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002528 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 char_u *expr_start, *expr_end;
2532 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 dictitem_T *v;
2534 typval_T var1;
2535 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002540 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002543 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544
2545 if (skip)
2546 {
2547 /* When skipping just find the end of the name. */
2548 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 }
2551
2552 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (expr_start != NULL)
2555 {
2556 /* Don't expand the name when we already know there is an error. */
2557 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2558 && *p != '[' && *p != '.')
2559 {
2560 EMSG(_(e_trailing));
2561 return NULL;
2562 }
2563
2564 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2565 if (lp->ll_exp_name == NULL)
2566 {
2567 /* Report an invalid expression in braces, unless the
2568 * expression evaluation has been cancelled due to an
2569 * aborting error, an interrupt, or an exception. */
2570 if (!aborting() && !quiet)
2571 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002572 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 EMSG2(_(e_invarg2), name);
2574 return NULL;
2575 }
2576 }
2577 lp->ll_name = lp->ll_exp_name;
2578 }
2579 else
2580 lp->ll_name = name;
2581
2582 /* Without [idx] or .key we are done. */
2583 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2584 return p;
2585
2586 cc = *p;
2587 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002588 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (v == NULL && !quiet)
2590 EMSG2(_(e_undefvar), lp->ll_name);
2591 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 if (v == NULL)
2593 return NULL;
2594
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 /*
2596 * Loop until no more [idx] or .key is following.
2597 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2602 && !(lp->ll_tv->v_type == VAR_DICT
2603 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!quiet)
2606 EMSG(_("E689: Can only index a List or Dictionary"));
2607 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (!quiet)
2612 EMSG(_("E708: [:] must come last"));
2613 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 len = -1;
2617 if (*p == '.')
2618 {
2619 key = p + 1;
2620 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2621 ;
2622 if (len == 0)
2623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_(e_emptykey));
2626 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 }
2628 p = key + len;
2629 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 else
2631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002633 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 if (*p == ':')
2635 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002636 else
2637 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 empty1 = FALSE;
2639 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002641 if (get_tv_string_chk(&var1) == NULL)
2642 {
2643 /* not a number or string */
2644 clear_tv(&var1);
2645 return NULL;
2646 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 }
2648
2649 /* Optionally get the second index [ :expr]. */
2650 if (*p == ':')
2651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002655 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002656 if (!empty1)
2657 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (rettv != NULL && (rettv->v_type != VAR_LIST
2661 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 if (!empty1)
2666 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 }
2669 p = skipwhite(p + 1);
2670 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 else
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 if (!empty1)
2678 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002681 if (get_tv_string_chk(&var2) == NULL)
2682 {
2683 /* not a number or string */
2684 if (!empty1)
2685 clear_tv(&var1);
2686 clear_tv(&var2);
2687 return NULL;
2688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (!quiet)
2698 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (!empty1)
2700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 }
2705
2706 /* Skip to past ']'. */
2707 ++p;
2708 }
2709
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 {
2712 if (len == -1)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002715 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 if (*key == NUL)
2717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002724 lp->ll_list = NULL;
2725 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002726 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002727
2728 /* When assigning to g: check that a function and variable name is
2729 * valid. */
2730 if (rettv != NULL && lp->ll_dict == &globvardict)
2731 {
2732 if (rettv->v_type == VAR_FUNC
2733 && var_check_func_name(key, lp->ll_di == NULL))
2734 return NULL;
2735 if (!valid_varname(key))
2736 return NULL;
2737 }
2738
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002741 /* Can't add "v:" variable. */
2742 if (lp->ll_dict == &vimvardict)
2743 {
2744 EMSG2(_(e_illvar), name);
2745 return NULL;
2746 }
2747
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002748 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (len == -1)
2754 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 }
2757 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (len == -1)
2762 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 p = NULL;
2765 break;
2766 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 /* existing variable, need to check if it can be changed */
2768 else if (var_check_ro(lp->ll_di->di_flags, name))
2769 return NULL;
2770
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (len == -1)
2772 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
2775 else
2776 {
2777 /*
2778 * Get the number and item for the only or first index of the List.
2779 */
2780 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 else
2783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002784 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 clear_tv(&var1);
2786 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002787 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_list = lp->ll_tv->vval.v_list;
2789 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2790 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002792 if (lp->ll_n1 < 0)
2793 {
2794 lp->ll_n1 = 0;
2795 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2796 }
2797 }
2798 if (lp->ll_li == NULL)
2799 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002802 if (!quiet)
2803 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 }
2806
2807 /*
2808 * May need to find the item or absolute index for the second
2809 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 * When no index given: "lp->ll_empty2" is TRUE.
2811 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002815 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002821 {
2822 if (!quiet)
2823 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002825 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 }
2828
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2830 if (lp->ll_n1 < 0)
2831 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2832 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002833 {
2834 if (!quiet)
2835 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002837 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002838 }
2839
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002842 }
2843
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 return p;
2845}
2846
2847/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002848 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 */
2850 static void
2851clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002852 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853{
2854 vim_free(lp->ll_exp_name);
2855 vim_free(lp->ll_newkey);
2856}
2857
2858/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002859 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002861 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 */
2863 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002864set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002865 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002869 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870{
2871 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002872 listitem_T *ri;
2873 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874
2875 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 cc = *endp;
2880 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002881 if (op != NULL && *op != '=')
2882 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002885 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002886 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002887 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002888 {
2889 if (tv_op(&tv, rettv, op) == OK)
2890 set_var(lp->ll_name, &tv, FALSE);
2891 clear_tv(&tv);
2892 }
2893 }
2894 else
2895 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002897 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002899 else if (tv_check_lock(lp->ll_newkey == NULL
2900 ? lp->ll_tv->v_lock
2901 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2902 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 else if (lp->ll_range)
2904 {
2905 /*
2906 * Assign the List values to the list items.
2907 */
2908 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002909 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910 if (op != NULL && *op != '=')
2911 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2912 else
2913 {
2914 clear_tv(&lp->ll_li->li_tv);
2915 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2916 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 ri = ri->li_next;
2918 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2919 break;
2920 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002923 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 ri = NULL;
2926 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002927 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002928 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 lp->ll_li = lp->ll_li->li_next;
2930 ++lp->ll_n1;
2931 }
2932 if (ri != NULL)
2933 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002934 else if (lp->ll_empty2
2935 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 : lp->ll_n1 != lp->ll_n2)
2937 EMSG(_("E711: List value has not enough items"));
2938 }
2939 else
2940 {
2941 /*
2942 * Assign to a List or Dictionary item.
2943 */
2944 if (lp->ll_newkey != NULL)
2945 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 if (op != NULL && *op != '=')
2947 {
2948 EMSG2(_(e_letwrong), op);
2949 return;
2950 }
2951
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002953 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 if (di == NULL)
2955 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002956 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2957 {
2958 vim_free(di);
2959 return;
2960 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002962 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 else if (op != NULL && *op != '=')
2964 {
2965 tv_op(lp->ll_tv, rettv, op);
2966 return;
2967 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002968 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002970
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 /*
2972 * Assign the value to the variable or list item.
2973 */
2974 if (copy)
2975 copy_tv(rettv, lp->ll_tv);
2976 else
2977 {
2978 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002979 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002981 }
2982 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983}
2984
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002985/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2987 * Returns OK or FAIL.
2988 */
2989 static int
2990tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002991 typval_T *tv1;
2992 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 char_u *op;
2994{
2995 long n;
2996 char_u numbuf[NUMBUFLEN];
2997 char_u *s;
2998
2999 /* Can't do anything with a Funcref or a Dict on the right. */
3000 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3001 {
3002 switch (tv1->v_type)
3003 {
3004 case VAR_DICT:
3005 case VAR_FUNC:
3006 break;
3007
3008 case VAR_LIST:
3009 if (*op != '+' || tv2->v_type != VAR_LIST)
3010 break;
3011 /* List += List */
3012 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3013 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3014 return OK;
3015
3016 case VAR_NUMBER:
3017 case VAR_STRING:
3018 if (tv2->v_type == VAR_LIST)
3019 break;
3020 if (*op == '+' || *op == '-')
3021 {
3022 /* nr += nr or nr -= nr*/
3023 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024#ifdef FEAT_FLOAT
3025 if (tv2->v_type == VAR_FLOAT)
3026 {
3027 float_T f = n;
3028
3029 if (*op == '+')
3030 f += tv2->vval.v_float;
3031 else
3032 f -= tv2->vval.v_float;
3033 clear_tv(tv1);
3034 tv1->v_type = VAR_FLOAT;
3035 tv1->vval.v_float = f;
3036 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003038#endif
3039 {
3040 if (*op == '+')
3041 n += get_tv_number(tv2);
3042 else
3043 n -= get_tv_number(tv2);
3044 clear_tv(tv1);
3045 tv1->v_type = VAR_NUMBER;
3046 tv1->vval.v_number = n;
3047 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 }
3049 else
3050 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003051 if (tv2->v_type == VAR_FLOAT)
3052 break;
3053
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054 /* str .= str */
3055 s = get_tv_string(tv1);
3056 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3057 clear_tv(tv1);
3058 tv1->v_type = VAR_STRING;
3059 tv1->vval.v_string = s;
3060 }
3061 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003062
3063#ifdef FEAT_FLOAT
3064 case VAR_FLOAT:
3065 {
3066 float_T f;
3067
3068 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3069 && tv2->v_type != VAR_NUMBER
3070 && tv2->v_type != VAR_STRING))
3071 break;
3072 if (tv2->v_type == VAR_FLOAT)
3073 f = tv2->vval.v_float;
3074 else
3075 f = get_tv_number(tv2);
3076 if (*op == '+')
3077 tv1->vval.v_float += f;
3078 else
3079 tv1->vval.v_float -= f;
3080 }
3081 return OK;
3082#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003083 }
3084 }
3085
3086 EMSG2(_(e_letwrong), op);
3087 return FAIL;
3088}
3089
3090/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003091 * Add a watcher to a list.
3092 */
3093 static void
3094list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003095 list_T *l;
3096 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003097{
3098 lw->lw_next = l->lv_watch;
3099 l->lv_watch = lw;
3100}
3101
3102/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003103 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003104 * No warning when it isn't found...
3105 */
3106 static void
3107list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003108 list_T *l;
3109 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110{
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112
3113 lwp = &l->lv_watch;
3114 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3115 {
3116 if (lw == lwrem)
3117 {
3118 *lwp = lw->lw_next;
3119 break;
3120 }
3121 lwp = &lw->lw_next;
3122 }
3123}
3124
3125/*
3126 * Just before removing an item from a list: advance watchers to the next
3127 * item.
3128 */
3129 static void
3130list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 list_T *l;
3132 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133{
Bram Moolenaar33570922005-01-25 22:26:29 +00003134 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003135
3136 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3137 if (lw->lw_item == item)
3138 lw->lw_item = item->li_next;
3139}
3140
3141/*
3142 * Evaluate the expression used in a ":for var in expr" command.
3143 * "arg" points to "var".
3144 * Set "*errp" to TRUE for an error, FALSE otherwise;
3145 * Return a pointer that holds the info. Null when there is an error.
3146 */
3147 void *
3148eval_for_line(arg, errp, nextcmdp, skip)
3149 char_u *arg;
3150 int *errp;
3151 char_u **nextcmdp;
3152 int skip;
3153{
Bram Moolenaar33570922005-01-25 22:26:29 +00003154 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 typval_T tv;
3157 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003158
3159 *errp = TRUE; /* default: there is an error */
3160
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162 if (fi == NULL)
3163 return NULL;
3164
3165 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3166 if (expr == NULL)
3167 return fi;
3168
3169 expr = skipwhite(expr);
3170 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3171 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003172 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173 return fi;
3174 }
3175
3176 if (skip)
3177 ++emsg_skip;
3178 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3179 {
3180 *errp = FALSE;
3181 if (!skip)
3182 {
3183 l = tv.vval.v_list;
3184 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003185 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003186 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003187 clear_tv(&tv);
3188 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 else
3190 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003191 /* No need to increment the refcount, it's already set for the
3192 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193 fi->fi_list = l;
3194 list_add_watch(l, &fi->fi_lw);
3195 fi->fi_lw.lw_item = l->lv_first;
3196 }
3197 }
3198 }
3199 if (skip)
3200 --emsg_skip;
3201
3202 return fi;
3203}
3204
3205/*
3206 * Use the first item in a ":for" list. Advance to the next.
3207 * Assign the values to the variable (list). "arg" points to the first one.
3208 * Return TRUE when a valid item was found, FALSE when at end of list or
3209 * something wrong.
3210 */
3211 int
3212next_for_item(fi_void, arg)
3213 void *fi_void;
3214 char_u *arg;
3215{
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219
3220 item = fi->fi_lw.lw_item;
3221 if (item == NULL)
3222 result = FALSE;
3223 else
3224 {
3225 fi->fi_lw.lw_item = item->li_next;
3226 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3227 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3228 }
3229 return result;
3230}
3231
3232/*
3233 * Free the structure used to store info used by ":for".
3234 */
3235 void
3236free_for_info(fi_void)
3237 void *fi_void;
3238{
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003241 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003242 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003244 list_unref(fi->fi_list);
3245 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 vim_free(fi);
3247}
3248
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3250
3251 void
3252set_context_for_expression(xp, arg, cmdidx)
3253 expand_T *xp;
3254 char_u *arg;
3255 cmdidx_T cmdidx;
3256{
3257 int got_eq = FALSE;
3258 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 if (cmdidx == CMD_let)
3262 {
3263 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003264 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 {
3266 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003267 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 {
3269 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 if (vim_iswhite(*p))
3272 break;
3273 }
3274 return;
3275 }
3276 }
3277 else
3278 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3279 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 while ((xp->xp_pattern = vim_strpbrk(arg,
3281 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3282 {
3283 c = *xp->xp_pattern;
3284 if (c == '&')
3285 {
3286 c = xp->xp_pattern[1];
3287 if (c == '&')
3288 {
3289 ++xp->xp_pattern;
3290 xp->xp_context = cmdidx != CMD_let || got_eq
3291 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3292 }
3293 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003296 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3297 xp->xp_pattern += 2;
3298
3299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301 else if (c == '$')
3302 {
3303 /* environment variable */
3304 xp->xp_context = EXPAND_ENV_VARS;
3305 }
3306 else if (c == '=')
3307 {
3308 got_eq = TRUE;
3309 xp->xp_context = EXPAND_EXPRESSION;
3310 }
3311 else if (c == '<'
3312 && xp->xp_context == EXPAND_FUNCTIONS
3313 && vim_strchr(xp->xp_pattern, '(') == NULL)
3314 {
3315 /* Function name can start with "<SNR>" */
3316 break;
3317 }
3318 else if (cmdidx != CMD_let || got_eq)
3319 {
3320 if (c == '"') /* string */
3321 {
3322 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3323 if (c == '\\' && xp->xp_pattern[1] != NUL)
3324 ++xp->xp_pattern;
3325 xp->xp_context = EXPAND_NOTHING;
3326 }
3327 else if (c == '\'') /* literal string */
3328 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003329 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3331 /* skip */ ;
3332 xp->xp_context = EXPAND_NOTHING;
3333 }
3334 else if (c == '|')
3335 {
3336 if (xp->xp_pattern[1] == '|')
3337 {
3338 ++xp->xp_pattern;
3339 xp->xp_context = EXPAND_EXPRESSION;
3340 }
3341 else
3342 xp->xp_context = EXPAND_COMMANDS;
3343 }
3344 else
3345 xp->xp_context = EXPAND_EXPRESSION;
3346 }
3347 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 /* Doesn't look like something valid, expand as an expression
3349 * anyway. */
3350 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 arg = xp->xp_pattern;
3352 if (*arg != NUL)
3353 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3354 /* skip */ ;
3355 }
3356 xp->xp_pattern = arg;
3357}
3358
3359#endif /* FEAT_CMDL_COMPL */
3360
3361/*
3362 * ":1,25call func(arg1, arg2)" function call.
3363 */
3364 void
3365ex_call(eap)
3366 exarg_T *eap;
3367{
3368 char_u *arg = eap->arg;
3369 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003371 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 linenr_T lnum;
3375 int doesrange;
3376 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003377 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003379 if (eap->skip)
3380 {
3381 /* trans_function_name() doesn't work well when skipping, use eval0()
3382 * instead to skip to any following command, e.g. for:
3383 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003384 ++emsg_skip;
3385 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3386 clear_tv(&rettv);
3387 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003388 return;
3389 }
3390
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003391 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003392 if (fudi.fd_newkey != NULL)
3393 {
3394 /* Still need to give an error message for missing key. */
3395 EMSG2(_(e_dictkey), fudi.fd_newkey);
3396 vim_free(fudi.fd_newkey);
3397 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003398 if (tofree == NULL)
3399 return;
3400
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003401 /* Increase refcount on dictionary, it could get deleted when evaluating
3402 * the arguments. */
3403 if (fudi.fd_dict != NULL)
3404 ++fudi.fd_dict->dv_refcount;
3405
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003406 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003407 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003408 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
Bram Moolenaar532c7802005-01-27 14:44:31 +00003410 /* Skip white space to allow ":call func ()". Not good, but required for
3411 * backward compatibility. */
3412 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003413 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
3415 if (*startarg != '(')
3416 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003417 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 goto end;
3419 }
3420
3421 /*
3422 * When skipping, evaluate the function once, to find the end of the
3423 * arguments.
3424 * When the function takes a range, this is discovered after the first
3425 * call, and the loop is broken.
3426 */
3427 if (eap->skip)
3428 {
3429 ++emsg_skip;
3430 lnum = eap->line2; /* do it once, also with an invalid range */
3431 }
3432 else
3433 lnum = eap->line1;
3434 for ( ; lnum <= eap->line2; ++lnum)
3435 {
3436 if (!eap->skip && eap->addr_count > 0)
3437 {
3438 curwin->w_cursor.lnum = lnum;
3439 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003440#ifdef FEAT_VIRTUALEDIT
3441 curwin->w_cursor.coladd = 0;
3442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
3444 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003445 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003446 eap->line1, eap->line2, &doesrange,
3447 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
3449 failed = TRUE;
3450 break;
3451 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003452
3453 /* Handle a function returning a Funcref, Dictionary or List. */
3454 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3455 {
3456 failed = TRUE;
3457 break;
3458 }
3459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003460 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (doesrange || eap->skip)
3462 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003463
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003465 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003466 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003467 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (aborting())
3469 break;
3470 }
3471 if (eap->skip)
3472 --emsg_skip;
3473
3474 if (!failed)
3475 {
3476 /* Check for trailing illegal characters and a following command. */
3477 if (!ends_excmd(*arg))
3478 {
3479 emsg_severe = TRUE;
3480 EMSG(_(e_trailing));
3481 }
3482 else
3483 eap->nextcmd = check_nextcmd(arg);
3484 }
3485
3486end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003487 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003488 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489}
3490
3491/*
3492 * ":unlet[!] var1 ... " command.
3493 */
3494 void
3495ex_unlet(eap)
3496 exarg_T *eap;
3497{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003498 ex_unletlock(eap, eap->arg, 0);
3499}
3500
3501/*
3502 * ":lockvar" and ":unlockvar" commands
3503 */
3504 void
3505ex_lockvar(eap)
3506 exarg_T *eap;
3507{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003509 int deep = 2;
3510
3511 if (eap->forceit)
3512 deep = -1;
3513 else if (vim_isdigit(*arg))
3514 {
3515 deep = getdigits(&arg);
3516 arg = skipwhite(arg);
3517 }
3518
3519 ex_unletlock(eap, arg, deep);
3520}
3521
3522/*
3523 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3524 */
3525 static void
3526ex_unletlock(eap, argstart, deep)
3527 exarg_T *eap;
3528 char_u *argstart;
3529 int deep;
3530{
3531 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535
3536 do
3537 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003538 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003539 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3540 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003541 if (lv.ll_name == NULL)
3542 error = TRUE; /* error but continue parsing */
3543 if (name_end == NULL || (!vim_iswhite(*name_end)
3544 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003546 if (name_end != NULL)
3547 {
3548 emsg_severe = TRUE;
3549 EMSG(_(e_trailing));
3550 }
3551 if (!(eap->skip || error))
3552 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 break;
3554 }
3555
3556 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003557 {
3558 if (eap->cmdidx == CMD_unlet)
3559 {
3560 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3561 error = TRUE;
3562 }
3563 else
3564 {
3565 if (do_lock_var(&lv, name_end, deep,
3566 eap->cmdidx == CMD_lockvar) == FAIL)
3567 error = TRUE;
3568 }
3569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571 if (!eap->skip)
3572 clear_lval(&lv);
3573
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 arg = skipwhite(name_end);
3575 } while (!ends_excmd(*arg));
3576
3577 eap->nextcmd = check_nextcmd(arg);
3578}
3579
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003581do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003582 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003583 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003584 int forceit;
3585{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003586 int ret = OK;
3587 int cc;
3588
3589 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591 cc = *name_end;
3592 *name_end = NUL;
3593
3594 /* Normal name or expanded name. */
3595 if (check_changedtick(lp->ll_name))
3596 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003597 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003601 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3602 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 else if (lp->ll_range)
3604 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003605 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606
3607 /* Delete a range of List items. */
3608 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3609 {
3610 li = lp->ll_li->li_next;
3611 listitem_remove(lp->ll_list, lp->ll_li);
3612 lp->ll_li = li;
3613 ++lp->ll_n1;
3614 }
3615 }
3616 else
3617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 /* unlet a List item. */
3620 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003623 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 }
3625
3626 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627}
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629/*
3630 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003631 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 */
3633 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637{
Bram Moolenaar33570922005-01-25 22:26:29 +00003638 hashtab_T *ht;
3639 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003640 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003641 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
Bram Moolenaar33570922005-01-25 22:26:29 +00003643 ht = find_var_ht(name, &varname);
3644 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003646 hi = hash_find(ht, varname);
3647 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003648 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003649 di = HI2DI(hi);
3650 if (var_check_fixed(di->di_flags, name)
3651 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003652 return FAIL;
3653 delete_var(ht, hi);
3654 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 if (forceit)
3658 return OK;
3659 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 return FAIL;
3661}
3662
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663/*
3664 * Lock or unlock variable indicated by "lp".
3665 * "deep" is the levels to go (-1 for unlimited);
3666 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3667 */
3668 static int
3669do_lock_var(lp, name_end, deep, lock)
3670 lval_T *lp;
3671 char_u *name_end;
3672 int deep;
3673 int lock;
3674{
3675 int ret = OK;
3676 int cc;
3677 dictitem_T *di;
3678
3679 if (deep == 0) /* nothing to do */
3680 return OK;
3681
3682 if (lp->ll_tv == NULL)
3683 {
3684 cc = *name_end;
3685 *name_end = NUL;
3686
3687 /* Normal name or expanded name. */
3688 if (check_changedtick(lp->ll_name))
3689 ret = FAIL;
3690 else
3691 {
3692 di = find_var(lp->ll_name, NULL);
3693 if (di == NULL)
3694 ret = FAIL;
3695 else
3696 {
3697 if (lock)
3698 di->di_flags |= DI_FLAGS_LOCK;
3699 else
3700 di->di_flags &= ~DI_FLAGS_LOCK;
3701 item_lock(&di->di_tv, deep, lock);
3702 }
3703 }
3704 *name_end = cc;
3705 }
3706 else if (lp->ll_range)
3707 {
3708 listitem_T *li = lp->ll_li;
3709
3710 /* (un)lock a range of List items. */
3711 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3712 {
3713 item_lock(&li->li_tv, deep, lock);
3714 li = li->li_next;
3715 ++lp->ll_n1;
3716 }
3717 }
3718 else if (lp->ll_list != NULL)
3719 /* (un)lock a List item. */
3720 item_lock(&lp->ll_li->li_tv, deep, lock);
3721 else
3722 /* un(lock) a Dictionary item. */
3723 item_lock(&lp->ll_di->di_tv, deep, lock);
3724
3725 return ret;
3726}
3727
3728/*
3729 * Lock or unlock an item. "deep" is nr of levels to go.
3730 */
3731 static void
3732item_lock(tv, deep, lock)
3733 typval_T *tv;
3734 int deep;
3735 int lock;
3736{
3737 static int recurse = 0;
3738 list_T *l;
3739 listitem_T *li;
3740 dict_T *d;
3741 hashitem_T *hi;
3742 int todo;
3743
3744 if (recurse >= DICT_MAXNEST)
3745 {
3746 EMSG(_("E743: variable nested too deep for (un)lock"));
3747 return;
3748 }
3749 if (deep == 0)
3750 return;
3751 ++recurse;
3752
3753 /* lock/unlock the item itself */
3754 if (lock)
3755 tv->v_lock |= VAR_LOCKED;
3756 else
3757 tv->v_lock &= ~VAR_LOCKED;
3758
3759 switch (tv->v_type)
3760 {
3761 case VAR_LIST:
3762 if ((l = tv->vval.v_list) != NULL)
3763 {
3764 if (lock)
3765 l->lv_lock |= VAR_LOCKED;
3766 else
3767 l->lv_lock &= ~VAR_LOCKED;
3768 if (deep < 0 || deep > 1)
3769 /* recursive: lock/unlock the items the List contains */
3770 for (li = l->lv_first; li != NULL; li = li->li_next)
3771 item_lock(&li->li_tv, deep - 1, lock);
3772 }
3773 break;
3774 case VAR_DICT:
3775 if ((d = tv->vval.v_dict) != NULL)
3776 {
3777 if (lock)
3778 d->dv_lock |= VAR_LOCKED;
3779 else
3780 d->dv_lock &= ~VAR_LOCKED;
3781 if (deep < 0 || deep > 1)
3782 {
3783 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003784 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003785 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3786 {
3787 if (!HASHITEM_EMPTY(hi))
3788 {
3789 --todo;
3790 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3791 }
3792 }
3793 }
3794 }
3795 }
3796 --recurse;
3797}
3798
Bram Moolenaara40058a2005-07-11 22:42:07 +00003799/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003800 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3801 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003802 */
3803 static int
3804tv_islocked(tv)
3805 typval_T *tv;
3806{
3807 return (tv->v_lock & VAR_LOCKED)
3808 || (tv->v_type == VAR_LIST
3809 && tv->vval.v_list != NULL
3810 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3811 || (tv->v_type == VAR_DICT
3812 && tv->vval.v_dict != NULL
3813 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3814}
3815
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3817/*
3818 * Delete all "menutrans_" variables.
3819 */
3820 void
3821del_menutrans_vars()
3822{
Bram Moolenaar33570922005-01-25 22:26:29 +00003823 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003824 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
Bram Moolenaar33570922005-01-25 22:26:29 +00003826 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003827 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003828 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003829 {
3830 if (!HASHITEM_EMPTY(hi))
3831 {
3832 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003833 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3834 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003835 }
3836 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003837 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838}
3839#endif
3840
3841#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3842
3843/*
3844 * Local string buffer for the next two functions to store a variable name
3845 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3846 * get_user_var_name().
3847 */
3848
3849static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3850
3851static char_u *varnamebuf = NULL;
3852static int varnamebuflen = 0;
3853
3854/*
3855 * Function to concatenate a prefix and a variable name.
3856 */
3857 static char_u *
3858cat_prefix_varname(prefix, name)
3859 int prefix;
3860 char_u *name;
3861{
3862 int len;
3863
3864 len = (int)STRLEN(name) + 3;
3865 if (len > varnamebuflen)
3866 {
3867 vim_free(varnamebuf);
3868 len += 10; /* some additional space */
3869 varnamebuf = alloc(len);
3870 if (varnamebuf == NULL)
3871 {
3872 varnamebuflen = 0;
3873 return NULL;
3874 }
3875 varnamebuflen = len;
3876 }
3877 *varnamebuf = prefix;
3878 varnamebuf[1] = ':';
3879 STRCPY(varnamebuf + 2, name);
3880 return varnamebuf;
3881}
3882
3883/*
3884 * Function given to ExpandGeneric() to obtain the list of user defined
3885 * (global/buffer/window/built-in) variable names.
3886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 char_u *
3888get_user_var_name(xp, idx)
3889 expand_T *xp;
3890 int idx;
3891{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003892 static long_u gdone;
3893 static long_u bdone;
3894 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003895#ifdef FEAT_WINDOWS
3896 static long_u tdone;
3897#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003898 static int vidx;
3899 static hashitem_T *hi;
3900 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
3902 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003903 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003904 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003905#ifdef FEAT_WINDOWS
3906 tdone = 0;
3907#endif
3908 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003909
3910 /* Global variables */
3911 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003913 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003915 else
3916 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003917 while (HASHITEM_EMPTY(hi))
3918 ++hi;
3919 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3920 return cat_prefix_varname('g', hi->hi_key);
3921 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003923
3924 /* b: variables */
3925 ht = &curbuf->b_vars.dv_hashtab;
3926 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003929 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003930 else
3931 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 while (HASHITEM_EMPTY(hi))
3933 ++hi;
3934 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003938 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 return (char_u *)"b:changedtick";
3940 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003941
3942 /* w: variables */
3943 ht = &curwin->w_vars.dv_hashtab;
3944 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003946 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003948 else
3949 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 while (HASHITEM_EMPTY(hi))
3951 ++hi;
3952 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003954
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003955#ifdef FEAT_WINDOWS
3956 /* t: variables */
3957 ht = &curtab->tp_vars.dv_hashtab;
3958 if (tdone < ht->ht_used)
3959 {
3960 if (tdone++ == 0)
3961 hi = ht->ht_array;
3962 else
3963 ++hi;
3964 while (HASHITEM_EMPTY(hi))
3965 ++hi;
3966 return cat_prefix_varname('t', hi->hi_key);
3967 }
3968#endif
3969
Bram Moolenaar33570922005-01-25 22:26:29 +00003970 /* v: variables */
3971 if (vidx < VV_LEN)
3972 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
3974 vim_free(varnamebuf);
3975 varnamebuf = NULL;
3976 varnamebuflen = 0;
3977 return NULL;
3978}
3979
3980#endif /* FEAT_CMDL_COMPL */
3981
3982/*
3983 * types for expressions.
3984 */
3985typedef enum
3986{
3987 TYPE_UNKNOWN = 0
3988 , TYPE_EQUAL /* == */
3989 , TYPE_NEQUAL /* != */
3990 , TYPE_GREATER /* > */
3991 , TYPE_GEQUAL /* >= */
3992 , TYPE_SMALLER /* < */
3993 , TYPE_SEQUAL /* <= */
3994 , TYPE_MATCH /* =~ */
3995 , TYPE_NOMATCH /* !~ */
3996} exptype_T;
3997
3998/*
3999 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004000 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4002 */
4003
4004/*
4005 * Handle zero level expression.
4006 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004008 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 * Return OK or FAIL.
4010 */
4011 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004012eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 char_u **nextcmd;
4016 int evaluate;
4017{
4018 int ret;
4019 char_u *p;
4020
4021 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 if (ret == FAIL || !ends_excmd(*p))
4024 {
4025 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 /*
4028 * Report the invalid expression unless the expression evaluation has
4029 * been cancelled due to an aborting error, an interrupt, or an
4030 * exception.
4031 */
4032 if (!aborting())
4033 EMSG2(_(e_invexpr2), arg);
4034 ret = FAIL;
4035 }
4036 if (nextcmd != NULL)
4037 *nextcmd = check_nextcmd(p);
4038
4039 return ret;
4040}
4041
4042/*
4043 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004044 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 *
4046 * "arg" must point to the first non-white of the expression.
4047 * "arg" is advanced to the next non-white after the recognized expression.
4048 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004049 * Note: "rettv.v_lock" is not set.
4050 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 * Return OK or FAIL.
4052 */
4053 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004054eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 int evaluate;
4058{
4059 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004060 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
4062 /*
4063 * Get the first variable.
4064 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004065 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 return FAIL;
4067
4068 if ((*arg)[0] == '?')
4069 {
4070 result = FALSE;
4071 if (evaluate)
4072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004073 int error = FALSE;
4074
4075 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004078 if (error)
4079 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 }
4081
4082 /*
4083 * Get the second variable.
4084 */
4085 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 return FAIL;
4088
4089 /*
4090 * Check for the ":".
4091 */
4092 if ((*arg)[0] != ':')
4093 {
4094 EMSG(_("E109: Missing ':' after '?'"));
4095 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 return FAIL;
4098 }
4099
4100 /*
4101 * Get the third variable.
4102 */
4103 *arg = skipwhite(*arg + 1);
4104 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4105 {
4106 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 return FAIL;
4109 }
4110 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113
4114 return OK;
4115}
4116
4117/*
4118 * Handle first level expression:
4119 * expr2 || expr2 || expr2 logical OR
4120 *
4121 * "arg" must point to the first non-white of the expression.
4122 * "arg" is advanced to the next non-white after the recognized expression.
4123 *
4124 * Return OK or FAIL.
4125 */
4126 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 int evaluate;
4131{
Bram Moolenaar33570922005-01-25 22:26:29 +00004132 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 long result;
4134 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004135 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
4137 /*
4138 * Get the first variable.
4139 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 return FAIL;
4142
4143 /*
4144 * Repeat until there is no following "||".
4145 */
4146 first = TRUE;
4147 result = FALSE;
4148 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4149 {
4150 if (evaluate && first)
4151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004155 if (error)
4156 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 first = FALSE;
4158 }
4159
4160 /*
4161 * Get the second variable.
4162 */
4163 *arg = skipwhite(*arg + 2);
4164 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4165 return FAIL;
4166
4167 /*
4168 * Compute the result.
4169 */
4170 if (evaluate && !result)
4171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004175 if (error)
4176 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
4178 if (evaluate)
4179 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 rettv->v_type = VAR_NUMBER;
4181 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183 }
4184
4185 return OK;
4186}
4187
4188/*
4189 * Handle second level expression:
4190 * expr3 && expr3 && expr3 logical AND
4191 *
4192 * "arg" must point to the first non-white of the expression.
4193 * "arg" is advanced to the next non-white after the recognized expression.
4194 *
4195 * Return OK or FAIL.
4196 */
4197 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 int evaluate;
4202{
Bram Moolenaar33570922005-01-25 22:26:29 +00004203 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 long result;
4205 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208 /*
4209 * Get the first variable.
4210 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213
4214 /*
4215 * Repeat until there is no following "&&".
4216 */
4217 first = TRUE;
4218 result = TRUE;
4219 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4220 {
4221 if (evaluate && first)
4222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004226 if (error)
4227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 first = FALSE;
4229 }
4230
4231 /*
4232 * Get the second variable.
4233 */
4234 *arg = skipwhite(*arg + 2);
4235 if (eval4(arg, &var2, evaluate && result) == FAIL)
4236 return FAIL;
4237
4238 /*
4239 * Compute the result.
4240 */
4241 if (evaluate && result)
4242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 if (error)
4247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249 if (evaluate)
4250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 rettv->v_type = VAR_NUMBER;
4252 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 }
4255
4256 return OK;
4257}
4258
4259/*
4260 * Handle third level expression:
4261 * var1 == var2
4262 * var1 =~ var2
4263 * var1 != var2
4264 * var1 !~ var2
4265 * var1 > var2
4266 * var1 >= var2
4267 * var1 < var2
4268 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004269 * var1 is var2
4270 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 *
4272 * "arg" must point to the first non-white of the expression.
4273 * "arg" is advanced to the next non-white after the recognized expression.
4274 *
4275 * Return OK or FAIL.
4276 */
4277 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 int evaluate;
4282{
Bram Moolenaar33570922005-01-25 22:26:29 +00004283 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 char_u *p;
4285 int i;
4286 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004287 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 int len = 2;
4289 long n1, n2;
4290 char_u *s1, *s2;
4291 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4292 regmatch_T regmatch;
4293 int ic;
4294 char_u *save_cpo;
4295
4296 /*
4297 * Get the first variable.
4298 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 return FAIL;
4301
4302 p = *arg;
4303 switch (p[0])
4304 {
4305 case '=': if (p[1] == '=')
4306 type = TYPE_EQUAL;
4307 else if (p[1] == '~')
4308 type = TYPE_MATCH;
4309 break;
4310 case '!': if (p[1] == '=')
4311 type = TYPE_NEQUAL;
4312 else if (p[1] == '~')
4313 type = TYPE_NOMATCH;
4314 break;
4315 case '>': if (p[1] != '=')
4316 {
4317 type = TYPE_GREATER;
4318 len = 1;
4319 }
4320 else
4321 type = TYPE_GEQUAL;
4322 break;
4323 case '<': if (p[1] != '=')
4324 {
4325 type = TYPE_SMALLER;
4326 len = 1;
4327 }
4328 else
4329 type = TYPE_SEQUAL;
4330 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004331 case 'i': if (p[1] == 's')
4332 {
4333 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4334 len = 5;
4335 if (!vim_isIDc(p[len]))
4336 {
4337 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4338 type_is = TRUE;
4339 }
4340 }
4341 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 }
4343
4344 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004345 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 */
4347 if (type != TYPE_UNKNOWN)
4348 {
4349 /* extra question mark appended: ignore case */
4350 if (p[len] == '?')
4351 {
4352 ic = TRUE;
4353 ++len;
4354 }
4355 /* extra '#' appended: match case */
4356 else if (p[len] == '#')
4357 {
4358 ic = FALSE;
4359 ++len;
4360 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004361 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 else
4363 ic = p_ic;
4364
4365 /*
4366 * Get the second variable.
4367 */
4368 *arg = skipwhite(p + len);
4369 if (eval5(arg, &var2, evaluate) == FAIL)
4370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 return FAIL;
4373 }
4374
4375 if (evaluate)
4376 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004377 if (type_is && rettv->v_type != var2.v_type)
4378 {
4379 /* For "is" a different type always means FALSE, for "notis"
4380 * it means TRUE. */
4381 n1 = (type == TYPE_NEQUAL);
4382 }
4383 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4384 {
4385 if (type_is)
4386 {
4387 n1 = (rettv->v_type == var2.v_type
4388 && rettv->vval.v_list == var2.vval.v_list);
4389 if (type == TYPE_NEQUAL)
4390 n1 = !n1;
4391 }
4392 else if (rettv->v_type != var2.v_type
4393 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4394 {
4395 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004396 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004398 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004399 clear_tv(rettv);
4400 clear_tv(&var2);
4401 return FAIL;
4402 }
4403 else
4404 {
4405 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004406 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4407 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004408 if (type == TYPE_NEQUAL)
4409 n1 = !n1;
4410 }
4411 }
4412
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004413 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4414 {
4415 if (type_is)
4416 {
4417 n1 = (rettv->v_type == var2.v_type
4418 && rettv->vval.v_dict == var2.vval.v_dict);
4419 if (type == TYPE_NEQUAL)
4420 n1 = !n1;
4421 }
4422 else if (rettv->v_type != var2.v_type
4423 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4424 {
4425 if (rettv->v_type != var2.v_type)
4426 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4427 else
4428 EMSG(_("E736: Invalid operation for Dictionary"));
4429 clear_tv(rettv);
4430 clear_tv(&var2);
4431 return FAIL;
4432 }
4433 else
4434 {
4435 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004436 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4437 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004438 if (type == TYPE_NEQUAL)
4439 n1 = !n1;
4440 }
4441 }
4442
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004443 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4444 {
4445 if (rettv->v_type != var2.v_type
4446 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4447 {
4448 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004449 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004450 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004451 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004452 clear_tv(rettv);
4453 clear_tv(&var2);
4454 return FAIL;
4455 }
4456 else
4457 {
4458 /* Compare two Funcrefs for being equal or unequal. */
4459 if (rettv->vval.v_string == NULL
4460 || var2.vval.v_string == NULL)
4461 n1 = FALSE;
4462 else
4463 n1 = STRCMP(rettv->vval.v_string,
4464 var2.vval.v_string) == 0;
4465 if (type == TYPE_NEQUAL)
4466 n1 = !n1;
4467 }
4468 }
4469
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004470#ifdef FEAT_FLOAT
4471 /*
4472 * If one of the two variables is a float, compare as a float.
4473 * When using "=~" or "!~", always compare as string.
4474 */
4475 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4476 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4477 {
4478 float_T f1, f2;
4479
4480 if (rettv->v_type == VAR_FLOAT)
4481 f1 = rettv->vval.v_float;
4482 else
4483 f1 = get_tv_number(rettv);
4484 if (var2.v_type == VAR_FLOAT)
4485 f2 = var2.vval.v_float;
4486 else
4487 f2 = get_tv_number(&var2);
4488 n1 = FALSE;
4489 switch (type)
4490 {
4491 case TYPE_EQUAL: n1 = (f1 == f2); break;
4492 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4493 case TYPE_GREATER: n1 = (f1 > f2); break;
4494 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4495 case TYPE_SMALLER: n1 = (f1 < f2); break;
4496 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4497 case TYPE_UNKNOWN:
4498 case TYPE_MATCH:
4499 case TYPE_NOMATCH: break; /* avoid gcc warning */
4500 }
4501 }
4502#endif
4503
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 /*
4505 * If one of the two variables is a number, compare as a number.
4506 * When using "=~" or "!~", always compare as string.
4507 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004511 n1 = get_tv_number(rettv);
4512 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 switch (type)
4514 {
4515 case TYPE_EQUAL: n1 = (n1 == n2); break;
4516 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4517 case TYPE_GREATER: n1 = (n1 > n2); break;
4518 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4519 case TYPE_SMALLER: n1 = (n1 < n2); break;
4520 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4521 case TYPE_UNKNOWN:
4522 case TYPE_MATCH:
4523 case TYPE_NOMATCH: break; /* avoid gcc warning */
4524 }
4525 }
4526 else
4527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004528 s1 = get_tv_string_buf(rettv, buf1);
4529 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4531 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4532 else
4533 i = 0;
4534 n1 = FALSE;
4535 switch (type)
4536 {
4537 case TYPE_EQUAL: n1 = (i == 0); break;
4538 case TYPE_NEQUAL: n1 = (i != 0); break;
4539 case TYPE_GREATER: n1 = (i > 0); break;
4540 case TYPE_GEQUAL: n1 = (i >= 0); break;
4541 case TYPE_SMALLER: n1 = (i < 0); break;
4542 case TYPE_SEQUAL: n1 = (i <= 0); break;
4543
4544 case TYPE_MATCH:
4545 case TYPE_NOMATCH:
4546 /* avoid 'l' flag in 'cpoptions' */
4547 save_cpo = p_cpo;
4548 p_cpo = (char_u *)"";
4549 regmatch.regprog = vim_regcomp(s2,
4550 RE_MAGIC + RE_STRING);
4551 regmatch.rm_ic = ic;
4552 if (regmatch.regprog != NULL)
4553 {
4554 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4555 vim_free(regmatch.regprog);
4556 if (type == TYPE_NOMATCH)
4557 n1 = !n1;
4558 }
4559 p_cpo = save_cpo;
4560 break;
4561
4562 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4563 }
4564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004565 clear_tv(rettv);
4566 clear_tv(&var2);
4567 rettv->v_type = VAR_NUMBER;
4568 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570 }
4571
4572 return OK;
4573}
4574
4575/*
4576 * Handle fourth level expression:
4577 * + number addition
4578 * - number subtraction
4579 * . string concatenation
4580 *
4581 * "arg" must point to the first non-white of the expression.
4582 * "arg" is advanced to the next non-white after the recognized expression.
4583 *
4584 * Return OK or FAIL.
4585 */
4586 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004587eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 int evaluate;
4591{
Bram Moolenaar33570922005-01-25 22:26:29 +00004592 typval_T var2;
4593 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 int op;
4595 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004596#ifdef FEAT_FLOAT
4597 float_T f1 = 0, f2 = 0;
4598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 char_u *s1, *s2;
4600 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4601 char_u *p;
4602
4603 /*
4604 * Get the first variable.
4605 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004606 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 return FAIL;
4608
4609 /*
4610 * Repeat computing, until no '+', '-' or '.' is following.
4611 */
4612 for (;;)
4613 {
4614 op = **arg;
4615 if (op != '+' && op != '-' && op != '.')
4616 break;
4617
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004618 if ((op != '+' || rettv->v_type != VAR_LIST)
4619#ifdef FEAT_FLOAT
4620 && (op == '.' || rettv->v_type != VAR_FLOAT)
4621#endif
4622 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004623 {
4624 /* For "list + ...", an illegal use of the first operand as
4625 * a number cannot be determined before evaluating the 2nd
4626 * operand: if this is also a list, all is ok.
4627 * For "something . ...", "something - ..." or "non-list + ...",
4628 * we know that the first operand needs to be a string or number
4629 * without evaluating the 2nd operand. So check before to avoid
4630 * side effects after an error. */
4631 if (evaluate && get_tv_string_chk(rettv) == NULL)
4632 {
4633 clear_tv(rettv);
4634 return FAIL;
4635 }
4636 }
4637
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 /*
4639 * Get the second variable.
4640 */
4641 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004642 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 return FAIL;
4646 }
4647
4648 if (evaluate)
4649 {
4650 /*
4651 * Compute the result.
4652 */
4653 if (op == '.')
4654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004655 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4656 s2 = get_tv_string_buf_chk(&var2, buf2);
4657 if (s2 == NULL) /* type error ? */
4658 {
4659 clear_tv(rettv);
4660 clear_tv(&var2);
4661 return FAIL;
4662 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004663 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004664 clear_tv(rettv);
4665 rettv->v_type = VAR_STRING;
4666 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004668 else if (op == '+' && rettv->v_type == VAR_LIST
4669 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004670 {
4671 /* concatenate Lists */
4672 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4673 &var3) == FAIL)
4674 {
4675 clear_tv(rettv);
4676 clear_tv(&var2);
4677 return FAIL;
4678 }
4679 clear_tv(rettv);
4680 *rettv = var3;
4681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 else
4683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004684 int error = FALSE;
4685
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004686#ifdef FEAT_FLOAT
4687 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004689 f1 = rettv->vval.v_float;
4690 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004692 else
4693#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004694 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004695 n1 = get_tv_number_chk(rettv, &error);
4696 if (error)
4697 {
4698 /* This can only happen for "list + non-list". For
4699 * "non-list + ..." or "something - ...", we returned
4700 * before evaluating the 2nd operand. */
4701 clear_tv(rettv);
4702 return FAIL;
4703 }
4704#ifdef FEAT_FLOAT
4705 if (var2.v_type == VAR_FLOAT)
4706 f1 = n1;
4707#endif
4708 }
4709#ifdef FEAT_FLOAT
4710 if (var2.v_type == VAR_FLOAT)
4711 {
4712 f2 = var2.vval.v_float;
4713 n2 = 0;
4714 }
4715 else
4716#endif
4717 {
4718 n2 = get_tv_number_chk(&var2, &error);
4719 if (error)
4720 {
4721 clear_tv(rettv);
4722 clear_tv(&var2);
4723 return FAIL;
4724 }
4725#ifdef FEAT_FLOAT
4726 if (rettv->v_type == VAR_FLOAT)
4727 f2 = n2;
4728#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731
4732#ifdef FEAT_FLOAT
4733 /* If there is a float on either side the result is a float. */
4734 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4735 {
4736 if (op == '+')
4737 f1 = f1 + f2;
4738 else
4739 f1 = f1 - f2;
4740 rettv->v_type = VAR_FLOAT;
4741 rettv->vval.v_float = f1;
4742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004744#endif
4745 {
4746 if (op == '+')
4747 n1 = n1 + n2;
4748 else
4749 n1 = n1 - n2;
4750 rettv->v_type = VAR_NUMBER;
4751 rettv->vval.v_number = n1;
4752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
4756 }
4757 return OK;
4758}
4759
4760/*
4761 * Handle fifth level expression:
4762 * * number multiplication
4763 * / number division
4764 * % number modulo
4765 *
4766 * "arg" must point to the first non-white of the expression.
4767 * "arg" is advanced to the next non-white after the recognized expression.
4768 *
4769 * Return OK or FAIL.
4770 */
4771 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004772eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004776 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
Bram Moolenaar33570922005-01-25 22:26:29 +00004778 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 int op;
4780 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004781#ifdef FEAT_FLOAT
4782 int use_float = FALSE;
4783 float_T f1 = 0, f2;
4784#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
4787 /*
4788 * Get the first variable.
4789 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004790 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 return FAIL;
4792
4793 /*
4794 * Repeat computing, until no '*', '/' or '%' is following.
4795 */
4796 for (;;)
4797 {
4798 op = **arg;
4799 if (op != '*' && op != '/' && op != '%')
4800 break;
4801
4802 if (evaluate)
4803 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004804#ifdef FEAT_FLOAT
4805 if (rettv->v_type == VAR_FLOAT)
4806 {
4807 f1 = rettv->vval.v_float;
4808 use_float = TRUE;
4809 n1 = 0;
4810 }
4811 else
4812#endif
4813 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004815 if (error)
4816 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
4818 else
4819 n1 = 0;
4820
4821 /*
4822 * Get the second variable.
4823 */
4824 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004825 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 return FAIL;
4827
4828 if (evaluate)
4829 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830#ifdef FEAT_FLOAT
4831 if (var2.v_type == VAR_FLOAT)
4832 {
4833 if (!use_float)
4834 {
4835 f1 = n1;
4836 use_float = TRUE;
4837 }
4838 f2 = var2.vval.v_float;
4839 n2 = 0;
4840 }
4841 else
4842#endif
4843 {
4844 n2 = get_tv_number_chk(&var2, &error);
4845 clear_tv(&var2);
4846 if (error)
4847 return FAIL;
4848#ifdef FEAT_FLOAT
4849 if (use_float)
4850 f2 = n2;
4851#endif
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 /*
4855 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004858#ifdef FEAT_FLOAT
4859 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004861 if (op == '*')
4862 f1 = f1 * f2;
4863 else if (op == '/')
4864 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004865# ifdef VMS
4866 /* VMS crashes on divide by zero, work around it */
4867 if (f2 == 0.0)
4868 {
4869 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004870 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004871 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004872 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004873 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004874 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004875 }
4876 else
4877 f1 = f1 / f2;
4878# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879 /* We rely on the floating point library to handle divide
4880 * by zero to result in "inf" and not a crash. */
4881 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004882# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004885 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004886 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004887 return FAIL;
4888 }
4889 rettv->v_type = VAR_FLOAT;
4890 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 if (op == '*')
4896 n1 = n1 * n2;
4897 else if (op == '/')
4898 {
4899 if (n2 == 0) /* give an error message? */
4900 {
4901 if (n1 == 0)
4902 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4903 else if (n1 < 0)
4904 n1 = -0x7fffffffL;
4905 else
4906 n1 = 0x7fffffffL;
4907 }
4908 else
4909 n1 = n1 / n2;
4910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 {
4913 if (n2 == 0) /* give an error message? */
4914 n1 = 0;
4915 else
4916 n1 = n1 % n2;
4917 }
4918 rettv->v_type = VAR_NUMBER;
4919 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
4922 }
4923
4924 return OK;
4925}
4926
4927/*
4928 * Handle sixth level expression:
4929 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004930 * "string" string constant
4931 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 * &option-name option value
4933 * @r register contents
4934 * identifier variable value
4935 * function() function call
4936 * $VAR environment variable
4937 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004938 * [expr, expr] List
4939 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 *
4941 * Also handle:
4942 * ! in front logical NOT
4943 * - in front unary minus
4944 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004945 * trailing [] subscript in String or List
4946 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 *
4948 * "arg" must point to the first non-white of the expression.
4949 * "arg" is advanced to the next non-white after the recognized expression.
4950 *
4951 * Return OK or FAIL.
4952 */
4953 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004954eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004958 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 long n;
4961 int len;
4962 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 char_u *start_leader, *end_leader;
4964 int ret = OK;
4965 char_u *alias;
4966
4967 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004968 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004969 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004971 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
4973 /*
4974 * Skip '!' and '-' characters. They are handled later.
4975 */
4976 start_leader = *arg;
4977 while (**arg == '!' || **arg == '-' || **arg == '+')
4978 *arg = skipwhite(*arg + 1);
4979 end_leader = *arg;
4980
4981 switch (**arg)
4982 {
4983 /*
4984 * Number constant.
4985 */
4986 case '0':
4987 case '1':
4988 case '2':
4989 case '3':
4990 case '4':
4991 case '5':
4992 case '6':
4993 case '7':
4994 case '8':
4995 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 {
4997#ifdef FEAT_FLOAT
4998 char_u *p = skipdigits(*arg + 1);
4999 int get_float = FALSE;
5000
5001 /* We accept a float when the format matches
5002 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005003 * strict to avoid backwards compatibility problems.
5004 * Don't look for a float after the "." operator, so that
5005 * ":let vers = 1.2.3" doesn't fail. */
5006 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008 get_float = TRUE;
5009 p = skipdigits(p + 2);
5010 if (*p == 'e' || *p == 'E')
5011 {
5012 ++p;
5013 if (*p == '-' || *p == '+')
5014 ++p;
5015 if (!vim_isdigit(*p))
5016 get_float = FALSE;
5017 else
5018 p = skipdigits(p + 1);
5019 }
5020 if (ASCII_ISALPHA(*p) || *p == '.')
5021 get_float = FALSE;
5022 }
5023 if (get_float)
5024 {
5025 float_T f;
5026
5027 *arg += string2float(*arg, &f);
5028 if (evaluate)
5029 {
5030 rettv->v_type = VAR_FLOAT;
5031 rettv->vval.v_float = f;
5032 }
5033 }
5034 else
5035#endif
5036 {
5037 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5038 *arg += len;
5039 if (evaluate)
5040 {
5041 rettv->v_type = VAR_NUMBER;
5042 rettv->vval.v_number = n;
5043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
5045 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 /*
5049 * String constant: "string".
5050 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005051 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 break;
5053
5054 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005057 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005058 break;
5059
5060 /*
5061 * List: [expr, expr]
5062 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005063 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 break;
5065
5066 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 * Dictionary: {key: val, key: val}
5068 */
5069 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5070 break;
5071
5072 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005073 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005075 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 break;
5077
5078 /*
5079 * Environment variable: $VAR.
5080 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005081 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 break;
5083
5084 /*
5085 * Register contents: @r.
5086 */
5087 case '@': ++*arg;
5088 if (evaluate)
5089 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005091 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 if (**arg != NUL)
5094 ++*arg;
5095 break;
5096
5097 /*
5098 * nested expression: (expression).
5099 */
5100 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005101 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 if (**arg == ')')
5103 ++*arg;
5104 else if (ret == OK)
5105 {
5106 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005107 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 ret = FAIL;
5109 }
5110 break;
5111
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 break;
5114 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005115
5116 if (ret == NOTDONE)
5117 {
5118 /*
5119 * Must be a variable or function name.
5120 * Can also be a curly-braces kind of name: {expr}.
5121 */
5122 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005123 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 if (alias != NULL)
5125 s = alias;
5126
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005127 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128 ret = FAIL;
5129 else
5130 {
5131 if (**arg == '(') /* recursive! */
5132 {
5133 /* If "s" is the name of a variable of type VAR_FUNC
5134 * use its contents. */
5135 s = deref_func_name(s, &len);
5136
5137 /* Invoke the function. */
5138 ret = get_func_tv(s, len, rettv, arg,
5139 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005140 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 /* Stop the expression evaluation when immediately
5142 * aborting on error, or when an interrupt occurred or
5143 * an exception was thrown but not caught. */
5144 if (aborting())
5145 {
5146 if (ret == OK)
5147 clear_tv(rettv);
5148 ret = FAIL;
5149 }
5150 }
5151 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005152 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005153 else
5154 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005156 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 }
5158
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 *arg = skipwhite(*arg);
5160
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005161 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5162 * expr(expr). */
5163 if (ret == OK)
5164 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
5166 /*
5167 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5168 */
5169 if (ret == OK && evaluate && end_leader > start_leader)
5170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005171 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005172 int val = 0;
5173#ifdef FEAT_FLOAT
5174 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005175
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005176 if (rettv->v_type == VAR_FLOAT)
5177 f = rettv->vval.v_float;
5178 else
5179#endif
5180 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005181 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005183 clear_tv(rettv);
5184 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005186 else
5187 {
5188 while (end_leader > start_leader)
5189 {
5190 --end_leader;
5191 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005192 {
5193#ifdef FEAT_FLOAT
5194 if (rettv->v_type == VAR_FLOAT)
5195 f = !f;
5196 else
5197#endif
5198 val = !val;
5199 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005200 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005201 {
5202#ifdef FEAT_FLOAT
5203 if (rettv->v_type == VAR_FLOAT)
5204 f = -f;
5205 else
5206#endif
5207 val = -val;
5208 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005209 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005210#ifdef FEAT_FLOAT
5211 if (rettv->v_type == VAR_FLOAT)
5212 {
5213 clear_tv(rettv);
5214 rettv->vval.v_float = f;
5215 }
5216 else
5217#endif
5218 {
5219 clear_tv(rettv);
5220 rettv->v_type = VAR_NUMBER;
5221 rettv->vval.v_number = val;
5222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
5225
5226 return ret;
5227}
5228
5229/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005230 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5231 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5233 */
5234 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005235eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005237 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005239 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240{
5241 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005242 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005244 long len = -1;
5245 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005249 if (rettv->v_type == VAR_FUNC
5250#ifdef FEAT_FLOAT
5251 || rettv->v_type == VAR_FLOAT
5252#endif
5253 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005255 if (verbose)
5256 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 return FAIL;
5258 }
5259
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005262 /*
5263 * dict.name
5264 */
5265 key = *arg + 1;
5266 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5267 ;
5268 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 }
5272 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 /*
5275 * something[idx]
5276 *
5277 * Get the (first) variable from inside the [].
5278 */
5279 *arg = skipwhite(*arg + 1);
5280 if (**arg == ':')
5281 empty1 = TRUE;
5282 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5283 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005284 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5285 {
5286 /* not a number or string */
5287 clear_tv(&var1);
5288 return FAIL;
5289 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290
5291 /*
5292 * Get the second variable from inside the [:].
5293 */
5294 if (**arg == ':')
5295 {
5296 range = TRUE;
5297 *arg = skipwhite(*arg + 1);
5298 if (**arg == ']')
5299 empty2 = TRUE;
5300 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302 if (!empty1)
5303 clear_tv(&var1);
5304 return FAIL;
5305 }
5306 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5307 {
5308 /* not a number or string */
5309 if (!empty1)
5310 clear_tv(&var1);
5311 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312 return FAIL;
5313 }
5314 }
5315
5316 /* Check for the ']'. */
5317 if (**arg != ']')
5318 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005319 if (verbose)
5320 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 clear_tv(&var1);
5322 if (range)
5323 clear_tv(&var2);
5324 return FAIL;
5325 }
5326 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 }
5328
5329 if (evaluate)
5330 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331 n1 = 0;
5332 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005334 n1 = get_tv_number(&var1);
5335 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 }
5337 if (range)
5338 {
5339 if (empty2)
5340 n2 = -1;
5341 else
5342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005343 n2 = get_tv_number(&var2);
5344 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 }
5346 }
5347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005348 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 {
5350 case VAR_NUMBER:
5351 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005352 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 len = (long)STRLEN(s);
5354 if (range)
5355 {
5356 /* The resulting variable is a substring. If the indexes
5357 * are out of range the result is empty. */
5358 if (n1 < 0)
5359 {
5360 n1 = len + n1;
5361 if (n1 < 0)
5362 n1 = 0;
5363 }
5364 if (n2 < 0)
5365 n2 = len + n2;
5366 else if (n2 >= len)
5367 n2 = len;
5368 if (n1 >= len || n2 < 0 || n1 > n2)
5369 s = NULL;
5370 else
5371 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5372 }
5373 else
5374 {
5375 /* The resulting variable is a string of a single
5376 * character. If the index is too big or negative the
5377 * result is empty. */
5378 if (n1 >= len || n1 < 0)
5379 s = NULL;
5380 else
5381 s = vim_strnsave(s + n1, 1);
5382 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005383 clear_tv(rettv);
5384 rettv->v_type = VAR_STRING;
5385 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 break;
5387
5388 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005389 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 if (n1 < 0)
5391 n1 = len + n1;
5392 if (!empty1 && (n1 < 0 || n1 >= len))
5393 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005394 /* For a range we allow invalid values and return an empty
5395 * list. A list index out of range is an error. */
5396 if (!range)
5397 {
5398 if (verbose)
5399 EMSGN(_(e_listidx), n1);
5400 return FAIL;
5401 }
5402 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 }
5404 if (range)
5405 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005406 list_T *l;
5407 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408
5409 if (n2 < 0)
5410 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005411 else if (n2 >= len)
5412 n2 = len - 1;
5413 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005414 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 l = list_alloc();
5416 if (l == NULL)
5417 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 n1 <= n2; ++n1)
5420 {
5421 if (list_append_tv(l, &item->li_tv) == FAIL)
5422 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005423 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 return FAIL;
5425 }
5426 item = item->li_next;
5427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005428 clear_tv(rettv);
5429 rettv->v_type = VAR_LIST;
5430 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005431 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 }
5433 else
5434 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005435 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005436 clear_tv(rettv);
5437 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 }
5439 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005440
5441 case VAR_DICT:
5442 if (range)
5443 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005444 if (verbose)
5445 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 if (len == -1)
5447 clear_tv(&var1);
5448 return FAIL;
5449 }
5450 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005451 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005452
5453 if (len == -1)
5454 {
5455 key = get_tv_string(&var1);
5456 if (*key == NUL)
5457 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005458 if (verbose)
5459 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460 clear_tv(&var1);
5461 return FAIL;
5462 }
5463 }
5464
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005465 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005467 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005468 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469 if (len == -1)
5470 clear_tv(&var1);
5471 if (item == NULL)
5472 return FAIL;
5473
5474 copy_tv(&item->di_tv, &var1);
5475 clear_tv(rettv);
5476 *rettv = var1;
5477 }
5478 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 }
5481
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 return OK;
5483}
5484
5485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 * Get an option value.
5487 * "arg" points to the '&' or '+' before the option name.
5488 * "arg" is advanced to character after the option name.
5489 * Return OK or FAIL.
5490 */
5491 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005494 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 int evaluate;
5496{
5497 char_u *option_end;
5498 long numval;
5499 char_u *stringval;
5500 int opt_type;
5501 int c;
5502 int working = (**arg == '+'); /* has("+option") */
5503 int ret = OK;
5504 int opt_flags;
5505
5506 /*
5507 * Isolate the option name and find its value.
5508 */
5509 option_end = find_option_end(arg, &opt_flags);
5510 if (option_end == NULL)
5511 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 EMSG2(_("E112: Option name missing: %s"), *arg);
5514 return FAIL;
5515 }
5516
5517 if (!evaluate)
5518 {
5519 *arg = option_end;
5520 return OK;
5521 }
5522
5523 c = *option_end;
5524 *option_end = NUL;
5525 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527
5528 if (opt_type == -3) /* invalid name */
5529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 EMSG2(_("E113: Unknown option: %s"), *arg);
5532 ret = FAIL;
5533 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005534 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
5536 if (opt_type == -2) /* hidden string option */
5537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005538 rettv->v_type = VAR_STRING;
5539 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 }
5541 else if (opt_type == -1) /* hidden number option */
5542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005543 rettv->v_type = VAR_NUMBER;
5544 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546 else if (opt_type == 1) /* number option */
5547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 rettv->v_type = VAR_NUMBER;
5549 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
5551 else /* string option */
5552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 rettv->v_type = VAR_STRING;
5554 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556 }
5557 else if (working && (opt_type == -2 || opt_type == -1))
5558 ret = FAIL;
5559
5560 *option_end = c; /* put back for error messages */
5561 *arg = option_end;
5562
5563 return ret;
5564}
5565
5566/*
5567 * Allocate a variable for a string constant.
5568 * Return OK or FAIL.
5569 */
5570 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 int evaluate;
5575{
5576 char_u *p;
5577 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 int extra = 0;
5579
5580 /*
5581 * Find the end of the string, skipping backslashed characters.
5582 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005583 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 {
5585 if (*p == '\\' && p[1] != NUL)
5586 {
5587 ++p;
5588 /* A "\<x>" form occupies at least 4 characters, and produces up
5589 * to 6 characters: reserve space for 2 extra */
5590 if (*p == '<')
5591 extra += 2;
5592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 }
5594
5595 if (*p != '"')
5596 {
5597 EMSG2(_("E114: Missing quote: %s"), *arg);
5598 return FAIL;
5599 }
5600
5601 /* If only parsing, set *arg and return here */
5602 if (!evaluate)
5603 {
5604 *arg = p + 1;
5605 return OK;
5606 }
5607
5608 /*
5609 * Copy the string into allocated memory, handling backslashed
5610 * characters.
5611 */
5612 name = alloc((unsigned)(p - *arg + extra));
5613 if (name == NULL)
5614 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 rettv->v_type = VAR_STRING;
5616 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
Bram Moolenaar8c711452005-01-14 21:53:12 +00005618 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
5620 if (*p == '\\')
5621 {
5622 switch (*++p)
5623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005624 case 'b': *name++ = BS; ++p; break;
5625 case 'e': *name++ = ESC; ++p; break;
5626 case 'f': *name++ = FF; ++p; break;
5627 case 'n': *name++ = NL; ++p; break;
5628 case 'r': *name++ = CAR; ++p; break;
5629 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
5631 case 'X': /* hex: "\x1", "\x12" */
5632 case 'x':
5633 case 'u': /* Unicode: "\u0023" */
5634 case 'U':
5635 if (vim_isxdigit(p[1]))
5636 {
5637 int n, nr;
5638 int c = toupper(*p);
5639
5640 if (c == 'X')
5641 n = 2;
5642 else
5643 n = 4;
5644 nr = 0;
5645 while (--n >= 0 && vim_isxdigit(p[1]))
5646 {
5647 ++p;
5648 nr = (nr << 4) + hex2nr(*p);
5649 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651#ifdef FEAT_MBYTE
5652 /* For "\u" store the number according to
5653 * 'encoding'. */
5654 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005655 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 else
5657#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 break;
5661
5662 /* octal: "\1", "\12", "\123" */
5663 case '0':
5664 case '1':
5665 case '2':
5666 case '3':
5667 case '4':
5668 case '5':
5669 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 case '7': *name = *p++ - '0';
5671 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 *name = (*name << 3) + *p++ - '0';
5674 if (*p >= '0' && *p <= '7')
5675 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 break;
5679
5680 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 if (extra != 0)
5683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 break;
5686 }
5687 /* FALLTHROUGH */
5688
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 break;
5691 }
5692 }
5693 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 *arg = p + 1;
5699
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 return OK;
5701}
5702
5703/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 * Return OK or FAIL.
5706 */
5707 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005708get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 int evaluate;
5712{
5713 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005714 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005715 int reduce = 0;
5716
5717 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719 */
5720 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005723 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005725 break;
5726 ++reduce;
5727 ++p;
5728 }
5729 }
5730
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005734 return FAIL;
5735 }
5736
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005738 if (!evaluate)
5739 {
5740 *arg = p + 1;
5741 return OK;
5742 }
5743
5744 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005746 */
5747 str = alloc((unsigned)((p - *arg) - reduce));
5748 if (str == NULL)
5749 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 rettv->v_type = VAR_STRING;
5751 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 break;
5759 ++p;
5760 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 *arg = p + 1;
5765
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 return OK;
5767}
5768
5769/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770 * Allocate a variable for a List and fill it from "*arg".
5771 * Return OK or FAIL.
5772 */
5773 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005774get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005776 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777 int evaluate;
5778{
Bram Moolenaar33570922005-01-25 22:26:29 +00005779 list_T *l = NULL;
5780 typval_T tv;
5781 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782
5783 if (evaluate)
5784 {
5785 l = list_alloc();
5786 if (l == NULL)
5787 return FAIL;
5788 }
5789
5790 *arg = skipwhite(*arg + 1);
5791 while (**arg != ']' && **arg != NUL)
5792 {
5793 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5794 goto failret;
5795 if (evaluate)
5796 {
5797 item = listitem_alloc();
5798 if (item != NULL)
5799 {
5800 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005801 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802 list_append(l, item);
5803 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005804 else
5805 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 }
5807
5808 if (**arg == ']')
5809 break;
5810 if (**arg != ',')
5811 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 goto failret;
5814 }
5815 *arg = skipwhite(*arg + 1);
5816 }
5817
5818 if (**arg != ']')
5819 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821failret:
5822 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005823 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 return FAIL;
5825 }
5826
5827 *arg = skipwhite(*arg + 1);
5828 if (evaluate)
5829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005830 rettv->v_type = VAR_LIST;
5831 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 ++l->lv_refcount;
5833 }
5834
5835 return OK;
5836}
5837
5838/*
5839 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005840 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005842 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843list_alloc()
5844{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005845 list_T *l;
5846
5847 l = (list_T *)alloc_clear(sizeof(list_T));
5848 if (l != NULL)
5849 {
5850 /* Prepend the list to the list of lists for garbage collection. */
5851 if (first_list != NULL)
5852 first_list->lv_used_prev = l;
5853 l->lv_used_prev = NULL;
5854 l->lv_used_next = first_list;
5855 first_list = l;
5856 }
5857 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858}
5859
5860/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005861 * Allocate an empty list for a return value.
5862 * Returns OK or FAIL.
5863 */
5864 static int
5865rettv_list_alloc(rettv)
5866 typval_T *rettv;
5867{
5868 list_T *l = list_alloc();
5869
5870 if (l == NULL)
5871 return FAIL;
5872
5873 rettv->vval.v_list = l;
5874 rettv->v_type = VAR_LIST;
5875 ++l->lv_refcount;
5876 return OK;
5877}
5878
5879/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 * Unreference a list: decrement the reference count and free it when it
5881 * becomes zero.
5882 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005883 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005887 if (l != NULL && --l->lv_refcount <= 0)
5888 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889}
5890
5891/*
5892 * Free a list, including all items it points to.
5893 * Ignores the reference count.
5894 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005895 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005896list_free(l, recurse)
5897 list_T *l;
5898 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899{
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005902 /* Remove the list from the list of lists for garbage collection. */
5903 if (l->lv_used_prev == NULL)
5904 first_list = l->lv_used_next;
5905 else
5906 l->lv_used_prev->lv_used_next = l->lv_used_next;
5907 if (l->lv_used_next != NULL)
5908 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5909
Bram Moolenaard9fba312005-06-26 22:34:35 +00005910 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005912 /* Remove the item before deleting it. */
5913 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005914 if (recurse || (item->li_tv.v_type != VAR_LIST
5915 && item->li_tv.v_type != VAR_DICT))
5916 clear_tv(&item->li_tv);
5917 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 }
5919 vim_free(l);
5920}
5921
5922/*
5923 * Allocate a list item.
5924 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005925 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926listitem_alloc()
5927{
Bram Moolenaar33570922005-01-25 22:26:29 +00005928 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929}
5930
5931/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005932 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 */
5934 static void
5935listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005938 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 vim_free(item);
5940}
5941
5942/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005943 * Remove a list item from a List and free it. Also clears the value.
5944 */
5945 static void
5946listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005947 list_T *l;
5948 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005949{
5950 list_remove(l, item, item);
5951 listitem_free(item);
5952}
5953
5954/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 * Get the number of items in a list.
5956 */
5957 static long
5958list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005959 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961 if (l == NULL)
5962 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005963 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964}
5965
5966/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005967 * Return TRUE when two lists have exactly the same values.
5968 */
5969 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005970list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 list_T *l1;
5972 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005973 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005974 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005975{
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005977
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005978 if (l1 == NULL || l2 == NULL)
5979 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005980 if (l1 == l2)
5981 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005982 if (list_len(l1) != list_len(l2))
5983 return FALSE;
5984
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005985 for (item1 = l1->lv_first, item2 = l2->lv_first;
5986 item1 != NULL && item2 != NULL;
5987 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005988 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005989 return FALSE;
5990 return item1 == NULL && item2 == NULL;
5991}
5992
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005993#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5994 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005995/*
5996 * Return the dictitem that an entry in a hashtable points to.
5997 */
5998 dictitem_T *
5999dict_lookup(hi)
6000 hashitem_T *hi;
6001{
6002 return HI2DI(hi);
6003}
6004#endif
6005
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006006/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006007 * Return TRUE when two dictionaries have exactly the same key/values.
6008 */
6009 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006010dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 dict_T *d1;
6012 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006013 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006014 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006015{
Bram Moolenaar33570922005-01-25 22:26:29 +00006016 hashitem_T *hi;
6017 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006018 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006019
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006020 if (d1 == NULL || d2 == NULL)
6021 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006022 if (d1 == d2)
6023 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006024 if (dict_len(d1) != dict_len(d2))
6025 return FALSE;
6026
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006027 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006029 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006030 if (!HASHITEM_EMPTY(hi))
6031 {
6032 item2 = dict_find(d2, hi->hi_key, -1);
6033 if (item2 == NULL)
6034 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006035 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006036 return FALSE;
6037 --todo;
6038 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006039 }
6040 return TRUE;
6041}
6042
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006043static int tv_equal_recurse_limit;
6044
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006045/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006046 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006047 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006048 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006049 */
6050 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006051tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 typval_T *tv1;
6053 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006054 int ic; /* ignore case */
6055 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006056{
6057 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006058 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006059 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006060 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006061
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006062 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006063 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006064
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006065 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006066 * recursiveness to a limit. We guess they are equal then.
6067 * A fixed limit has the problem of still taking an awful long time.
6068 * Reduce the limit every time running into it. That should work fine for
6069 * deeply linked structures that are not recursively linked and catch
6070 * recursiveness quickly. */
6071 if (!recursive)
6072 tv_equal_recurse_limit = 1000;
6073 if (recursive_cnt >= tv_equal_recurse_limit)
6074 {
6075 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006076 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006077 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006078
6079 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006080 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006081 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006082 ++recursive_cnt;
6083 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6084 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006085 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006086
6087 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006088 ++recursive_cnt;
6089 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6090 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006091 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006092
6093 case VAR_FUNC:
6094 return (tv1->vval.v_string != NULL
6095 && tv2->vval.v_string != NULL
6096 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6097
6098 case VAR_NUMBER:
6099 return tv1->vval.v_number == tv2->vval.v_number;
6100
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006101#ifdef FEAT_FLOAT
6102 case VAR_FLOAT:
6103 return tv1->vval.v_float == tv2->vval.v_float;
6104#endif
6105
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006106 case VAR_STRING:
6107 s1 = get_tv_string_buf(tv1, buf1);
6108 s2 = get_tv_string_buf(tv2, buf2);
6109 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006111
6112 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113 return TRUE;
6114}
6115
6116/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117 * Locate item with index "n" in list "l" and return it.
6118 * A negative index is counted from the end; -1 is the last item.
6119 * Returns NULL when "n" is out of range.
6120 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006121 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006124 long n;
6125{
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006127 long idx;
6128
6129 if (l == NULL)
6130 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006131
6132 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006133 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006134 n = l->lv_len + n;
6135
6136 /* Check for index out of range. */
6137 if (n < 0 || n >= l->lv_len)
6138 return NULL;
6139
6140 /* When there is a cached index may start search from there. */
6141 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006143 if (n < l->lv_idx / 2)
6144 {
6145 /* closest to the start of the list */
6146 item = l->lv_first;
6147 idx = 0;
6148 }
6149 else if (n > (l->lv_idx + l->lv_len) / 2)
6150 {
6151 /* closest to the end of the list */
6152 item = l->lv_last;
6153 idx = l->lv_len - 1;
6154 }
6155 else
6156 {
6157 /* closest to the cached index */
6158 item = l->lv_idx_item;
6159 idx = l->lv_idx;
6160 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006161 }
6162 else
6163 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006164 if (n < l->lv_len / 2)
6165 {
6166 /* closest to the start of the list */
6167 item = l->lv_first;
6168 idx = 0;
6169 }
6170 else
6171 {
6172 /* closest to the end of the list */
6173 item = l->lv_last;
6174 idx = l->lv_len - 1;
6175 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006177
6178 while (n > idx)
6179 {
6180 /* search forward */
6181 item = item->li_next;
6182 ++idx;
6183 }
6184 while (n < idx)
6185 {
6186 /* search backward */
6187 item = item->li_prev;
6188 --idx;
6189 }
6190
6191 /* cache the used index */
6192 l->lv_idx = idx;
6193 l->lv_idx_item = item;
6194
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195 return item;
6196}
6197
6198/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006199 * Get list item "l[idx]" as a number.
6200 */
6201 static long
6202list_find_nr(l, idx, errorp)
6203 list_T *l;
6204 long idx;
6205 int *errorp; /* set to TRUE when something wrong */
6206{
6207 listitem_T *li;
6208
6209 li = list_find(l, idx);
6210 if (li == NULL)
6211 {
6212 if (errorp != NULL)
6213 *errorp = TRUE;
6214 return -1L;
6215 }
6216 return get_tv_number_chk(&li->li_tv, errorp);
6217}
6218
6219/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006220 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6221 */
6222 char_u *
6223list_find_str(l, idx)
6224 list_T *l;
6225 long idx;
6226{
6227 listitem_T *li;
6228
6229 li = list_find(l, idx - 1);
6230 if (li == NULL)
6231 {
6232 EMSGN(_(e_listidx), idx);
6233 return NULL;
6234 }
6235 return get_tv_string(&li->li_tv);
6236}
6237
6238/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006239 * Locate "item" list "l" and return its index.
6240 * Returns -1 when "item" is not in the list.
6241 */
6242 static long
6243list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006244 list_T *l;
6245 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006246{
6247 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006248 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006249
6250 if (l == NULL)
6251 return -1;
6252 idx = 0;
6253 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6254 ++idx;
6255 if (li == NULL)
6256 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006257 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006258}
6259
6260/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 * Append item "item" to the end of list "l".
6262 */
6263 static void
6264list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006265 list_T *l;
6266 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267{
6268 if (l->lv_last == NULL)
6269 {
6270 /* empty list */
6271 l->lv_first = item;
6272 l->lv_last = item;
6273 item->li_prev = NULL;
6274 }
6275 else
6276 {
6277 l->lv_last->li_next = item;
6278 item->li_prev = l->lv_last;
6279 l->lv_last = item;
6280 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006281 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006282 item->li_next = NULL;
6283}
6284
6285/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006287 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006289 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 list_T *l;
6292 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006294 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295
Bram Moolenaar05159a02005-02-26 23:04:13 +00006296 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006298 copy_tv(tv, &li->li_tv);
6299 list_append(l, li);
6300 return OK;
6301}
6302
6303/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006304 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006305 * Return FAIL when out of memory.
6306 */
6307 int
6308list_append_dict(list, dict)
6309 list_T *list;
6310 dict_T *dict;
6311{
6312 listitem_T *li = listitem_alloc();
6313
6314 if (li == NULL)
6315 return FAIL;
6316 li->li_tv.v_type = VAR_DICT;
6317 li->li_tv.v_lock = 0;
6318 li->li_tv.vval.v_dict = dict;
6319 list_append(list, li);
6320 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321 return OK;
6322}
6323
6324/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006325 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006326 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006327 * Returns FAIL when out of memory.
6328 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006329 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006330list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006331 list_T *l;
6332 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006333 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006334{
6335 listitem_T *li = listitem_alloc();
6336
6337 if (li == NULL)
6338 return FAIL;
6339 list_append(l, li);
6340 li->li_tv.v_type = VAR_STRING;
6341 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006342 if (str == NULL)
6343 li->li_tv.vval.v_string = NULL;
6344 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006345 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006346 return FAIL;
6347 return OK;
6348}
6349
6350/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006351 * Append "n" to list "l".
6352 * Returns FAIL when out of memory.
6353 */
6354 static int
6355list_append_number(l, n)
6356 list_T *l;
6357 varnumber_T n;
6358{
6359 listitem_T *li;
6360
6361 li = listitem_alloc();
6362 if (li == NULL)
6363 return FAIL;
6364 li->li_tv.v_type = VAR_NUMBER;
6365 li->li_tv.v_lock = 0;
6366 li->li_tv.vval.v_number = n;
6367 list_append(l, li);
6368 return OK;
6369}
6370
6371/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006373 * If "item" is NULL append at the end.
6374 * Return FAIL when out of memory.
6375 */
6376 static int
6377list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 list_T *l;
6379 typval_T *tv;
6380 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006381{
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006383
6384 if (ni == NULL)
6385 return FAIL;
6386 copy_tv(tv, &ni->li_tv);
6387 if (item == NULL)
6388 /* Append new item at end of list. */
6389 list_append(l, ni);
6390 else
6391 {
6392 /* Insert new item before existing item. */
6393 ni->li_prev = item->li_prev;
6394 ni->li_next = item;
6395 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006396 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006397 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006398 ++l->lv_idx;
6399 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006400 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006401 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006403 l->lv_idx_item = NULL;
6404 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006405 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006406 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407 }
6408 return OK;
6409}
6410
6411/*
6412 * Extend "l1" with "l2".
6413 * If "bef" is NULL append at the end, otherwise insert before this item.
6414 * Returns FAIL when out of memory.
6415 */
6416 static int
6417list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 list_T *l1;
6419 list_T *l2;
6420 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421{
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006423 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006425 /* We also quit the loop when we have inserted the original item count of
6426 * the list, avoid a hang when we extend a list with itself. */
6427 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6429 return FAIL;
6430 return OK;
6431}
6432
6433/*
6434 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6435 * Return FAIL when out of memory.
6436 */
6437 static int
6438list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006439 list_T *l1;
6440 list_T *l2;
6441 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006442{
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006444
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006445 if (l1 == NULL || l2 == NULL)
6446 return FAIL;
6447
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006449 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450 if (l == NULL)
6451 return FAIL;
6452 tv->v_type = VAR_LIST;
6453 tv->vval.v_list = l;
6454
6455 /* append all items from the second list */
6456 return list_extend(l, l2, NULL);
6457}
6458
6459/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006460 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006462 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463 * Returns NULL when out of memory.
6464 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006466list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006467 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006468 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006469 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006470{
Bram Moolenaar33570922005-01-25 22:26:29 +00006471 list_T *copy;
6472 listitem_T *item;
6473 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006474
6475 if (orig == NULL)
6476 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006477
6478 copy = list_alloc();
6479 if (copy != NULL)
6480 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006481 if (copyID != 0)
6482 {
6483 /* Do this before adding the items, because one of the items may
6484 * refer back to this list. */
6485 orig->lv_copyID = copyID;
6486 orig->lv_copylist = copy;
6487 }
6488 for (item = orig->lv_first; item != NULL && !got_int;
6489 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490 {
6491 ni = listitem_alloc();
6492 if (ni == NULL)
6493 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006495 {
6496 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6497 {
6498 vim_free(ni);
6499 break;
6500 }
6501 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006503 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504 list_append(copy, ni);
6505 }
6506 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006507 if (item != NULL)
6508 {
6509 list_unref(copy);
6510 copy = NULL;
6511 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 }
6513
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514 return copy;
6515}
6516
6517/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006519 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006521 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006522list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 list_T *l;
6524 listitem_T *item;
6525 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526{
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529 /* notify watchers */
6530 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006532 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 list_fix_watch(l, ip);
6534 if (ip == item2)
6535 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537
6538 if (item2->li_next == NULL)
6539 l->lv_last = item->li_prev;
6540 else
6541 item2->li_next->li_prev = item->li_prev;
6542 if (item->li_prev == NULL)
6543 l->lv_first = item2->li_next;
6544 else
6545 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006546 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547}
6548
6549/*
6550 * Return an allocated string with the string representation of a list.
6551 * May return NULL.
6552 */
6553 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006554list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006555 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006556 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557{
6558 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559
6560 if (tv->vval.v_list == NULL)
6561 return NULL;
6562 ga_init2(&ga, (int)sizeof(char), 80);
6563 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006564 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006565 {
6566 vim_free(ga.ga_data);
6567 return NULL;
6568 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569 ga_append(&ga, ']');
6570 ga_append(&ga, NUL);
6571 return (char_u *)ga.ga_data;
6572}
6573
6574/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006575 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006576 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006577 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006578 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006579 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006580list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006581 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006582 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006583 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006584 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006585 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006586{
6587 int first = TRUE;
6588 char_u *tofree;
6589 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006590 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006591 char_u *s;
6592
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006593 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006594 {
6595 if (first)
6596 first = FALSE;
6597 else
6598 ga_concat(gap, sep);
6599
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006600 if (echo_style)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006601 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006602 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006603 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006604 if (s != NULL)
6605 ga_concat(gap, s);
6606 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 if (s == NULL)
6608 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006609 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006610 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006612}
6613
6614/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006615 * Garbage collection for lists and dictionaries.
6616 *
6617 * We use reference counts to be able to free most items right away when they
6618 * are no longer used. But for composite items it's possible that it becomes
6619 * unused while the reference count is > 0: When there is a recursive
6620 * reference. Example:
6621 * :let l = [1, 2, 3]
6622 * :let d = {9: l}
6623 * :let l[1] = d
6624 *
6625 * Since this is quite unusual we handle this with garbage collection: every
6626 * once in a while find out which lists and dicts are not referenced from any
6627 * variable.
6628 *
6629 * Here is a good reference text about garbage collection (refers to Python
6630 * but it applies to all reference-counting mechanisms):
6631 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006632 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006633
6634/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006635 * Do garbage collection for lists and dicts.
6636 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006637 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006638 int
6639garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006640{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006641 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006642 buf_T *buf;
6643 win_T *wp;
6644 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006645 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006646 int did_free;
6647 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006648#ifdef FEAT_WINDOWS
6649 tabpage_T *tp;
6650#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006651
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006652 /* Only do this once. */
6653 want_garbage_collect = FALSE;
6654 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006655 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006656
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006657 /* We advance by two because we add one for items referenced through
6658 * previous_funccal. */
6659 current_copyID += COPYID_INC;
6660 copyID = current_copyID;
6661
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006662 /*
6663 * 1. Go through all accessible variables and mark all lists and dicts
6664 * with copyID.
6665 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006666
6667 /* Don't free variables in the previous_funccal list unless they are only
6668 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006669 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006670 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6671 {
6672 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6673 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6674 }
6675
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006676 /* script-local variables */
6677 for (i = 1; i <= ga_scripts.ga_len; ++i)
6678 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6679
6680 /* buffer-local variables */
6681 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6682 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6683
6684 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006685 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006686 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6687
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006688#ifdef FEAT_WINDOWS
6689 /* tabpage-local variables */
6690 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6691 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6692#endif
6693
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006694 /* global variables */
6695 set_ref_in_ht(&globvarht, copyID);
6696
6697 /* function-local variables */
6698 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6699 {
6700 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6701 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6702 }
6703
Bram Moolenaard812df62008-11-09 12:46:09 +00006704 /* v: vars */
6705 set_ref_in_ht(&vimvarht, copyID);
6706
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006707 /*
6708 * 2. Free lists and dictionaries that are not referenced.
6709 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006710 did_free = free_unref_items(copyID);
6711
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006712 /*
6713 * 3. Check if any funccal can be freed now.
6714 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006715 for (pfc = &previous_funccal; *pfc != NULL; )
6716 {
6717 if (can_free_funccal(*pfc, copyID))
6718 {
6719 fc = *pfc;
6720 *pfc = fc->caller;
6721 free_funccal(fc, TRUE);
6722 did_free = TRUE;
6723 did_free_funccal = TRUE;
6724 }
6725 else
6726 pfc = &(*pfc)->caller;
6727 }
6728 if (did_free_funccal)
6729 /* When a funccal was freed some more items might be garbage
6730 * collected, so run again. */
6731 (void)garbage_collect();
6732
6733 return did_free;
6734}
6735
6736/*
6737 * Free lists and dictionaries that are no longer referenced.
6738 */
6739 static int
6740free_unref_items(copyID)
6741 int copyID;
6742{
6743 dict_T *dd;
6744 list_T *ll;
6745 int did_free = FALSE;
6746
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006748 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006749 */
6750 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006751 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006752 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006753 /* Free the Dictionary and ordinary items it contains, but don't
6754 * recurse into Lists and Dictionaries, they will be in the list
6755 * of dicts or list of lists. */
6756 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757 did_free = TRUE;
6758
6759 /* restart, next dict may also have been freed */
6760 dd = first_dict;
6761 }
6762 else
6763 dd = dd->dv_used_next;
6764
6765 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006766 * Go through the list of lists and free items without the copyID.
6767 * But don't free a list that has a watcher (used in a for loop), these
6768 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006769 */
6770 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006771 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6772 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006773 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006774 /* Free the List and ordinary items it contains, but don't recurse
6775 * into Lists and Dictionaries, they will be in the list of dicts
6776 * or list of lists. */
6777 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006778 did_free = TRUE;
6779
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006780 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781 ll = first_list;
6782 }
6783 else
6784 ll = ll->lv_used_next;
6785
6786 return did_free;
6787}
6788
6789/*
6790 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6791 */
6792 static void
6793set_ref_in_ht(ht, copyID)
6794 hashtab_T *ht;
6795 int copyID;
6796{
6797 int todo;
6798 hashitem_T *hi;
6799
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006800 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006801 for (hi = ht->ht_array; todo > 0; ++hi)
6802 if (!HASHITEM_EMPTY(hi))
6803 {
6804 --todo;
6805 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6806 }
6807}
6808
6809/*
6810 * Mark all lists and dicts referenced through list "l" with "copyID".
6811 */
6812 static void
6813set_ref_in_list(l, copyID)
6814 list_T *l;
6815 int copyID;
6816{
6817 listitem_T *li;
6818
6819 for (li = l->lv_first; li != NULL; li = li->li_next)
6820 set_ref_in_item(&li->li_tv, copyID);
6821}
6822
6823/*
6824 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6825 */
6826 static void
6827set_ref_in_item(tv, copyID)
6828 typval_T *tv;
6829 int copyID;
6830{
6831 dict_T *dd;
6832 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006833
6834 switch (tv->v_type)
6835 {
6836 case VAR_DICT:
6837 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006838 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840 /* Didn't see this dict yet. */
6841 dd->dv_copyID = copyID;
6842 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006843 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006844 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006845
6846 case VAR_LIST:
6847 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006848 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 /* Didn't see this list yet. */
6851 ll->lv_copyID = copyID;
6852 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006853 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006855 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006857}
6858
6859/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006860 * Allocate an empty header for a dictionary.
6861 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006862 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006863dict_alloc()
6864{
Bram Moolenaar33570922005-01-25 22:26:29 +00006865 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006866
Bram Moolenaar33570922005-01-25 22:26:29 +00006867 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006868 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006869 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006870 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006871 if (first_dict != NULL)
6872 first_dict->dv_used_prev = d;
6873 d->dv_used_next = first_dict;
6874 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006875 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876
Bram Moolenaar33570922005-01-25 22:26:29 +00006877 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006878 d->dv_lock = 0;
6879 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006880 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006881 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006882 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006883}
6884
6885/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006886 * Allocate an empty dict for a return value.
6887 * Returns OK or FAIL.
6888 */
6889 static int
6890rettv_dict_alloc(rettv)
6891 typval_T *rettv;
6892{
6893 dict_T *d = dict_alloc();
6894
6895 if (d == NULL)
6896 return FAIL;
6897
6898 rettv->vval.v_dict = d;
6899 rettv->v_type = VAR_DICT;
6900 ++d->dv_refcount;
6901 return OK;
6902}
6903
6904
6905/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006906 * Unreference a Dictionary: decrement the reference count and free it when it
6907 * becomes zero.
6908 */
Bram Moolenaar82139082011-09-14 16:52:09 +02006909 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006910dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006911 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006912{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006913 if (d != NULL && --d->dv_refcount <= 0)
6914 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006915}
6916
6917/*
6918 * Free a Dictionary, including all items it contains.
6919 * Ignores the reference count.
6920 */
6921 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006922dict_free(d, recurse)
6923 dict_T *d;
6924 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006925{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006926 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006927 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006928 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006929
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930 /* Remove the dict from the list of dicts for garbage collection. */
6931 if (d->dv_used_prev == NULL)
6932 first_dict = d->dv_used_next;
6933 else
6934 d->dv_used_prev->dv_used_next = d->dv_used_next;
6935 if (d->dv_used_next != NULL)
6936 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6937
6938 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006939 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006940 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006941 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006942 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006943 if (!HASHITEM_EMPTY(hi))
6944 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006945 /* Remove the item before deleting it, just in case there is
6946 * something recursive causing trouble. */
6947 di = HI2DI(hi);
6948 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006949 if (recurse || (di->di_tv.v_type != VAR_LIST
6950 && di->di_tv.v_type != VAR_DICT))
6951 clear_tv(&di->di_tv);
6952 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006953 --todo;
6954 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006956 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006957 vim_free(d);
6958}
6959
6960/*
6961 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006962 * The "key" is copied to the new item.
6963 * Note that the value of the item "di_tv" still needs to be initialized!
6964 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006965 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006966 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006967dictitem_alloc(key)
6968 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969{
Bram Moolenaar33570922005-01-25 22:26:29 +00006970 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006971
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006972 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006973 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006974 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006975 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006976 di->di_flags = 0;
6977 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006978 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006979}
6980
6981/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006982 * Make a copy of a Dictionary item.
6983 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006985dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006986 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006987{
Bram Moolenaar33570922005-01-25 22:26:29 +00006988 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006989
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006990 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6991 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006992 if (di != NULL)
6993 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006994 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006995 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006996 copy_tv(&org->di_tv, &di->di_tv);
6997 }
6998 return di;
6999}
7000
7001/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007002 * Remove item "item" from Dictionary "dict" and free it.
7003 */
7004 static void
7005dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007006 dict_T *dict;
7007 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007008{
Bram Moolenaar33570922005-01-25 22:26:29 +00007009 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007010
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007012 if (HASHITEM_EMPTY(hi))
7013 EMSG2(_(e_intern2), "dictitem_remove()");
7014 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007016 dictitem_free(item);
7017}
7018
7019/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007020 * Free a dict item. Also clears the value.
7021 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007022 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007023dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007024 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007025{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007026 clear_tv(&item->di_tv);
7027 vim_free(item);
7028}
7029
7030/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007031 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7032 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007033 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007034 * Returns NULL when out of memory.
7035 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007037dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007040 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007041{
Bram Moolenaar33570922005-01-25 22:26:29 +00007042 dict_T *copy;
7043 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007044 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007046
7047 if (orig == NULL)
7048 return NULL;
7049
7050 copy = dict_alloc();
7051 if (copy != NULL)
7052 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007053 if (copyID != 0)
7054 {
7055 orig->dv_copyID = copyID;
7056 orig->dv_copydict = copy;
7057 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007058 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007059 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007060 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007061 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007062 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007063 --todo;
7064
7065 di = dictitem_alloc(hi->hi_key);
7066 if (di == NULL)
7067 break;
7068 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007069 {
7070 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7071 copyID) == FAIL)
7072 {
7073 vim_free(di);
7074 break;
7075 }
7076 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007077 else
7078 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7079 if (dict_add(copy, di) == FAIL)
7080 {
7081 dictitem_free(di);
7082 break;
7083 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007084 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007085 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007086
Bram Moolenaare9a41262005-01-15 22:18:47 +00007087 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007088 if (todo > 0)
7089 {
7090 dict_unref(copy);
7091 copy = NULL;
7092 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007093 }
7094
7095 return copy;
7096}
7097
7098/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007100 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007101 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007102 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 dict_T *d;
7105 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106{
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108}
7109
Bram Moolenaar8c711452005-01-14 21:53:12 +00007110/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007111 * Add a number or string entry to dictionary "d".
7112 * When "str" is NULL use number "nr", otherwise use "str".
7113 * Returns FAIL when out of memory and when key already exists.
7114 */
7115 int
7116dict_add_nr_str(d, key, nr, str)
7117 dict_T *d;
7118 char *key;
7119 long nr;
7120 char_u *str;
7121{
7122 dictitem_T *item;
7123
7124 item = dictitem_alloc((char_u *)key);
7125 if (item == NULL)
7126 return FAIL;
7127 item->di_tv.v_lock = 0;
7128 if (str == NULL)
7129 {
7130 item->di_tv.v_type = VAR_NUMBER;
7131 item->di_tv.vval.v_number = nr;
7132 }
7133 else
7134 {
7135 item->di_tv.v_type = VAR_STRING;
7136 item->di_tv.vval.v_string = vim_strsave(str);
7137 }
7138 if (dict_add(d, item) == FAIL)
7139 {
7140 dictitem_free(item);
7141 return FAIL;
7142 }
7143 return OK;
7144}
7145
7146/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007147 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007148 * Returns FAIL when out of memory and when key already exists.
7149 */
7150 int
7151dict_add_list(d, key, list)
7152 dict_T *d;
7153 char *key;
7154 list_T *list;
7155{
7156 dictitem_T *item;
7157
7158 item = dictitem_alloc((char_u *)key);
7159 if (item == NULL)
7160 return FAIL;
7161 item->di_tv.v_lock = 0;
7162 item->di_tv.v_type = VAR_LIST;
7163 item->di_tv.vval.v_list = list;
7164 if (dict_add(d, item) == FAIL)
7165 {
7166 dictitem_free(item);
7167 return FAIL;
7168 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007169 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007170 return OK;
7171}
7172
7173/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007174 * Get the number of items in a Dictionary.
7175 */
7176 static long
7177dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007178 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007179{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 if (d == NULL)
7181 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007182 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183}
7184
7185/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007186 * Find item "key[len]" in Dictionary "d".
7187 * If "len" is negative use strlen(key).
7188 * Returns NULL when not found.
7189 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007190 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007191dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007192 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193 char_u *key;
7194 int len;
7195{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196#define AKEYLEN 200
7197 char_u buf[AKEYLEN];
7198 char_u *akey;
7199 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007200 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 if (len < 0)
7203 akey = key;
7204 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007205 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007206 tofree = akey = vim_strnsave(key, len);
7207 if (akey == NULL)
7208 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007209 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 else
7211 {
7212 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007213 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 akey = buf;
7215 }
7216
Bram Moolenaar33570922005-01-25 22:26:29 +00007217 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007218 vim_free(tofree);
7219 if (HASHITEM_EMPTY(hi))
7220 return NULL;
7221 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222}
7223
7224/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007225 * Get a string item from a dictionary.
7226 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007227 * Returns NULL if the entry doesn't exist or out of memory.
7228 */
7229 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007230get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007231 dict_T *d;
7232 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007233 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007234{
7235 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007236 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007237
7238 di = dict_find(d, key, -1);
7239 if (di == NULL)
7240 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007241 s = get_tv_string(&di->di_tv);
7242 if (save && s != NULL)
7243 s = vim_strsave(s);
7244 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007245}
7246
7247/*
7248 * Get a number item from a dictionary.
7249 * Returns 0 if the entry doesn't exist or out of memory.
7250 */
7251 long
7252get_dict_number(d, key)
7253 dict_T *d;
7254 char_u *key;
7255{
7256 dictitem_T *di;
7257
7258 di = dict_find(d, key, -1);
7259 if (di == NULL)
7260 return 0;
7261 return get_tv_number(&di->di_tv);
7262}
7263
7264/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 * Return an allocated string with the string representation of a Dictionary.
7266 * May return NULL.
7267 */
7268 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007269dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007270 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007271 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272{
7273 garray_T ga;
7274 int first = TRUE;
7275 char_u *tofree;
7276 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283 return NULL;
7284 ga_init2(&ga, (int)sizeof(char), 80);
7285 ga_append(&ga, '{');
7286
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007287 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007288 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007292 --todo;
7293
7294 if (first)
7295 first = FALSE;
7296 else
7297 ga_concat(&ga, (char_u *)", ");
7298
7299 tofree = string_quote(hi->hi_key, FALSE);
7300 if (tofree != NULL)
7301 {
7302 ga_concat(&ga, tofree);
7303 vim_free(tofree);
7304 }
7305 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007306 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 if (s != NULL)
7308 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007310 if (s == NULL)
7311 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007313 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007314 if (todo > 0)
7315 {
7316 vim_free(ga.ga_data);
7317 return NULL;
7318 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319
7320 ga_append(&ga, '}');
7321 ga_append(&ga, NUL);
7322 return (char_u *)ga.ga_data;
7323}
7324
7325/*
7326 * Allocate a variable for a Dictionary and fill it from "*arg".
7327 * Return OK or FAIL. Returns NOTDONE for {expr}.
7328 */
7329 static int
7330get_dict_tv(arg, rettv, evaluate)
7331 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333 int evaluate;
7334{
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 dict_T *d = NULL;
7336 typval_T tvkey;
7337 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007338 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342
7343 /*
7344 * First check if it's not a curly-braces thing: {expr}.
7345 * Must do this without evaluating, otherwise a function may be called
7346 * twice. Unfortunately this means we need to call eval1() twice for the
7347 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007348 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007350 if (*start != '}')
7351 {
7352 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7353 return FAIL;
7354 if (*start == '}')
7355 return NOTDONE;
7356 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357
7358 if (evaluate)
7359 {
7360 d = dict_alloc();
7361 if (d == NULL)
7362 return FAIL;
7363 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 tvkey.v_type = VAR_UNKNOWN;
7365 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366
7367 *arg = skipwhite(*arg + 1);
7368 while (**arg != '}' && **arg != NUL)
7369 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007371 goto failret;
7372 if (**arg != ':')
7373 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007374 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007375 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376 goto failret;
7377 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007378 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007380 key = get_tv_string_buf_chk(&tvkey, buf);
7381 if (key == NULL || *key == NUL)
7382 {
7383 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7384 if (key != NULL)
7385 EMSG(_(e_emptykey));
7386 clear_tv(&tvkey);
7387 goto failret;
7388 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007389 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390
7391 *arg = skipwhite(*arg + 1);
7392 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7393 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007394 if (evaluate)
7395 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 goto failret;
7397 }
7398 if (evaluate)
7399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 if (item != NULL)
7402 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007403 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405 clear_tv(&tv);
7406 goto failret;
7407 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007408 item = dictitem_alloc(key);
7409 clear_tv(&tvkey);
7410 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007413 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007414 if (dict_add(d, item) == FAIL)
7415 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416 }
7417 }
7418
7419 if (**arg == '}')
7420 break;
7421 if (**arg != ',')
7422 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007423 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 goto failret;
7425 }
7426 *arg = skipwhite(*arg + 1);
7427 }
7428
7429 if (**arg != '}')
7430 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007431 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432failret:
7433 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007434 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435 return FAIL;
7436 }
7437
7438 *arg = skipwhite(*arg + 1);
7439 if (evaluate)
7440 {
7441 rettv->v_type = VAR_DICT;
7442 rettv->vval.v_dict = d;
7443 ++d->dv_refcount;
7444 }
7445
7446 return OK;
7447}
7448
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007450 * Return a string with the string representation of a variable.
7451 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007452 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007453 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007454 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007455 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007456 */
7457 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007458echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007459 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007460 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007461 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007462 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007463{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007464 static int recurse = 0;
7465 char_u *r = NULL;
7466
Bram Moolenaar33570922005-01-25 22:26:29 +00007467 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007469 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007470 *tofree = NULL;
7471 return NULL;
7472 }
7473 ++recurse;
7474
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007475 switch (tv->v_type)
7476 {
7477 case VAR_FUNC:
7478 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007479 r = tv->vval.v_string;
7480 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007481
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007482 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007483 if (tv->vval.v_list == NULL)
7484 {
7485 *tofree = NULL;
7486 r = NULL;
7487 }
7488 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7489 {
7490 *tofree = NULL;
7491 r = (char_u *)"[...]";
7492 }
7493 else
7494 {
7495 tv->vval.v_list->lv_copyID = copyID;
7496 *tofree = list2string(tv, copyID);
7497 r = *tofree;
7498 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007500
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007502 if (tv->vval.v_dict == NULL)
7503 {
7504 *tofree = NULL;
7505 r = NULL;
7506 }
7507 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7508 {
7509 *tofree = NULL;
7510 r = (char_u *)"{...}";
7511 }
7512 else
7513 {
7514 tv->vval.v_dict->dv_copyID = copyID;
7515 *tofree = dict2string(tv, copyID);
7516 r = *tofree;
7517 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007518 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007519
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007520 case VAR_STRING:
7521 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007522 *tofree = NULL;
7523 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007524 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007525
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007526#ifdef FEAT_FLOAT
7527 case VAR_FLOAT:
7528 *tofree = NULL;
7529 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7530 r = numbuf;
7531 break;
7532#endif
7533
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007534 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007535 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007536 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007537 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007538
7539 --recurse;
7540 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007541}
7542
7543/*
7544 * Return a string with the string representation of a variable.
7545 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7546 * "numbuf" is used for a number.
7547 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007548 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007549 */
7550 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007551tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007552 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007553 char_u **tofree;
7554 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007555 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007556{
7557 switch (tv->v_type)
7558 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007559 case VAR_FUNC:
7560 *tofree = string_quote(tv->vval.v_string, TRUE);
7561 return *tofree;
7562 case VAR_STRING:
7563 *tofree = string_quote(tv->vval.v_string, FALSE);
7564 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007565#ifdef FEAT_FLOAT
7566 case VAR_FLOAT:
7567 *tofree = NULL;
7568 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7569 return numbuf;
7570#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007572 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007575 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007576 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007577 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007578 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007579}
7580
7581/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 * Return string "str" in ' quotes, doubling ' characters.
7583 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007585 */
7586 static char_u *
7587string_quote(str, function)
7588 char_u *str;
7589 int function;
7590{
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007592 char_u *p, *r, *s;
7593
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 len = (function ? 13 : 3);
7595 if (str != NULL)
7596 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007597 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 for (p = str; *p != NUL; mb_ptr_adv(p))
7599 if (*p == '\'')
7600 ++len;
7601 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007602 s = r = alloc(len);
7603 if (r != NULL)
7604 {
7605 if (function)
7606 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007608 r += 10;
7609 }
7610 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 if (str != NULL)
7613 for (p = str; *p != NUL; )
7614 {
7615 if (*p == '\'')
7616 *r++ = '\'';
7617 MB_COPY_CHAR(p, r);
7618 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007620 if (function)
7621 *r++ = ')';
7622 *r++ = NUL;
7623 }
7624 return s;
7625}
7626
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007627#ifdef FEAT_FLOAT
7628/*
7629 * Convert the string "text" to a floating point number.
7630 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7631 * this always uses a decimal point.
7632 * Returns the length of the text that was consumed.
7633 */
7634 static int
7635string2float(text, value)
7636 char_u *text;
7637 float_T *value; /* result stored here */
7638{
7639 char *s = (char *)text;
7640 float_T f;
7641
7642 f = strtod(s, &s);
7643 *value = f;
7644 return (int)((char_u *)s - text);
7645}
7646#endif
7647
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 * Get the value of an environment variable.
7650 * "arg" is pointing to the '$'. It is advanced to after the name.
7651 * If the environment variable was not set, silently assume it is empty.
7652 * Always return OK.
7653 */
7654 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007655get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 int evaluate;
7659{
7660 char_u *string = NULL;
7661 int len;
7662 int cc;
7663 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007664 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665
7666 ++*arg;
7667 name = *arg;
7668 len = get_env_len(arg);
7669 if (evaluate)
7670 {
7671 if (len != 0)
7672 {
7673 cc = name[len];
7674 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007675 /* first try vim_getenv(), fast for normal environment vars */
7676 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007678 {
7679 if (!mustfree)
7680 string = vim_strsave(string);
7681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 else
7683 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007684 if (mustfree)
7685 vim_free(string);
7686
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 /* next try expanding things like $VIM and ${HOME} */
7688 string = expand_env_save(name - 1);
7689 if (string != NULL && *string == '$')
7690 {
7691 vim_free(string);
7692 string = NULL;
7693 }
7694 }
7695 name[len] = cc;
7696 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697 rettv->v_type = VAR_STRING;
7698 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 }
7700
7701 return OK;
7702}
7703
7704/*
7705 * Array with names and number of arguments of all internal functions
7706 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7707 */
7708static struct fst
7709{
7710 char *f_name; /* function name */
7711 char f_min_argc; /* minimal number of arguments */
7712 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007714 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715} functions[] =
7716{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007717#ifdef FEAT_FLOAT
7718 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007719 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007720#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007721 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007722 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {"append", 2, 2, f_append},
7724 {"argc", 0, 0, f_argc},
7725 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007726 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007727#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007728 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007729 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007730 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007733 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 {"bufexists", 1, 1, f_bufexists},
7735 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7736 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7737 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7738 {"buflisted", 1, 1, f_buflisted},
7739 {"bufloaded", 1, 1, f_bufloaded},
7740 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007741 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 {"bufwinnr", 1, 1, f_bufwinnr},
7743 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007744 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007745 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007746#ifdef FEAT_FLOAT
7747 {"ceil", 1, 1, f_ceil},
7748#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007749 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 {"char2nr", 1, 1, f_char2nr},
7751 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007752 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007754#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007755 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007756 {"complete_add", 1, 1, f_complete_add},
7757 {"complete_check", 0, 0, f_complete_check},
7758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007760 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007761#ifdef FEAT_FLOAT
7762 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007763 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007764#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007765 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007767 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007768 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 {"delete", 1, 1, f_delete},
7770 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007771 {"diff_filler", 1, 1, f_diff_filler},
7772 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007773 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007775 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 {"eventhandler", 0, 0, f_eventhandler},
7777 {"executable", 1, 1, f_executable},
7778 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007779#ifdef FEAT_FLOAT
7780 {"exp", 1, 1, f_exp},
7781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007783 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007784 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7786 {"filereadable", 1, 1, f_filereadable},
7787 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007788 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007789 {"finddir", 1, 3, f_finddir},
7790 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007791#ifdef FEAT_FLOAT
7792 {"float2nr", 1, 1, f_float2nr},
7793 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007794 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007795#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007796 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 {"fnamemodify", 2, 2, f_fnamemodify},
7798 {"foldclosed", 1, 1, f_foldclosed},
7799 {"foldclosedend", 1, 1, f_foldclosedend},
7800 {"foldlevel", 1, 1, f_foldlevel},
7801 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007802 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007804 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007805 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007806 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007807 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 {"getbufvar", 2, 2, f_getbufvar},
7809 {"getchar", 0, 1, f_getchar},
7810 {"getcharmod", 0, 0, f_getcharmod},
7811 {"getcmdline", 0, 0, f_getcmdline},
7812 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007813 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007815 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007816 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {"getfsize", 1, 1, f_getfsize},
7818 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007819 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007820 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007821 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007822 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007823 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007824 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007825 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007826 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007828 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007829 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {"getwinposx", 0, 0, f_getwinposx},
7831 {"getwinposy", 0, 0, f_getwinposy},
7832 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007833 {"glob", 1, 2, f_glob},
7834 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007836 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007837 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007838 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7840 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7841 {"histadd", 2, 2, f_histadd},
7842 {"histdel", 1, 2, f_histdel},
7843 {"histget", 1, 2, f_histget},
7844 {"histnr", 1, 1, f_histnr},
7845 {"hlID", 1, 1, f_hlID},
7846 {"hlexists", 1, 1, f_hlexists},
7847 {"hostname", 0, 0, f_hostname},
7848 {"iconv", 3, 3, f_iconv},
7849 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007850 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007851 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007853 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {"inputrestore", 0, 0, f_inputrestore},
7855 {"inputsave", 0, 0, f_inputsave},
7856 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007857 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007858 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007860 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007861 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007862 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007863 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007865 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {"libcall", 3, 3, f_libcall},
7867 {"libcallnr", 3, 3, f_libcallnr},
7868 {"line", 1, 1, f_line},
7869 {"line2byte", 1, 1, f_line2byte},
7870 {"lispindent", 1, 1, f_lispindent},
7871 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007873 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007874 {"log10", 1, 1, f_log10},
7875#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007876 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007877 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007878 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007879 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007880 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007881 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007882 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007883 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007884 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007885 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007886 {"max", 1, 1, f_max},
7887 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007888#ifdef vim_mkdir
7889 {"mkdir", 1, 3, f_mkdir},
7890#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007891 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007892#ifdef FEAT_MZSCHEME
7893 {"mzeval", 1, 1, f_mzeval},
7894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"nextnonblank", 1, 1, f_nextnonblank},
7896 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007897 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007898 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007899#ifdef FEAT_FLOAT
7900 {"pow", 2, 2, f_pow},
7901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007903 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007904 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007905 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007906 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007907 {"reltime", 0, 2, f_reltime},
7908 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {"remote_expr", 2, 3, f_remote_expr},
7910 {"remote_foreground", 1, 1, f_remote_foreground},
7911 {"remote_peek", 1, 2, f_remote_peek},
7912 {"remote_read", 1, 1, f_remote_read},
7913 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007914 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007916 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007918 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007919#ifdef FEAT_FLOAT
7920 {"round", 1, 1, f_round},
7921#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007922 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007923 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007924 {"searchpair", 3, 7, f_searchpair},
7925 {"searchpairpos", 3, 7, f_searchpairpos},
7926 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"server2client", 2, 2, f_server2client},
7928 {"serverlist", 0, 0, f_serverlist},
7929 {"setbufvar", 3, 3, f_setbufvar},
7930 {"setcmdpos", 1, 1, f_setcmdpos},
7931 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007932 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007933 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007934 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007935 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007937 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007938 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007940 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007942#ifdef FEAT_FLOAT
7943 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007944 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007945#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02007946 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007947 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007948 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007949 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007950 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007951#ifdef FEAT_FLOAT
7952 {"sqrt", 1, 1, f_sqrt},
7953 {"str2float", 1, 1, f_str2float},
7954#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007955 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007956 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007957 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958#ifdef HAVE_STRFTIME
7959 {"strftime", 1, 2, f_strftime},
7960#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"strlen", 1, 1, f_strlen},
7964 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007965 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007967 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"submatch", 1, 1, f_submatch},
7969 {"substitute", 4, 4, f_substitute},
7970 {"synID", 3, 3, f_synID},
7971 {"synIDattr", 2, 3, f_synIDattr},
7972 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007973 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007974 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007975 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007976 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007977 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007978 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007979 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007980 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007981#ifdef FEAT_FLOAT
7982 {"tan", 1, 1, f_tan},
7983 {"tanh", 1, 1, f_tanh},
7984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007986 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {"tolower", 1, 1, f_tolower},
7988 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007989 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007990#ifdef FEAT_FLOAT
7991 {"trunc", 1, 1, f_trunc},
7992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007994 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007995 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007996 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {"virtcol", 1, 1, f_virtcol},
7998 {"visualmode", 0, 1, f_visualmode},
7999 {"winbufnr", 1, 1, f_winbufnr},
8000 {"wincol", 0, 0, f_wincol},
8001 {"winheight", 1, 1, f_winheight},
8002 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008003 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008005 {"winrestview", 1, 1, f_winrestview},
8006 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008008 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008009 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010};
8011
8012#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8013
8014/*
8015 * Function given to ExpandGeneric() to obtain the list of internal
8016 * or user defined function names.
8017 */
8018 char_u *
8019get_function_name(xp, idx)
8020 expand_T *xp;
8021 int idx;
8022{
8023 static int intidx = -1;
8024 char_u *name;
8025
8026 if (idx == 0)
8027 intidx = -1;
8028 if (intidx < 0)
8029 {
8030 name = get_user_func_name(xp, idx);
8031 if (name != NULL)
8032 return name;
8033 }
8034 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8035 {
8036 STRCPY(IObuff, functions[intidx].f_name);
8037 STRCAT(IObuff, "(");
8038 if (functions[intidx].f_max_argc == 0)
8039 STRCAT(IObuff, ")");
8040 return IObuff;
8041 }
8042
8043 return NULL;
8044}
8045
8046/*
8047 * Function given to ExpandGeneric() to obtain the list of internal or
8048 * user defined variable or function names.
8049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 char_u *
8051get_expr_name(xp, idx)
8052 expand_T *xp;
8053 int idx;
8054{
8055 static int intidx = -1;
8056 char_u *name;
8057
8058 if (idx == 0)
8059 intidx = -1;
8060 if (intidx < 0)
8061 {
8062 name = get_function_name(xp, idx);
8063 if (name != NULL)
8064 return name;
8065 }
8066 return get_user_var_name(xp, ++intidx);
8067}
8068
8069#endif /* FEAT_CMDL_COMPL */
8070
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008071#if defined(EBCDIC) || defined(PROTO)
8072/*
8073 * Compare struct fst by function name.
8074 */
8075 static int
8076compare_func_name(s1, s2)
8077 const void *s1;
8078 const void *s2;
8079{
8080 struct fst *p1 = (struct fst *)s1;
8081 struct fst *p2 = (struct fst *)s2;
8082
8083 return STRCMP(p1->f_name, p2->f_name);
8084}
8085
8086/*
8087 * Sort the function table by function name.
8088 * The sorting of the table above is ASCII dependant.
8089 * On machines using EBCDIC we have to sort it.
8090 */
8091 static void
8092sortFunctions()
8093{
8094 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8095
8096 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8097}
8098#endif
8099
8100
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101/*
8102 * Find internal function in table above.
8103 * Return index, or -1 if not found
8104 */
8105 static int
8106find_internal_func(name)
8107 char_u *name; /* name of the function */
8108{
8109 int first = 0;
8110 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8111 int cmp;
8112 int x;
8113
8114 /*
8115 * Find the function name in the table. Binary search.
8116 */
8117 while (first <= last)
8118 {
8119 x = first + ((unsigned)(last - first) >> 1);
8120 cmp = STRCMP(name, functions[x].f_name);
8121 if (cmp < 0)
8122 last = x - 1;
8123 else if (cmp > 0)
8124 first = x + 1;
8125 else
8126 return x;
8127 }
8128 return -1;
8129}
8130
8131/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008132 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8133 * name it contains, otherwise return "name".
8134 */
8135 static char_u *
8136deref_func_name(name, lenp)
8137 char_u *name;
8138 int *lenp;
8139{
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008141 int cc;
8142
8143 cc = name[*lenp];
8144 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008145 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008146 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008147 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008148 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008149 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008150 {
8151 *lenp = 0;
8152 return (char_u *)""; /* just in case */
8153 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008154 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008155 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008156 }
8157
8158 return name;
8159}
8160
8161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 * Allocate a variable for the result of a function.
8163 * Return OK or FAIL.
8164 */
8165 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008166get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8167 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 char_u *name; /* name of the function */
8169 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 char_u **arg; /* argument, pointing to the '(' */
8172 linenr_T firstline; /* first line of range */
8173 linenr_T lastline; /* last line of range */
8174 int *doesrange; /* return: function handled range */
8175 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008176 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177{
8178 char_u *argp;
8179 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008180 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 int argcount = 0; /* number of arguments found */
8182
8183 /*
8184 * Get the arguments.
8185 */
8186 argp = *arg;
8187 while (argcount < MAX_FUNC_ARGS)
8188 {
8189 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8190 if (*argp == ')' || *argp == ',' || *argp == NUL)
8191 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8193 {
8194 ret = FAIL;
8195 break;
8196 }
8197 ++argcount;
8198 if (*argp != ',')
8199 break;
8200 }
8201 if (*argp == ')')
8202 ++argp;
8203 else
8204 ret = FAIL;
8205
8206 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008207 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008208 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008210 {
8211 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008212 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008213 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008214 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216
8217 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008218 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219
8220 *arg = skipwhite(argp);
8221 return ret;
8222}
8223
8224
8225/*
8226 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008227 * Return OK when the function can't be called, FAIL otherwise.
8228 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 */
8230 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008231call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008232 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008233 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008235 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008237 typval_T *argvars; /* vars for arguments, must have "argcount"
8238 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 linenr_T firstline; /* first line of range */
8240 linenr_T lastline; /* last line of range */
8241 int *doesrange; /* return: function handled range */
8242 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008243 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244{
8245 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246#define ERROR_UNKNOWN 0
8247#define ERROR_TOOMANY 1
8248#define ERROR_TOOFEW 2
8249#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008250#define ERROR_DICT 4
8251#define ERROR_NONE 5
8252#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 int error = ERROR_NONE;
8254 int i;
8255 int llen;
8256 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257#define FLEN_FIXED 40
8258 char_u fname_buf[FLEN_FIXED + 1];
8259 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008260 char_u *name;
8261
8262 /* Make a copy of the name, if it comes from a funcref variable it could
8263 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008264 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008265 if (name == NULL)
8266 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267
8268 /*
8269 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8270 * Change <SNR>123_name() to K_SNR 123_name().
8271 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 llen = eval_fname_script(name);
8274 if (llen > 0)
8275 {
8276 fname_buf[0] = K_SPECIAL;
8277 fname_buf[1] = KS_EXTRA;
8278 fname_buf[2] = (int)KE_SNR;
8279 i = 3;
8280 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8281 {
8282 if (current_SID <= 0)
8283 error = ERROR_SCRIPT;
8284 else
8285 {
8286 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8287 i = (int)STRLEN(fname_buf);
8288 }
8289 }
8290 if (i + STRLEN(name + llen) < FLEN_FIXED)
8291 {
8292 STRCPY(fname_buf + i, name + llen);
8293 fname = fname_buf;
8294 }
8295 else
8296 {
8297 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8298 if (fname == NULL)
8299 error = ERROR_OTHER;
8300 else
8301 {
8302 mch_memmove(fname, fname_buf, (size_t)i);
8303 STRCPY(fname + i, name + llen);
8304 }
8305 }
8306 }
8307 else
8308 fname = name;
8309
8310 *doesrange = FALSE;
8311
8312
8313 /* execute the function if no errors detected and executing */
8314 if (evaluate && error == ERROR_NONE)
8315 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008316 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8317 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 error = ERROR_UNKNOWN;
8319
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008320 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {
8322 /*
8323 * User defined function.
8324 */
8325 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008326
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008328 /* Trigger FuncUndefined event, may load the function. */
8329 if (fp == NULL
8330 && apply_autocmds(EVENT_FUNCUNDEFINED,
8331 fname, fname, TRUE, NULL)
8332 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008334 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 fp = find_func(fname);
8336 }
8337#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008338 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008339 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008340 {
8341 /* loaded a package, search for the function again */
8342 fp = find_func(fname);
8343 }
8344
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 if (fp != NULL)
8346 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008347 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008349 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008351 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008353 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008354 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 else
8356 {
8357 /*
8358 * Call the user function.
8359 * Save and restore search patterns, script variables and
8360 * redo buffer.
8361 */
8362 save_search_patterns();
8363 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008364 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008365 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008366 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008367 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8368 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8369 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008370 /* Function was unreferenced while being used, free it
8371 * now. */
8372 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 restoreRedobuff();
8374 restore_search_patterns();
8375 error = ERROR_NONE;
8376 }
8377 }
8378 }
8379 else
8380 {
8381 /*
8382 * Find the function name in the table, call its implementation.
8383 */
8384 i = find_internal_func(fname);
8385 if (i >= 0)
8386 {
8387 if (argcount < functions[i].f_min_argc)
8388 error = ERROR_TOOFEW;
8389 else if (argcount > functions[i].f_max_argc)
8390 error = ERROR_TOOMANY;
8391 else
8392 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008393 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008394 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 error = ERROR_NONE;
8396 }
8397 }
8398 }
8399 /*
8400 * The function call (or "FuncUndefined" autocommand sequence) might
8401 * have been aborted by an error, an interrupt, or an explicitly thrown
8402 * exception that has not been caught so far. This situation can be
8403 * tested for by calling aborting(). For an error in an internal
8404 * function or for the "E132" error in call_user_func(), however, the
8405 * throw point at which the "force_abort" flag (temporarily reset by
8406 * emsg()) is normally updated has not been reached yet. We need to
8407 * update that flag first to make aborting() reliable.
8408 */
8409 update_force_abort();
8410 }
8411 if (error == ERROR_NONE)
8412 ret = OK;
8413
8414 /*
8415 * Report an error unless the argument evaluation or function call has been
8416 * cancelled due to an aborting error, an interrupt, or an exception.
8417 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008418 if (!aborting())
8419 {
8420 switch (error)
8421 {
8422 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008423 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008424 break;
8425 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008426 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008427 break;
8428 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008429 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008430 name);
8431 break;
8432 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008433 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008434 name);
8435 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008436 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008437 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008438 name);
8439 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008440 }
8441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 if (fname != name && fname != fname_buf)
8444 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008445 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446
8447 return ret;
8448}
8449
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008450/*
8451 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008452 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008453 */
8454 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008455emsg_funcname(ermsg, name)
8456 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008457 char_u *name;
8458{
8459 char_u *p;
8460
8461 if (*name == K_SPECIAL)
8462 p = concat_str((char_u *)"<SNR>", name + 3);
8463 else
8464 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008465 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008466 if (p != name)
8467 vim_free(p);
8468}
8469
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008470/*
8471 * Return TRUE for a non-zero Number and a non-empty String.
8472 */
8473 static int
8474non_zero_arg(argvars)
8475 typval_T *argvars;
8476{
8477 return ((argvars[0].v_type == VAR_NUMBER
8478 && argvars[0].vval.v_number != 0)
8479 || (argvars[0].v_type == VAR_STRING
8480 && argvars[0].vval.v_string != NULL
8481 && *argvars[0].vval.v_string != NUL));
8482}
8483
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484/*********************************************
8485 * Implementation of the built-in functions
8486 */
8487
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008488#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008489static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8490
8491/*
8492 * Get the float value of "argvars[0]" into "f".
8493 * Returns FAIL when the argument is not a Number or Float.
8494 */
8495 static int
8496get_float_arg(argvars, f)
8497 typval_T *argvars;
8498 float_T *f;
8499{
8500 if (argvars[0].v_type == VAR_FLOAT)
8501 {
8502 *f = argvars[0].vval.v_float;
8503 return OK;
8504 }
8505 if (argvars[0].v_type == VAR_NUMBER)
8506 {
8507 *f = (float_T)argvars[0].vval.v_number;
8508 return OK;
8509 }
8510 EMSG(_("E808: Number or Float required"));
8511 return FAIL;
8512}
8513
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008514/*
8515 * "abs(expr)" function
8516 */
8517 static void
8518f_abs(argvars, rettv)
8519 typval_T *argvars;
8520 typval_T *rettv;
8521{
8522 if (argvars[0].v_type == VAR_FLOAT)
8523 {
8524 rettv->v_type = VAR_FLOAT;
8525 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8526 }
8527 else
8528 {
8529 varnumber_T n;
8530 int error = FALSE;
8531
8532 n = get_tv_number_chk(&argvars[0], &error);
8533 if (error)
8534 rettv->vval.v_number = -1;
8535 else if (n > 0)
8536 rettv->vval.v_number = n;
8537 else
8538 rettv->vval.v_number = -n;
8539 }
8540}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008541
8542/*
8543 * "acos()" function
8544 */
8545 static void
8546f_acos(argvars, rettv)
8547 typval_T *argvars;
8548 typval_T *rettv;
8549{
8550 float_T f;
8551
8552 rettv->v_type = VAR_FLOAT;
8553 if (get_float_arg(argvars, &f) == OK)
8554 rettv->vval.v_float = acos(f);
8555 else
8556 rettv->vval.v_float = 0.0;
8557}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008558#endif
8559
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008561 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 */
8563 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008564f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008565 typval_T *argvars;
8566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
Bram Moolenaar33570922005-01-25 22:26:29 +00008568 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008570 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008571 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008573 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008574 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008575 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008576 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008577 }
8578 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008579 EMSG(_(e_listreq));
8580}
8581
8582/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008583 * "and(expr, expr)" function
8584 */
8585 static void
8586f_and(argvars, rettv)
8587 typval_T *argvars;
8588 typval_T *rettv;
8589{
8590 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8591 & get_tv_number_chk(&argvars[1], NULL);
8592}
8593
8594/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008595 * "append(lnum, string/list)" function
8596 */
8597 static void
8598f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008599 typval_T *argvars;
8600 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008601{
8602 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008603 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 list_T *l = NULL;
8605 listitem_T *li = NULL;
8606 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008607 long added = 0;
8608
Bram Moolenaar0d660222005-01-07 21:51:51 +00008609 lnum = get_tv_lnum(argvars);
8610 if (lnum >= 0
8611 && lnum <= curbuf->b_ml.ml_line_count
8612 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008613 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008614 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008615 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008616 l = argvars[1].vval.v_list;
8617 if (l == NULL)
8618 return;
8619 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008620 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008621 for (;;)
8622 {
8623 if (l == NULL)
8624 tv = &argvars[1]; /* append a string */
8625 else if (li == NULL)
8626 break; /* end of list */
8627 else
8628 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008629 line = get_tv_string_chk(tv);
8630 if (line == NULL) /* type error */
8631 {
8632 rettv->vval.v_number = 1; /* Failed */
8633 break;
8634 }
8635 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008636 ++added;
8637 if (l == NULL)
8638 break;
8639 li = li->li_next;
8640 }
8641
8642 appended_lines_mark(lnum, added);
8643 if (curwin->w_cursor.lnum > lnum)
8644 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646 else
8647 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648}
8649
8650/*
8651 * "argc()" function
8652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008654f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008655 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008658 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659}
8660
8661/*
8662 * "argidx()" function
8663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008666 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008669 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670}
8671
8672/*
8673 * "argv(nr)" function
8674 */
8675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008676f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008677 typval_T *argvars;
8678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679{
8680 int idx;
8681
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008682 if (argvars[0].v_type != VAR_UNKNOWN)
8683 {
8684 idx = get_tv_number_chk(&argvars[0], NULL);
8685 if (idx >= 0 && idx < ARGCOUNT)
8686 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8687 else
8688 rettv->vval.v_string = NULL;
8689 rettv->v_type = VAR_STRING;
8690 }
8691 else if (rettv_list_alloc(rettv) == OK)
8692 for (idx = 0; idx < ARGCOUNT; ++idx)
8693 list_append_string(rettv->vval.v_list,
8694 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695}
8696
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008697#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008698/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008699 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008700 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008701 static void
8702f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008703 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008704 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008705{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008706 float_T f;
8707
8708 rettv->v_type = VAR_FLOAT;
8709 if (get_float_arg(argvars, &f) == OK)
8710 rettv->vval.v_float = asin(f);
8711 else
8712 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008713}
8714
8715/*
8716 * "atan()" function
8717 */
8718 static void
8719f_atan(argvars, rettv)
8720 typval_T *argvars;
8721 typval_T *rettv;
8722{
8723 float_T f;
8724
8725 rettv->v_type = VAR_FLOAT;
8726 if (get_float_arg(argvars, &f) == OK)
8727 rettv->vval.v_float = atan(f);
8728 else
8729 rettv->vval.v_float = 0.0;
8730}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008731
8732/*
8733 * "atan2()" function
8734 */
8735 static void
8736f_atan2(argvars, rettv)
8737 typval_T *argvars;
8738 typval_T *rettv;
8739{
8740 float_T fx, fy;
8741
8742 rettv->v_type = VAR_FLOAT;
8743 if (get_float_arg(argvars, &fx) == OK
8744 && get_float_arg(&argvars[1], &fy) == OK)
8745 rettv->vval.v_float = atan2(fx, fy);
8746 else
8747 rettv->vval.v_float = 0.0;
8748}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008749#endif
8750
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751/*
8752 * "browse(save, title, initdir, default)" function
8753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758{
8759#ifdef FEAT_BROWSE
8760 int save;
8761 char_u *title;
8762 char_u *initdir;
8763 char_u *defname;
8764 char_u buf[NUMBUFLEN];
8765 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008766 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008768 save = get_tv_number_chk(&argvars[0], &error);
8769 title = get_tv_string_chk(&argvars[1]);
8770 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8771 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 if (error || title == NULL || initdir == NULL || defname == NULL)
8774 rettv->vval.v_string = NULL;
8775 else
8776 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008777 do_browse(save ? BROWSE_SAVE : 0,
8778 title, defname, NULL, initdir, NULL, curbuf);
8779#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008781#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008782 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008783}
8784
8785/*
8786 * "browsedir(title, initdir)" function
8787 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008789f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008790 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008791 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008792{
8793#ifdef FEAT_BROWSE
8794 char_u *title;
8795 char_u *initdir;
8796 char_u buf[NUMBUFLEN];
8797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008798 title = get_tv_string_chk(&argvars[0]);
8799 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008801 if (title == NULL || initdir == NULL)
8802 rettv->vval.v_string = NULL;
8803 else
8804 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008805 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008807 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008809 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810}
8811
Bram Moolenaar33570922005-01-25 22:26:29 +00008812static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008813
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814/*
8815 * Find a buffer by number or exact name.
8816 */
8817 static buf_T *
8818find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008819 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008823 if (avar->v_type == VAR_NUMBER)
8824 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008825 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008827 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008828 if (buf == NULL)
8829 {
8830 /* No full path name match, try a match with a URL or a "nofile"
8831 * buffer, these don't use the full path. */
8832 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8833 if (buf->b_fname != NULL
8834 && (path_with_url(buf->b_fname)
8835#ifdef FEAT_QUICKFIX
8836 || bt_nofile(buf)
8837#endif
8838 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008839 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008840 break;
8841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 }
8843 return buf;
8844}
8845
8846/*
8847 * "bufexists(expr)" function
8848 */
8849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008850f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008851 typval_T *argvars;
8852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008854 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855}
8856
8857/*
8858 * "buflisted(expr)" function
8859 */
8860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 typval_T *argvars;
8863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864{
8865 buf_T *buf;
8866
8867 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869}
8870
8871/*
8872 * "bufloaded(expr)" function
8873 */
8874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008875f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008876 typval_T *argvars;
8877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
8879 buf_T *buf;
8880
8881 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883}
8884
Bram Moolenaar33570922005-01-25 22:26:29 +00008885static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008886
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887/*
8888 * Get buffer by number or pattern.
8889 */
8890 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008892 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008894 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 int save_magic;
8896 char_u *save_cpo;
8897 buf_T *buf;
8898
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899 if (tv->v_type == VAR_NUMBER)
8900 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008901 if (tv->v_type != VAR_STRING)
8902 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 if (name == NULL || *name == NUL)
8904 return curbuf;
8905 if (name[0] == '$' && name[1] == NUL)
8906 return lastbuf;
8907
8908 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8909 save_magic = p_magic;
8910 p_magic = TRUE;
8911 save_cpo = p_cpo;
8912 p_cpo = (char_u *)"";
8913
8914 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8915 TRUE, FALSE));
8916
8917 p_magic = save_magic;
8918 p_cpo = save_cpo;
8919
8920 /* If not found, try expanding the name, like done for bufexists(). */
8921 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923
8924 return buf;
8925}
8926
8927/*
8928 * "bufname(expr)" function
8929 */
8930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008931f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008932 typval_T *argvars;
8933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
8935 buf_T *buf;
8936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008937 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008939 buf = get_buf_tv(&argvars[0]);
8940 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 --emsg_off;
8946}
8947
8948/*
8949 * "bufnr(expr)" function
8950 */
8951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 typval_T *argvars;
8954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
8956 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008957 int error = FALSE;
8958 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008960 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008963 --emsg_off;
8964
8965 /* If the buffer isn't found and the second argument is not zero create a
8966 * new buffer. */
8967 if (buf == NULL
8968 && argvars[1].v_type != VAR_UNKNOWN
8969 && get_tv_number_chk(&argvars[1], &error) != 0
8970 && !error
8971 && (name = get_tv_string_chk(&argvars[0])) != NULL
8972 && !error)
8973 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8974
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979}
8980
8981/*
8982 * "bufwinnr(nr)" function
8983 */
8984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008985f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008986 typval_T *argvars;
8987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
8989#ifdef FEAT_WINDOWS
8990 win_T *wp;
8991 int winnr = 0;
8992#endif
8993 buf_T *buf;
8994
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008995 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008997 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998#ifdef FEAT_WINDOWS
8999 for (wp = firstwin; wp; wp = wp->w_next)
9000 {
9001 ++winnr;
9002 if (wp->w_buffer == buf)
9003 break;
9004 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009007 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008#endif
9009 --emsg_off;
9010}
9011
9012/*
9013 * "byte2line(byte)" function
9014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009017 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019{
9020#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022#else
9023 long boff = 0;
9024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009025 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009029 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 (linenr_T)0, &boff);
9031#endif
9032}
9033
9034/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009035 * "byteidx()" function
9036 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009038f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009039 typval_T *argvars;
9040 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009041{
9042#ifdef FEAT_MBYTE
9043 char_u *t;
9044#endif
9045 char_u *str;
9046 long idx;
9047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009048 str = get_tv_string_chk(&argvars[0]);
9049 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009052 return;
9053
9054#ifdef FEAT_MBYTE
9055 t = str;
9056 for ( ; idx > 0; idx--)
9057 {
9058 if (*t == NUL) /* EOL reached */
9059 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009060 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009061 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009062 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009063#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009064 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009065 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009066#endif
9067}
9068
9069/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009070 * "call(func, arglist)" function
9071 */
9072 static void
9073f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009074 typval_T *argvars;
9075 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009076{
9077 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009078 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009079 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009080 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009081 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009082 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009083
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009084 if (argvars[1].v_type != VAR_LIST)
9085 {
9086 EMSG(_(e_listreq));
9087 return;
9088 }
9089 if (argvars[1].vval.v_list == NULL)
9090 return;
9091
9092 if (argvars[0].v_type == VAR_FUNC)
9093 func = argvars[0].vval.v_string;
9094 else
9095 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009096 if (*func == NUL)
9097 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009098
Bram Moolenaare9a41262005-01-15 22:18:47 +00009099 if (argvars[2].v_type != VAR_UNKNOWN)
9100 {
9101 if (argvars[2].v_type != VAR_DICT)
9102 {
9103 EMSG(_(e_dictreq));
9104 return;
9105 }
9106 selfdict = argvars[2].vval.v_dict;
9107 }
9108
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009109 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9110 item = item->li_next)
9111 {
9112 if (argc == MAX_FUNC_ARGS)
9113 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009114 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009115 break;
9116 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009117 /* Make a copy of each argument. This is needed to be able to set
9118 * v_lock to VAR_FIXED in the copy without changing the original list.
9119 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009120 copy_tv(&item->li_tv, &argv[argc++]);
9121 }
9122
9123 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009124 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009125 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9126 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009127
9128 /* Free the arguments. */
9129 while (argc > 0)
9130 clear_tv(&argv[--argc]);
9131}
9132
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009133#ifdef FEAT_FLOAT
9134/*
9135 * "ceil({float})" function
9136 */
9137 static void
9138f_ceil(argvars, rettv)
9139 typval_T *argvars;
9140 typval_T *rettv;
9141{
9142 float_T f;
9143
9144 rettv->v_type = VAR_FLOAT;
9145 if (get_float_arg(argvars, &f) == OK)
9146 rettv->vval.v_float = ceil(f);
9147 else
9148 rettv->vval.v_float = 0.0;
9149}
9150#endif
9151
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009152/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009153 * "changenr()" function
9154 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009155 static void
9156f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009157 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009158 typval_T *rettv;
9159{
9160 rettv->vval.v_number = curbuf->b_u_seq_cur;
9161}
9162
9163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 * "char2nr(string)" function
9165 */
9166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009168 typval_T *argvars;
9169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170{
9171#ifdef FEAT_MBYTE
9172 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009173 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 else
9175#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177}
9178
9179/*
9180 * "cindent(lnum)" function
9181 */
9182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009184 typval_T *argvars;
9185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186{
9187#ifdef FEAT_CINDENT
9188 pos_T pos;
9189 linenr_T lnum;
9190
9191 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9194 {
9195 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 curwin->w_cursor = pos;
9198 }
9199 else
9200#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202}
9203
9204/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009205 * "clearmatches()" function
9206 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009207 static void
9208f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009209 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009210 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009211{
9212#ifdef FEAT_SEARCH_EXTRA
9213 clear_matches(curwin);
9214#endif
9215}
9216
9217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 * "col(string)" function
9219 */
9220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009221f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009222 typval_T *argvars;
9223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224{
9225 colnr_T col = 0;
9226 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009227 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009229 fp = var2fpos(&argvars[0], FALSE, &fnum);
9230 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 {
9232 if (fp->col == MAXCOL)
9233 {
9234 /* '> can be MAXCOL, get the length of the line then */
9235 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009236 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 else
9238 col = MAXCOL;
9239 }
9240 else
9241 {
9242 col = fp->col + 1;
9243#ifdef FEAT_VIRTUALEDIT
9244 /* col(".") when the cursor is on the NUL at the end of the line
9245 * because of "coladd" can be seen as an extra column. */
9246 if (virtual_active() && fp == &curwin->w_cursor)
9247 {
9248 char_u *p = ml_get_cursor();
9249
9250 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9251 curwin->w_virtcol - curwin->w_cursor.coladd))
9252 {
9253# ifdef FEAT_MBYTE
9254 int l;
9255
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009256 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 col += l;
9258# else
9259 if (*p != NUL && p[1] == NUL)
9260 ++col;
9261# endif
9262 }
9263 }
9264#endif
9265 }
9266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009267 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268}
9269
Bram Moolenaar572cb562005-08-05 21:35:02 +00009270#if defined(FEAT_INS_EXPAND)
9271/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009272 * "complete()" function
9273 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009274 static void
9275f_complete(argvars, rettv)
9276 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009277 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009278{
9279 int startcol;
9280
9281 if ((State & INSERT) == 0)
9282 {
9283 EMSG(_("E785: complete() can only be used in Insert mode"));
9284 return;
9285 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009286
9287 /* Check for undo allowed here, because if something was already inserted
9288 * the line was already saved for undo and this check isn't done. */
9289 if (!undo_allowed())
9290 return;
9291
Bram Moolenaarade00832006-03-10 21:46:58 +00009292 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9293 {
9294 EMSG(_(e_invarg));
9295 return;
9296 }
9297
9298 startcol = get_tv_number_chk(&argvars[0], NULL);
9299 if (startcol <= 0)
9300 return;
9301
9302 set_completion(startcol - 1, argvars[1].vval.v_list);
9303}
9304
9305/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009306 * "complete_add()" function
9307 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009308 static void
9309f_complete_add(argvars, rettv)
9310 typval_T *argvars;
9311 typval_T *rettv;
9312{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009313 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009314}
9315
9316/*
9317 * "complete_check()" function
9318 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009319 static void
9320f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009321 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009322 typval_T *rettv;
9323{
9324 int saved = RedrawingDisabled;
9325
9326 RedrawingDisabled = 0;
9327 ins_compl_check_keys(0);
9328 rettv->vval.v_number = compl_interrupted;
9329 RedrawingDisabled = saved;
9330}
9331#endif
9332
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333/*
9334 * "confirm(message, buttons[, default [, type]])" function
9335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009337f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009338 typval_T *argvars UNUSED;
9339 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
9341#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9342 char_u *message;
9343 char_u *buttons = NULL;
9344 char_u buf[NUMBUFLEN];
9345 char_u buf2[NUMBUFLEN];
9346 int def = 1;
9347 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009348 char_u *typestr;
9349 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009351 message = get_tv_string_chk(&argvars[0]);
9352 if (message == NULL)
9353 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009354 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009356 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9357 if (buttons == NULL)
9358 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009359 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009361 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009362 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009364 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9365 if (typestr == NULL)
9366 error = TRUE;
9367 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009369 switch (TOUPPER_ASC(*typestr))
9370 {
9371 case 'E': type = VIM_ERROR; break;
9372 case 'Q': type = VIM_QUESTION; break;
9373 case 'I': type = VIM_INFO; break;
9374 case 'W': type = VIM_WARNING; break;
9375 case 'G': type = VIM_GENERIC; break;
9376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 }
9378 }
9379 }
9380 }
9381
9382 if (buttons == NULL || *buttons == NUL)
9383 buttons = (char_u *)_("&Ok");
9384
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009385 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009386 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009387 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388#endif
9389}
9390
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009391/*
9392 * "copy()" function
9393 */
9394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009396 typval_T *argvars;
9397 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009398{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009399 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009400}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009402#ifdef FEAT_FLOAT
9403/*
9404 * "cos()" function
9405 */
9406 static void
9407f_cos(argvars, rettv)
9408 typval_T *argvars;
9409 typval_T *rettv;
9410{
9411 float_T f;
9412
9413 rettv->v_type = VAR_FLOAT;
9414 if (get_float_arg(argvars, &f) == OK)
9415 rettv->vval.v_float = cos(f);
9416 else
9417 rettv->vval.v_float = 0.0;
9418}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009419
9420/*
9421 * "cosh()" function
9422 */
9423 static void
9424f_cosh(argvars, rettv)
9425 typval_T *argvars;
9426 typval_T *rettv;
9427{
9428 float_T f;
9429
9430 rettv->v_type = VAR_FLOAT;
9431 if (get_float_arg(argvars, &f) == OK)
9432 rettv->vval.v_float = cosh(f);
9433 else
9434 rettv->vval.v_float = 0.0;
9435}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009436#endif
9437
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009439 * "count()" function
9440 */
9441 static void
9442f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009443 typval_T *argvars;
9444 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009445{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009446 long n = 0;
9447 int ic = FALSE;
9448
Bram Moolenaare9a41262005-01-15 22:18:47 +00009449 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009450 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 listitem_T *li;
9452 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009453 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009454
Bram Moolenaare9a41262005-01-15 22:18:47 +00009455 if ((l = argvars[0].vval.v_list) != NULL)
9456 {
9457 li = l->lv_first;
9458 if (argvars[2].v_type != VAR_UNKNOWN)
9459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009460 int error = FALSE;
9461
9462 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009463 if (argvars[3].v_type != VAR_UNKNOWN)
9464 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009465 idx = get_tv_number_chk(&argvars[3], &error);
9466 if (!error)
9467 {
9468 li = list_find(l, idx);
9469 if (li == NULL)
9470 EMSGN(_(e_listidx), idx);
9471 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009472 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009473 if (error)
9474 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009475 }
9476
9477 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009478 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009479 ++n;
9480 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009482 else if (argvars[0].v_type == VAR_DICT)
9483 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009484 int todo;
9485 dict_T *d;
9486 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009487
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009488 if ((d = argvars[0].vval.v_dict) != NULL)
9489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009490 int error = FALSE;
9491
Bram Moolenaare9a41262005-01-15 22:18:47 +00009492 if (argvars[2].v_type != VAR_UNKNOWN)
9493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009494 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009495 if (argvars[3].v_type != VAR_UNKNOWN)
9496 EMSG(_(e_invarg));
9497 }
9498
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009499 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009500 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009501 {
9502 if (!HASHITEM_EMPTY(hi))
9503 {
9504 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009505 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009506 ++n;
9507 }
9508 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009509 }
9510 }
9511 else
9512 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009513 rettv->vval.v_number = n;
9514}
9515
9516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9518 *
9519 * Checks the existence of a cscope connection.
9520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009522f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009523 typval_T *argvars UNUSED;
9524 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525{
9526#ifdef FEAT_CSCOPE
9527 int num = 0;
9528 char_u *dbpath = NULL;
9529 char_u *prepend = NULL;
9530 char_u buf[NUMBUFLEN];
9531
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009532 if (argvars[0].v_type != VAR_UNKNOWN
9533 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 num = (int)get_tv_number(&argvars[0]);
9536 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009537 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 }
9540
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542#endif
9543}
9544
9545/*
9546 * "cursor(lnum, col)" function
9547 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009548 * Moves the cursor to the specified line and column.
9549 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009553 typval_T *argvars;
9554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
9556 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009557#ifdef FEAT_VIRTUALEDIT
9558 long coladd = 0;
9559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009561 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009562 if (argvars[1].v_type == VAR_UNKNOWN)
9563 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009564 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009565
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009566 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009567 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009568 line = pos.lnum;
9569 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009570#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009571 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009572#endif
9573 }
9574 else
9575 {
9576 line = get_tv_lnum(argvars);
9577 col = get_tv_number_chk(&argvars[1], NULL);
9578#ifdef FEAT_VIRTUALEDIT
9579 if (argvars[2].v_type != VAR_UNKNOWN)
9580 coladd = get_tv_number_chk(&argvars[2], NULL);
9581#endif
9582 }
9583 if (line < 0 || col < 0
9584#ifdef FEAT_VIRTUALEDIT
9585 || coladd < 0
9586#endif
9587 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009588 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 if (line > 0)
9590 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 if (col > 0)
9592 curwin->w_cursor.col = col - 1;
9593#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009594 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595#endif
9596
9597 /* Make sure the cursor is in a valid position. */
9598 check_cursor();
9599#ifdef FEAT_MBYTE
9600 /* Correct cursor for multi-byte character. */
9601 if (has_mbyte)
9602 mb_adjust_cursor();
9603#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009604
9605 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009606 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607}
9608
9609/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009610 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 */
9612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009613f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009614 typval_T *argvars;
9615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009617 int noref = 0;
9618
9619 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009620 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009621 if (noref < 0 || noref > 1)
9622 EMSG(_(e_invarg));
9623 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009624 {
9625 current_copyID += COPYID_INC;
9626 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628}
9629
9630/*
9631 * "delete()" function
9632 */
9633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009635 typval_T *argvars;
9636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637{
9638 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642}
9643
9644/*
9645 * "did_filetype()" function
9646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009649 typval_T *argvars UNUSED;
9650 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
9652#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654#endif
9655}
9656
9657/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009658 * "diff_filler()" function
9659 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009662 typval_T *argvars UNUSED;
9663 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009664{
9665#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009667#endif
9668}
9669
9670/*
9671 * "diff_hlID()" function
9672 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009675 typval_T *argvars UNUSED;
9676 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009677{
9678#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009680 static linenr_T prev_lnum = 0;
9681 static int changedtick = 0;
9682 static int fnum = 0;
9683 static int change_start = 0;
9684 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009685 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009686 int filler_lines;
9687 int col;
9688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009689 if (lnum < 0) /* ignore type error in {lnum} arg */
9690 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009691 if (lnum != prev_lnum
9692 || changedtick != curbuf->b_changedtick
9693 || fnum != curbuf->b_fnum)
9694 {
9695 /* New line, buffer, change: need to get the values. */
9696 filler_lines = diff_check(curwin, lnum);
9697 if (filler_lines < 0)
9698 {
9699 if (filler_lines == -1)
9700 {
9701 change_start = MAXCOL;
9702 change_end = -1;
9703 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9704 hlID = HLF_ADD; /* added line */
9705 else
9706 hlID = HLF_CHD; /* changed line */
9707 }
9708 else
9709 hlID = HLF_ADD; /* added line */
9710 }
9711 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009712 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009713 prev_lnum = lnum;
9714 changedtick = curbuf->b_changedtick;
9715 fnum = curbuf->b_fnum;
9716 }
9717
9718 if (hlID == HLF_CHD || hlID == HLF_TXD)
9719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009720 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009721 if (col >= change_start && col <= change_end)
9722 hlID = HLF_TXD; /* changed text */
9723 else
9724 hlID = HLF_CHD; /* changed line */
9725 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009726 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009727#endif
9728}
9729
9730/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009731 * "empty({expr})" function
9732 */
9733 static void
9734f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009735 typval_T *argvars;
9736 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009737{
9738 int n;
9739
9740 switch (argvars[0].v_type)
9741 {
9742 case VAR_STRING:
9743 case VAR_FUNC:
9744 n = argvars[0].vval.v_string == NULL
9745 || *argvars[0].vval.v_string == NUL;
9746 break;
9747 case VAR_NUMBER:
9748 n = argvars[0].vval.v_number == 0;
9749 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009750#ifdef FEAT_FLOAT
9751 case VAR_FLOAT:
9752 n = argvars[0].vval.v_float == 0.0;
9753 break;
9754#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009755 case VAR_LIST:
9756 n = argvars[0].vval.v_list == NULL
9757 || argvars[0].vval.v_list->lv_first == NULL;
9758 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009759 case VAR_DICT:
9760 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009761 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009762 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009763 default:
9764 EMSG2(_(e_intern2), "f_empty()");
9765 n = 0;
9766 }
9767
9768 rettv->vval.v_number = n;
9769}
9770
9771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 * "escape({string}, {chars})" function
9773 */
9774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009776 typval_T *argvars;
9777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779 char_u buf[NUMBUFLEN];
9780
Bram Moolenaar758711c2005-02-02 23:11:38 +00009781 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9782 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784}
9785
9786/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009787 * "eval()" function
9788 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009789 static void
9790f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009791 typval_T *argvars;
9792 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009793{
9794 char_u *s;
9795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009796 s = get_tv_string_chk(&argvars[0]);
9797 if (s != NULL)
9798 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009800 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9801 {
9802 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009803 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009804 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009805 else if (*s != NUL)
9806 EMSG(_(e_trailing));
9807}
9808
9809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 * "eventhandler()" function
9811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009814 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818}
9819
9820/*
9821 * "executable()" function
9822 */
9823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009824f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009825 typval_T *argvars;
9826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829}
9830
9831/*
9832 * "exists()" function
9833 */
9834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009836 typval_T *argvars;
9837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838{
9839 char_u *p;
9840 char_u *name;
9841 int n = FALSE;
9842 int len = 0;
9843
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009844 no_autoload = TRUE;
9845
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009846 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 if (*p == '$') /* environment variable */
9848 {
9849 /* first try "normal" environment variables (fast) */
9850 if (mch_getenv(p + 1) != NULL)
9851 n = TRUE;
9852 else
9853 {
9854 /* try expanding things like $VIM and ${HOME} */
9855 p = expand_env_save(p);
9856 if (p != NULL && *p != '$')
9857 n = TRUE;
9858 vim_free(p);
9859 }
9860 }
9861 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009864 if (*skipwhite(p) != NUL)
9865 n = FALSE; /* trailing garbage */
9866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 else if (*p == '*') /* internal or user defined function */
9868 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009869 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 }
9871 else if (*p == ':')
9872 {
9873 n = cmd_exists(p + 1);
9874 }
9875 else if (*p == '#')
9876 {
9877#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009878 if (p[1] == '#')
9879 n = autocmd_supported(p + 2);
9880 else
9881 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882#endif
9883 }
9884 else /* internal variable */
9885 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009886 char_u *tofree;
9887 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009889 /* get_name_len() takes care of expanding curly braces */
9890 name = p;
9891 len = get_name_len(&p, &tofree, TRUE, FALSE);
9892 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009894 if (tofree != NULL)
9895 name = tofree;
9896 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9897 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009899 /* handle d.key, l[idx], f(expr) */
9900 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9901 if (n)
9902 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 }
9904 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009905 if (*p != NUL)
9906 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009908 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 }
9910
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009911 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009912
9913 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914}
9915
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009916#ifdef FEAT_FLOAT
9917/*
9918 * "exp()" function
9919 */
9920 static void
9921f_exp(argvars, rettv)
9922 typval_T *argvars;
9923 typval_T *rettv;
9924{
9925 float_T f;
9926
9927 rettv->v_type = VAR_FLOAT;
9928 if (get_float_arg(argvars, &f) == OK)
9929 rettv->vval.v_float = exp(f);
9930 else
9931 rettv->vval.v_float = 0.0;
9932}
9933#endif
9934
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935/*
9936 * "expand()" function
9937 */
9938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009940 typval_T *argvars;
9941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942{
9943 char_u *s;
9944 int len;
9945 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009946 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009950 rettv->v_type = VAR_STRING;
9951 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 if (*s == '%' || *s == '#' || *s == '<')
9953 {
9954 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009955 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 --emsg_off;
9957 }
9958 else
9959 {
9960 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009961 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009962 if (argvars[1].v_type != VAR_UNKNOWN
9963 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009964 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009965 if (!error)
9966 {
9967 ExpandInit(&xpc);
9968 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009969 if (p_wic)
9970 options += WILD_ICASE;
9971 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009972 }
9973 else
9974 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 }
9976}
9977
9978/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009979 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009980 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009981 */
9982 static void
9983f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009984 typval_T *argvars;
9985 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009986{
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009987 char *arg_errmsg = N_("extend() argument");
9988
Bram Moolenaare9a41262005-01-15 22:18:47 +00009989 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009990 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009991 list_T *l1, *l2;
9992 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009993 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009994 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009995
Bram Moolenaare9a41262005-01-15 22:18:47 +00009996 l1 = argvars[0].vval.v_list;
9997 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009998 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009999 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010000 {
10001 if (argvars[2].v_type != VAR_UNKNOWN)
10002 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010003 before = get_tv_number_chk(&argvars[2], &error);
10004 if (error)
10005 return; /* type error; errmsg already given */
10006
Bram Moolenaar758711c2005-02-02 23:11:38 +000010007 if (before == l1->lv_len)
10008 item = NULL;
10009 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010010 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010011 item = list_find(l1, before);
10012 if (item == NULL)
10013 {
10014 EMSGN(_(e_listidx), before);
10015 return;
10016 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010017 }
10018 }
10019 else
10020 item = NULL;
10021 list_extend(l1, l2, item);
10022
Bram Moolenaare9a41262005-01-15 22:18:47 +000010023 copy_tv(&argvars[0], rettv);
10024 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010025 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010026 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10027 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010028 dict_T *d1, *d2;
10029 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010030 char_u *action;
10031 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010032 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010033 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010034
10035 d1 = argvars[0].vval.v_dict;
10036 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010037 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010038 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010039 {
10040 /* Check the third argument. */
10041 if (argvars[2].v_type != VAR_UNKNOWN)
10042 {
10043 static char *(av[]) = {"keep", "force", "error"};
10044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010045 action = get_tv_string_chk(&argvars[2]);
10046 if (action == NULL)
10047 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010048 for (i = 0; i < 3; ++i)
10049 if (STRCMP(action, av[i]) == 0)
10050 break;
10051 if (i == 3)
10052 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010053 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010054 return;
10055 }
10056 }
10057 else
10058 action = (char_u *)"force";
10059
10060 /* Go over all entries in the second dict and add them to the
10061 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010062 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010063 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010064 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010065 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010066 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010067 --todo;
10068 di1 = dict_find(d1, hi2->hi_key, -1);
10069 if (di1 == NULL)
10070 {
10071 di1 = dictitem_copy(HI2DI(hi2));
10072 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10073 dictitem_free(di1);
10074 }
10075 else if (*action == 'e')
10076 {
10077 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10078 break;
10079 }
10080 else if (*action == 'f')
10081 {
10082 clear_tv(&di1->di_tv);
10083 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10084 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010085 }
10086 }
10087
Bram Moolenaare9a41262005-01-15 22:18:47 +000010088 copy_tv(&argvars[0], rettv);
10089 }
10090 }
10091 else
10092 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010093}
10094
10095/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010096 * "feedkeys()" function
10097 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010098 static void
10099f_feedkeys(argvars, rettv)
10100 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010101 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010102{
10103 int remap = TRUE;
10104 char_u *keys, *flags;
10105 char_u nbuf[NUMBUFLEN];
10106 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010107 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010108
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010109 /* This is not allowed in the sandbox. If the commands would still be
10110 * executed in the sandbox it would be OK, but it probably happens later,
10111 * when "sandbox" is no longer set. */
10112 if (check_secure())
10113 return;
10114
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010115 keys = get_tv_string(&argvars[0]);
10116 if (*keys != NUL)
10117 {
10118 if (argvars[1].v_type != VAR_UNKNOWN)
10119 {
10120 flags = get_tv_string_buf(&argvars[1], nbuf);
10121 for ( ; *flags != NUL; ++flags)
10122 {
10123 switch (*flags)
10124 {
10125 case 'n': remap = FALSE; break;
10126 case 'm': remap = TRUE; break;
10127 case 't': typed = TRUE; break;
10128 }
10129 }
10130 }
10131
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010132 /* Need to escape K_SPECIAL and CSI before putting the string in the
10133 * typeahead buffer. */
10134 keys_esc = vim_strsave_escape_csi(keys);
10135 if (keys_esc != NULL)
10136 {
10137 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010138 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010139 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010140 if (vgetc_busy)
10141 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010142 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010143 }
10144}
10145
10146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 * "filereadable()" function
10148 */
10149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010151 typval_T *argvars;
10152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010154 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 char_u *p;
10156 int n;
10157
Bram Moolenaarc236c162008-07-13 17:41:49 +000010158#ifndef O_NONBLOCK
10159# define O_NONBLOCK 0
10160#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010162 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10163 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 {
10165 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010166 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 }
10168 else
10169 n = FALSE;
10170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172}
10173
10174/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010175 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 * rights to write into.
10177 */
10178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010179f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010180 typval_T *argvars;
10181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010183 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184}
10185
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010186static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010187
10188 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010189findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010190 typval_T *argvars;
10191 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010192 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010193{
10194#ifdef FEAT_SEARCHPATH
10195 char_u *fname;
10196 char_u *fresult = NULL;
10197 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10198 char_u *p;
10199 char_u pathbuf[NUMBUFLEN];
10200 int count = 1;
10201 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010202 int error = FALSE;
10203#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010204
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010205 rettv->vval.v_string = NULL;
10206 rettv->v_type = VAR_STRING;
10207
10208#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010209 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010210
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010211 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10214 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010215 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010216 else
10217 {
10218 if (*p != NUL)
10219 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010222 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010224 }
10225
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010226 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10227 error = TRUE;
10228
10229 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 do
10232 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010233 if (rettv->v_type == VAR_STRING)
10234 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235 fresult = find_file_in_path_option(first ? fname : NULL,
10236 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010237 0, first, path,
10238 find_what,
10239 curbuf->b_ffname,
10240 find_what == FINDFILE_DIR
10241 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010242 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010243
10244 if (fresult != NULL && rettv->v_type == VAR_LIST)
10245 list_append_string(rettv->vval.v_list, fresult, -1);
10246
10247 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010248 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010249
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010250 if (rettv->v_type == VAR_STRING)
10251 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010252#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010253}
10254
Bram Moolenaar33570922005-01-25 22:26:29 +000010255static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10256static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010257
10258/*
10259 * Implementation of map() and filter().
10260 */
10261 static void
10262filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010263 typval_T *argvars;
10264 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010265 int map;
10266{
10267 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010268 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010269 listitem_T *li, *nli;
10270 list_T *l = NULL;
10271 dictitem_T *di;
10272 hashtab_T *ht;
10273 hashitem_T *hi;
10274 dict_T *d = NULL;
10275 typval_T save_val;
10276 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010278 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010279 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10280 char *arg_errmsg = (map ? N_("map() argument")
10281 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010282 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010283 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010284
Bram Moolenaare9a41262005-01-15 22:18:47 +000010285 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010286 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010287 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010288 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010289 return;
10290 }
10291 else if (argvars[0].v_type == VAR_DICT)
10292 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010293 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010294 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010295 return;
10296 }
10297 else
10298 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010299 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010300 return;
10301 }
10302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010303 expr = get_tv_string_buf_chk(&argvars[1], buf);
10304 /* On type errors, the preceding call has already displayed an error
10305 * message. Avoid a misleading error message for an empty string that
10306 * was not passed as argument. */
10307 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010309 prepare_vimvar(VV_VAL, &save_val);
10310 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010311
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010312 /* We reset "did_emsg" to be able to detect whether an error
10313 * occurred during evaluation of the expression. */
10314 save_did_emsg = did_emsg;
10315 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010316
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010317 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010318 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010320 vimvars[VV_KEY].vv_type = VAR_STRING;
10321
10322 ht = &d->dv_hashtab;
10323 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010324 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010325 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010327 if (!HASHITEM_EMPTY(hi))
10328 {
10329 --todo;
10330 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010331 if (tv_check_lock(di->di_tv.v_lock,
10332 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010333 break;
10334 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010335 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010336 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010337 break;
10338 if (!map && rem)
10339 dictitem_remove(d, di);
10340 clear_tv(&vimvars[VV_KEY].vv_tv);
10341 }
10342 }
10343 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 }
10345 else
10346 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010347 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010349 for (li = l->lv_first; li != NULL; li = nli)
10350 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010351 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010352 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010353 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010354 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010355 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010356 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010357 break;
10358 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010359 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010360 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010361 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010362 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010363
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010364 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010365 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010366
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010367 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010368 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010369
10370 copy_tv(&argvars[0], rettv);
10371}
10372
10373 static int
10374filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010375 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010376 char_u *expr;
10377 int map;
10378 int *remp;
10379{
Bram Moolenaar33570922005-01-25 22:26:29 +000010380 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010381 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010382 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010383
Bram Moolenaar33570922005-01-25 22:26:29 +000010384 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010385 s = expr;
10386 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010387 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010388 if (*s != NUL) /* check for trailing chars after expr */
10389 {
10390 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010391 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010392 }
10393 if (map)
10394 {
10395 /* map(): replace the list item value */
10396 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010397 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010398 *tv = rettv;
10399 }
10400 else
10401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010402 int error = FALSE;
10403
Bram Moolenaare9a41262005-01-15 22:18:47 +000010404 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010405 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010406 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010407 /* On type error, nothing has been removed; return FAIL to stop the
10408 * loop. The error message was given by get_tv_number_chk(). */
10409 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010410 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010411 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010412 retval = OK;
10413theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010414 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010415 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010416}
10417
10418/*
10419 * "filter()" function
10420 */
10421 static void
10422f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010423 typval_T *argvars;
10424 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010425{
10426 filter_map(argvars, rettv, FALSE);
10427}
10428
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010429/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010430 * "finddir({fname}[, {path}[, {count}]])" function
10431 */
10432 static void
10433f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010434 typval_T *argvars;
10435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010436{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010437 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010438}
10439
10440/*
10441 * "findfile({fname}[, {path}[, {count}]])" function
10442 */
10443 static void
10444f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010445 typval_T *argvars;
10446 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010447{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010448 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010449}
10450
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010451#ifdef FEAT_FLOAT
10452/*
10453 * "float2nr({float})" function
10454 */
10455 static void
10456f_float2nr(argvars, rettv)
10457 typval_T *argvars;
10458 typval_T *rettv;
10459{
10460 float_T f;
10461
10462 if (get_float_arg(argvars, &f) == OK)
10463 {
10464 if (f < -0x7fffffff)
10465 rettv->vval.v_number = -0x7fffffff;
10466 else if (f > 0x7fffffff)
10467 rettv->vval.v_number = 0x7fffffff;
10468 else
10469 rettv->vval.v_number = (varnumber_T)f;
10470 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010471}
10472
10473/*
10474 * "floor({float})" function
10475 */
10476 static void
10477f_floor(argvars, rettv)
10478 typval_T *argvars;
10479 typval_T *rettv;
10480{
10481 float_T f;
10482
10483 rettv->v_type = VAR_FLOAT;
10484 if (get_float_arg(argvars, &f) == OK)
10485 rettv->vval.v_float = floor(f);
10486 else
10487 rettv->vval.v_float = 0.0;
10488}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010489
10490/*
10491 * "fmod()" function
10492 */
10493 static void
10494f_fmod(argvars, rettv)
10495 typval_T *argvars;
10496 typval_T *rettv;
10497{
10498 float_T fx, fy;
10499
10500 rettv->v_type = VAR_FLOAT;
10501 if (get_float_arg(argvars, &fx) == OK
10502 && get_float_arg(&argvars[1], &fy) == OK)
10503 rettv->vval.v_float = fmod(fx, fy);
10504 else
10505 rettv->vval.v_float = 0.0;
10506}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010507#endif
10508
Bram Moolenaar0d660222005-01-07 21:51:51 +000010509/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010510 * "fnameescape({string})" function
10511 */
10512 static void
10513f_fnameescape(argvars, rettv)
10514 typval_T *argvars;
10515 typval_T *rettv;
10516{
10517 rettv->vval.v_string = vim_strsave_fnameescape(
10518 get_tv_string(&argvars[0]), FALSE);
10519 rettv->v_type = VAR_STRING;
10520}
10521
10522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 * "fnamemodify({fname}, {mods})" function
10524 */
10525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010526f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010527 typval_T *argvars;
10528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529{
10530 char_u *fname;
10531 char_u *mods;
10532 int usedlen = 0;
10533 int len;
10534 char_u *fbuf = NULL;
10535 char_u buf[NUMBUFLEN];
10536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010537 fname = get_tv_string_chk(&argvars[0]);
10538 mods = get_tv_string_buf_chk(&argvars[1], buf);
10539 if (fname == NULL || mods == NULL)
10540 fname = NULL;
10541 else
10542 {
10543 len = (int)STRLEN(fname);
10544 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010547 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 vim_free(fbuf);
10553}
10554
Bram Moolenaar33570922005-01-25 22:26:29 +000010555static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556
10557/*
10558 * "foldclosed()" function
10559 */
10560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010562 typval_T *argvars;
10563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 int end;
10565{
10566#ifdef FEAT_FOLDING
10567 linenr_T lnum;
10568 linenr_T first, last;
10569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010570 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10572 {
10573 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10574 {
10575 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010578 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 return;
10580 }
10581 }
10582#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010583 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584}
10585
10586/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010587 * "foldclosed()" function
10588 */
10589 static void
10590f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010591 typval_T *argvars;
10592 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010593{
10594 foldclosed_both(argvars, rettv, FALSE);
10595}
10596
10597/*
10598 * "foldclosedend()" function
10599 */
10600 static void
10601f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010602 typval_T *argvars;
10603 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010604{
10605 foldclosed_both(argvars, rettv, TRUE);
10606}
10607
10608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 * "foldlevel()" function
10610 */
10611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010612f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010613 typval_T *argvars;
10614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615{
10616#ifdef FEAT_FOLDING
10617 linenr_T lnum;
10618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010619 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010621 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623}
10624
10625/*
10626 * "foldtext()" function
10627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010630 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632{
10633#ifdef FEAT_FOLDING
10634 linenr_T lnum;
10635 char_u *s;
10636 char_u *r;
10637 int len;
10638 char *txt;
10639#endif
10640
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010641 rettv->v_type = VAR_STRING;
10642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010644 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10645 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10646 <= curbuf->b_ml.ml_line_count
10647 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 {
10649 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010650 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10651 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 {
10653 if (!linewhite(lnum))
10654 break;
10655 ++lnum;
10656 }
10657
10658 /* Find interesting text in this line. */
10659 s = skipwhite(ml_get(lnum));
10660 /* skip C comment-start */
10661 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010664 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010665 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010666 {
10667 s = skipwhite(ml_get(lnum + 1));
10668 if (*s == '*')
10669 s = skipwhite(s + 1);
10670 }
10671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 txt = _("+-%s%3ld lines: ");
10673 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010674 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 + 20 /* for %3ld */
10676 + STRLEN(s))); /* concatenated */
10677 if (r != NULL)
10678 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010679 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10680 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10681 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 len = (int)STRLEN(r);
10683 STRCAT(r, s);
10684 /* remove 'foldmarker' and 'commentstring' */
10685 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 }
10688 }
10689#endif
10690}
10691
10692/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010693 * "foldtextresult(lnum)" function
10694 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010696f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010697 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010698 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010699{
10700#ifdef FEAT_FOLDING
10701 linenr_T lnum;
10702 char_u *text;
10703 char_u buf[51];
10704 foldinfo_T foldinfo;
10705 int fold_count;
10706#endif
10707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708 rettv->v_type = VAR_STRING;
10709 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010710#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010712 /* treat illegal types and illegal string values for {lnum} the same */
10713 if (lnum < 0)
10714 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010715 fold_count = foldedCount(curwin, lnum, &foldinfo);
10716 if (fold_count > 0)
10717 {
10718 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10719 &foldinfo, buf);
10720 if (text == buf)
10721 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010722 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010723 }
10724#endif
10725}
10726
10727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 * "foreground()" function
10729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010732 typval_T *argvars UNUSED;
10733 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735#ifdef FEAT_GUI
10736 if (gui.in_use)
10737 gui_mch_set_foreground();
10738#else
10739# ifdef WIN32
10740 win32_set_foreground();
10741# endif
10742#endif
10743}
10744
10745/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010746 * "function()" function
10747 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010750 typval_T *argvars;
10751 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010752{
10753 char_u *s;
10754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010756 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010757 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010758 /* Don't check an autoload name for existence here. */
10759 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010760 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010761 else
10762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010763 rettv->vval.v_string = vim_strsave(s);
10764 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010765 }
10766}
10767
10768/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010769 * "garbagecollect()" function
10770 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010771 static void
10772f_garbagecollect(argvars, rettv)
10773 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010774 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010775{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010776 /* This is postponed until we are back at the toplevel, because we may be
10777 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10778 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010779
10780 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10781 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010782}
10783
10784/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010785 * "get()" function
10786 */
10787 static void
10788f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010789 typval_T *argvars;
10790 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010791{
Bram Moolenaar33570922005-01-25 22:26:29 +000010792 listitem_T *li;
10793 list_T *l;
10794 dictitem_T *di;
10795 dict_T *d;
10796 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010797
Bram Moolenaare9a41262005-01-15 22:18:47 +000010798 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010799 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010800 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010802 int error = FALSE;
10803
10804 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10805 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010806 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010807 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010808 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010809 else if (argvars[0].v_type == VAR_DICT)
10810 {
10811 if ((d = argvars[0].vval.v_dict) != NULL)
10812 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010813 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010814 if (di != NULL)
10815 tv = &di->di_tv;
10816 }
10817 }
10818 else
10819 EMSG2(_(e_listdictarg), "get()");
10820
10821 if (tv == NULL)
10822 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010823 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010824 copy_tv(&argvars[2], rettv);
10825 }
10826 else
10827 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010828}
10829
Bram Moolenaar342337a2005-07-21 21:11:17 +000010830static 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 +000010831
10832/*
10833 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010834 * Return a range (from start to end) of lines in rettv from the specified
10835 * buffer.
10836 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010837 */
10838 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010839get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010840 buf_T *buf;
10841 linenr_T start;
10842 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010843 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010844 typval_T *rettv;
10845{
10846 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010847
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010848 if (retlist && rettv_list_alloc(rettv) == FAIL)
10849 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010850
10851 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10852 return;
10853
10854 if (!retlist)
10855 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010856 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10857 p = ml_get_buf(buf, start, FALSE);
10858 else
10859 p = (char_u *)"";
10860
10861 rettv->v_type = VAR_STRING;
10862 rettv->vval.v_string = vim_strsave(p);
10863 }
10864 else
10865 {
10866 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010867 return;
10868
10869 if (start < 1)
10870 start = 1;
10871 if (end > buf->b_ml.ml_line_count)
10872 end = buf->b_ml.ml_line_count;
10873 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010874 if (list_append_string(rettv->vval.v_list,
10875 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010876 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010877 }
10878}
10879
10880/*
10881 * "getbufline()" function
10882 */
10883 static void
10884f_getbufline(argvars, rettv)
10885 typval_T *argvars;
10886 typval_T *rettv;
10887{
10888 linenr_T lnum;
10889 linenr_T end;
10890 buf_T *buf;
10891
10892 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10893 ++emsg_off;
10894 buf = get_buf_tv(&argvars[0]);
10895 --emsg_off;
10896
Bram Moolenaar661b1822005-07-28 22:36:45 +000010897 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010898 if (argvars[2].v_type == VAR_UNKNOWN)
10899 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010900 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010901 end = get_tv_lnum_buf(&argvars[2], buf);
10902
Bram Moolenaar342337a2005-07-21 21:11:17 +000010903 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010904}
10905
Bram Moolenaar0d660222005-01-07 21:51:51 +000010906/*
10907 * "getbufvar()" function
10908 */
10909 static void
10910f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010911 typval_T *argvars;
10912 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010913{
10914 buf_T *buf;
10915 buf_T *save_curbuf;
10916 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010917 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010919 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10920 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010921 ++emsg_off;
10922 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010923
10924 rettv->v_type = VAR_STRING;
10925 rettv->vval.v_string = NULL;
10926
10927 if (buf != NULL && varname != NULL)
10928 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010929 /* set curbuf to be our buf, temporarily */
10930 save_curbuf = curbuf;
10931 curbuf = buf;
10932
Bram Moolenaar0d660222005-01-07 21:51:51 +000010933 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010934 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010010935 else if (STRCMP(varname, "changedtick") == 0)
10936 {
10937 rettv->v_type = VAR_NUMBER;
10938 rettv->vval.v_number = curbuf->b_changedtick;
10939 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010940 else
10941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 if (*varname == NUL)
10943 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10944 * scope prefix before the NUL byte is required by
10945 * find_var_in_ht(). */
10946 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010947 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010948 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010949 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010950 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010951 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010952
10953 /* restore previous notion of curbuf */
10954 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010955 }
10956
10957 --emsg_off;
10958}
10959
10960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 * "getchar()" function
10962 */
10963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010965 typval_T *argvars;
10966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967{
10968 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010969 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010971 /* Position the cursor. Needed after a message that ends in a space. */
10972 windgoto(msg_row, msg_col);
10973
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 ++no_mapping;
10975 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010976 for (;;)
10977 {
10978 if (argvars[0].v_type == VAR_UNKNOWN)
10979 /* getchar(): blocking wait. */
10980 n = safe_vgetc();
10981 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10982 /* getchar(1): only check if char avail */
10983 n = vpeekc();
10984 else if (error || vpeekc() == NUL)
10985 /* illegal argument or getchar(0) and no char avail: return zero */
10986 n = 0;
10987 else
10988 /* getchar(0) and char avail: return char */
10989 n = safe_vgetc();
10990 if (n == K_IGNORE)
10991 continue;
10992 break;
10993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 --no_mapping;
10995 --allow_keys;
10996
Bram Moolenaar219b8702006-11-01 14:32:36 +000010997 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10998 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10999 vimvars[VV_MOUSE_COL].vv_nr = 0;
11000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011001 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 if (IS_SPECIAL(n) || mod_mask != 0)
11003 {
11004 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11005 int i = 0;
11006
11007 /* Turn a special key into three bytes, plus modifier. */
11008 if (mod_mask != 0)
11009 {
11010 temp[i++] = K_SPECIAL;
11011 temp[i++] = KS_MODIFIER;
11012 temp[i++] = mod_mask;
11013 }
11014 if (IS_SPECIAL(n))
11015 {
11016 temp[i++] = K_SPECIAL;
11017 temp[i++] = K_SECOND(n);
11018 temp[i++] = K_THIRD(n);
11019 }
11020#ifdef FEAT_MBYTE
11021 else if (has_mbyte)
11022 i += (*mb_char2bytes)(n, temp + i);
11023#endif
11024 else
11025 temp[i++] = n;
11026 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011027 rettv->v_type = VAR_STRING;
11028 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011029
11030#ifdef FEAT_MOUSE
11031 if (n == K_LEFTMOUSE
11032 || n == K_LEFTMOUSE_NM
11033 || n == K_LEFTDRAG
11034 || n == K_LEFTRELEASE
11035 || n == K_LEFTRELEASE_NM
11036 || n == K_MIDDLEMOUSE
11037 || n == K_MIDDLEDRAG
11038 || n == K_MIDDLERELEASE
11039 || n == K_RIGHTMOUSE
11040 || n == K_RIGHTDRAG
11041 || n == K_RIGHTRELEASE
11042 || n == K_X1MOUSE
11043 || n == K_X1DRAG
11044 || n == K_X1RELEASE
11045 || n == K_X2MOUSE
11046 || n == K_X2DRAG
11047 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011048 || n == K_MOUSELEFT
11049 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011050 || n == K_MOUSEDOWN
11051 || n == K_MOUSEUP)
11052 {
11053 int row = mouse_row;
11054 int col = mouse_col;
11055 win_T *win;
11056 linenr_T lnum;
11057# ifdef FEAT_WINDOWS
11058 win_T *wp;
11059# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011060 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011061
11062 if (row >= 0 && col >= 0)
11063 {
11064 /* Find the window at the mouse coordinates and compute the
11065 * text position. */
11066 win = mouse_find_win(&row, &col);
11067 (void)mouse_comp_pos(win, &row, &col, &lnum);
11068# ifdef FEAT_WINDOWS
11069 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011070 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011071# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011072 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011073 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11074 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11075 }
11076 }
11077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 }
11079}
11080
11081/*
11082 * "getcharmod()" function
11083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011086 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090}
11091
11092/*
11093 * "getcmdline()" function
11094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011096f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011097 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011100 rettv->v_type = VAR_STRING;
11101 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102}
11103
11104/*
11105 * "getcmdpos()" function
11106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011109 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113}
11114
11115/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011116 * "getcmdtype()" function
11117 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011118 static void
11119f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011120 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011121 typval_T *rettv;
11122{
11123 rettv->v_type = VAR_STRING;
11124 rettv->vval.v_string = alloc(2);
11125 if (rettv->vval.v_string != NULL)
11126 {
11127 rettv->vval.v_string[0] = get_cmdline_type();
11128 rettv->vval.v_string[1] = NUL;
11129 }
11130}
11131
11132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 * "getcwd()" function
11134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011136f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011137 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011140 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011143 rettv->vval.v_string = NULL;
11144 cwd = alloc(MAXPATHL);
11145 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011147 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11148 {
11149 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011151 if (rettv->vval.v_string != NULL)
11152 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011154 }
11155 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 }
11157}
11158
11159/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011160 * "getfontname()" function
11161 */
11162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011164 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011165 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011166{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167 rettv->v_type = VAR_STRING;
11168 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011169#ifdef FEAT_GUI
11170 if (gui.in_use)
11171 {
11172 GuiFont font;
11173 char_u *name = NULL;
11174
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011175 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011176 {
11177 /* Get the "Normal" font. Either the name saved by
11178 * hl_set_font_name() or from the font ID. */
11179 font = gui.norm_font;
11180 name = hl_get_font_name();
11181 }
11182 else
11183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011185 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11186 return;
11187 font = gui_mch_get_font(name, FALSE);
11188 if (font == NOFONT)
11189 return; /* Invalid font name, return empty string. */
11190 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011191 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011192 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011193 gui_mch_free_font(font);
11194 }
11195#endif
11196}
11197
11198/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011199 * "getfperm({fname})" function
11200 */
11201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011203 typval_T *argvars;
11204 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011205{
11206 char_u *fname;
11207 struct stat st;
11208 char_u *perm = NULL;
11209 char_u flags[] = "rwx";
11210 int i;
11211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011212 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011214 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011215 if (mch_stat((char *)fname, &st) >= 0)
11216 {
11217 perm = vim_strsave((char_u *)"---------");
11218 if (perm != NULL)
11219 {
11220 for (i = 0; i < 9; i++)
11221 {
11222 if (st.st_mode & (1 << (8 - i)))
11223 perm[i] = flags[i % 3];
11224 }
11225 }
11226 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011227 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011228}
11229
11230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 * "getfsize({fname})" function
11232 */
11233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011234f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011235 typval_T *argvars;
11236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237{
11238 char_u *fname;
11239 struct stat st;
11240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244
11245 if (mch_stat((char *)fname, &st) >= 0)
11246 {
11247 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011248 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011251 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011252
11253 /* non-perfect check for overflow */
11254 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11255 rettv->vval.v_number = -2;
11256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 }
11258 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260}
11261
11262/*
11263 * "getftime({fname})" function
11264 */
11265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011267 typval_T *argvars;
11268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269{
11270 char_u *fname;
11271 struct stat st;
11272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274
11275 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011278 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279}
11280
11281/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011282 * "getftype({fname})" function
11283 */
11284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011286 typval_T *argvars;
11287 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011288{
11289 char_u *fname;
11290 struct stat st;
11291 char_u *type = NULL;
11292 char *t;
11293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011297 if (mch_lstat((char *)fname, &st) >= 0)
11298 {
11299#ifdef S_ISREG
11300 if (S_ISREG(st.st_mode))
11301 t = "file";
11302 else if (S_ISDIR(st.st_mode))
11303 t = "dir";
11304# ifdef S_ISLNK
11305 else if (S_ISLNK(st.st_mode))
11306 t = "link";
11307# endif
11308# ifdef S_ISBLK
11309 else if (S_ISBLK(st.st_mode))
11310 t = "bdev";
11311# endif
11312# ifdef S_ISCHR
11313 else if (S_ISCHR(st.st_mode))
11314 t = "cdev";
11315# endif
11316# ifdef S_ISFIFO
11317 else if (S_ISFIFO(st.st_mode))
11318 t = "fifo";
11319# endif
11320# ifdef S_ISSOCK
11321 else if (S_ISSOCK(st.st_mode))
11322 t = "fifo";
11323# endif
11324 else
11325 t = "other";
11326#else
11327# ifdef S_IFMT
11328 switch (st.st_mode & S_IFMT)
11329 {
11330 case S_IFREG: t = "file"; break;
11331 case S_IFDIR: t = "dir"; break;
11332# ifdef S_IFLNK
11333 case S_IFLNK: t = "link"; break;
11334# endif
11335# ifdef S_IFBLK
11336 case S_IFBLK: t = "bdev"; break;
11337# endif
11338# ifdef S_IFCHR
11339 case S_IFCHR: t = "cdev"; break;
11340# endif
11341# ifdef S_IFIFO
11342 case S_IFIFO: t = "fifo"; break;
11343# endif
11344# ifdef S_IFSOCK
11345 case S_IFSOCK: t = "socket"; break;
11346# endif
11347 default: t = "other";
11348 }
11349# else
11350 if (mch_isdir(fname))
11351 t = "dir";
11352 else
11353 t = "file";
11354# endif
11355#endif
11356 type = vim_strsave((char_u *)t);
11357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011359}
11360
11361/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011362 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011363 */
11364 static void
11365f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011366 typval_T *argvars;
11367 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011368{
11369 linenr_T lnum;
11370 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011371 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011372
11373 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011374 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011375 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011376 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011377 retlist = FALSE;
11378 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011379 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011380 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011381 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011382 retlist = TRUE;
11383 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011384
Bram Moolenaar342337a2005-07-21 21:11:17 +000011385 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011386}
11387
11388/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011389 * "getmatches()" function
11390 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011391 static void
11392f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011393 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011394 typval_T *rettv;
11395{
11396#ifdef FEAT_SEARCH_EXTRA
11397 dict_T *dict;
11398 matchitem_T *cur = curwin->w_match_head;
11399
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011400 if (rettv_list_alloc(rettv) == OK)
11401 {
11402 while (cur != NULL)
11403 {
11404 dict = dict_alloc();
11405 if (dict == NULL)
11406 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011407 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11408 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11409 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11410 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11411 list_append_dict(rettv->vval.v_list, dict);
11412 cur = cur->next;
11413 }
11414 }
11415#endif
11416}
11417
11418/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011419 * "getpid()" function
11420 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011421 static void
11422f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011423 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011424 typval_T *rettv;
11425{
11426 rettv->vval.v_number = mch_get_pid();
11427}
11428
11429/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011430 * "getpos(string)" function
11431 */
11432 static void
11433f_getpos(argvars, rettv)
11434 typval_T *argvars;
11435 typval_T *rettv;
11436{
11437 pos_T *fp;
11438 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011439 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011440
11441 if (rettv_list_alloc(rettv) == OK)
11442 {
11443 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011444 fp = var2fpos(&argvars[0], TRUE, &fnum);
11445 if (fnum != -1)
11446 list_append_number(l, (varnumber_T)fnum);
11447 else
11448 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011449 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11450 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011451 list_append_number(l, (fp != NULL)
11452 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011453 : (varnumber_T)0);
11454 list_append_number(l,
11455#ifdef FEAT_VIRTUALEDIT
11456 (fp != NULL) ? (varnumber_T)fp->coladd :
11457#endif
11458 (varnumber_T)0);
11459 }
11460 else
11461 rettv->vval.v_number = FALSE;
11462}
11463
11464/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011465 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011466 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011467 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011468f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011469 typval_T *argvars UNUSED;
11470 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011471{
11472#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011473 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011474#endif
11475
Bram Moolenaar2641f772005-03-25 21:58:17 +000011476#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011477 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011478 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011479 wp = NULL;
11480 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11481 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011482 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011483 if (wp == NULL)
11484 return;
11485 }
11486
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011487 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011488 }
11489#endif
11490}
11491
11492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 * "getreg()" function
11494 */
11495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011497 typval_T *argvars;
11498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499{
11500 char_u *strregname;
11501 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011502 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011503 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011505 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011507 strregname = get_tv_string_chk(&argvars[0]);
11508 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011509 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011510 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011513 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514 regname = (strregname == NULL ? '"' : *strregname);
11515 if (regname == 0)
11516 regname = '"';
11517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011519 rettv->vval.v_string = error ? NULL :
11520 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521}
11522
11523/*
11524 * "getregtype()" function
11525 */
11526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011528 typval_T *argvars;
11529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530{
11531 char_u *strregname;
11532 int regname;
11533 char_u buf[NUMBUFLEN + 2];
11534 long reglen = 0;
11535
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011536 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011537 {
11538 strregname = get_tv_string_chk(&argvars[0]);
11539 if (strregname == NULL) /* type error; errmsg already given */
11540 {
11541 rettv->v_type = VAR_STRING;
11542 rettv->vval.v_string = NULL;
11543 return;
11544 }
11545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 else
11547 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011548 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549
11550 regname = (strregname == NULL ? '"' : *strregname);
11551 if (regname == 0)
11552 regname = '"';
11553
11554 buf[0] = NUL;
11555 buf[1] = NUL;
11556 switch (get_reg_type(regname, &reglen))
11557 {
11558 case MLINE: buf[0] = 'V'; break;
11559 case MCHAR: buf[0] = 'v'; break;
11560#ifdef FEAT_VISUAL
11561 case MBLOCK:
11562 buf[0] = Ctrl_V;
11563 sprintf((char *)buf + 1, "%ld", reglen + 1);
11564 break;
11565#endif
11566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011567 rettv->v_type = VAR_STRING;
11568 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569}
11570
11571/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011572 * "gettabvar()" function
11573 */
11574 static void
11575f_gettabvar(argvars, rettv)
11576 typval_T *argvars;
11577 typval_T *rettv;
11578{
11579 tabpage_T *tp;
11580 dictitem_T *v;
11581 char_u *varname;
11582
11583 rettv->v_type = VAR_STRING;
11584 rettv->vval.v_string = NULL;
11585
11586 varname = get_tv_string_chk(&argvars[1]);
11587 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11588 if (tp != NULL && varname != NULL)
11589 {
11590 /* look up the variable */
11591 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11592 if (v != NULL)
11593 copy_tv(&v->di_tv, rettv);
11594 }
11595}
11596
11597/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011598 * "gettabwinvar()" function
11599 */
11600 static void
11601f_gettabwinvar(argvars, rettv)
11602 typval_T *argvars;
11603 typval_T *rettv;
11604{
11605 getwinvar(argvars, rettv, 1);
11606}
11607
11608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609 * "getwinposx()" function
11610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011612f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011613 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011616 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617#ifdef FEAT_GUI
11618 if (gui.in_use)
11619 {
11620 int x, y;
11621
11622 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011623 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 }
11625#endif
11626}
11627
11628/*
11629 * "getwinposy()" function
11630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011633 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011636 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637#ifdef FEAT_GUI
11638 if (gui.in_use)
11639 {
11640 int x, y;
11641
11642 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011643 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 }
11645#endif
11646}
11647
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011648/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011649 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011650 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011651 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011652find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011653 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011654 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011655{
11656#ifdef FEAT_WINDOWS
11657 win_T *wp;
11658#endif
11659 int nr;
11660
11661 nr = get_tv_number_chk(vp, NULL);
11662
11663#ifdef FEAT_WINDOWS
11664 if (nr < 0)
11665 return NULL;
11666 if (nr == 0)
11667 return curwin;
11668
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011669 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11670 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011671 if (--nr <= 0)
11672 break;
11673 return wp;
11674#else
11675 if (nr == 0 || nr == 1)
11676 return curwin;
11677 return NULL;
11678#endif
11679}
11680
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681/*
11682 * "getwinvar()" function
11683 */
11684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011685f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011686 typval_T *argvars;
11687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011689 getwinvar(argvars, rettv, 0);
11690}
11691
11692/*
11693 * getwinvar() and gettabwinvar()
11694 */
11695 static void
11696getwinvar(argvars, rettv, off)
11697 typval_T *argvars;
11698 typval_T *rettv;
11699 int off; /* 1 for gettabwinvar() */
11700{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701 win_T *win, *oldcurwin;
11702 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011703 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011704 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011706#ifdef FEAT_WINDOWS
11707 if (off == 1)
11708 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11709 else
11710 tp = curtab;
11711#endif
11712 win = find_win_by_nr(&argvars[off], tp);
11713 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011714 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011716 rettv->v_type = VAR_STRING;
11717 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718
11719 if (win != NULL && varname != NULL)
11720 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011721 /* Set curwin to be our win, temporarily. Also set curbuf, so
11722 * that we can get buffer-local options. */
11723 oldcurwin = curwin;
11724 curwin = win;
11725 curbuf = win->w_buffer;
11726
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011728 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729 else
11730 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011731 if (*varname == NUL)
11732 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11733 * scope prefix before the NUL byte is required by
11734 * find_var_in_ht(). */
11735 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011737 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011739 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011741
11742 /* restore previous notion of curwin */
11743 curwin = oldcurwin;
11744 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745 }
11746
11747 --emsg_off;
11748}
11749
11750/*
11751 * "glob()" function
11752 */
11753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011754f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011755 typval_T *argvars;
11756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011758 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011760 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011762 /* When the optional second argument is non-zero, don't remove matches
11763 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11764 if (argvars[1].v_type != VAR_UNKNOWN
11765 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011766 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011768 if (!error)
11769 {
11770 ExpandInit(&xpc);
11771 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011772 if (p_wic)
11773 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011774 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011775 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011776 }
11777 else
11778 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779}
11780
11781/*
11782 * "globpath()" function
11783 */
11784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011785f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011786 typval_T *argvars;
11787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011789 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011791 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011792 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011794 /* When the optional second argument is non-zero, don't remove matches
11795 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11796 if (argvars[2].v_type != VAR_UNKNOWN
11797 && get_tv_number_chk(&argvars[2], &error))
11798 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011799 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011800 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 rettv->vval.v_string = NULL;
11802 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011803 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11804 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805}
11806
11807/*
11808 * "has()" function
11809 */
11810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011811f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011812 typval_T *argvars;
11813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814{
11815 int i;
11816 char_u *name;
11817 int n = FALSE;
11818 static char *(has_list[]) =
11819 {
11820#ifdef AMIGA
11821 "amiga",
11822# ifdef FEAT_ARP
11823 "arp",
11824# endif
11825#endif
11826#ifdef __BEOS__
11827 "beos",
11828#endif
11829#ifdef MSDOS
11830# ifdef DJGPP
11831 "dos32",
11832# else
11833 "dos16",
11834# endif
11835#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011836#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 "mac",
11838#endif
11839#if defined(MACOS_X_UNIX)
11840 "macunix",
11841#endif
11842#ifdef OS2
11843 "os2",
11844#endif
11845#ifdef __QNX__
11846 "qnx",
11847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848#ifdef UNIX
11849 "unix",
11850#endif
11851#ifdef VMS
11852 "vms",
11853#endif
11854#ifdef WIN16
11855 "win16",
11856#endif
11857#ifdef WIN32
11858 "win32",
11859#endif
11860#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11861 "win32unix",
11862#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011863#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 "win64",
11865#endif
11866#ifdef EBCDIC
11867 "ebcdic",
11868#endif
11869#ifndef CASE_INSENSITIVE_FILENAME
11870 "fname_case",
11871#endif
11872#ifdef FEAT_ARABIC
11873 "arabic",
11874#endif
11875#ifdef FEAT_AUTOCMD
11876 "autocmd",
11877#endif
11878#ifdef FEAT_BEVAL
11879 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011880# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11881 "balloon_multiline",
11882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883#endif
11884#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11885 "builtin_terms",
11886# ifdef ALL_BUILTIN_TCAPS
11887 "all_builtin_terms",
11888# endif
11889#endif
11890#ifdef FEAT_BYTEOFF
11891 "byte_offset",
11892#endif
11893#ifdef FEAT_CINDENT
11894 "cindent",
11895#endif
11896#ifdef FEAT_CLIENTSERVER
11897 "clientserver",
11898#endif
11899#ifdef FEAT_CLIPBOARD
11900 "clipboard",
11901#endif
11902#ifdef FEAT_CMDL_COMPL
11903 "cmdline_compl",
11904#endif
11905#ifdef FEAT_CMDHIST
11906 "cmdline_hist",
11907#endif
11908#ifdef FEAT_COMMENTS
11909 "comments",
11910#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011911#ifdef FEAT_CONCEAL
11912 "conceal",
11913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914#ifdef FEAT_CRYPT
11915 "cryptv",
11916#endif
11917#ifdef FEAT_CSCOPE
11918 "cscope",
11919#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011920#ifdef FEAT_CURSORBIND
11921 "cursorbind",
11922#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011923#ifdef CURSOR_SHAPE
11924 "cursorshape",
11925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926#ifdef DEBUG
11927 "debug",
11928#endif
11929#ifdef FEAT_CON_DIALOG
11930 "dialog_con",
11931#endif
11932#ifdef FEAT_GUI_DIALOG
11933 "dialog_gui",
11934#endif
11935#ifdef FEAT_DIFF
11936 "diff",
11937#endif
11938#ifdef FEAT_DIGRAPHS
11939 "digraphs",
11940#endif
11941#ifdef FEAT_DND
11942 "dnd",
11943#endif
11944#ifdef FEAT_EMACS_TAGS
11945 "emacs_tags",
11946#endif
11947 "eval", /* always present, of course! */
11948#ifdef FEAT_EX_EXTRA
11949 "ex_extra",
11950#endif
11951#ifdef FEAT_SEARCH_EXTRA
11952 "extra_search",
11953#endif
11954#ifdef FEAT_FKMAP
11955 "farsi",
11956#endif
11957#ifdef FEAT_SEARCHPATH
11958 "file_in_path",
11959#endif
Bram Moolenaar4b9669f2011-07-07 16:20:52 +020011960#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011961 "filterpipe",
11962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963#ifdef FEAT_FIND_ID
11964 "find_in_path",
11965#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011966#ifdef FEAT_FLOAT
11967 "float",
11968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969#ifdef FEAT_FOLDING
11970 "folding",
11971#endif
11972#ifdef FEAT_FOOTER
11973 "footer",
11974#endif
11975#if !defined(USE_SYSTEM) && defined(UNIX)
11976 "fork",
11977#endif
11978#ifdef FEAT_GETTEXT
11979 "gettext",
11980#endif
11981#ifdef FEAT_GUI
11982 "gui",
11983#endif
11984#ifdef FEAT_GUI_ATHENA
11985# ifdef FEAT_GUI_NEXTAW
11986 "gui_neXtaw",
11987# else
11988 "gui_athena",
11989# endif
11990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991#ifdef FEAT_GUI_GTK
11992 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011995#ifdef FEAT_GUI_GNOME
11996 "gui_gnome",
11997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998#ifdef FEAT_GUI_MAC
11999 "gui_mac",
12000#endif
12001#ifdef FEAT_GUI_MOTIF
12002 "gui_motif",
12003#endif
12004#ifdef FEAT_GUI_PHOTON
12005 "gui_photon",
12006#endif
12007#ifdef FEAT_GUI_W16
12008 "gui_win16",
12009#endif
12010#ifdef FEAT_GUI_W32
12011 "gui_win32",
12012#endif
12013#ifdef FEAT_HANGULIN
12014 "hangul_input",
12015#endif
12016#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12017 "iconv",
12018#endif
12019#ifdef FEAT_INS_EXPAND
12020 "insert_expand",
12021#endif
12022#ifdef FEAT_JUMPLIST
12023 "jumplist",
12024#endif
12025#ifdef FEAT_KEYMAP
12026 "keymap",
12027#endif
12028#ifdef FEAT_LANGMAP
12029 "langmap",
12030#endif
12031#ifdef FEAT_LIBCALL
12032 "libcall",
12033#endif
12034#ifdef FEAT_LINEBREAK
12035 "linebreak",
12036#endif
12037#ifdef FEAT_LISP
12038 "lispindent",
12039#endif
12040#ifdef FEAT_LISTCMDS
12041 "listcmds",
12042#endif
12043#ifdef FEAT_LOCALMAP
12044 "localmap",
12045#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012046#ifdef FEAT_LUA
12047# ifndef DYNAMIC_LUA
12048 "lua",
12049# endif
12050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051#ifdef FEAT_MENU
12052 "menu",
12053#endif
12054#ifdef FEAT_SESSION
12055 "mksession",
12056#endif
12057#ifdef FEAT_MODIFY_FNAME
12058 "modify_fname",
12059#endif
12060#ifdef FEAT_MOUSE
12061 "mouse",
12062#endif
12063#ifdef FEAT_MOUSESHAPE
12064 "mouseshape",
12065#endif
12066#if defined(UNIX) || defined(VMS)
12067# ifdef FEAT_MOUSE_DEC
12068 "mouse_dec",
12069# endif
12070# ifdef FEAT_MOUSE_GPM
12071 "mouse_gpm",
12072# endif
12073# ifdef FEAT_MOUSE_JSB
12074 "mouse_jsbterm",
12075# endif
12076# ifdef FEAT_MOUSE_NET
12077 "mouse_netterm",
12078# endif
12079# ifdef FEAT_MOUSE_PTERM
12080 "mouse_pterm",
12081# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012082# ifdef FEAT_SYSMOUSE
12083 "mouse_sysmouse",
12084# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085# ifdef FEAT_MOUSE_XTERM
12086 "mouse_xterm",
12087# endif
12088#endif
12089#ifdef FEAT_MBYTE
12090 "multi_byte",
12091#endif
12092#ifdef FEAT_MBYTE_IME
12093 "multi_byte_ime",
12094#endif
12095#ifdef FEAT_MULTI_LANG
12096 "multi_lang",
12097#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012098#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012099#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012100 "mzscheme",
12101#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103#ifdef FEAT_OLE
12104 "ole",
12105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106#ifdef FEAT_PATH_EXTRA
12107 "path_extra",
12108#endif
12109#ifdef FEAT_PERL
12110#ifndef DYNAMIC_PERL
12111 "perl",
12112#endif
12113#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012114#ifdef FEAT_PERSISTENT_UNDO
12115 "persistent_undo",
12116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117#ifdef FEAT_PYTHON
12118#ifndef DYNAMIC_PYTHON
12119 "python",
12120#endif
12121#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012122#ifdef FEAT_PYTHON3
12123#ifndef DYNAMIC_PYTHON3
12124 "python3",
12125#endif
12126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127#ifdef FEAT_POSTSCRIPT
12128 "postscript",
12129#endif
12130#ifdef FEAT_PRINTER
12131 "printer",
12132#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012133#ifdef FEAT_PROFILE
12134 "profile",
12135#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012136#ifdef FEAT_RELTIME
12137 "reltime",
12138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139#ifdef FEAT_QUICKFIX
12140 "quickfix",
12141#endif
12142#ifdef FEAT_RIGHTLEFT
12143 "rightleft",
12144#endif
12145#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12146 "ruby",
12147#endif
12148#ifdef FEAT_SCROLLBIND
12149 "scrollbind",
12150#endif
12151#ifdef FEAT_CMDL_INFO
12152 "showcmd",
12153 "cmdline_info",
12154#endif
12155#ifdef FEAT_SIGNS
12156 "signs",
12157#endif
12158#ifdef FEAT_SMARTINDENT
12159 "smartindent",
12160#endif
12161#ifdef FEAT_SNIFF
12162 "sniff",
12163#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012164#ifdef STARTUPTIME
12165 "startuptime",
12166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167#ifdef FEAT_STL_OPT
12168 "statusline",
12169#endif
12170#ifdef FEAT_SUN_WORKSHOP
12171 "sun_workshop",
12172#endif
12173#ifdef FEAT_NETBEANS_INTG
12174 "netbeans_intg",
12175#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012176#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012177 "spell",
12178#endif
12179#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180 "syntax",
12181#endif
12182#if defined(USE_SYSTEM) || !defined(UNIX)
12183 "system",
12184#endif
12185#ifdef FEAT_TAG_BINS
12186 "tag_binary",
12187#endif
12188#ifdef FEAT_TAG_OLDSTATIC
12189 "tag_old_static",
12190#endif
12191#ifdef FEAT_TAG_ANYWHITE
12192 "tag_any_white",
12193#endif
12194#ifdef FEAT_TCL
12195# ifndef DYNAMIC_TCL
12196 "tcl",
12197# endif
12198#endif
12199#ifdef TERMINFO
12200 "terminfo",
12201#endif
12202#ifdef FEAT_TERMRESPONSE
12203 "termresponse",
12204#endif
12205#ifdef FEAT_TEXTOBJ
12206 "textobjects",
12207#endif
12208#ifdef HAVE_TGETENT
12209 "tgetent",
12210#endif
12211#ifdef FEAT_TITLE
12212 "title",
12213#endif
12214#ifdef FEAT_TOOLBAR
12215 "toolbar",
12216#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012217#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12218 "unnamedplus",
12219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220#ifdef FEAT_USR_CMDS
12221 "user-commands", /* was accidentally included in 5.4 */
12222 "user_commands",
12223#endif
12224#ifdef FEAT_VIMINFO
12225 "viminfo",
12226#endif
12227#ifdef FEAT_VERTSPLIT
12228 "vertsplit",
12229#endif
12230#ifdef FEAT_VIRTUALEDIT
12231 "virtualedit",
12232#endif
12233#ifdef FEAT_VISUAL
12234 "visual",
12235#endif
12236#ifdef FEAT_VISUALEXTRA
12237 "visualextra",
12238#endif
12239#ifdef FEAT_VREPLACE
12240 "vreplace",
12241#endif
12242#ifdef FEAT_WILDIGN
12243 "wildignore",
12244#endif
12245#ifdef FEAT_WILDMENU
12246 "wildmenu",
12247#endif
12248#ifdef FEAT_WINDOWS
12249 "windows",
12250#endif
12251#ifdef FEAT_WAK
12252 "winaltkeys",
12253#endif
12254#ifdef FEAT_WRITEBACKUP
12255 "writebackup",
12256#endif
12257#ifdef FEAT_XIM
12258 "xim",
12259#endif
12260#ifdef FEAT_XFONTSET
12261 "xfontset",
12262#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012263#ifdef FEAT_XPM_W32
12264 "xpm_w32",
12265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266#ifdef USE_XSMP
12267 "xsmp",
12268#endif
12269#ifdef USE_XSMP_INTERACT
12270 "xsmp_interact",
12271#endif
12272#ifdef FEAT_XCLIPBOARD
12273 "xterm_clipboard",
12274#endif
12275#ifdef FEAT_XTERM_SAVE
12276 "xterm_save",
12277#endif
12278#if defined(UNIX) && defined(FEAT_X11)
12279 "X11",
12280#endif
12281 NULL
12282 };
12283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012284 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 for (i = 0; has_list[i] != NULL; ++i)
12286 if (STRICMP(name, has_list[i]) == 0)
12287 {
12288 n = TRUE;
12289 break;
12290 }
12291
12292 if (n == FALSE)
12293 {
12294 if (STRNICMP(name, "patch", 5) == 0)
12295 n = has_patch(atoi((char *)name + 5));
12296 else if (STRICMP(name, "vim_starting") == 0)
12297 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012298#ifdef FEAT_MBYTE
12299 else if (STRICMP(name, "multi_byte_encoding") == 0)
12300 n = has_mbyte;
12301#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012302#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12303 else if (STRICMP(name, "balloon_multiline") == 0)
12304 n = multiline_balloon_available();
12305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306#ifdef DYNAMIC_TCL
12307 else if (STRICMP(name, "tcl") == 0)
12308 n = tcl_enabled(FALSE);
12309#endif
12310#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12311 else if (STRICMP(name, "iconv") == 0)
12312 n = iconv_enabled(FALSE);
12313#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012314#ifdef DYNAMIC_LUA
12315 else if (STRICMP(name, "lua") == 0)
12316 n = lua_enabled(FALSE);
12317#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012318#ifdef DYNAMIC_MZSCHEME
12319 else if (STRICMP(name, "mzscheme") == 0)
12320 n = mzscheme_enabled(FALSE);
12321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322#ifdef DYNAMIC_RUBY
12323 else if (STRICMP(name, "ruby") == 0)
12324 n = ruby_enabled(FALSE);
12325#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012326#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327#ifdef DYNAMIC_PYTHON
12328 else if (STRICMP(name, "python") == 0)
12329 n = python_enabled(FALSE);
12330#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012331#endif
12332#ifdef FEAT_PYTHON3
12333#ifdef DYNAMIC_PYTHON3
12334 else if (STRICMP(name, "python3") == 0)
12335 n = python3_enabled(FALSE);
12336#endif
12337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338#ifdef DYNAMIC_PERL
12339 else if (STRICMP(name, "perl") == 0)
12340 n = perl_enabled(FALSE);
12341#endif
12342#ifdef FEAT_GUI
12343 else if (STRICMP(name, "gui_running") == 0)
12344 n = (gui.in_use || gui.starting);
12345# ifdef FEAT_GUI_W32
12346 else if (STRICMP(name, "gui_win32s") == 0)
12347 n = gui_is_win32s();
12348# endif
12349# ifdef FEAT_BROWSE
12350 else if (STRICMP(name, "browse") == 0)
12351 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12352# endif
12353#endif
12354#ifdef FEAT_SYN_HL
12355 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012356 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357#endif
12358#if defined(WIN3264)
12359 else if (STRICMP(name, "win95") == 0)
12360 n = mch_windows95();
12361#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012362#ifdef FEAT_NETBEANS_INTG
12363 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012364 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 }
12367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369}
12370
12371/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012372 * "has_key()" function
12373 */
12374 static void
12375f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012376 typval_T *argvars;
12377 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012378{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012379 if (argvars[0].v_type != VAR_DICT)
12380 {
12381 EMSG(_(e_dictreq));
12382 return;
12383 }
12384 if (argvars[0].vval.v_dict == NULL)
12385 return;
12386
12387 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012388 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012389}
12390
12391/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012392 * "haslocaldir()" function
12393 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012394 static void
12395f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012396 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012397 typval_T *rettv;
12398{
12399 rettv->vval.v_number = (curwin->w_localdir != NULL);
12400}
12401
12402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403 * "hasmapto()" function
12404 */
12405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012407 typval_T *argvars;
12408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409{
12410 char_u *name;
12411 char_u *mode;
12412 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012413 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012416 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417 mode = (char_u *)"nvo";
12418 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012421 if (argvars[2].v_type != VAR_UNKNOWN)
12422 abbr = get_tv_number(&argvars[2]);
12423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424
Bram Moolenaar2c932302006-03-18 21:42:09 +000012425 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012426 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429}
12430
12431/*
12432 * "histadd()" function
12433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012436 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438{
12439#ifdef FEAT_CMDHIST
12440 int histype;
12441 char_u *str;
12442 char_u buf[NUMBUFLEN];
12443#endif
12444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012445 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 if (check_restricted() || check_secure())
12447 return;
12448#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012449 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12450 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451 if (histype >= 0)
12452 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012453 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454 if (*str != NUL)
12455 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012456 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 return;
12460 }
12461 }
12462#endif
12463}
12464
12465/*
12466 * "histdel()" function
12467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012469f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012470 typval_T *argvars UNUSED;
12471 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472{
12473#ifdef FEAT_CMDHIST
12474 int n;
12475 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012476 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012478 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12479 if (str == NULL)
12480 n = 0;
12481 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012483 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012484 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012486 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012487 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488 else
12489 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012490 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 get_tv_string_buf(&argvars[1], buf));
12492 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493#endif
12494}
12495
12496/*
12497 * "histget()" function
12498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012501 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503{
12504#ifdef FEAT_CMDHIST
12505 int type;
12506 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012507 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012509 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12510 if (str == NULL)
12511 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012513 {
12514 type = get_histtype(str);
12515 if (argvars[1].v_type == VAR_UNKNOWN)
12516 idx = get_history_idx(type);
12517 else
12518 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12519 /* -1 on type error */
12520 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012523 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012525 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526}
12527
12528/*
12529 * "histnr()" function
12530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012533 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535{
12536 int i;
12537
12538#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012539 char_u *history = get_tv_string_chk(&argvars[0]);
12540
12541 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542 if (i >= HIST_CMD && i < HIST_COUNT)
12543 i = get_history_idx(i);
12544 else
12545#endif
12546 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012547 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548}
12549
12550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551 * "highlightID(name)" function
12552 */
12553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012554f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012555 typval_T *argvars;
12556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559}
12560
12561/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012562 * "highlight_exists()" function
12563 */
12564 static void
12565f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012566 typval_T *argvars;
12567 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012568{
12569 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12570}
12571
12572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573 * "hostname()" function
12574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012576f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012577 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579{
12580 char_u hostname[256];
12581
12582 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012583 rettv->v_type = VAR_STRING;
12584 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585}
12586
12587/*
12588 * iconv() function
12589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012592 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594{
12595#ifdef FEAT_MBYTE
12596 char_u buf1[NUMBUFLEN];
12597 char_u buf2[NUMBUFLEN];
12598 char_u *from, *to, *str;
12599 vimconv_T vimconv;
12600#endif
12601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012602 rettv->v_type = VAR_STRING;
12603 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604
12605#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012606 str = get_tv_string(&argvars[0]);
12607 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12608 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609 vimconv.vc_type = CONV_NONE;
12610 convert_setup(&vimconv, from, to);
12611
12612 /* If the encodings are equal, no conversion needed. */
12613 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012614 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617
12618 convert_setup(&vimconv, NULL, NULL);
12619 vim_free(from);
12620 vim_free(to);
12621#endif
12622}
12623
12624/*
12625 * "indent()" function
12626 */
12627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012628f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012629 typval_T *argvars;
12630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631{
12632 linenr_T lnum;
12633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012634 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012636 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639}
12640
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012641/*
12642 * "index()" function
12643 */
12644 static void
12645f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012646 typval_T *argvars;
12647 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012648{
Bram Moolenaar33570922005-01-25 22:26:29 +000012649 list_T *l;
12650 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012651 long idx = 0;
12652 int ic = FALSE;
12653
12654 rettv->vval.v_number = -1;
12655 if (argvars[0].v_type != VAR_LIST)
12656 {
12657 EMSG(_(e_listreq));
12658 return;
12659 }
12660 l = argvars[0].vval.v_list;
12661 if (l != NULL)
12662 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012663 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012664 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012666 int error = FALSE;
12667
Bram Moolenaar758711c2005-02-02 23:11:38 +000012668 /* Start at specified item. Use the cached index that list_find()
12669 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012670 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012671 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012672 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012673 ic = get_tv_number_chk(&argvars[3], &error);
12674 if (error)
12675 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012676 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012677
Bram Moolenaar758711c2005-02-02 23:11:38 +000012678 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012679 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012680 {
12681 rettv->vval.v_number = idx;
12682 break;
12683 }
12684 }
12685}
12686
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687static int inputsecret_flag = 0;
12688
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012689static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12690
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012692 * This function is used by f_input() and f_inputdialog() functions. The third
12693 * argument to f_input() specifies the type of completion to use at the
12694 * prompt. The third argument to f_inputdialog() specifies the value to return
12695 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 */
12697 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012698get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012699 typval_T *argvars;
12700 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012701 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012703 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 char_u *p = NULL;
12705 int c;
12706 char_u buf[NUMBUFLEN];
12707 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012708 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012709 int xp_type = EXPAND_NOTHING;
12710 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012713 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714
12715#ifdef NO_CONSOLE_INPUT
12716 /* While starting up, there is no place to enter text. */
12717 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719#endif
12720
12721 cmd_silent = FALSE; /* Want to see the prompt. */
12722 if (prompt != NULL)
12723 {
12724 /* Only the part of the message after the last NL is considered as
12725 * prompt for the command line */
12726 p = vim_strrchr(prompt, '\n');
12727 if (p == NULL)
12728 p = prompt;
12729 else
12730 {
12731 ++p;
12732 c = *p;
12733 *p = NUL;
12734 msg_start();
12735 msg_clr_eos();
12736 msg_puts_attr(prompt, echo_attr);
12737 msg_didout = FALSE;
12738 msg_starthere();
12739 *p = c;
12740 }
12741 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012743 if (argvars[1].v_type != VAR_UNKNOWN)
12744 {
12745 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12746 if (defstr != NULL)
12747 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012749 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012750 {
12751 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012752 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012753 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012754
Bram Moolenaar4463f292005-09-25 22:20:24 +000012755 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012756
Bram Moolenaar4463f292005-09-25 22:20:24 +000012757 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12758 if (xp_name == NULL)
12759 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012760
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012761 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012762
Bram Moolenaar4463f292005-09-25 22:20:24 +000012763 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12764 &xp_arg) == FAIL)
12765 return;
12766 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012767 }
12768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012769 if (defstr != NULL)
12770 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012771 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12772 xp_type, xp_arg);
12773
12774 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012776 /* since the user typed this, no need to wait for return */
12777 need_wait_return = FALSE;
12778 msg_didout = FALSE;
12779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 cmd_silent = cmd_silent_save;
12781}
12782
12783/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012784 * "input()" function
12785 * Also handles inputsecret() when inputsecret is set.
12786 */
12787 static void
12788f_input(argvars, rettv)
12789 typval_T *argvars;
12790 typval_T *rettv;
12791{
12792 get_user_input(argvars, rettv, FALSE);
12793}
12794
12795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 * "inputdialog()" function
12797 */
12798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012800 typval_T *argvars;
12801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802{
12803#if defined(FEAT_GUI_TEXTDIALOG)
12804 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12805 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12806 {
12807 char_u *message;
12808 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012809 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012811 message = get_tv_string_chk(&argvars[0]);
12812 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012813 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012814 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 else
12816 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012817 if (message != NULL && defstr != NULL
12818 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012819 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012820 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 else
12822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012823 if (message != NULL && defstr != NULL
12824 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012825 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012826 rettv->vval.v_string = vim_strsave(
12827 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832 }
12833 else
12834#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012835 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836}
12837
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012838/*
12839 * "inputlist()" function
12840 */
12841 static void
12842f_inputlist(argvars, rettv)
12843 typval_T *argvars;
12844 typval_T *rettv;
12845{
12846 listitem_T *li;
12847 int selected;
12848 int mouse_used;
12849
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012850#ifdef NO_CONSOLE_INPUT
12851 /* While starting up, there is no place to enter text. */
12852 if (no_console_input())
12853 return;
12854#endif
12855 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12856 {
12857 EMSG2(_(e_listarg), "inputlist()");
12858 return;
12859 }
12860
12861 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012862 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012863 lines_left = Rows; /* avoid more prompt */
12864 msg_scroll = TRUE;
12865 msg_clr_eos();
12866
12867 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12868 {
12869 msg_puts(get_tv_string(&li->li_tv));
12870 msg_putchar('\n');
12871 }
12872
12873 /* Ask for choice. */
12874 selected = prompt_for_number(&mouse_used);
12875 if (mouse_used)
12876 selected -= lines_left;
12877
12878 rettv->vval.v_number = selected;
12879}
12880
12881
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12883
12884/*
12885 * "inputrestore()" function
12886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012889 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891{
12892 if (ga_userinput.ga_len > 0)
12893 {
12894 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12896 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012897 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898 }
12899 else if (p_verbose > 1)
12900 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012901 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012902 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 }
12904}
12905
12906/*
12907 * "inputsave()" function
12908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012910f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012911 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012914 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915 if (ga_grow(&ga_userinput, 1) == OK)
12916 {
12917 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12918 + ga_userinput.ga_len);
12919 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012920 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 }
12922 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012923 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924}
12925
12926/*
12927 * "inputsecret()" function
12928 */
12929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012930f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012931 typval_T *argvars;
12932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933{
12934 ++cmdline_star;
12935 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012936 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 --cmdline_star;
12938 --inputsecret_flag;
12939}
12940
12941/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012942 * "insert()" function
12943 */
12944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012946 typval_T *argvars;
12947 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012948{
12949 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012950 listitem_T *item;
12951 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012952 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012953
12954 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012955 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012956 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012957 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012958 {
12959 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012960 before = get_tv_number_chk(&argvars[2], &error);
12961 if (error)
12962 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012963
Bram Moolenaar758711c2005-02-02 23:11:38 +000012964 if (before == l->lv_len)
12965 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012966 else
12967 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012968 item = list_find(l, before);
12969 if (item == NULL)
12970 {
12971 EMSGN(_(e_listidx), before);
12972 l = NULL;
12973 }
12974 }
12975 if (l != NULL)
12976 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012977 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012978 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012979 }
12980 }
12981}
12982
12983/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010012984 * "invert(expr)" function
12985 */
12986 static void
12987f_invert(argvars, rettv)
12988 typval_T *argvars;
12989 typval_T *rettv;
12990{
12991 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
12992}
12993
12994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995 * "isdirectory()" function
12996 */
12997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012998f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012999 typval_T *argvars;
13000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013002 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003}
13004
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013005/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013006 * "islocked()" function
13007 */
13008 static void
13009f_islocked(argvars, rettv)
13010 typval_T *argvars;
13011 typval_T *rettv;
13012{
13013 lval_T lv;
13014 char_u *end;
13015 dictitem_T *di;
13016
13017 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013018 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13019 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013020 if (end != NULL && lv.ll_name != NULL)
13021 {
13022 if (*end != NUL)
13023 EMSG(_(e_trailing));
13024 else
13025 {
13026 if (lv.ll_tv == NULL)
13027 {
13028 if (check_changedtick(lv.ll_name))
13029 rettv->vval.v_number = 1; /* always locked */
13030 else
13031 {
13032 di = find_var(lv.ll_name, NULL);
13033 if (di != NULL)
13034 {
13035 /* Consider a variable locked when:
13036 * 1. the variable itself is locked
13037 * 2. the value of the variable is locked.
13038 * 3. the List or Dict value is locked.
13039 */
13040 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13041 || tv_islocked(&di->di_tv));
13042 }
13043 }
13044 }
13045 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013046 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013047 else if (lv.ll_newkey != NULL)
13048 EMSG2(_(e_dictkey), lv.ll_newkey);
13049 else if (lv.ll_list != NULL)
13050 /* List item. */
13051 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13052 else
13053 /* Dictionary item. */
13054 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13055 }
13056 }
13057
13058 clear_lval(&lv);
13059}
13060
Bram Moolenaar33570922005-01-25 22:26:29 +000013061static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013062
13063/*
13064 * Turn a dict into a list:
13065 * "what" == 0: list of keys
13066 * "what" == 1: list of values
13067 * "what" == 2: list of items
13068 */
13069 static void
13070dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013071 typval_T *argvars;
13072 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013073 int what;
13074{
Bram Moolenaar33570922005-01-25 22:26:29 +000013075 list_T *l2;
13076 dictitem_T *di;
13077 hashitem_T *hi;
13078 listitem_T *li;
13079 listitem_T *li2;
13080 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013081 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013082
Bram Moolenaar8c711452005-01-14 21:53:12 +000013083 if (argvars[0].v_type != VAR_DICT)
13084 {
13085 EMSG(_(e_dictreq));
13086 return;
13087 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013088 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013089 return;
13090
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013091 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013092 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013093
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013094 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013095 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013096 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013097 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013099 --todo;
13100 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013101
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013102 li = listitem_alloc();
13103 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013104 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013105 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013106
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013107 if (what == 0)
13108 {
13109 /* keys() */
13110 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013111 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013112 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13113 }
13114 else if (what == 1)
13115 {
13116 /* values() */
13117 copy_tv(&di->di_tv, &li->li_tv);
13118 }
13119 else
13120 {
13121 /* items() */
13122 l2 = list_alloc();
13123 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013124 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013125 li->li_tv.vval.v_list = l2;
13126 if (l2 == NULL)
13127 break;
13128 ++l2->lv_refcount;
13129
13130 li2 = listitem_alloc();
13131 if (li2 == NULL)
13132 break;
13133 list_append(l2, li2);
13134 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013135 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013136 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13137
13138 li2 = listitem_alloc();
13139 if (li2 == NULL)
13140 break;
13141 list_append(l2, li2);
13142 copy_tv(&di->di_tv, &li2->li_tv);
13143 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013144 }
13145 }
13146}
13147
13148/*
13149 * "items(dict)" function
13150 */
13151 static void
13152f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013153 typval_T *argvars;
13154 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013155{
13156 dict_list(argvars, rettv, 2);
13157}
13158
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013160 * "join()" function
13161 */
13162 static void
13163f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013164 typval_T *argvars;
13165 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013166{
13167 garray_T ga;
13168 char_u *sep;
13169
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013170 if (argvars[0].v_type != VAR_LIST)
13171 {
13172 EMSG(_(e_listreq));
13173 return;
13174 }
13175 if (argvars[0].vval.v_list == NULL)
13176 return;
13177 if (argvars[1].v_type == VAR_UNKNOWN)
13178 sep = (char_u *)" ";
13179 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013180 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013181
13182 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013183
13184 if (sep != NULL)
13185 {
13186 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013187 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013188 ga_append(&ga, NUL);
13189 rettv->vval.v_string = (char_u *)ga.ga_data;
13190 }
13191 else
13192 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013193}
13194
13195/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013196 * "keys()" function
13197 */
13198 static void
13199f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013200 typval_T *argvars;
13201 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013202{
13203 dict_list(argvars, rettv, 0);
13204}
13205
13206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207 * "last_buffer_nr()" function.
13208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013210f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013211 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213{
13214 int n = 0;
13215 buf_T *buf;
13216
13217 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13218 if (n < buf->b_fnum)
13219 n = buf->b_fnum;
13220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013221 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013222}
13223
13224/*
13225 * "len()" function
13226 */
13227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013229 typval_T *argvars;
13230 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013231{
13232 switch (argvars[0].v_type)
13233 {
13234 case VAR_STRING:
13235 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236 rettv->vval.v_number = (varnumber_T)STRLEN(
13237 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013238 break;
13239 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013240 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013241 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013242 case VAR_DICT:
13243 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13244 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013245 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013246 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013247 break;
13248 }
13249}
13250
Bram Moolenaar33570922005-01-25 22:26:29 +000013251static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013252
13253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013254libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013255 typval_T *argvars;
13256 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013257 int type;
13258{
13259#ifdef FEAT_LIBCALL
13260 char_u *string_in;
13261 char_u **string_result;
13262 int nr_result;
13263#endif
13264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013265 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013266 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013267 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013268
13269 if (check_restricted() || check_secure())
13270 return;
13271
13272#ifdef FEAT_LIBCALL
13273 /* The first two args must be strings, otherwise its meaningless */
13274 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13275 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013276 string_in = NULL;
13277 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013278 string_in = argvars[2].vval.v_string;
13279 if (type == VAR_NUMBER)
13280 string_result = NULL;
13281 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013282 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013283 if (mch_libcall(argvars[0].vval.v_string,
13284 argvars[1].vval.v_string,
13285 string_in,
13286 argvars[2].vval.v_number,
13287 string_result,
13288 &nr_result) == OK
13289 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013290 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013291 }
13292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293}
13294
13295/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013296 * "libcall()" function
13297 */
13298 static void
13299f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013300 typval_T *argvars;
13301 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013302{
13303 libcall_common(argvars, rettv, VAR_STRING);
13304}
13305
13306/*
13307 * "libcallnr()" function
13308 */
13309 static void
13310f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013311 typval_T *argvars;
13312 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013313{
13314 libcall_common(argvars, rettv, VAR_NUMBER);
13315}
13316
13317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 * "line(string)" function
13319 */
13320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013321f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013322 typval_T *argvars;
13323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324{
13325 linenr_T lnum = 0;
13326 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013327 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013329 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 if (fp != NULL)
13331 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013332 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333}
13334
13335/*
13336 * "line2byte(lnum)" function
13337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013340 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342{
13343#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013344 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345#else
13346 linenr_T lnum;
13347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013348 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013352 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13353 if (rettv->vval.v_number >= 0)
13354 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355#endif
13356}
13357
13358/*
13359 * "lispindent(lnum)" function
13360 */
13361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013362f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013363 typval_T *argvars;
13364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365{
13366#ifdef FEAT_LISP
13367 pos_T pos;
13368 linenr_T lnum;
13369
13370 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013371 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13373 {
13374 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013375 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376 curwin->w_cursor = pos;
13377 }
13378 else
13379#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013380 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381}
13382
13383/*
13384 * "localtime()" function
13385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013387f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013388 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013391 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392}
13393
Bram Moolenaar33570922005-01-25 22:26:29 +000013394static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395
13396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013397get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 typval_T *argvars;
13399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400 int exact;
13401{
13402 char_u *keys;
13403 char_u *which;
13404 char_u buf[NUMBUFLEN];
13405 char_u *keys_buf = NULL;
13406 char_u *rhs;
13407 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013408 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013409 int get_dict = FALSE;
13410 mapblock_T *mp;
13411 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412
13413 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013414 rettv->v_type = VAR_STRING;
13415 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013417 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 if (*keys == NUL)
13419 return;
13420
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013421 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013423 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013424 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013425 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013426 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013427 if (argvars[3].v_type != VAR_UNKNOWN)
13428 get_dict = get_tv_number(&argvars[3]);
13429 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431 else
13432 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013433 if (which == NULL)
13434 return;
13435
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 mode = get_map_mode(&which, 0);
13437
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013438 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013439 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013441
13442 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013444 /* Return a string. */
13445 if (rhs != NULL)
13446 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447
Bram Moolenaarbd743252010-10-20 21:23:33 +020013448 }
13449 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13450 {
13451 /* Return a dictionary. */
13452 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13453 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13454 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455
Bram Moolenaarbd743252010-10-20 21:23:33 +020013456 dict_add_nr_str(dict, "lhs", 0L, lhs);
13457 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13458 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13459 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13460 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13461 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13462 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13463 dict_add_nr_str(dict, "mode", 0L, mapmode);
13464
13465 vim_free(lhs);
13466 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 }
13468}
13469
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013470#ifdef FEAT_FLOAT
13471/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013472 * "log()" function
13473 */
13474 static void
13475f_log(argvars, rettv)
13476 typval_T *argvars;
13477 typval_T *rettv;
13478{
13479 float_T f;
13480
13481 rettv->v_type = VAR_FLOAT;
13482 if (get_float_arg(argvars, &f) == OK)
13483 rettv->vval.v_float = log(f);
13484 else
13485 rettv->vval.v_float = 0.0;
13486}
13487
13488/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013489 * "log10()" function
13490 */
13491 static void
13492f_log10(argvars, rettv)
13493 typval_T *argvars;
13494 typval_T *rettv;
13495{
13496 float_T f;
13497
13498 rettv->v_type = VAR_FLOAT;
13499 if (get_float_arg(argvars, &f) == OK)
13500 rettv->vval.v_float = log10(f);
13501 else
13502 rettv->vval.v_float = 0.0;
13503}
13504#endif
13505
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013507 * "map()" function
13508 */
13509 static void
13510f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013511 typval_T *argvars;
13512 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013513{
13514 filter_map(argvars, rettv, TRUE);
13515}
13516
13517/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013518 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519 */
13520 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013521f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013522 typval_T *argvars;
13523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013525 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526}
13527
13528/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013529 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 */
13531 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013532f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013533 typval_T *argvars;
13534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013536 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537}
13538
Bram Moolenaar33570922005-01-25 22:26:29 +000013539static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540
13541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013543 typval_T *argvars;
13544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 int type;
13546{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013547 char_u *str = NULL;
13548 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 char_u *pat;
13550 regmatch_T regmatch;
13551 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013552 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 char_u *save_cpo;
13554 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013555 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013556 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013557 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013558 list_T *l = NULL;
13559 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013560 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013561 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562
13563 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13564 save_cpo = p_cpo;
13565 p_cpo = (char_u *)"";
13566
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013567 rettv->vval.v_number = -1;
13568 if (type == 3)
13569 {
13570 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013571 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013572 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013573 }
13574 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013576 rettv->v_type = VAR_STRING;
13577 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013580 if (argvars[0].v_type == VAR_LIST)
13581 {
13582 if ((l = argvars[0].vval.v_list) == NULL)
13583 goto theend;
13584 li = l->lv_first;
13585 }
13586 else
13587 expr = str = get_tv_string(&argvars[0]);
13588
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013589 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13590 if (pat == NULL)
13591 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013592
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013593 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013595 int error = FALSE;
13596
13597 start = get_tv_number_chk(&argvars[2], &error);
13598 if (error)
13599 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013600 if (l != NULL)
13601 {
13602 li = list_find(l, start);
13603 if (li == NULL)
13604 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013605 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013606 }
13607 else
13608 {
13609 if (start < 0)
13610 start = 0;
13611 if (start > (long)STRLEN(str))
13612 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013613 /* When "count" argument is there ignore matches before "start",
13614 * otherwise skip part of the string. Differs when pattern is "^"
13615 * or "\<". */
13616 if (argvars[3].v_type != VAR_UNKNOWN)
13617 startcol = start;
13618 else
13619 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013620 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013621
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013622 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 nth = get_tv_number_chk(&argvars[3], &error);
13624 if (error)
13625 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 }
13627
13628 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13629 if (regmatch.regprog != NULL)
13630 {
13631 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013632
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013633 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013634 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013635 if (l != NULL)
13636 {
13637 if (li == NULL)
13638 {
13639 match = FALSE;
13640 break;
13641 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013642 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013643 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013644 if (str == NULL)
13645 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013646 }
13647
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013648 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013649
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013650 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013651 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013652 if (l == NULL && !match)
13653 break;
13654
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013655 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013656 if (l != NULL)
13657 {
13658 li = li->li_next;
13659 ++idx;
13660 }
13661 else
13662 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013663#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013664 startcol = (colnr_T)(regmatch.startp[0]
13665 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013666#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013667 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013668#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013669 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013670 }
13671
13672 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013674 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013675 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013676 int i;
13677
13678 /* return list with matched string and submatches */
13679 for (i = 0; i < NSUBEXP; ++i)
13680 {
13681 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013682 {
13683 if (list_append_string(rettv->vval.v_list,
13684 (char_u *)"", 0) == FAIL)
13685 break;
13686 }
13687 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013688 regmatch.startp[i],
13689 (int)(regmatch.endp[i] - regmatch.startp[i]))
13690 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013691 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013692 }
13693 }
13694 else if (type == 2)
13695 {
13696 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013697 if (l != NULL)
13698 copy_tv(&li->li_tv, rettv);
13699 else
13700 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013702 }
13703 else if (l != NULL)
13704 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705 else
13706 {
13707 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013708 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 (varnumber_T)(regmatch.startp[0] - str);
13710 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013711 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013713 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 }
13715 }
13716 vim_free(regmatch.regprog);
13717 }
13718
13719theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013720 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 p_cpo = save_cpo;
13722}
13723
13724/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013725 * "match()" function
13726 */
13727 static void
13728f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013729 typval_T *argvars;
13730 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013731{
13732 find_some_match(argvars, rettv, 1);
13733}
13734
13735/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013736 * "matchadd()" function
13737 */
13738 static void
13739f_matchadd(argvars, rettv)
13740 typval_T *argvars;
13741 typval_T *rettv;
13742{
13743#ifdef FEAT_SEARCH_EXTRA
13744 char_u buf[NUMBUFLEN];
13745 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13746 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13747 int prio = 10; /* default priority */
13748 int id = -1;
13749 int error = FALSE;
13750
13751 rettv->vval.v_number = -1;
13752
13753 if (grp == NULL || pat == NULL)
13754 return;
13755 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013756 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013757 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013758 if (argvars[3].v_type != VAR_UNKNOWN)
13759 id = get_tv_number_chk(&argvars[3], &error);
13760 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013761 if (error == TRUE)
13762 return;
13763 if (id >= 1 && id <= 3)
13764 {
13765 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13766 return;
13767 }
13768
13769 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13770#endif
13771}
13772
13773/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013774 * "matcharg()" function
13775 */
13776 static void
13777f_matcharg(argvars, rettv)
13778 typval_T *argvars;
13779 typval_T *rettv;
13780{
13781 if (rettv_list_alloc(rettv) == OK)
13782 {
13783#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013784 int id = get_tv_number(&argvars[0]);
13785 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013786
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013787 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013788 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013789 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13790 {
13791 list_append_string(rettv->vval.v_list,
13792 syn_id2name(m->hlg_id), -1);
13793 list_append_string(rettv->vval.v_list, m->pattern, -1);
13794 }
13795 else
13796 {
13797 list_append_string(rettv->vval.v_list, NUL, -1);
13798 list_append_string(rettv->vval.v_list, NUL, -1);
13799 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013800 }
13801#endif
13802 }
13803}
13804
13805/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013806 * "matchdelete()" function
13807 */
13808 static void
13809f_matchdelete(argvars, rettv)
13810 typval_T *argvars;
13811 typval_T *rettv;
13812{
13813#ifdef FEAT_SEARCH_EXTRA
13814 rettv->vval.v_number = match_delete(curwin,
13815 (int)get_tv_number(&argvars[0]), TRUE);
13816#endif
13817}
13818
13819/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013820 * "matchend()" function
13821 */
13822 static void
13823f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013824 typval_T *argvars;
13825 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013826{
13827 find_some_match(argvars, rettv, 0);
13828}
13829
13830/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013831 * "matchlist()" function
13832 */
13833 static void
13834f_matchlist(argvars, rettv)
13835 typval_T *argvars;
13836 typval_T *rettv;
13837{
13838 find_some_match(argvars, rettv, 3);
13839}
13840
13841/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013842 * "matchstr()" function
13843 */
13844 static void
13845f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013846 typval_T *argvars;
13847 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013848{
13849 find_some_match(argvars, rettv, 2);
13850}
13851
Bram Moolenaar33570922005-01-25 22:26:29 +000013852static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013853
13854 static void
13855max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013856 typval_T *argvars;
13857 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013858 int domax;
13859{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013860 long n = 0;
13861 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013863
13864 if (argvars[0].v_type == VAR_LIST)
13865 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013866 list_T *l;
13867 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013868
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013869 l = argvars[0].vval.v_list;
13870 if (l != NULL)
13871 {
13872 li = l->lv_first;
13873 if (li != NULL)
13874 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013875 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013876 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013877 {
13878 li = li->li_next;
13879 if (li == NULL)
13880 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013881 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013882 if (domax ? i > n : i < n)
13883 n = i;
13884 }
13885 }
13886 }
13887 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013888 else if (argvars[0].v_type == VAR_DICT)
13889 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013890 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013891 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013892 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013893 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013894
13895 d = argvars[0].vval.v_dict;
13896 if (d != NULL)
13897 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013898 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013899 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013900 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013901 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013902 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013903 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013904 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013905 if (first)
13906 {
13907 n = i;
13908 first = FALSE;
13909 }
13910 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013911 n = i;
13912 }
13913 }
13914 }
13915 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013916 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013917 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013918 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013919}
13920
13921/*
13922 * "max()" function
13923 */
13924 static void
13925f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013926 typval_T *argvars;
13927 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013928{
13929 max_min(argvars, rettv, TRUE);
13930}
13931
13932/*
13933 * "min()" function
13934 */
13935 static void
13936f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013937 typval_T *argvars;
13938 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013939{
13940 max_min(argvars, rettv, FALSE);
13941}
13942
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013943static int mkdir_recurse __ARGS((char_u *dir, int prot));
13944
13945/*
13946 * Create the directory in which "dir" is located, and higher levels when
13947 * needed.
13948 */
13949 static int
13950mkdir_recurse(dir, prot)
13951 char_u *dir;
13952 int prot;
13953{
13954 char_u *p;
13955 char_u *updir;
13956 int r = FAIL;
13957
13958 /* Get end of directory name in "dir".
13959 * We're done when it's "/" or "c:/". */
13960 p = gettail_sep(dir);
13961 if (p <= get_past_head(dir))
13962 return OK;
13963
13964 /* If the directory exists we're done. Otherwise: create it.*/
13965 updir = vim_strnsave(dir, (int)(p - dir));
13966 if (updir == NULL)
13967 return FAIL;
13968 if (mch_isdir(updir))
13969 r = OK;
13970 else if (mkdir_recurse(updir, prot) == OK)
13971 r = vim_mkdir_emsg(updir, prot);
13972 vim_free(updir);
13973 return r;
13974}
13975
13976#ifdef vim_mkdir
13977/*
13978 * "mkdir()" function
13979 */
13980 static void
13981f_mkdir(argvars, rettv)
13982 typval_T *argvars;
13983 typval_T *rettv;
13984{
13985 char_u *dir;
13986 char_u buf[NUMBUFLEN];
13987 int prot = 0755;
13988
13989 rettv->vval.v_number = FAIL;
13990 if (check_restricted() || check_secure())
13991 return;
13992
13993 dir = get_tv_string_buf(&argvars[0], buf);
13994 if (argvars[1].v_type != VAR_UNKNOWN)
13995 {
13996 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013997 prot = get_tv_number_chk(&argvars[2], NULL);
13998 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013999 mkdir_recurse(dir, prot);
14000 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014001 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014002}
14003#endif
14004
Bram Moolenaar0d660222005-01-07 21:51:51 +000014005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006 * "mode()" function
14007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014009f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014010 typval_T *argvars;
14011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014013 char_u buf[3];
14014
14015 buf[1] = NUL;
14016 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017
14018#ifdef FEAT_VISUAL
14019 if (VIsual_active)
14020 {
14021 if (VIsual_select)
14022 buf[0] = VIsual_mode + 's' - 'v';
14023 else
14024 buf[0] = VIsual_mode;
14025 }
14026 else
14027#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014028 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14029 || State == CONFIRM)
14030 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014032 if (State == ASKMORE)
14033 buf[1] = 'm';
14034 else if (State == CONFIRM)
14035 buf[1] = '?';
14036 }
14037 else if (State == EXTERNCMD)
14038 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039 else if (State & INSERT)
14040 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014041#ifdef FEAT_VREPLACE
14042 if (State & VREPLACE_FLAG)
14043 {
14044 buf[0] = 'R';
14045 buf[1] = 'v';
14046 }
14047 else
14048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049 if (State & REPLACE_FLAG)
14050 buf[0] = 'R';
14051 else
14052 buf[0] = 'i';
14053 }
14054 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014057 if (exmode_active)
14058 buf[1] = 'v';
14059 }
14060 else if (exmode_active)
14061 {
14062 buf[0] = 'c';
14063 buf[1] = 'e';
14064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014068 if (finish_op)
14069 buf[1] = 'o';
14070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014072 /* Clear out the minor mode when the argument is not a non-zero number or
14073 * non-empty string. */
14074 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014075 buf[1] = NUL;
14076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014077 rettv->vval.v_string = vim_strsave(buf);
14078 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079}
14080
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014081#ifdef FEAT_MZSCHEME
14082/*
14083 * "mzeval()" function
14084 */
14085 static void
14086f_mzeval(argvars, rettv)
14087 typval_T *argvars;
14088 typval_T *rettv;
14089{
14090 char_u *str;
14091 char_u buf[NUMBUFLEN];
14092
14093 str = get_tv_string_buf(&argvars[0], buf);
14094 do_mzeval(str, rettv);
14095}
14096#endif
14097
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014099 * "nextnonblank()" function
14100 */
14101 static void
14102f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014103 typval_T *argvars;
14104 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014105{
14106 linenr_T lnum;
14107
14108 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014110 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014111 {
14112 lnum = 0;
14113 break;
14114 }
14115 if (*skipwhite(ml_get(lnum)) != NUL)
14116 break;
14117 }
14118 rettv->vval.v_number = lnum;
14119}
14120
14121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 * "nr2char()" function
14123 */
14124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014125f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014126 typval_T *argvars;
14127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128{
14129 char_u buf[NUMBUFLEN];
14130
14131#ifdef FEAT_MBYTE
14132 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014133 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 else
14135#endif
14136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014137 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 buf[1] = NUL;
14139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014140 rettv->v_type = VAR_STRING;
14141 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014142}
14143
14144/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014145 * "or(expr, expr)" function
14146 */
14147 static void
14148f_or(argvars, rettv)
14149 typval_T *argvars;
14150 typval_T *rettv;
14151{
14152 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14153 | get_tv_number_chk(&argvars[1], NULL);
14154}
14155
14156/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014157 * "pathshorten()" function
14158 */
14159 static void
14160f_pathshorten(argvars, rettv)
14161 typval_T *argvars;
14162 typval_T *rettv;
14163{
14164 char_u *p;
14165
14166 rettv->v_type = VAR_STRING;
14167 p = get_tv_string_chk(&argvars[0]);
14168 if (p == NULL)
14169 rettv->vval.v_string = NULL;
14170 else
14171 {
14172 p = vim_strsave(p);
14173 rettv->vval.v_string = p;
14174 if (p != NULL)
14175 shorten_dir(p);
14176 }
14177}
14178
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014179#ifdef FEAT_FLOAT
14180/*
14181 * "pow()" function
14182 */
14183 static void
14184f_pow(argvars, rettv)
14185 typval_T *argvars;
14186 typval_T *rettv;
14187{
14188 float_T fx, fy;
14189
14190 rettv->v_type = VAR_FLOAT;
14191 if (get_float_arg(argvars, &fx) == OK
14192 && get_float_arg(&argvars[1], &fy) == OK)
14193 rettv->vval.v_float = pow(fx, fy);
14194 else
14195 rettv->vval.v_float = 0.0;
14196}
14197#endif
14198
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014199/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014200 * "prevnonblank()" function
14201 */
14202 static void
14203f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014204 typval_T *argvars;
14205 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014206{
14207 linenr_T lnum;
14208
14209 lnum = get_tv_lnum(argvars);
14210 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14211 lnum = 0;
14212 else
14213 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14214 --lnum;
14215 rettv->vval.v_number = lnum;
14216}
14217
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014218#ifdef HAVE_STDARG_H
14219/* This dummy va_list is here because:
14220 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14221 * - locally in the function results in a "used before set" warning
14222 * - using va_start() to initialize it gives "function with fixed args" error */
14223static va_list ap;
14224#endif
14225
Bram Moolenaar8c711452005-01-14 21:53:12 +000014226/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014227 * "printf()" function
14228 */
14229 static void
14230f_printf(argvars, rettv)
14231 typval_T *argvars;
14232 typval_T *rettv;
14233{
14234 rettv->v_type = VAR_STRING;
14235 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014236#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014237 {
14238 char_u buf[NUMBUFLEN];
14239 int len;
14240 char_u *s;
14241 int saved_did_emsg = did_emsg;
14242 char *fmt;
14243
14244 /* Get the required length, allocate the buffer and do it for real. */
14245 did_emsg = FALSE;
14246 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014247 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014248 if (!did_emsg)
14249 {
14250 s = alloc(len + 1);
14251 if (s != NULL)
14252 {
14253 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014254 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014255 }
14256 }
14257 did_emsg |= saved_did_emsg;
14258 }
14259#endif
14260}
14261
14262/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014263 * "pumvisible()" function
14264 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014265 static void
14266f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014267 typval_T *argvars UNUSED;
14268 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014269{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014270#ifdef FEAT_INS_EXPAND
14271 if (pum_visible())
14272 rettv->vval.v_number = 1;
14273#endif
14274}
14275
14276/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014277 * "range()" function
14278 */
14279 static void
14280f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014281 typval_T *argvars;
14282 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014283{
14284 long start;
14285 long end;
14286 long stride = 1;
14287 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014288 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014289
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014290 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014291 if (argvars[1].v_type == VAR_UNKNOWN)
14292 {
14293 end = start - 1;
14294 start = 0;
14295 }
14296 else
14297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014299 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014300 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014301 }
14302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014303 if (error)
14304 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014305 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014306 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014307 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014308 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014309 else
14310 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014311 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014312 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014313 if (list_append_number(rettv->vval.v_list,
14314 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014315 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014316 }
14317}
14318
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014319/*
14320 * "readfile()" function
14321 */
14322 static void
14323f_readfile(argvars, rettv)
14324 typval_T *argvars;
14325 typval_T *rettv;
14326{
14327 int binary = FALSE;
14328 char_u *fname;
14329 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014330 listitem_T *li;
14331#define FREAD_SIZE 200 /* optimized for text lines */
14332 char_u buf[FREAD_SIZE];
14333 int readlen; /* size of last fread() */
14334 int buflen; /* nr of valid chars in buf[] */
14335 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14336 int tolist; /* first byte in buf[] still to be put in list */
14337 int chop; /* how many CR to chop off */
14338 char_u *prev = NULL; /* previously read bytes, if any */
14339 int prevlen = 0; /* length of "prev" if not NULL */
14340 char_u *s;
14341 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014342 long maxline = MAXLNUM;
14343 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014344
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014345 if (argvars[1].v_type != VAR_UNKNOWN)
14346 {
14347 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14348 binary = TRUE;
14349 if (argvars[2].v_type != VAR_UNKNOWN)
14350 maxline = get_tv_number(&argvars[2]);
14351 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014352
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014353 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014354 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014355
14356 /* Always open the file in binary mode, library functions have a mind of
14357 * their own about CR-LF conversion. */
14358 fname = get_tv_string(&argvars[0]);
14359 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14360 {
14361 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14362 return;
14363 }
14364
14365 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014366 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014367 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014368 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014369 buflen = filtd + readlen;
14370 tolist = 0;
14371 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14372 {
Bram Moolenaarf56a6de2011-07-07 17:36:56 +020014373 if (readlen <= 0 || buf[filtd] == '\n')
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014374 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014375 /* In binary mode add an empty list item when the last
14376 * non-empty line ends in a '\n'. */
14377 if (!binary && readlen == 0 && filtd == 0 && prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014378 break;
14379
14380 /* Found end-of-line or end-of-file: add a text line to the
14381 * list. */
14382 chop = 0;
14383 if (!binary)
14384 while (filtd - chop - 1 >= tolist
14385 && buf[filtd - chop - 1] == '\r')
14386 ++chop;
14387 len = filtd - tolist - chop;
14388 if (prev == NULL)
14389 s = vim_strnsave(buf + tolist, len);
14390 else
14391 {
14392 s = alloc((unsigned)(prevlen + len + 1));
14393 if (s != NULL)
14394 {
14395 mch_memmove(s, prev, prevlen);
14396 vim_free(prev);
14397 prev = NULL;
14398 mch_memmove(s + prevlen, buf + tolist, len);
14399 s[prevlen + len] = NUL;
14400 }
14401 }
14402 tolist = filtd + 1;
14403
14404 li = listitem_alloc();
14405 if (li == NULL)
14406 {
14407 vim_free(s);
14408 break;
14409 }
14410 li->li_tv.v_type = VAR_STRING;
14411 li->li_tv.v_lock = 0;
14412 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014413 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014414
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014415 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014416 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014417 if (readlen <= 0)
14418 break;
14419 }
14420 else if (buf[filtd] == NUL)
14421 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014422#ifdef FEAT_MBYTE
14423 else if (buf[filtd] == 0xef
14424 && enc_utf8
14425 && filtd + 2 < buflen
14426 && !binary
14427 && buf[filtd + 1] == 0xbb
14428 && buf[filtd + 2] == 0xbf)
14429 {
14430 /* remove utf-8 byte order mark */
14431 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14432 --filtd;
14433 buflen -= 3;
14434 }
14435#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014436 }
14437 if (readlen <= 0)
14438 break;
14439
14440 if (tolist == 0)
14441 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014442 if (buflen >= FREAD_SIZE / 2)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014443 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014444 /* "buf" is full, need to move text to an allocated buffer */
14445 if (prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014446 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014447 prev = vim_strnsave(buf, buflen);
14448 prevlen = buflen;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014449 }
Bram Moolenaar27b60562011-04-01 16:07:46 +020014450 else
14451 {
14452 s = alloc((unsigned)(prevlen + buflen));
14453 if (s != NULL)
14454 {
14455 mch_memmove(s, prev, prevlen);
14456 mch_memmove(s + prevlen, buf, buflen);
14457 vim_free(prev);
14458 prev = s;
14459 prevlen += buflen;
14460 }
14461 }
14462 filtd = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014463 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014464 }
14465 else
14466 {
14467 mch_memmove(buf, buf + tolist, buflen - tolist);
14468 filtd -= tolist;
14469 }
14470 }
14471
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014472 /*
14473 * For a negative line count use only the lines at the end of the file,
14474 * free the rest.
14475 */
14476 if (maxline < 0)
14477 while (cnt > -maxline)
14478 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014479 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014480 --cnt;
14481 }
14482
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014483 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014484 fclose(fd);
14485}
14486
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014487#if defined(FEAT_RELTIME)
14488static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14489
14490/*
14491 * Convert a List to proftime_T.
14492 * Return FAIL when there is something wrong.
14493 */
14494 static int
14495list2proftime(arg, tm)
14496 typval_T *arg;
14497 proftime_T *tm;
14498{
14499 long n1, n2;
14500 int error = FALSE;
14501
14502 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14503 || arg->vval.v_list->lv_len != 2)
14504 return FAIL;
14505 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14506 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14507# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014508 tm->HighPart = n1;
14509 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014510# else
14511 tm->tv_sec = n1;
14512 tm->tv_usec = n2;
14513# endif
14514 return error ? FAIL : OK;
14515}
14516#endif /* FEAT_RELTIME */
14517
14518/*
14519 * "reltime()" function
14520 */
14521 static void
14522f_reltime(argvars, rettv)
14523 typval_T *argvars;
14524 typval_T *rettv;
14525{
14526#ifdef FEAT_RELTIME
14527 proftime_T res;
14528 proftime_T start;
14529
14530 if (argvars[0].v_type == VAR_UNKNOWN)
14531 {
14532 /* No arguments: get current time. */
14533 profile_start(&res);
14534 }
14535 else if (argvars[1].v_type == VAR_UNKNOWN)
14536 {
14537 if (list2proftime(&argvars[0], &res) == FAIL)
14538 return;
14539 profile_end(&res);
14540 }
14541 else
14542 {
14543 /* Two arguments: compute the difference. */
14544 if (list2proftime(&argvars[0], &start) == FAIL
14545 || list2proftime(&argvars[1], &res) == FAIL)
14546 return;
14547 profile_sub(&res, &start);
14548 }
14549
14550 if (rettv_list_alloc(rettv) == OK)
14551 {
14552 long n1, n2;
14553
14554# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014555 n1 = res.HighPart;
14556 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014557# else
14558 n1 = res.tv_sec;
14559 n2 = res.tv_usec;
14560# endif
14561 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14562 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14563 }
14564#endif
14565}
14566
14567/*
14568 * "reltimestr()" function
14569 */
14570 static void
14571f_reltimestr(argvars, rettv)
14572 typval_T *argvars;
14573 typval_T *rettv;
14574{
14575#ifdef FEAT_RELTIME
14576 proftime_T tm;
14577#endif
14578
14579 rettv->v_type = VAR_STRING;
14580 rettv->vval.v_string = NULL;
14581#ifdef FEAT_RELTIME
14582 if (list2proftime(&argvars[0], &tm) == OK)
14583 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14584#endif
14585}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014586
Bram Moolenaar0d660222005-01-07 21:51:51 +000014587#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14588static void make_connection __ARGS((void));
14589static int check_connection __ARGS((void));
14590
14591 static void
14592make_connection()
14593{
14594 if (X_DISPLAY == NULL
14595# ifdef FEAT_GUI
14596 && !gui.in_use
14597# endif
14598 )
14599 {
14600 x_force_connect = TRUE;
14601 setup_term_clip();
14602 x_force_connect = FALSE;
14603 }
14604}
14605
14606 static int
14607check_connection()
14608{
14609 make_connection();
14610 if (X_DISPLAY == NULL)
14611 {
14612 EMSG(_("E240: No connection to Vim server"));
14613 return FAIL;
14614 }
14615 return OK;
14616}
14617#endif
14618
14619#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014620static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014621
14622 static void
14623remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014624 typval_T *argvars;
14625 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014626 int expr;
14627{
14628 char_u *server_name;
14629 char_u *keys;
14630 char_u *r = NULL;
14631 char_u buf[NUMBUFLEN];
14632# ifdef WIN32
14633 HWND w;
14634# else
14635 Window w;
14636# endif
14637
14638 if (check_restricted() || check_secure())
14639 return;
14640
14641# ifdef FEAT_X11
14642 if (check_connection() == FAIL)
14643 return;
14644# endif
14645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014646 server_name = get_tv_string_chk(&argvars[0]);
14647 if (server_name == NULL)
14648 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649 keys = get_tv_string_buf(&argvars[1], buf);
14650# ifdef WIN32
14651 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14652# else
14653 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14654 < 0)
14655# endif
14656 {
14657 if (r != NULL)
14658 EMSG(r); /* sending worked but evaluation failed */
14659 else
14660 EMSG2(_("E241: Unable to send to %s"), server_name);
14661 return;
14662 }
14663
14664 rettv->vval.v_string = r;
14665
14666 if (argvars[2].v_type != VAR_UNKNOWN)
14667 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014668 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014669 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014670 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014671
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014672 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014673 v.di_tv.v_type = VAR_STRING;
14674 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014675 idvar = get_tv_string_chk(&argvars[2]);
14676 if (idvar != NULL)
14677 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014678 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014679 }
14680}
14681#endif
14682
14683/*
14684 * "remote_expr()" function
14685 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014686 static void
14687f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014688 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014689 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014690{
14691 rettv->v_type = VAR_STRING;
14692 rettv->vval.v_string = NULL;
14693#ifdef FEAT_CLIENTSERVER
14694 remote_common(argvars, rettv, TRUE);
14695#endif
14696}
14697
14698/*
14699 * "remote_foreground()" function
14700 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014701 static void
14702f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014703 typval_T *argvars UNUSED;
14704 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014705{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014706#ifdef FEAT_CLIENTSERVER
14707# ifdef WIN32
14708 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014709 {
14710 char_u *server_name = get_tv_string_chk(&argvars[0]);
14711
14712 if (server_name != NULL)
14713 serverForeground(server_name);
14714 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014715# else
14716 /* Send a foreground() expression to the server. */
14717 argvars[1].v_type = VAR_STRING;
14718 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14719 argvars[2].v_type = VAR_UNKNOWN;
14720 remote_common(argvars, rettv, TRUE);
14721 vim_free(argvars[1].vval.v_string);
14722# endif
14723#endif
14724}
14725
Bram Moolenaar0d660222005-01-07 21:51:51 +000014726 static void
14727f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014728 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014729 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014730{
14731#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014732 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014733 char_u *s = NULL;
14734# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014735 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014736# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014737 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014738
14739 if (check_restricted() || check_secure())
14740 {
14741 rettv->vval.v_number = -1;
14742 return;
14743 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014744 serverid = get_tv_string_chk(&argvars[0]);
14745 if (serverid == NULL)
14746 {
14747 rettv->vval.v_number = -1;
14748 return; /* type error; errmsg already given */
14749 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014750# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014751 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014752 if (n == 0)
14753 rettv->vval.v_number = -1;
14754 else
14755 {
14756 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14757 rettv->vval.v_number = (s != NULL);
14758 }
14759# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014760 if (check_connection() == FAIL)
14761 return;
14762
14763 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014764 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014765# endif
14766
14767 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014769 char_u *retvar;
14770
Bram Moolenaar33570922005-01-25 22:26:29 +000014771 v.di_tv.v_type = VAR_STRING;
14772 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014773 retvar = get_tv_string_chk(&argvars[1]);
14774 if (retvar != NULL)
14775 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014776 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014777 }
14778#else
14779 rettv->vval.v_number = -1;
14780#endif
14781}
14782
Bram Moolenaar0d660222005-01-07 21:51:51 +000014783 static void
14784f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014785 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014786 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014787{
14788 char_u *r = NULL;
14789
14790#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014791 char_u *serverid = get_tv_string_chk(&argvars[0]);
14792
14793 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014794 {
14795# ifdef WIN32
14796 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014797 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014798
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014799 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014800 if (n != 0)
14801 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14802 if (r == NULL)
14803# else
14804 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014805 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014806# endif
14807 EMSG(_("E277: Unable to read a server reply"));
14808 }
14809#endif
14810 rettv->v_type = VAR_STRING;
14811 rettv->vval.v_string = r;
14812}
14813
14814/*
14815 * "remote_send()" function
14816 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014817 static void
14818f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014819 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014820 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014821{
14822 rettv->v_type = VAR_STRING;
14823 rettv->vval.v_string = NULL;
14824#ifdef FEAT_CLIENTSERVER
14825 remote_common(argvars, rettv, FALSE);
14826#endif
14827}
14828
14829/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014830 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014831 */
14832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014834 typval_T *argvars;
14835 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014836{
Bram Moolenaar33570922005-01-25 22:26:29 +000014837 list_T *l;
14838 listitem_T *item, *item2;
14839 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014840 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014841 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014842 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014843 dict_T *d;
14844 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014845 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014846
Bram Moolenaar8c711452005-01-14 21:53:12 +000014847 if (argvars[0].v_type == VAR_DICT)
14848 {
14849 if (argvars[2].v_type != VAR_UNKNOWN)
14850 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014851 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014852 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014854 key = get_tv_string_chk(&argvars[1]);
14855 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014857 di = dict_find(d, key, -1);
14858 if (di == NULL)
14859 EMSG2(_(e_dictkey), key);
14860 else
14861 {
14862 *rettv = di->di_tv;
14863 init_tv(&di->di_tv);
14864 dictitem_remove(d, di);
14865 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014866 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014867 }
14868 }
14869 else if (argvars[0].v_type != VAR_LIST)
14870 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014871 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014872 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014874 int error = FALSE;
14875
14876 idx = get_tv_number_chk(&argvars[1], &error);
14877 if (error)
14878 ; /* type error: do nothing, errmsg already given */
14879 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014880 EMSGN(_(e_listidx), idx);
14881 else
14882 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014883 if (argvars[2].v_type == VAR_UNKNOWN)
14884 {
14885 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014886 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014887 *rettv = item->li_tv;
14888 vim_free(item);
14889 }
14890 else
14891 {
14892 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014893 end = get_tv_number_chk(&argvars[2], &error);
14894 if (error)
14895 ; /* type error: do nothing */
14896 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014897 EMSGN(_(e_listidx), end);
14898 else
14899 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014900 int cnt = 0;
14901
14902 for (li = item; li != NULL; li = li->li_next)
14903 {
14904 ++cnt;
14905 if (li == item2)
14906 break;
14907 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014908 if (li == NULL) /* didn't find "item2" after "item" */
14909 EMSG(_(e_invrange));
14910 else
14911 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014912 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014913 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014914 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014915 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014916 l->lv_first = item;
14917 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014918 item->li_prev = NULL;
14919 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014920 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014921 }
14922 }
14923 }
14924 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014925 }
14926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927}
14928
14929/*
14930 * "rename({from}, {to})" function
14931 */
14932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014933f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014934 typval_T *argvars;
14935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936{
14937 char_u buf[NUMBUFLEN];
14938
14939 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014940 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014942 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14943 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944}
14945
14946/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014947 * "repeat()" function
14948 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014949 static void
14950f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014951 typval_T *argvars;
14952 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014953{
14954 char_u *p;
14955 int n;
14956 int slen;
14957 int len;
14958 char_u *r;
14959 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014960
14961 n = get_tv_number(&argvars[1]);
14962 if (argvars[0].v_type == VAR_LIST)
14963 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014964 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014965 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014966 if (list_extend(rettv->vval.v_list,
14967 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014968 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014969 }
14970 else
14971 {
14972 p = get_tv_string(&argvars[0]);
14973 rettv->v_type = VAR_STRING;
14974 rettv->vval.v_string = NULL;
14975
14976 slen = (int)STRLEN(p);
14977 len = slen * n;
14978 if (len <= 0)
14979 return;
14980
14981 r = alloc(len + 1);
14982 if (r != NULL)
14983 {
14984 for (i = 0; i < n; i++)
14985 mch_memmove(r + i * slen, p, (size_t)slen);
14986 r[len] = NUL;
14987 }
14988
14989 rettv->vval.v_string = r;
14990 }
14991}
14992
14993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 * "resolve()" function
14995 */
14996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014997f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014998 typval_T *argvars;
14999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000{
15001 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015002#ifdef HAVE_READLINK
15003 char_u *buf = NULL;
15004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015006 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007#ifdef FEAT_SHORTCUT
15008 {
15009 char_u *v = NULL;
15010
15011 v = mch_resolve_shortcut(p);
15012 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015013 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 }
15017#else
15018# ifdef HAVE_READLINK
15019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 char_u *cpy;
15021 int len;
15022 char_u *remain = NULL;
15023 char_u *q;
15024 int is_relative_to_current = FALSE;
15025 int has_trailing_pathsep = FALSE;
15026 int limit = 100;
15027
15028 p = vim_strsave(p);
15029
15030 if (p[0] == '.' && (vim_ispathsep(p[1])
15031 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15032 is_relative_to_current = TRUE;
15033
15034 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015035 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015038 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040
15041 q = getnextcomp(p);
15042 if (*q != NUL)
15043 {
15044 /* Separate the first path component in "p", and keep the
15045 * remainder (beginning with the path separator). */
15046 remain = vim_strsave(q - 1);
15047 q[-1] = NUL;
15048 }
15049
Bram Moolenaard9462e32011-04-11 21:35:11 +020015050 buf = alloc(MAXPATHL + 1);
15051 if (buf == NULL)
15052 goto fail;
15053
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 for (;;)
15055 {
15056 for (;;)
15057 {
15058 len = readlink((char *)p, (char *)buf, MAXPATHL);
15059 if (len <= 0)
15060 break;
15061 buf[len] = NUL;
15062
15063 if (limit-- == 0)
15064 {
15065 vim_free(p);
15066 vim_free(remain);
15067 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015068 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069 goto fail;
15070 }
15071
15072 /* Ensure that the result will have a trailing path separator
15073 * if the argument has one. */
15074 if (remain == NULL && has_trailing_pathsep)
15075 add_pathsep(buf);
15076
15077 /* Separate the first path component in the link value and
15078 * concatenate the remainders. */
15079 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15080 if (*q != NUL)
15081 {
15082 if (remain == NULL)
15083 remain = vim_strsave(q - 1);
15084 else
15085 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015086 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087 if (cpy != NULL)
15088 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089 vim_free(remain);
15090 remain = cpy;
15091 }
15092 }
15093 q[-1] = NUL;
15094 }
15095
15096 q = gettail(p);
15097 if (q > p && *q == NUL)
15098 {
15099 /* Ignore trailing path separator. */
15100 q[-1] = NUL;
15101 q = gettail(p);
15102 }
15103 if (q > p && !mch_isFullName(buf))
15104 {
15105 /* symlink is relative to directory of argument */
15106 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15107 if (cpy != NULL)
15108 {
15109 STRCPY(cpy, p);
15110 STRCPY(gettail(cpy), buf);
15111 vim_free(p);
15112 p = cpy;
15113 }
15114 }
15115 else
15116 {
15117 vim_free(p);
15118 p = vim_strsave(buf);
15119 }
15120 }
15121
15122 if (remain == NULL)
15123 break;
15124
15125 /* Append the first path component of "remain" to "p". */
15126 q = getnextcomp(remain + 1);
15127 len = q - remain - (*q != NUL);
15128 cpy = vim_strnsave(p, STRLEN(p) + len);
15129 if (cpy != NULL)
15130 {
15131 STRNCAT(cpy, remain, len);
15132 vim_free(p);
15133 p = cpy;
15134 }
15135 /* Shorten "remain". */
15136 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015137 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 else
15139 {
15140 vim_free(remain);
15141 remain = NULL;
15142 }
15143 }
15144
15145 /* If the result is a relative path name, make it explicitly relative to
15146 * the current directory if and only if the argument had this form. */
15147 if (!vim_ispathsep(*p))
15148 {
15149 if (is_relative_to_current
15150 && *p != NUL
15151 && !(p[0] == '.'
15152 && (p[1] == NUL
15153 || vim_ispathsep(p[1])
15154 || (p[1] == '.'
15155 && (p[2] == NUL
15156 || vim_ispathsep(p[2]))))))
15157 {
15158 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015159 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160 if (cpy != NULL)
15161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162 vim_free(p);
15163 p = cpy;
15164 }
15165 }
15166 else if (!is_relative_to_current)
15167 {
15168 /* Strip leading "./". */
15169 q = p;
15170 while (q[0] == '.' && vim_ispathsep(q[1]))
15171 q += 2;
15172 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015173 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174 }
15175 }
15176
15177 /* Ensure that the result will have no trailing path separator
15178 * if the argument had none. But keep "/" or "//". */
15179 if (!has_trailing_pathsep)
15180 {
15181 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015182 if (after_pathsep(p, q))
15183 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184 }
15185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015186 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187 }
15188# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015189 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190# endif
15191#endif
15192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015193 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194
15195#ifdef HAVE_READLINK
15196fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015197 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015199 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200}
15201
15202/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015203 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204 */
15205 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015206f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015207 typval_T *argvars;
15208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209{
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 list_T *l;
15211 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212
Bram Moolenaar0d660222005-01-07 21:51:51 +000015213 if (argvars[0].v_type != VAR_LIST)
15214 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015215 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015216 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015217 {
15218 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015219 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015220 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015221 while (li != NULL)
15222 {
15223 ni = li->li_prev;
15224 list_append(l, li);
15225 li = ni;
15226 }
15227 rettv->vval.v_list = l;
15228 rettv->v_type = VAR_LIST;
15229 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015230 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232}
15233
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015234#define SP_NOMOVE 0x01 /* don't move cursor */
15235#define SP_REPEAT 0x02 /* repeat to find outer pair */
15236#define SP_RETCOUNT 0x04 /* return matchcount */
15237#define SP_SETPCMARK 0x08 /* set previous context mark */
15238#define SP_START 0x10 /* accept match at start position */
15239#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15240#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015241
Bram Moolenaar33570922005-01-25 22:26:29 +000015242static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015243
15244/*
15245 * Get flags for a search function.
15246 * Possibly sets "p_ws".
15247 * Returns BACKWARD, FORWARD or zero (for an error).
15248 */
15249 static int
15250get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015251 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015252 int *flagsp;
15253{
15254 int dir = FORWARD;
15255 char_u *flags;
15256 char_u nbuf[NUMBUFLEN];
15257 int mask;
15258
15259 if (varp->v_type != VAR_UNKNOWN)
15260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015261 flags = get_tv_string_buf_chk(varp, nbuf);
15262 if (flags == NULL)
15263 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015264 while (*flags != NUL)
15265 {
15266 switch (*flags)
15267 {
15268 case 'b': dir = BACKWARD; break;
15269 case 'w': p_ws = TRUE; break;
15270 case 'W': p_ws = FALSE; break;
15271 default: mask = 0;
15272 if (flagsp != NULL)
15273 switch (*flags)
15274 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015275 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015276 case 'e': mask = SP_END; break;
15277 case 'm': mask = SP_RETCOUNT; break;
15278 case 'n': mask = SP_NOMOVE; break;
15279 case 'p': mask = SP_SUBPAT; break;
15280 case 'r': mask = SP_REPEAT; break;
15281 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015282 }
15283 if (mask == 0)
15284 {
15285 EMSG2(_(e_invarg2), flags);
15286 dir = 0;
15287 }
15288 else
15289 *flagsp |= mask;
15290 }
15291 if (dir == 0)
15292 break;
15293 ++flags;
15294 }
15295 }
15296 return dir;
15297}
15298
Bram Moolenaar071d4272004-06-13 20:20:40 +000015299/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015300 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015302 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015303search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015304 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015305 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015306 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015308 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309 char_u *pat;
15310 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015311 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312 int save_p_ws = p_ws;
15313 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015314 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015315 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015316 proftime_T tm;
15317#ifdef FEAT_RELTIME
15318 long time_limit = 0;
15319#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015320 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015321 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015323 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015324 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015325 if (dir == 0)
15326 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015327 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015328 if (flags & SP_START)
15329 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015330 if (flags & SP_END)
15331 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015332
Bram Moolenaar76929292008-01-06 19:07:36 +000015333 /* Optional arguments: line number to stop searching and timeout. */
15334 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015335 {
15336 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15337 if (lnum_stop < 0)
15338 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015339#ifdef FEAT_RELTIME
15340 if (argvars[3].v_type != VAR_UNKNOWN)
15341 {
15342 time_limit = get_tv_number_chk(&argvars[3], NULL);
15343 if (time_limit < 0)
15344 goto theend;
15345 }
15346#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015347 }
15348
Bram Moolenaar76929292008-01-06 19:07:36 +000015349#ifdef FEAT_RELTIME
15350 /* Set the time limit, if there is one. */
15351 profile_setlimit(time_limit, &tm);
15352#endif
15353
Bram Moolenaar231334e2005-07-25 20:46:57 +000015354 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015355 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015356 * Check to make sure only those flags are set.
15357 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15358 * flags cannot be set. Check for that condition also.
15359 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015360 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015361 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015364 goto theend;
15365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015367 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015368 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015369 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015370 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015372 if (flags & SP_SUBPAT)
15373 retval = subpatnum;
15374 else
15375 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015376 if (flags & SP_SETPCMARK)
15377 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015379 if (match_pos != NULL)
15380 {
15381 /* Store the match cursor position */
15382 match_pos->lnum = pos.lnum;
15383 match_pos->col = pos.col + 1;
15384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385 /* "/$" will put the cursor after the end of the line, may need to
15386 * correct that here */
15387 check_cursor();
15388 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015389
15390 /* If 'n' flag is used: restore cursor position. */
15391 if (flags & SP_NOMOVE)
15392 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015393 else
15394 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015395theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015397
15398 return retval;
15399}
15400
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015401#ifdef FEAT_FLOAT
15402/*
15403 * "round({float})" function
15404 */
15405 static void
15406f_round(argvars, rettv)
15407 typval_T *argvars;
15408 typval_T *rettv;
15409{
15410 float_T f;
15411
15412 rettv->v_type = VAR_FLOAT;
15413 if (get_float_arg(argvars, &f) == OK)
15414 /* round() is not in C90, use ceil() or floor() instead. */
15415 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15416 else
15417 rettv->vval.v_float = 0.0;
15418}
15419#endif
15420
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015421/*
15422 * "search()" function
15423 */
15424 static void
15425f_search(argvars, rettv)
15426 typval_T *argvars;
15427 typval_T *rettv;
15428{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015429 int flags = 0;
15430
15431 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432}
15433
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015435 * "searchdecl()" function
15436 */
15437 static void
15438f_searchdecl(argvars, rettv)
15439 typval_T *argvars;
15440 typval_T *rettv;
15441{
15442 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015443 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015444 int error = FALSE;
15445 char_u *name;
15446
15447 rettv->vval.v_number = 1; /* default: FAIL */
15448
15449 name = get_tv_string_chk(&argvars[0]);
15450 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015451 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015452 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015453 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15454 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15455 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015456 if (!error && name != NULL)
15457 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015458 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015459}
15460
15461/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015462 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015464 static int
15465searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015466 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015467 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468{
15469 char_u *spat, *mpat, *epat;
15470 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 int dir;
15473 int flags = 0;
15474 char_u nbuf1[NUMBUFLEN];
15475 char_u nbuf2[NUMBUFLEN];
15476 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015477 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015478 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015479 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015482 spat = get_tv_string_chk(&argvars[0]);
15483 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15484 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15485 if (spat == NULL || mpat == NULL || epat == NULL)
15486 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488 /* Handle the optional fourth argument: flags */
15489 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015490 if (dir == 0)
15491 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015492
15493 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015494 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15495 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015496 if ((flags & (SP_END | SP_SUBPAT)) != 0
15497 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015498 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015499 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015500 goto theend;
15501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015503 /* Using 'r' implies 'W', otherwise it doesn't work. */
15504 if (flags & SP_REPEAT)
15505 p_ws = FALSE;
15506
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015507 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015508 if (argvars[3].v_type == VAR_UNKNOWN
15509 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 skip = (char_u *)"";
15511 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015513 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015514 if (argvars[5].v_type != VAR_UNKNOWN)
15515 {
15516 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15517 if (lnum_stop < 0)
15518 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015519#ifdef FEAT_RELTIME
15520 if (argvars[6].v_type != VAR_UNKNOWN)
15521 {
15522 time_limit = get_tv_number_chk(&argvars[6], NULL);
15523 if (time_limit < 0)
15524 goto theend;
15525 }
15526#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015527 }
15528 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015529 if (skip == NULL)
15530 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015532 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015533 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015534
15535theend:
15536 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015537
15538 return retval;
15539}
15540
15541/*
15542 * "searchpair()" function
15543 */
15544 static void
15545f_searchpair(argvars, rettv)
15546 typval_T *argvars;
15547 typval_T *rettv;
15548{
15549 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15550}
15551
15552/*
15553 * "searchpairpos()" function
15554 */
15555 static void
15556f_searchpairpos(argvars, rettv)
15557 typval_T *argvars;
15558 typval_T *rettv;
15559{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015560 pos_T match_pos;
15561 int lnum = 0;
15562 int col = 0;
15563
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015564 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015565 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015566
15567 if (searchpair_cmn(argvars, &match_pos) > 0)
15568 {
15569 lnum = match_pos.lnum;
15570 col = match_pos.col;
15571 }
15572
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015573 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15574 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015575}
15576
15577/*
15578 * Search for a start/middle/end thing.
15579 * Used by searchpair(), see its documentation for the details.
15580 * Returns 0 or -1 for no match,
15581 */
15582 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015583do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15584 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015585 char_u *spat; /* start pattern */
15586 char_u *mpat; /* middle pattern */
15587 char_u *epat; /* end pattern */
15588 int dir; /* BACKWARD or FORWARD */
15589 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015590 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015591 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015592 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015593 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015594{
15595 char_u *save_cpo;
15596 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15597 long retval = 0;
15598 pos_T pos;
15599 pos_T firstpos;
15600 pos_T foundpos;
15601 pos_T save_cursor;
15602 pos_T save_pos;
15603 int n;
15604 int r;
15605 int nest = 1;
15606 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015607 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015608 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015609
15610 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15611 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015612 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015613
Bram Moolenaar76929292008-01-06 19:07:36 +000015614#ifdef FEAT_RELTIME
15615 /* Set the time limit, if there is one. */
15616 profile_setlimit(time_limit, &tm);
15617#endif
15618
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015619 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15620 * start/middle/end (pat3, for the top pair). */
15621 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15622 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15623 if (pat2 == NULL || pat3 == NULL)
15624 goto theend;
15625 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15626 if (*mpat == NUL)
15627 STRCPY(pat3, pat2);
15628 else
15629 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15630 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015631 if (flags & SP_START)
15632 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015633
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 save_cursor = curwin->w_cursor;
15635 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015636 clearpos(&firstpos);
15637 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 pat = pat3;
15639 for (;;)
15640 {
15641 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015642 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15644 /* didn't find it or found the first match again: FAIL */
15645 break;
15646
15647 if (firstpos.lnum == 0)
15648 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015649 if (equalpos(pos, foundpos))
15650 {
15651 /* Found the same position again. Can happen with a pattern that
15652 * has "\zs" at the end and searching backwards. Advance one
15653 * character and try again. */
15654 if (dir == BACKWARD)
15655 decl(&pos);
15656 else
15657 incl(&pos);
15658 }
15659 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015661 /* clear the start flag to avoid getting stuck here */
15662 options &= ~SEARCH_START;
15663
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 /* If the skip pattern matches, ignore this match. */
15665 if (*skip != NUL)
15666 {
15667 save_pos = curwin->w_cursor;
15668 curwin->w_cursor = pos;
15669 r = eval_to_bool(skip, &err, NULL, FALSE);
15670 curwin->w_cursor = save_pos;
15671 if (err)
15672 {
15673 /* Evaluating {skip} caused an error, break here. */
15674 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015675 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 break;
15677 }
15678 if (r)
15679 continue;
15680 }
15681
15682 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15683 {
15684 /* Found end when searching backwards or start when searching
15685 * forward: nested pair. */
15686 ++nest;
15687 pat = pat2; /* nested, don't search for middle */
15688 }
15689 else
15690 {
15691 /* Found end when searching forward or start when searching
15692 * backward: end of (nested) pair; or found middle in outer pair. */
15693 if (--nest == 1)
15694 pat = pat3; /* outer level, search for middle */
15695 }
15696
15697 if (nest == 0)
15698 {
15699 /* Found the match: return matchcount or line number. */
15700 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015701 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015703 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015704 if (flags & SP_SETPCMARK)
15705 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706 curwin->w_cursor = pos;
15707 if (!(flags & SP_REPEAT))
15708 break;
15709 nest = 1; /* search for next unmatched */
15710 }
15711 }
15712
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015713 if (match_pos != NULL)
15714 {
15715 /* Store the match cursor position */
15716 match_pos->lnum = curwin->w_cursor.lnum;
15717 match_pos->col = curwin->w_cursor.col + 1;
15718 }
15719
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015721 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722 curwin->w_cursor = save_cursor;
15723
15724theend:
15725 vim_free(pat2);
15726 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015727 if (p_cpo == empty_option)
15728 p_cpo = save_cpo;
15729 else
15730 /* Darn, evaluating the {skip} expression changed the value. */
15731 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015732
15733 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734}
15735
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015736/*
15737 * "searchpos()" function
15738 */
15739 static void
15740f_searchpos(argvars, rettv)
15741 typval_T *argvars;
15742 typval_T *rettv;
15743{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015744 pos_T match_pos;
15745 int lnum = 0;
15746 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015747 int n;
15748 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015749
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015750 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015751 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015752
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015753 n = search_cmn(argvars, &match_pos, &flags);
15754 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015755 {
15756 lnum = match_pos.lnum;
15757 col = match_pos.col;
15758 }
15759
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015760 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15761 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015762 if (flags & SP_SUBPAT)
15763 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015764}
15765
15766
Bram Moolenaar0d660222005-01-07 21:51:51 +000015767 static void
15768f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015769 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015772#ifdef FEAT_CLIENTSERVER
15773 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015774 char_u *server = get_tv_string_chk(&argvars[0]);
15775 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776
Bram Moolenaar0d660222005-01-07 21:51:51 +000015777 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015778 if (server == NULL || reply == NULL)
15779 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780 if (check_restricted() || check_secure())
15781 return;
15782# ifdef FEAT_X11
15783 if (check_connection() == FAIL)
15784 return;
15785# endif
15786
15787 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015789 EMSG(_("E258: Unable to send to client"));
15790 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015792 rettv->vval.v_number = 0;
15793#else
15794 rettv->vval.v_number = -1;
15795#endif
15796}
15797
Bram Moolenaar0d660222005-01-07 21:51:51 +000015798 static void
15799f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015800 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015801 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015802{
15803 char_u *r = NULL;
15804
15805#ifdef FEAT_CLIENTSERVER
15806# ifdef WIN32
15807 r = serverGetVimNames();
15808# else
15809 make_connection();
15810 if (X_DISPLAY != NULL)
15811 r = serverGetVimNames(X_DISPLAY);
15812# endif
15813#endif
15814 rettv->v_type = VAR_STRING;
15815 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816}
15817
15818/*
15819 * "setbufvar()" function
15820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015822f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015823 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015824 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825{
15826 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015829 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830 char_u nbuf[NUMBUFLEN];
15831
15832 if (check_restricted() || check_secure())
15833 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015834 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15835 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015836 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837 varp = &argvars[2];
15838
15839 if (buf != NULL && varname != NULL && varp != NULL)
15840 {
15841 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843
15844 if (*varname == '&')
15845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015846 long numval;
15847 char_u *strval;
15848 int error = FALSE;
15849
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015851 numval = get_tv_number_chk(varp, &error);
15852 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015853 if (!error && strval != NULL)
15854 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855 }
15856 else
15857 {
15858 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15859 if (bufvarname != NULL)
15860 {
15861 STRCPY(bufvarname, "b:");
15862 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015863 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 vim_free(bufvarname);
15865 }
15866 }
15867
15868 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871}
15872
15873/*
15874 * "setcmdpos()" function
15875 */
15876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015877f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015878 typval_T *argvars;
15879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015881 int pos = (int)get_tv_number(&argvars[0]) - 1;
15882
15883 if (pos >= 0)
15884 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885}
15886
15887/*
15888 * "setline()" function
15889 */
15890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015891f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015892 typval_T *argvars;
15893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015894{
15895 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015896 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015897 list_T *l = NULL;
15898 listitem_T *li = NULL;
15899 long added = 0;
15900 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015902 lnum = get_tv_lnum(&argvars[0]);
15903 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015905 l = argvars[1].vval.v_list;
15906 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015908 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015909 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015910
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015911 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015912 for (;;)
15913 {
15914 if (l != NULL)
15915 {
15916 /* list argument, get next string */
15917 if (li == NULL)
15918 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015919 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015920 li = li->li_next;
15921 }
15922
15923 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015924 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015925 break;
15926 if (lnum <= curbuf->b_ml.ml_line_count)
15927 {
15928 /* existing line, replace it */
15929 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15930 {
15931 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015932 if (lnum == curwin->w_cursor.lnum)
15933 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015934 rettv->vval.v_number = 0; /* OK */
15935 }
15936 }
15937 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15938 {
15939 /* lnum is one past the last line, append the line */
15940 ++added;
15941 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15942 rettv->vval.v_number = 0; /* OK */
15943 }
15944
15945 if (l == NULL) /* only one string argument */
15946 break;
15947 ++lnum;
15948 }
15949
15950 if (added > 0)
15951 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952}
15953
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015954static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15955
Bram Moolenaar071d4272004-06-13 20:20:40 +000015956/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015957 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015958 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015959 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015960set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015961 win_T *wp UNUSED;
15962 typval_T *list_arg UNUSED;
15963 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015964 typval_T *rettv;
15965{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015966#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015967 char_u *act;
15968 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015969#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015970
Bram Moolenaar2641f772005-03-25 21:58:17 +000015971 rettv->vval.v_number = -1;
15972
15973#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015974 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015975 EMSG(_(e_listreq));
15976 else
15977 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015978 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015979
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015980 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015981 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015982 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015983 if (act == NULL)
15984 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015985 if (*act == 'a' || *act == 'r')
15986 action = *act;
15987 }
15988
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015989 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015990 rettv->vval.v_number = 0;
15991 }
15992#endif
15993}
15994
15995/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015996 * "setloclist()" function
15997 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015998 static void
15999f_setloclist(argvars, rettv)
16000 typval_T *argvars;
16001 typval_T *rettv;
16002{
16003 win_T *win;
16004
16005 rettv->vval.v_number = -1;
16006
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016007 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016008 if (win != NULL)
16009 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16010}
16011
16012/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016013 * "setmatches()" function
16014 */
16015 static void
16016f_setmatches(argvars, rettv)
16017 typval_T *argvars;
16018 typval_T *rettv;
16019{
16020#ifdef FEAT_SEARCH_EXTRA
16021 list_T *l;
16022 listitem_T *li;
16023 dict_T *d;
16024
16025 rettv->vval.v_number = -1;
16026 if (argvars[0].v_type != VAR_LIST)
16027 {
16028 EMSG(_(e_listreq));
16029 return;
16030 }
16031 if ((l = argvars[0].vval.v_list) != NULL)
16032 {
16033
16034 /* To some extent make sure that we are dealing with a list from
16035 * "getmatches()". */
16036 li = l->lv_first;
16037 while (li != NULL)
16038 {
16039 if (li->li_tv.v_type != VAR_DICT
16040 || (d = li->li_tv.vval.v_dict) == NULL)
16041 {
16042 EMSG(_(e_invarg));
16043 return;
16044 }
16045 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16046 && dict_find(d, (char_u *)"pattern", -1) != NULL
16047 && dict_find(d, (char_u *)"priority", -1) != NULL
16048 && dict_find(d, (char_u *)"id", -1) != NULL))
16049 {
16050 EMSG(_(e_invarg));
16051 return;
16052 }
16053 li = li->li_next;
16054 }
16055
16056 clear_matches(curwin);
16057 li = l->lv_first;
16058 while (li != NULL)
16059 {
16060 d = li->li_tv.vval.v_dict;
16061 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16062 get_dict_string(d, (char_u *)"pattern", FALSE),
16063 (int)get_dict_number(d, (char_u *)"priority"),
16064 (int)get_dict_number(d, (char_u *)"id"));
16065 li = li->li_next;
16066 }
16067 rettv->vval.v_number = 0;
16068 }
16069#endif
16070}
16071
16072/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016073 * "setpos()" function
16074 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016075 static void
16076f_setpos(argvars, rettv)
16077 typval_T *argvars;
16078 typval_T *rettv;
16079{
16080 pos_T pos;
16081 int fnum;
16082 char_u *name;
16083
Bram Moolenaar08250432008-02-13 11:42:46 +000016084 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016085 name = get_tv_string_chk(argvars);
16086 if (name != NULL)
16087 {
16088 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16089 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016090 if (--pos.col < 0)
16091 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016092 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016093 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016094 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016095 if (fnum == curbuf->b_fnum)
16096 {
16097 curwin->w_cursor = pos;
16098 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016099 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016100 }
16101 else
16102 EMSG(_(e_invarg));
16103 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016104 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16105 {
16106 /* set mark */
16107 if (setmark_pos(name[1], &pos, fnum) == OK)
16108 rettv->vval.v_number = 0;
16109 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016110 else
16111 EMSG(_(e_invarg));
16112 }
16113 }
16114}
16115
16116/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016117 * "setqflist()" function
16118 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016119 static void
16120f_setqflist(argvars, rettv)
16121 typval_T *argvars;
16122 typval_T *rettv;
16123{
16124 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16125}
16126
16127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 * "setreg()" function
16129 */
16130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016131f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016132 typval_T *argvars;
16133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134{
16135 int regname;
16136 char_u *strregname;
16137 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016138 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139 int append;
16140 char_u yank_type;
16141 long block_len;
16142
16143 block_len = -1;
16144 yank_type = MAUTO;
16145 append = FALSE;
16146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016147 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016148 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016150 if (strregname == NULL)
16151 return; /* type error; errmsg already given */
16152 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153 if (regname == 0 || regname == '@')
16154 regname = '"';
16155 else if (regname == '=')
16156 return;
16157
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016158 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016160 stropt = get_tv_string_chk(&argvars[2]);
16161 if (stropt == NULL)
16162 return; /* type error */
16163 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 switch (*stropt)
16165 {
16166 case 'a': case 'A': /* append */
16167 append = TRUE;
16168 break;
16169 case 'v': case 'c': /* character-wise selection */
16170 yank_type = MCHAR;
16171 break;
16172 case 'V': case 'l': /* line-wise selection */
16173 yank_type = MLINE;
16174 break;
16175#ifdef FEAT_VISUAL
16176 case 'b': case Ctrl_V: /* block-wise selection */
16177 yank_type = MBLOCK;
16178 if (VIM_ISDIGIT(stropt[1]))
16179 {
16180 ++stropt;
16181 block_len = getdigits(&stropt) - 1;
16182 --stropt;
16183 }
16184 break;
16185#endif
16186 }
16187 }
16188
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016189 strval = get_tv_string_chk(&argvars[1]);
16190 if (strval != NULL)
16191 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016193 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016194}
16195
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016196/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016197 * "settabvar()" function
16198 */
16199 static void
16200f_settabvar(argvars, rettv)
16201 typval_T *argvars;
16202 typval_T *rettv;
16203{
16204 tabpage_T *save_curtab;
16205 char_u *varname, *tabvarname;
16206 typval_T *varp;
16207 tabpage_T *tp;
16208
16209 rettv->vval.v_number = 0;
16210
16211 if (check_restricted() || check_secure())
16212 return;
16213
16214 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16215 varname = get_tv_string_chk(&argvars[1]);
16216 varp = &argvars[2];
16217
16218 if (tp != NULL && varname != NULL && varp != NULL)
16219 {
16220 save_curtab = curtab;
16221 goto_tabpage_tp(tp);
16222
16223 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16224 if (tabvarname != NULL)
16225 {
16226 STRCPY(tabvarname, "t:");
16227 STRCPY(tabvarname + 2, varname);
16228 set_var(tabvarname, varp, TRUE);
16229 vim_free(tabvarname);
16230 }
16231
16232 /* Restore current tabpage */
16233 if (valid_tabpage(save_curtab))
16234 goto_tabpage_tp(save_curtab);
16235 }
16236}
16237
16238/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016239 * "settabwinvar()" function
16240 */
16241 static void
16242f_settabwinvar(argvars, rettv)
16243 typval_T *argvars;
16244 typval_T *rettv;
16245{
16246 setwinvar(argvars, rettv, 1);
16247}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248
16249/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016250 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016253f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016254 typval_T *argvars;
16255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016257 setwinvar(argvars, rettv, 0);
16258}
16259
16260/*
16261 * "setwinvar()" and "settabwinvar()" functions
16262 */
16263 static void
16264setwinvar(argvars, rettv, off)
16265 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016266 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016267 int off;
16268{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 win_T *win;
16270#ifdef FEAT_WINDOWS
16271 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016272 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273#endif
16274 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016275 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016277 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278
16279 if (check_restricted() || check_secure())
16280 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016281
16282#ifdef FEAT_WINDOWS
16283 if (off == 1)
16284 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16285 else
16286 tp = curtab;
16287#endif
16288 win = find_win_by_nr(&argvars[off], tp);
16289 varname = get_tv_string_chk(&argvars[off + 1]);
16290 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291
16292 if (win != NULL && varname != NULL && varp != NULL)
16293 {
16294#ifdef FEAT_WINDOWS
16295 /* set curwin to be our win, temporarily */
16296 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016297 save_curtab = curtab;
16298 goto_tabpage_tp(tp);
16299 if (!win_valid(win))
16300 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 curwin = win;
16302 curbuf = curwin->w_buffer;
16303#endif
16304
16305 if (*varname == '&')
16306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016307 long numval;
16308 char_u *strval;
16309 int error = FALSE;
16310
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016312 numval = get_tv_number_chk(varp, &error);
16313 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016314 if (!error && strval != NULL)
16315 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316 }
16317 else
16318 {
16319 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16320 if (winvarname != NULL)
16321 {
16322 STRCPY(winvarname, "w:");
16323 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016324 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325 vim_free(winvarname);
16326 }
16327 }
16328
16329#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016330 /* Restore current tabpage and window, if still valid (autocomands can
16331 * make them invalid). */
16332 if (valid_tabpage(save_curtab))
16333 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 if (win_valid(save_curwin))
16335 {
16336 curwin = save_curwin;
16337 curbuf = curwin->w_buffer;
16338 }
16339#endif
16340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341}
16342
16343/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016344 * "shellescape({string})" function
16345 */
16346 static void
16347f_shellescape(argvars, rettv)
16348 typval_T *argvars;
16349 typval_T *rettv;
16350{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016351 rettv->vval.v_string = vim_strsave_shellescape(
16352 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016353 rettv->v_type = VAR_STRING;
16354}
16355
16356/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016357 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358 */
16359 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016360f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016361 typval_T *argvars;
16362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016364 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365
Bram Moolenaar0d660222005-01-07 21:51:51 +000016366 p = get_tv_string(&argvars[0]);
16367 rettv->vval.v_string = vim_strsave(p);
16368 simplify_filename(rettv->vval.v_string); /* simplify in place */
16369 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370}
16371
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016372#ifdef FEAT_FLOAT
16373/*
16374 * "sin()" function
16375 */
16376 static void
16377f_sin(argvars, rettv)
16378 typval_T *argvars;
16379 typval_T *rettv;
16380{
16381 float_T f;
16382
16383 rettv->v_type = VAR_FLOAT;
16384 if (get_float_arg(argvars, &f) == OK)
16385 rettv->vval.v_float = sin(f);
16386 else
16387 rettv->vval.v_float = 0.0;
16388}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016389
16390/*
16391 * "sinh()" function
16392 */
16393 static void
16394f_sinh(argvars, rettv)
16395 typval_T *argvars;
16396 typval_T *rettv;
16397{
16398 float_T f;
16399
16400 rettv->v_type = VAR_FLOAT;
16401 if (get_float_arg(argvars, &f) == OK)
16402 rettv->vval.v_float = sinh(f);
16403 else
16404 rettv->vval.v_float = 0.0;
16405}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016406#endif
16407
Bram Moolenaar0d660222005-01-07 21:51:51 +000016408static int
16409#ifdef __BORLANDC__
16410 _RTLENTRYF
16411#endif
16412 item_compare __ARGS((const void *s1, const void *s2));
16413static int
16414#ifdef __BORLANDC__
16415 _RTLENTRYF
16416#endif
16417 item_compare2 __ARGS((const void *s1, const void *s2));
16418
16419static int item_compare_ic;
16420static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016421static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016422static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016423#define ITEM_COMPARE_FAIL 999
16424
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016426 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016428 static int
16429#ifdef __BORLANDC__
16430_RTLENTRYF
16431#endif
16432item_compare(s1, s2)
16433 const void *s1;
16434 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436 char_u *p1, *p2;
16437 char_u *tofree1, *tofree2;
16438 int res;
16439 char_u numbuf1[NUMBUFLEN];
16440 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016442 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16443 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016444 if (p1 == NULL)
16445 p1 = (char_u *)"";
16446 if (p2 == NULL)
16447 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016448 if (item_compare_ic)
16449 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016451 res = STRCMP(p1, p2);
16452 vim_free(tofree1);
16453 vim_free(tofree2);
16454 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455}
16456
16457 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458#ifdef __BORLANDC__
16459_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016461item_compare2(s1, s2)
16462 const void *s1;
16463 const void *s2;
16464{
16465 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016466 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016467 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016468 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016470 /* shortcut after failure in previous call; compare all items equal */
16471 if (item_compare_func_err)
16472 return 0;
16473
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016474 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16475 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016476 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16477 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016478
16479 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016480 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016481 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16482 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016483 clear_tv(&argv[0]);
16484 clear_tv(&argv[1]);
16485
16486 if (res == FAIL)
16487 res = ITEM_COMPARE_FAIL;
16488 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016489 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16490 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016491 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016492 clear_tv(&rettv);
16493 return res;
16494}
16495
16496/*
16497 * "sort({list})" function
16498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016501 typval_T *argvars;
16502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503{
Bram Moolenaar33570922005-01-25 22:26:29 +000016504 list_T *l;
16505 listitem_T *li;
16506 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016507 long len;
16508 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509
Bram Moolenaar0d660222005-01-07 21:51:51 +000016510 if (argvars[0].v_type != VAR_LIST)
16511 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512 else
16513 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016514 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016515 if (l == NULL || tv_check_lock(l->lv_lock,
16516 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517 return;
16518 rettv->vval.v_list = l;
16519 rettv->v_type = VAR_LIST;
16520 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521
Bram Moolenaar0d660222005-01-07 21:51:51 +000016522 len = list_len(l);
16523 if (len <= 1)
16524 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525
Bram Moolenaar0d660222005-01-07 21:51:51 +000016526 item_compare_ic = FALSE;
16527 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016528 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016529 if (argvars[1].v_type != VAR_UNKNOWN)
16530 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016531 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016532 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016533 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016534 else
16535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016536 int error = FALSE;
16537
16538 i = get_tv_number_chk(&argvars[1], &error);
16539 if (error)
16540 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016541 if (i == 1)
16542 item_compare_ic = TRUE;
16543 else
16544 item_compare_func = get_tv_string(&argvars[1]);
16545 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016546
16547 if (argvars[2].v_type != VAR_UNKNOWN)
16548 {
16549 /* optional third argument: {dict} */
16550 if (argvars[2].v_type != VAR_DICT)
16551 {
16552 EMSG(_(e_dictreq));
16553 return;
16554 }
16555 item_compare_selfdict = argvars[2].vval.v_dict;
16556 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558
Bram Moolenaar0d660222005-01-07 21:51:51 +000016559 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016560 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016561 if (ptrs == NULL)
16562 return;
16563 i = 0;
16564 for (li = l->lv_first; li != NULL; li = li->li_next)
16565 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016567 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016568 /* test the compare function */
16569 if (item_compare_func != NULL
16570 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16571 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016572 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016574 {
16575 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016576 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016577 item_compare_func == NULL ? item_compare : item_compare2);
16578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016579 if (!item_compare_func_err)
16580 {
16581 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016582 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016583 l->lv_len = 0;
16584 for (i = 0; i < len; ++i)
16585 list_append(l, ptrs[i]);
16586 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016587 }
16588
16589 vim_free(ptrs);
16590 }
16591}
16592
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016593/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016594 * "soundfold({word})" function
16595 */
16596 static void
16597f_soundfold(argvars, rettv)
16598 typval_T *argvars;
16599 typval_T *rettv;
16600{
16601 char_u *s;
16602
16603 rettv->v_type = VAR_STRING;
16604 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016605#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016606 rettv->vval.v_string = eval_soundfold(s);
16607#else
16608 rettv->vval.v_string = vim_strsave(s);
16609#endif
16610}
16611
16612/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016613 * "spellbadword()" function
16614 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016615 static void
16616f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016617 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016618 typval_T *rettv;
16619{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016620 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016621 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016622 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016623
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016624 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016625 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016626
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016627#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016628 if (argvars[0].v_type == VAR_UNKNOWN)
16629 {
16630 /* Find the start and length of the badly spelled word. */
16631 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16632 if (len != 0)
16633 word = ml_get_cursor();
16634 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016635 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016636 {
16637 char_u *str = get_tv_string_chk(&argvars[0]);
16638 int capcol = -1;
16639
16640 if (str != NULL)
16641 {
16642 /* Check the argument for spelling. */
16643 while (*str != NUL)
16644 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016645 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016646 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016647 {
16648 word = str;
16649 break;
16650 }
16651 str += len;
16652 }
16653 }
16654 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016655#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016656
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016657 list_append_string(rettv->vval.v_list, word, len);
16658 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016659 attr == HLF_SPB ? "bad" :
16660 attr == HLF_SPR ? "rare" :
16661 attr == HLF_SPL ? "local" :
16662 attr == HLF_SPC ? "caps" :
16663 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016664}
16665
16666/*
16667 * "spellsuggest()" function
16668 */
16669 static void
16670f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016671 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016672 typval_T *rettv;
16673{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016674#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016675 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016676 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016677 int maxcount;
16678 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016679 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016680 listitem_T *li;
16681 int need_capital = FALSE;
16682#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016683
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016684 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016685 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016686
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016687#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016688 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016689 {
16690 str = get_tv_string(&argvars[0]);
16691 if (argvars[1].v_type != VAR_UNKNOWN)
16692 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016693 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016694 if (maxcount <= 0)
16695 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016696 if (argvars[2].v_type != VAR_UNKNOWN)
16697 {
16698 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16699 if (typeerr)
16700 return;
16701 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016702 }
16703 else
16704 maxcount = 25;
16705
Bram Moolenaar4770d092006-01-12 23:22:24 +000016706 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016707
16708 for (i = 0; i < ga.ga_len; ++i)
16709 {
16710 str = ((char_u **)ga.ga_data)[i];
16711
16712 li = listitem_alloc();
16713 if (li == NULL)
16714 vim_free(str);
16715 else
16716 {
16717 li->li_tv.v_type = VAR_STRING;
16718 li->li_tv.v_lock = 0;
16719 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016720 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016721 }
16722 }
16723 ga_clear(&ga);
16724 }
16725#endif
16726}
16727
Bram Moolenaar0d660222005-01-07 21:51:51 +000016728 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016729f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016730 typval_T *argvars;
16731 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016732{
16733 char_u *str;
16734 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016735 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016736 regmatch_T regmatch;
16737 char_u patbuf[NUMBUFLEN];
16738 char_u *save_cpo;
16739 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016740 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016741 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016742 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016743
16744 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16745 save_cpo = p_cpo;
16746 p_cpo = (char_u *)"";
16747
16748 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016749 if (argvars[1].v_type != VAR_UNKNOWN)
16750 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016751 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16752 if (pat == NULL)
16753 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016754 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016755 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016756 }
16757 if (pat == NULL || *pat == NUL)
16758 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016759
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016760 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016762 if (typeerr)
16763 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764
Bram Moolenaar0d660222005-01-07 21:51:51 +000016765 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16766 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016768 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016769 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016770 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016771 if (*str == NUL)
16772 match = FALSE; /* empty item at the end */
16773 else
16774 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016775 if (match)
16776 end = regmatch.startp[0];
16777 else
16778 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016779 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16780 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016781 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016782 if (list_append_string(rettv->vval.v_list, str,
16783 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016784 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016785 }
16786 if (!match)
16787 break;
16788 /* Advance to just after the match. */
16789 if (regmatch.endp[0] > str)
16790 col = 0;
16791 else
16792 {
16793 /* Don't get stuck at the same match. */
16794#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016795 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016796#else
16797 col = 1;
16798#endif
16799 }
16800 str = regmatch.endp[0];
16801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802
Bram Moolenaar0d660222005-01-07 21:51:51 +000016803 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805
Bram Moolenaar0d660222005-01-07 21:51:51 +000016806 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807}
16808
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016809#ifdef FEAT_FLOAT
16810/*
16811 * "sqrt()" function
16812 */
16813 static void
16814f_sqrt(argvars, rettv)
16815 typval_T *argvars;
16816 typval_T *rettv;
16817{
16818 float_T f;
16819
16820 rettv->v_type = VAR_FLOAT;
16821 if (get_float_arg(argvars, &f) == OK)
16822 rettv->vval.v_float = sqrt(f);
16823 else
16824 rettv->vval.v_float = 0.0;
16825}
16826
16827/*
16828 * "str2float()" function
16829 */
16830 static void
16831f_str2float(argvars, rettv)
16832 typval_T *argvars;
16833 typval_T *rettv;
16834{
16835 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16836
16837 if (*p == '+')
16838 p = skipwhite(p + 1);
16839 (void)string2float(p, &rettv->vval.v_float);
16840 rettv->v_type = VAR_FLOAT;
16841}
16842#endif
16843
Bram Moolenaar2c932302006-03-18 21:42:09 +000016844/*
16845 * "str2nr()" function
16846 */
16847 static void
16848f_str2nr(argvars, rettv)
16849 typval_T *argvars;
16850 typval_T *rettv;
16851{
16852 int base = 10;
16853 char_u *p;
16854 long n;
16855
16856 if (argvars[1].v_type != VAR_UNKNOWN)
16857 {
16858 base = get_tv_number(&argvars[1]);
16859 if (base != 8 && base != 10 && base != 16)
16860 {
16861 EMSG(_(e_invarg));
16862 return;
16863 }
16864 }
16865
16866 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016867 if (*p == '+')
16868 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016869 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16870 rettv->vval.v_number = n;
16871}
16872
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873#ifdef HAVE_STRFTIME
16874/*
16875 * "strftime({format}[, {time}])" function
16876 */
16877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016878f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016879 typval_T *argvars;
16880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881{
16882 char_u result_buf[256];
16883 struct tm *curtime;
16884 time_t seconds;
16885 char_u *p;
16886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016887 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016889 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016890 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891 seconds = time(NULL);
16892 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016893 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 curtime = localtime(&seconds);
16895 /* MSVC returns NULL for an invalid value of seconds. */
16896 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016897 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 else
16899 {
16900# ifdef FEAT_MBYTE
16901 vimconv_T conv;
16902 char_u *enc;
16903
16904 conv.vc_type = CONV_NONE;
16905 enc = enc_locale();
16906 convert_setup(&conv, p_enc, enc);
16907 if (conv.vc_type != CONV_NONE)
16908 p = string_convert(&conv, p, NULL);
16909# endif
16910 if (p != NULL)
16911 (void)strftime((char *)result_buf, sizeof(result_buf),
16912 (char *)p, curtime);
16913 else
16914 result_buf[0] = NUL;
16915
16916# ifdef FEAT_MBYTE
16917 if (conv.vc_type != CONV_NONE)
16918 vim_free(p);
16919 convert_setup(&conv, enc, p_enc);
16920 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016921 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922 else
16923# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016924 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925
16926# ifdef FEAT_MBYTE
16927 /* Release conversion descriptors */
16928 convert_setup(&conv, NULL, NULL);
16929 vim_free(enc);
16930# endif
16931 }
16932}
16933#endif
16934
16935/*
16936 * "stridx()" function
16937 */
16938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016939f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016940 typval_T *argvars;
16941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942{
16943 char_u buf[NUMBUFLEN];
16944 char_u *needle;
16945 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016946 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016948 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016950 needle = get_tv_string_chk(&argvars[1]);
16951 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016952 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016953 if (needle == NULL || haystack == NULL)
16954 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955
Bram Moolenaar33570922005-01-25 22:26:29 +000016956 if (argvars[2].v_type != VAR_UNKNOWN)
16957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016958 int error = FALSE;
16959
16960 start_idx = get_tv_number_chk(&argvars[2], &error);
16961 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016962 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016963 if (start_idx >= 0)
16964 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016965 }
16966
16967 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16968 if (pos != NULL)
16969 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970}
16971
16972/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016973 * "string()" function
16974 */
16975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016976f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016977 typval_T *argvars;
16978 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016979{
16980 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016981 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016983 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016984 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016985 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016986 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016987 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988}
16989
16990/*
16991 * "strlen()" function
16992 */
16993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016994f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016995 typval_T *argvars;
16996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016998 rettv->vval.v_number = (varnumber_T)(STRLEN(
16999 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000}
17001
17002/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017003 * "strchars()" function
17004 */
17005 static void
17006f_strchars(argvars, rettv)
17007 typval_T *argvars;
17008 typval_T *rettv;
17009{
17010 char_u *s = get_tv_string(&argvars[0]);
17011#ifdef FEAT_MBYTE
17012 varnumber_T len = 0;
17013
17014 while (*s != NUL)
17015 {
17016 mb_cptr2char_adv(&s);
17017 ++len;
17018 }
17019 rettv->vval.v_number = len;
17020#else
17021 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17022#endif
17023}
17024
17025/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017026 * "strdisplaywidth()" function
17027 */
17028 static void
17029f_strdisplaywidth(argvars, rettv)
17030 typval_T *argvars;
17031 typval_T *rettv;
17032{
17033 char_u *s = get_tv_string(&argvars[0]);
17034 int col = 0;
17035
17036 if (argvars[1].v_type != VAR_UNKNOWN)
17037 col = get_tv_number(&argvars[1]);
17038
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017039 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017040}
17041
17042/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017043 * "strwidth()" function
17044 */
17045 static void
17046f_strwidth(argvars, rettv)
17047 typval_T *argvars;
17048 typval_T *rettv;
17049{
17050 char_u *s = get_tv_string(&argvars[0]);
17051
17052 rettv->vval.v_number = (varnumber_T)(
17053#ifdef FEAT_MBYTE
17054 mb_string2cells(s, -1)
17055#else
17056 STRLEN(s)
17057#endif
17058 );
17059}
17060
17061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 * "strpart()" function
17063 */
17064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017065f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017066 typval_T *argvars;
17067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068{
17069 char_u *p;
17070 int n;
17071 int len;
17072 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017073 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017075 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 slen = (int)STRLEN(p);
17077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017078 n = get_tv_number_chk(&argvars[1], &error);
17079 if (error)
17080 len = 0;
17081 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017082 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 else
17084 len = slen - n; /* default len: all bytes that are available. */
17085
17086 /*
17087 * Only return the overlap between the specified part and the actual
17088 * string.
17089 */
17090 if (n < 0)
17091 {
17092 len += n;
17093 n = 0;
17094 }
17095 else if (n > slen)
17096 n = slen;
17097 if (len < 0)
17098 len = 0;
17099 else if (n + len > slen)
17100 len = slen - n;
17101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017102 rettv->v_type = VAR_STRING;
17103 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104}
17105
17106/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017107 * "strridx()" function
17108 */
17109 static void
17110f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017111 typval_T *argvars;
17112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017113{
17114 char_u buf[NUMBUFLEN];
17115 char_u *needle;
17116 char_u *haystack;
17117 char_u *rest;
17118 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017119 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017121 needle = get_tv_string_chk(&argvars[1]);
17122 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017123
17124 rettv->vval.v_number = -1;
17125 if (needle == NULL || haystack == NULL)
17126 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017127
17128 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017129 if (argvars[2].v_type != VAR_UNKNOWN)
17130 {
17131 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017132 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017133 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017134 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017135 }
17136 else
17137 end_idx = haystack_len;
17138
Bram Moolenaar0d660222005-01-07 21:51:51 +000017139 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017140 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017141 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017142 lastmatch = haystack + end_idx;
17143 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017144 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017145 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017146 for (rest = haystack; *rest != '\0'; ++rest)
17147 {
17148 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017149 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017150 break;
17151 lastmatch = rest;
17152 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017153 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017154
17155 if (lastmatch == NULL)
17156 rettv->vval.v_number = -1;
17157 else
17158 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17159}
17160
17161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162 * "strtrans()" function
17163 */
17164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017165f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017166 typval_T *argvars;
17167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017168{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017169 rettv->v_type = VAR_STRING;
17170 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171}
17172
17173/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017174 * "submatch()" function
17175 */
17176 static void
17177f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017178 typval_T *argvars;
17179 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180{
17181 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017182 rettv->vval.v_string =
17183 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017184}
17185
17186/*
17187 * "substitute()" function
17188 */
17189 static void
17190f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017191 typval_T *argvars;
17192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017193{
17194 char_u patbuf[NUMBUFLEN];
17195 char_u subbuf[NUMBUFLEN];
17196 char_u flagsbuf[NUMBUFLEN];
17197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017198 char_u *str = get_tv_string_chk(&argvars[0]);
17199 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17200 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17201 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17202
Bram Moolenaar0d660222005-01-07 21:51:51 +000017203 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017204 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17205 rettv->vval.v_string = NULL;
17206 else
17207 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017208}
17209
17210/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017211 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017214f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017215 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217{
17218 int id = 0;
17219#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017220 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 long col;
17222 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017223 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017225 lnum = get_tv_lnum(argvars); /* -1 on type error */
17226 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17227 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017229 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017230 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017231 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232#endif
17233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017234 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235}
17236
17237/*
17238 * "synIDattr(id, what [, mode])" function
17239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017241f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017242 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244{
17245 char_u *p = NULL;
17246#ifdef FEAT_SYN_HL
17247 int id;
17248 char_u *what;
17249 char_u *mode;
17250 char_u modebuf[NUMBUFLEN];
17251 int modec;
17252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017253 id = get_tv_number(&argvars[0]);
17254 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017255 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017257 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017259 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 modec = 0; /* replace invalid with current */
17261 }
17262 else
17263 {
17264#ifdef FEAT_GUI
17265 if (gui.in_use)
17266 modec = 'g';
17267 else
17268#endif
17269 if (t_colors > 1)
17270 modec = 'c';
17271 else
17272 modec = 't';
17273 }
17274
17275
17276 switch (TOLOWER_ASC(what[0]))
17277 {
17278 case 'b':
17279 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17280 p = highlight_color(id, what, modec);
17281 else /* bold */
17282 p = highlight_has_attr(id, HL_BOLD, modec);
17283 break;
17284
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017285 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 p = highlight_color(id, what, modec);
17287 break;
17288
17289 case 'i':
17290 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17291 p = highlight_has_attr(id, HL_INVERSE, modec);
17292 else /* italic */
17293 p = highlight_has_attr(id, HL_ITALIC, modec);
17294 break;
17295
17296 case 'n': /* name */
17297 p = get_highlight_name(NULL, id - 1);
17298 break;
17299
17300 case 'r': /* reverse */
17301 p = highlight_has_attr(id, HL_INVERSE, modec);
17302 break;
17303
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017304 case 's':
17305 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17306 p = highlight_color(id, what, modec);
17307 else /* standout */
17308 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 break;
17310
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017311 case 'u':
17312 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17313 /* underline */
17314 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17315 else
17316 /* undercurl */
17317 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318 break;
17319 }
17320
17321 if (p != NULL)
17322 p = vim_strsave(p);
17323#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017324 rettv->v_type = VAR_STRING;
17325 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326}
17327
17328/*
17329 * "synIDtrans(id)" function
17330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017332f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017333 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335{
17336 int id;
17337
17338#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017339 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340
17341 if (id > 0)
17342 id = syn_get_final_id(id);
17343 else
17344#endif
17345 id = 0;
17346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017347 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348}
17349
17350/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017351 * "synconcealed(lnum, col)" function
17352 */
17353 static void
17354f_synconcealed(argvars, rettv)
17355 typval_T *argvars UNUSED;
17356 typval_T *rettv;
17357{
17358#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17359 long lnum;
17360 long col;
17361 int syntax_flags = 0;
17362 int cchar;
17363 int matchid = 0;
17364 char_u str[NUMBUFLEN];
17365#endif
17366
17367 rettv->v_type = VAR_LIST;
17368 rettv->vval.v_list = NULL;
17369
17370#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17371 lnum = get_tv_lnum(argvars); /* -1 on type error */
17372 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17373
17374 vim_memset(str, NUL, sizeof(str));
17375
17376 if (rettv_list_alloc(rettv) != FAIL)
17377 {
17378 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17379 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17380 && curwin->w_p_cole > 0)
17381 {
17382 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17383 syntax_flags = get_syntax_info(&matchid);
17384
17385 /* get the conceal character */
17386 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17387 {
17388 cchar = syn_get_sub_char();
17389 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17390 cchar = lcs_conceal;
17391 if (cchar != NUL)
17392 {
17393# ifdef FEAT_MBYTE
17394 if (has_mbyte)
17395 (*mb_char2bytes)(cchar, str);
17396 else
17397# endif
17398 str[0] = cchar;
17399 }
17400 }
17401 }
17402
17403 list_append_number(rettv->vval.v_list,
17404 (syntax_flags & HL_CONCEAL) != 0);
17405 /* -1 to auto-determine strlen */
17406 list_append_string(rettv->vval.v_list, str, -1);
17407 list_append_number(rettv->vval.v_list, matchid);
17408 }
17409#endif
17410}
17411
17412/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017413 * "synstack(lnum, col)" function
17414 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017415 static void
17416f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017417 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017418 typval_T *rettv;
17419{
17420#ifdef FEAT_SYN_HL
17421 long lnum;
17422 long col;
17423 int i;
17424 int id;
17425#endif
17426
17427 rettv->v_type = VAR_LIST;
17428 rettv->vval.v_list = NULL;
17429
17430#ifdef FEAT_SYN_HL
17431 lnum = get_tv_lnum(argvars); /* -1 on type error */
17432 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17433
17434 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017435 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017436 && rettv_list_alloc(rettv) != FAIL)
17437 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017438 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017439 for (i = 0; ; ++i)
17440 {
17441 id = syn_get_stack_item(i);
17442 if (id < 0)
17443 break;
17444 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17445 break;
17446 }
17447 }
17448#endif
17449}
17450
17451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 * "system()" function
17453 */
17454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017455f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017456 typval_T *argvars;
17457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017459 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017461 char_u *infile = NULL;
17462 char_u buf[NUMBUFLEN];
17463 int err = FALSE;
17464 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017466 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017467 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017468
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017469 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017470 {
17471 /*
17472 * Write the string to a temp file, to be used for input of the shell
17473 * command.
17474 */
17475 if ((infile = vim_tempname('i')) == NULL)
17476 {
17477 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017478 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017479 }
17480
17481 fd = mch_fopen((char *)infile, WRITEBIN);
17482 if (fd == NULL)
17483 {
17484 EMSG2(_(e_notopen), infile);
17485 goto done;
17486 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017487 p = get_tv_string_buf_chk(&argvars[1], buf);
17488 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017489 {
17490 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017491 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017492 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017493 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17494 err = TRUE;
17495 if (fclose(fd) != 0)
17496 err = TRUE;
17497 if (err)
17498 {
17499 EMSG(_("E677: Error writing temp file"));
17500 goto done;
17501 }
17502 }
17503
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017504 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17505 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017506
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507#ifdef USE_CR
17508 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017509 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 {
17511 char_u *s;
17512
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017513 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 {
17515 if (*s == CAR)
17516 *s = NL;
17517 }
17518 }
17519#else
17520# ifdef USE_CRNL
17521 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017522 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 {
17524 char_u *s, *d;
17525
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017526 d = res;
17527 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 {
17529 if (s[0] == CAR && s[1] == NL)
17530 ++s;
17531 *d++ = *s;
17532 }
17533 *d = NUL;
17534 }
17535# endif
17536#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017537
17538done:
17539 if (infile != NULL)
17540 {
17541 mch_remove(infile);
17542 vim_free(infile);
17543 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017544 rettv->v_type = VAR_STRING;
17545 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546}
17547
17548/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017549 * "tabpagebuflist()" function
17550 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017551 static void
17552f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017553 typval_T *argvars UNUSED;
17554 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017555{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017556#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017557 tabpage_T *tp;
17558 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017559
17560 if (argvars[0].v_type == VAR_UNKNOWN)
17561 wp = firstwin;
17562 else
17563 {
17564 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17565 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017566 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017567 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017568 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017569 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017570 for (; wp != NULL; wp = wp->w_next)
17571 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017572 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017573 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017574 }
17575#endif
17576}
17577
17578
17579/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017580 * "tabpagenr()" function
17581 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017582 static void
17583f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017584 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017585 typval_T *rettv;
17586{
17587 int nr = 1;
17588#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017589 char_u *arg;
17590
17591 if (argvars[0].v_type != VAR_UNKNOWN)
17592 {
17593 arg = get_tv_string_chk(&argvars[0]);
17594 nr = 0;
17595 if (arg != NULL)
17596 {
17597 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017598 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017599 else
17600 EMSG2(_(e_invexpr2), arg);
17601 }
17602 }
17603 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017604 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017605#endif
17606 rettv->vval.v_number = nr;
17607}
17608
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017609
17610#ifdef FEAT_WINDOWS
17611static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17612
17613/*
17614 * Common code for tabpagewinnr() and winnr().
17615 */
17616 static int
17617get_winnr(tp, argvar)
17618 tabpage_T *tp;
17619 typval_T *argvar;
17620{
17621 win_T *twin;
17622 int nr = 1;
17623 win_T *wp;
17624 char_u *arg;
17625
17626 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17627 if (argvar->v_type != VAR_UNKNOWN)
17628 {
17629 arg = get_tv_string_chk(argvar);
17630 if (arg == NULL)
17631 nr = 0; /* type error; errmsg already given */
17632 else if (STRCMP(arg, "$") == 0)
17633 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17634 else if (STRCMP(arg, "#") == 0)
17635 {
17636 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17637 if (twin == NULL)
17638 nr = 0;
17639 }
17640 else
17641 {
17642 EMSG2(_(e_invexpr2), arg);
17643 nr = 0;
17644 }
17645 }
17646
17647 if (nr > 0)
17648 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17649 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017650 {
17651 if (wp == NULL)
17652 {
17653 /* didn't find it in this tabpage */
17654 nr = 0;
17655 break;
17656 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017657 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017658 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017659 return nr;
17660}
17661#endif
17662
17663/*
17664 * "tabpagewinnr()" function
17665 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017666 static void
17667f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017668 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017669 typval_T *rettv;
17670{
17671 int nr = 1;
17672#ifdef FEAT_WINDOWS
17673 tabpage_T *tp;
17674
17675 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17676 if (tp == NULL)
17677 nr = 0;
17678 else
17679 nr = get_winnr(tp, &argvars[1]);
17680#endif
17681 rettv->vval.v_number = nr;
17682}
17683
17684
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017685/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017686 * "tagfiles()" function
17687 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017688 static void
17689f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017690 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017691 typval_T *rettv;
17692{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017693 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017694 tagname_T tn;
17695 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017696
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017697 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017698 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017699 fname = alloc(MAXPATHL);
17700 if (fname == NULL)
17701 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017702
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017703 for (first = TRUE; ; first = FALSE)
17704 if (get_tagfname(&tn, first, fname) == FAIL
17705 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017706 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017707 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017708 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017709}
17710
17711/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017712 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017713 */
17714 static void
17715f_taglist(argvars, rettv)
17716 typval_T *argvars;
17717 typval_T *rettv;
17718{
17719 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017720
17721 tag_pattern = get_tv_string(&argvars[0]);
17722
17723 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017724 if (*tag_pattern == NUL)
17725 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017726
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017727 if (rettv_list_alloc(rettv) == OK)
17728 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017729}
17730
17731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732 * "tempname()" function
17733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017735f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017736 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738{
17739 static int x = 'A';
17740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017741 rettv->v_type = VAR_STRING;
17742 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743
17744 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17745 * names. Skip 'I' and 'O', they are used for shell redirection. */
17746 do
17747 {
17748 if (x == 'Z')
17749 x = '0';
17750 else if (x == '9')
17751 x = 'A';
17752 else
17753 {
17754#ifdef EBCDIC
17755 if (x == 'I')
17756 x = 'J';
17757 else if (x == 'R')
17758 x = 'S';
17759 else
17760#endif
17761 ++x;
17762 }
17763 } while (x == 'I' || x == 'O');
17764}
17765
17766/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017767 * "test(list)" function: Just checking the walls...
17768 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017769 static void
17770f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017771 typval_T *argvars UNUSED;
17772 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017773{
17774 /* Used for unit testing. Change the code below to your liking. */
17775#if 0
17776 listitem_T *li;
17777 list_T *l;
17778 char_u *bad, *good;
17779
17780 if (argvars[0].v_type != VAR_LIST)
17781 return;
17782 l = argvars[0].vval.v_list;
17783 if (l == NULL)
17784 return;
17785 li = l->lv_first;
17786 if (li == NULL)
17787 return;
17788 bad = get_tv_string(&li->li_tv);
17789 li = li->li_next;
17790 if (li == NULL)
17791 return;
17792 good = get_tv_string(&li->li_tv);
17793 rettv->vval.v_number = test_edit_score(bad, good);
17794#endif
17795}
17796
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017797#ifdef FEAT_FLOAT
17798/*
17799 * "tan()" function
17800 */
17801 static void
17802f_tan(argvars, rettv)
17803 typval_T *argvars;
17804 typval_T *rettv;
17805{
17806 float_T f;
17807
17808 rettv->v_type = VAR_FLOAT;
17809 if (get_float_arg(argvars, &f) == OK)
17810 rettv->vval.v_float = tan(f);
17811 else
17812 rettv->vval.v_float = 0.0;
17813}
17814
17815/*
17816 * "tanh()" function
17817 */
17818 static void
17819f_tanh(argvars, rettv)
17820 typval_T *argvars;
17821 typval_T *rettv;
17822{
17823 float_T f;
17824
17825 rettv->v_type = VAR_FLOAT;
17826 if (get_float_arg(argvars, &f) == OK)
17827 rettv->vval.v_float = tanh(f);
17828 else
17829 rettv->vval.v_float = 0.0;
17830}
17831#endif
17832
Bram Moolenaard52d9742005-08-21 22:20:28 +000017833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 * "tolower(string)" function
17835 */
17836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017837f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017838 typval_T *argvars;
17839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840{
17841 char_u *p;
17842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017843 p = vim_strsave(get_tv_string(&argvars[0]));
17844 rettv->v_type = VAR_STRING;
17845 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846
17847 if (p != NULL)
17848 while (*p != NUL)
17849 {
17850#ifdef FEAT_MBYTE
17851 int l;
17852
17853 if (enc_utf8)
17854 {
17855 int c, lc;
17856
17857 c = utf_ptr2char(p);
17858 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017859 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 /* TODO: reallocate string when byte count changes. */
17861 if (utf_char2len(lc) == l)
17862 utf_char2bytes(lc, p);
17863 p += l;
17864 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017865 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 p += l; /* skip multi-byte character */
17867 else
17868#endif
17869 {
17870 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17871 ++p;
17872 }
17873 }
17874}
17875
17876/*
17877 * "toupper(string)" function
17878 */
17879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017880f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017881 typval_T *argvars;
17882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017884 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017885 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886}
17887
17888/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017889 * "tr(string, fromstr, tostr)" function
17890 */
17891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017892f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017893 typval_T *argvars;
17894 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017895{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010017896 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017897 char_u *fromstr;
17898 char_u *tostr;
17899 char_u *p;
17900#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017901 int inlen;
17902 int fromlen;
17903 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017904 int idx;
17905 char_u *cpstr;
17906 int cplen;
17907 int first = TRUE;
17908#endif
17909 char_u buf[NUMBUFLEN];
17910 char_u buf2[NUMBUFLEN];
17911 garray_T ga;
17912
Bram Moolenaar70b2a562012-01-10 22:26:17 +010017913 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017914 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17915 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017916
17917 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017918 rettv->v_type = VAR_STRING;
17919 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017920 if (fromstr == NULL || tostr == NULL)
17921 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017922 ga_init2(&ga, (int)sizeof(char), 80);
17923
17924#ifdef FEAT_MBYTE
17925 if (!has_mbyte)
17926#endif
17927 /* not multi-byte: fromstr and tostr must be the same length */
17928 if (STRLEN(fromstr) != STRLEN(tostr))
17929 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017930#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017931error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017932#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017933 EMSG2(_(e_invarg2), fromstr);
17934 ga_clear(&ga);
17935 return;
17936 }
17937
17938 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010017939 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000017940 {
17941#ifdef FEAT_MBYTE
17942 if (has_mbyte)
17943 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010017944 inlen = (*mb_ptr2len)(in_str);
17945 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017946 cplen = inlen;
17947 idx = 0;
17948 for (p = fromstr; *p != NUL; p += fromlen)
17949 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017950 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010017951 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000017952 {
17953 for (p = tostr; *p != NUL; p += tolen)
17954 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017955 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017956 if (idx-- == 0)
17957 {
17958 cplen = tolen;
17959 cpstr = p;
17960 break;
17961 }
17962 }
17963 if (*p == NUL) /* tostr is shorter than fromstr */
17964 goto error;
17965 break;
17966 }
17967 ++idx;
17968 }
17969
Bram Moolenaar70b2a562012-01-10 22:26:17 +010017970 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000017971 {
17972 /* Check that fromstr and tostr have the same number of
17973 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010017974 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017975 first = FALSE;
17976 for (p = tostr; *p != NUL; p += tolen)
17977 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017978 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017979 --idx;
17980 }
17981 if (idx != 0)
17982 goto error;
17983 }
17984
17985 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017986 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017987 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017988
Bram Moolenaar70b2a562012-01-10 22:26:17 +010017989 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017990 }
17991 else
17992#endif
17993 {
17994 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010017995 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017996 if (p != NULL)
17997 ga_append(&ga, tostr[p - fromstr]);
17998 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010017999 ga_append(&ga, *in_str);
18000 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018001 }
18002 }
18003
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018004 /* add a terminating NUL */
18005 ga_grow(&ga, 1);
18006 ga_append(&ga, NUL);
18007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018008 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018009}
18010
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018011#ifdef FEAT_FLOAT
18012/*
18013 * "trunc({float})" function
18014 */
18015 static void
18016f_trunc(argvars, rettv)
18017 typval_T *argvars;
18018 typval_T *rettv;
18019{
18020 float_T f;
18021
18022 rettv->v_type = VAR_FLOAT;
18023 if (get_float_arg(argvars, &f) == OK)
18024 /* trunc() is not in C90, use floor() or ceil() instead. */
18025 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18026 else
18027 rettv->vval.v_float = 0.0;
18028}
18029#endif
18030
Bram Moolenaar8299df92004-07-10 09:47:34 +000018031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 * "type(expr)" function
18033 */
18034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018035f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018036 typval_T *argvars;
18037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018039 int n;
18040
18041 switch (argvars[0].v_type)
18042 {
18043 case VAR_NUMBER: n = 0; break;
18044 case VAR_STRING: n = 1; break;
18045 case VAR_FUNC: n = 2; break;
18046 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018047 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018048#ifdef FEAT_FLOAT
18049 case VAR_FLOAT: n = 5; break;
18050#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018051 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18052 }
18053 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054}
18055
18056/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018057 * "undofile(name)" function
18058 */
18059 static void
18060f_undofile(argvars, rettv)
18061 typval_T *argvars;
18062 typval_T *rettv;
18063{
18064 rettv->v_type = VAR_STRING;
18065#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018066 {
18067 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
18068
18069 if (ffname != NULL)
18070 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18071 vim_free(ffname);
18072 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018073#else
18074 rettv->vval.v_string = NULL;
18075#endif
18076}
18077
18078/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018079 * "undotree()" function
18080 */
18081 static void
18082f_undotree(argvars, rettv)
18083 typval_T *argvars UNUSED;
18084 typval_T *rettv;
18085{
18086 if (rettv_dict_alloc(rettv) == OK)
18087 {
18088 dict_T *dict = rettv->vval.v_dict;
18089 list_T *list;
18090
Bram Moolenaar730cde92010-06-27 05:18:54 +020018091 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018092 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018093 dict_add_nr_str(dict, "save_last",
18094 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018095 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18096 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018097 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018098
18099 list = list_alloc();
18100 if (list != NULL)
18101 {
18102 u_eval_tree(curbuf->b_u_oldhead, list);
18103 dict_add_list(dict, "entries", list);
18104 }
18105 }
18106}
18107
18108/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018109 * "values(dict)" function
18110 */
18111 static void
18112f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018113 typval_T *argvars;
18114 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018115{
18116 dict_list(argvars, rettv, 1);
18117}
18118
18119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 * "virtcol(string)" function
18121 */
18122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018123f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018124 typval_T *argvars;
18125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126{
18127 colnr_T vcol = 0;
18128 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018129 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018131 fp = var2fpos(&argvars[0], FALSE, &fnum);
18132 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18133 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 {
18135 getvvcol(curwin, fp, NULL, NULL, &vcol);
18136 ++vcol;
18137 }
18138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018139 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140}
18141
18142/*
18143 * "visualmode()" function
18144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018146f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018147 typval_T *argvars UNUSED;
18148 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149{
18150#ifdef FEAT_VISUAL
18151 char_u str[2];
18152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018153 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 str[0] = curbuf->b_visual_mode_eval;
18155 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018156 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157
18158 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018159 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161#endif
18162}
18163
18164/*
18165 * "winbufnr(nr)" function
18166 */
18167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018168f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018169 typval_T *argvars;
18170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171{
18172 win_T *wp;
18173
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018174 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018176 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018178 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179}
18180
18181/*
18182 * "wincol()" function
18183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018185f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018186 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188{
18189 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018190 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191}
18192
18193/*
18194 * "winheight(nr)" function
18195 */
18196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018197f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018198 typval_T *argvars;
18199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200{
18201 win_T *wp;
18202
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018203 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018205 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018207 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208}
18209
18210/*
18211 * "winline()" function
18212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018214f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018215 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217{
18218 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018219 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220}
18221
18222/*
18223 * "winnr()" function
18224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018226f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018227 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229{
18230 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018231
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018233 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018235 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236}
18237
18238/*
18239 * "winrestcmd()" function
18240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018242f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018243 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245{
18246#ifdef FEAT_WINDOWS
18247 win_T *wp;
18248 int winnr = 1;
18249 garray_T ga;
18250 char_u buf[50];
18251
18252 ga_init2(&ga, (int)sizeof(char), 70);
18253 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18254 {
18255 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18256 ga_concat(&ga, buf);
18257# ifdef FEAT_VERTSPLIT
18258 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18259 ga_concat(&ga, buf);
18260# endif
18261 ++winnr;
18262 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018263 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018265 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018267 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018269 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270}
18271
18272/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018273 * "winrestview()" function
18274 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018275 static void
18276f_winrestview(argvars, rettv)
18277 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018278 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018279{
18280 dict_T *dict;
18281
18282 if (argvars[0].v_type != VAR_DICT
18283 || (dict = argvars[0].vval.v_dict) == NULL)
18284 EMSG(_(e_invarg));
18285 else
18286 {
18287 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18288 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18289#ifdef FEAT_VIRTUALEDIT
18290 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18291#endif
18292 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018293 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018294
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018295 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018296#ifdef FEAT_DIFF
18297 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18298#endif
18299 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18300 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18301
18302 check_cursor();
18303 changed_cline_bef_curs();
18304 invalidate_botline();
18305 redraw_later(VALID);
18306
18307 if (curwin->w_topline == 0)
18308 curwin->w_topline = 1;
18309 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18310 curwin->w_topline = curbuf->b_ml.ml_line_count;
18311#ifdef FEAT_DIFF
18312 check_topfill(curwin, TRUE);
18313#endif
18314 }
18315}
18316
18317/*
18318 * "winsaveview()" function
18319 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018320 static void
18321f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018322 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018323 typval_T *rettv;
18324{
18325 dict_T *dict;
18326
Bram Moolenaara800b422010-06-27 01:15:55 +020018327 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018328 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018329 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018330
18331 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18332 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18333#ifdef FEAT_VIRTUALEDIT
18334 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18335#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018336 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018337 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18338
18339 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18340#ifdef FEAT_DIFF
18341 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18342#endif
18343 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18344 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18345}
18346
18347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348 * "winwidth(nr)" function
18349 */
18350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018351f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018352 typval_T *argvars;
18353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354{
18355 win_T *wp;
18356
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018357 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018359 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 else
18361#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018362 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018364 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365#endif
18366}
18367
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018369 * "writefile()" function
18370 */
18371 static void
18372f_writefile(argvars, rettv)
18373 typval_T *argvars;
18374 typval_T *rettv;
18375{
18376 int binary = FALSE;
18377 char_u *fname;
18378 FILE *fd;
18379 listitem_T *li;
18380 char_u *s;
18381 int ret = 0;
18382 int c;
18383
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018384 if (check_restricted() || check_secure())
18385 return;
18386
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018387 if (argvars[0].v_type != VAR_LIST)
18388 {
18389 EMSG2(_(e_listarg), "writefile()");
18390 return;
18391 }
18392 if (argvars[0].vval.v_list == NULL)
18393 return;
18394
18395 if (argvars[2].v_type != VAR_UNKNOWN
18396 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18397 binary = TRUE;
18398
18399 /* Always open the file in binary mode, library functions have a mind of
18400 * their own about CR-LF conversion. */
18401 fname = get_tv_string(&argvars[1]);
18402 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18403 {
18404 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18405 ret = -1;
18406 }
18407 else
18408 {
18409 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18410 li = li->li_next)
18411 {
18412 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18413 {
18414 if (*s == '\n')
18415 c = putc(NUL, fd);
18416 else
18417 c = putc(*s, fd);
18418 if (c == EOF)
18419 {
18420 ret = -1;
18421 break;
18422 }
18423 }
18424 if (!binary || li->li_next != NULL)
18425 if (putc('\n', fd) == EOF)
18426 {
18427 ret = -1;
18428 break;
18429 }
18430 if (ret < 0)
18431 {
18432 EMSG(_(e_write));
18433 break;
18434 }
18435 }
18436 fclose(fd);
18437 }
18438
18439 rettv->vval.v_number = ret;
18440}
18441
18442/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018443 * "xor(expr, expr)" function
18444 */
18445 static void
18446f_xor(argvars, rettv)
18447 typval_T *argvars;
18448 typval_T *rettv;
18449{
18450 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18451 ^ get_tv_number_chk(&argvars[1], NULL);
18452}
18453
18454
18455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018457 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 */
18459 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018460var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018461 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018462 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018463 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018465 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018467 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468
Bram Moolenaara5525202006-03-02 22:52:09 +000018469 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018470 if (varp->v_type == VAR_LIST)
18471 {
18472 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018473 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018474 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018475 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018476
18477 l = varp->vval.v_list;
18478 if (l == NULL)
18479 return NULL;
18480
18481 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018482 pos.lnum = list_find_nr(l, 0L, &error);
18483 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018484 return NULL; /* invalid line number */
18485
18486 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018487 pos.col = list_find_nr(l, 1L, &error);
18488 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018489 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018490 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018491
18492 /* We accept "$" for the column number: last column. */
18493 li = list_find(l, 1L);
18494 if (li != NULL && li->li_tv.v_type == VAR_STRING
18495 && li->li_tv.vval.v_string != NULL
18496 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18497 pos.col = len + 1;
18498
Bram Moolenaara5525202006-03-02 22:52:09 +000018499 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018500 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018501 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018502 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018503
Bram Moolenaara5525202006-03-02 22:52:09 +000018504#ifdef FEAT_VIRTUALEDIT
18505 /* Get the virtual offset. Defaults to zero. */
18506 pos.coladd = list_find_nr(l, 2L, &error);
18507 if (error)
18508 pos.coladd = 0;
18509#endif
18510
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018511 return &pos;
18512 }
18513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018514 name = get_tv_string_chk(varp);
18515 if (name == NULL)
18516 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018517 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018519#ifdef FEAT_VISUAL
18520 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18521 {
18522 if (VIsual_active)
18523 return &VIsual;
18524 return &curwin->w_cursor;
18525 }
18526#endif
18527 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018529 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18531 return NULL;
18532 return pp;
18533 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018534
18535#ifdef FEAT_VIRTUALEDIT
18536 pos.coladd = 0;
18537#endif
18538
Bram Moolenaar477933c2007-07-17 14:32:23 +000018539 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018540 {
18541 pos.col = 0;
18542 if (name[1] == '0') /* "w0": first visible line */
18543 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018544 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018545 pos.lnum = curwin->w_topline;
18546 return &pos;
18547 }
18548 else if (name[1] == '$') /* "w$": last visible line */
18549 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018550 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018551 pos.lnum = curwin->w_botline - 1;
18552 return &pos;
18553 }
18554 }
18555 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018557 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 {
18559 pos.lnum = curbuf->b_ml.ml_line_count;
18560 pos.col = 0;
18561 }
18562 else
18563 {
18564 pos.lnum = curwin->w_cursor.lnum;
18565 pos.col = (colnr_T)STRLEN(ml_get_curline());
18566 }
18567 return &pos;
18568 }
18569 return NULL;
18570}
18571
18572/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018573 * Convert list in "arg" into a position and optional file number.
18574 * When "fnump" is NULL there is no file number, only 3 items.
18575 * Note that the column is passed on as-is, the caller may want to decrement
18576 * it to use 1 for the first column.
18577 * Return FAIL when conversion is not possible, doesn't check the position for
18578 * validity.
18579 */
18580 static int
18581list2fpos(arg, posp, fnump)
18582 typval_T *arg;
18583 pos_T *posp;
18584 int *fnump;
18585{
18586 list_T *l = arg->vval.v_list;
18587 long i = 0;
18588 long n;
18589
Bram Moolenaarbde35262006-07-23 20:12:24 +000018590 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18591 * when "fnump" isn't NULL and "coladd" is optional. */
18592 if (arg->v_type != VAR_LIST
18593 || l == NULL
18594 || l->lv_len < (fnump == NULL ? 2 : 3)
18595 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018596 return FAIL;
18597
18598 if (fnump != NULL)
18599 {
18600 n = list_find_nr(l, i++, NULL); /* fnum */
18601 if (n < 0)
18602 return FAIL;
18603 if (n == 0)
18604 n = curbuf->b_fnum; /* current buffer */
18605 *fnump = n;
18606 }
18607
18608 n = list_find_nr(l, i++, NULL); /* lnum */
18609 if (n < 0)
18610 return FAIL;
18611 posp->lnum = n;
18612
18613 n = list_find_nr(l, i++, NULL); /* col */
18614 if (n < 0)
18615 return FAIL;
18616 posp->col = n;
18617
18618#ifdef FEAT_VIRTUALEDIT
18619 n = list_find_nr(l, i, NULL);
18620 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018621 posp->coladd = 0;
18622 else
18623 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018624#endif
18625
18626 return OK;
18627}
18628
18629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 * Get the length of an environment variable name.
18631 * Advance "arg" to the first character after the name.
18632 * Return 0 for error.
18633 */
18634 static int
18635get_env_len(arg)
18636 char_u **arg;
18637{
18638 char_u *p;
18639 int len;
18640
18641 for (p = *arg; vim_isIDc(*p); ++p)
18642 ;
18643 if (p == *arg) /* no name found */
18644 return 0;
18645
18646 len = (int)(p - *arg);
18647 *arg = p;
18648 return len;
18649}
18650
18651/*
18652 * Get the length of the name of a function or internal variable.
18653 * "arg" is advanced to the first non-white character after the name.
18654 * Return 0 if something is wrong.
18655 */
18656 static int
18657get_id_len(arg)
18658 char_u **arg;
18659{
18660 char_u *p;
18661 int len;
18662
18663 /* Find the end of the name. */
18664 for (p = *arg; eval_isnamec(*p); ++p)
18665 ;
18666 if (p == *arg) /* no name found */
18667 return 0;
18668
18669 len = (int)(p - *arg);
18670 *arg = skipwhite(p);
18671
18672 return len;
18673}
18674
18675/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018676 * Get the length of the name of a variable or function.
18677 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018679 * Return -1 if curly braces expansion failed.
18680 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 * If the name contains 'magic' {}'s, expand them and return the
18682 * expanded name in an allocated string via 'alias' - caller must free.
18683 */
18684 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018685get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 char_u **arg;
18687 char_u **alias;
18688 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018689 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690{
18691 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692 char_u *p;
18693 char_u *expr_start;
18694 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695
18696 *alias = NULL; /* default to no alias */
18697
18698 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18699 && (*arg)[2] == (int)KE_SNR)
18700 {
18701 /* hard coded <SNR>, already translated */
18702 *arg += 3;
18703 return get_id_len(arg) + 3;
18704 }
18705 len = eval_fname_script(*arg);
18706 if (len > 0)
18707 {
18708 /* literal "<SID>", "s:" or "<SNR>" */
18709 *arg += len;
18710 }
18711
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018713 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018715 p = find_name_end(*arg, &expr_start, &expr_end,
18716 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717 if (expr_start != NULL)
18718 {
18719 char_u *temp_string;
18720
18721 if (!evaluate)
18722 {
18723 len += (int)(p - *arg);
18724 *arg = skipwhite(p);
18725 return len;
18726 }
18727
18728 /*
18729 * Include any <SID> etc in the expanded string:
18730 * Thus the -len here.
18731 */
18732 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18733 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018734 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 *alias = temp_string;
18736 *arg = skipwhite(p);
18737 return (int)STRLEN(temp_string);
18738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739
18740 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018741 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 EMSG2(_(e_invexpr2), *arg);
18743
18744 return len;
18745}
18746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018747/*
18748 * Find the end of a variable or function name, taking care of magic braces.
18749 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18750 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018751 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018752 * Return a pointer to just after the name. Equal to "arg" if there is no
18753 * valid name.
18754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018756find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 char_u *arg;
18758 char_u **expr_start;
18759 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018760 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018762 int mb_nest = 0;
18763 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 char_u *p;
18765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018766 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018768 *expr_start = NULL;
18769 *expr_end = NULL;
18770 }
18771
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018772 /* Quick check for valid starting character. */
18773 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18774 return arg;
18775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018776 for (p = arg; *p != NUL
18777 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018778 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018779 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018780 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018781 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018782 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018783 if (*p == '\'')
18784 {
18785 /* skip over 'string' to avoid counting [ and ] inside it. */
18786 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18787 ;
18788 if (*p == NUL)
18789 break;
18790 }
18791 else if (*p == '"')
18792 {
18793 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18794 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18795 if (*p == '\\' && p[1] != NUL)
18796 ++p;
18797 if (*p == NUL)
18798 break;
18799 }
18800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018801 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018803 if (*p == '[')
18804 ++br_nest;
18805 else if (*p == ']')
18806 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018809 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018811 if (*p == '{')
18812 {
18813 mb_nest++;
18814 if (expr_start != NULL && *expr_start == NULL)
18815 *expr_start = p;
18816 }
18817 else if (*p == '}')
18818 {
18819 mb_nest--;
18820 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18821 *expr_end = p;
18822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 }
18825
18826 return p;
18827}
18828
18829/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018830 * Expands out the 'magic' {}'s in a variable/function name.
18831 * Note that this can call itself recursively, to deal with
18832 * constructs like foo{bar}{baz}{bam}
18833 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18834 * "in_start" ^
18835 * "expr_start" ^
18836 * "expr_end" ^
18837 * "in_end" ^
18838 *
18839 * Returns a new allocated string, which the caller must free.
18840 * Returns NULL for failure.
18841 */
18842 static char_u *
18843make_expanded_name(in_start, expr_start, expr_end, in_end)
18844 char_u *in_start;
18845 char_u *expr_start;
18846 char_u *expr_end;
18847 char_u *in_end;
18848{
18849 char_u c1;
18850 char_u *retval = NULL;
18851 char_u *temp_result;
18852 char_u *nextcmd = NULL;
18853
18854 if (expr_end == NULL || in_end == NULL)
18855 return NULL;
18856 *expr_start = NUL;
18857 *expr_end = NUL;
18858 c1 = *in_end;
18859 *in_end = NUL;
18860
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018861 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018862 if (temp_result != NULL && nextcmd == NULL)
18863 {
18864 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18865 + (in_end - expr_end) + 1));
18866 if (retval != NULL)
18867 {
18868 STRCPY(retval, in_start);
18869 STRCAT(retval, temp_result);
18870 STRCAT(retval, expr_end + 1);
18871 }
18872 }
18873 vim_free(temp_result);
18874
18875 *in_end = c1; /* put char back for error messages */
18876 *expr_start = '{';
18877 *expr_end = '}';
18878
18879 if (retval != NULL)
18880 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018881 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018882 if (expr_start != NULL)
18883 {
18884 /* Further expansion! */
18885 temp_result = make_expanded_name(retval, expr_start,
18886 expr_end, temp_result);
18887 vim_free(retval);
18888 retval = temp_result;
18889 }
18890 }
18891
18892 return retval;
18893}
18894
18895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018897 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 */
18899 static int
18900eval_isnamec(c)
18901 int c;
18902{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018903 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18904}
18905
18906/*
18907 * Return TRUE if character "c" can be used as the first character in a
18908 * variable or function name (excluding '{' and '}').
18909 */
18910 static int
18911eval_isnamec1(c)
18912 int c;
18913{
18914 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915}
18916
18917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 * Set number v: variable to "val".
18919 */
18920 void
18921set_vim_var_nr(idx, val)
18922 int idx;
18923 long val;
18924{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018925 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926}
18927
18928/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018929 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930 */
18931 long
18932get_vim_var_nr(idx)
18933 int idx;
18934{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018935 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936}
18937
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018938/*
18939 * Get string v: variable value. Uses a static buffer, can only be used once.
18940 */
18941 char_u *
18942get_vim_var_str(idx)
18943 int idx;
18944{
18945 return get_tv_string(&vimvars[idx].vv_tv);
18946}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018947
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018949 * Get List v: variable value. Caller must take care of reference count when
18950 * needed.
18951 */
18952 list_T *
18953get_vim_var_list(idx)
18954 int idx;
18955{
18956 return vimvars[idx].vv_list;
18957}
18958
18959/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018960 * Set v:char to character "c".
18961 */
18962 void
18963set_vim_var_char(c)
18964 int c;
18965{
18966#ifdef FEAT_MBYTE
18967 char_u buf[MB_MAXBYTES];
18968#else
18969 char_u buf[2];
18970#endif
18971
18972#ifdef FEAT_MBYTE
18973 if (has_mbyte)
18974 buf[(*mb_char2bytes)(c, buf)] = NUL;
18975 else
18976#endif
18977 {
18978 buf[0] = c;
18979 buf[1] = NUL;
18980 }
18981 set_vim_var_string(VV_CHAR, buf, -1);
18982}
18983
18984/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018985 * Set v:count to "count" and v:count1 to "count1".
18986 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 */
18988 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018989set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 long count;
18991 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018992 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018994 if (set_prevcount)
18995 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018996 vimvars[VV_COUNT].vv_nr = count;
18997 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998}
18999
19000/*
19001 * Set string v: variable to a copy of "val".
19002 */
19003 void
19004set_vim_var_string(idx, val, len)
19005 int idx;
19006 char_u *val;
19007 int len; /* length of "val" to use or -1 (whole string) */
19008{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019009 /* Need to do this (at least) once, since we can't initialize a union.
19010 * Will always be invoked when "v:progname" is set. */
19011 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19012
Bram Moolenaare9a41262005-01-15 22:18:47 +000019013 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019015 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019017 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019019 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020}
19021
19022/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019023 * Set List v: variable to "val".
19024 */
19025 void
19026set_vim_var_list(idx, val)
19027 int idx;
19028 list_T *val;
19029{
19030 list_unref(vimvars[idx].vv_list);
19031 vimvars[idx].vv_list = val;
19032 if (val != NULL)
19033 ++val->lv_refcount;
19034}
19035
19036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 * Set v:register if needed.
19038 */
19039 void
19040set_reg_var(c)
19041 int c;
19042{
19043 char_u regname;
19044
19045 if (c == 0 || c == ' ')
19046 regname = '"';
19047 else
19048 regname = c;
19049 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019050 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 set_vim_var_string(VV_REG, &regname, 1);
19052}
19053
19054/*
19055 * Get or set v:exception. If "oldval" == NULL, return the current value.
19056 * Otherwise, restore the value to "oldval" and return NULL.
19057 * Must always be called in pairs to save and restore v:exception! Does not
19058 * take care of memory allocations.
19059 */
19060 char_u *
19061v_exception(oldval)
19062 char_u *oldval;
19063{
19064 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019065 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066
Bram Moolenaare9a41262005-01-15 22:18:47 +000019067 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068 return NULL;
19069}
19070
19071/*
19072 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19073 * Otherwise, restore the value to "oldval" and return NULL.
19074 * Must always be called in pairs to save and restore v:throwpoint! Does not
19075 * take care of memory allocations.
19076 */
19077 char_u *
19078v_throwpoint(oldval)
19079 char_u *oldval;
19080{
19081 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019082 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083
Bram Moolenaare9a41262005-01-15 22:18:47 +000019084 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 return NULL;
19086}
19087
19088#if defined(FEAT_AUTOCMD) || defined(PROTO)
19089/*
19090 * Set v:cmdarg.
19091 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19092 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19093 * Must always be called in pairs!
19094 */
19095 char_u *
19096set_cmdarg(eap, oldarg)
19097 exarg_T *eap;
19098 char_u *oldarg;
19099{
19100 char_u *oldval;
19101 char_u *newval;
19102 unsigned len;
19103
Bram Moolenaare9a41262005-01-15 22:18:47 +000019104 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019105 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019107 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019108 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019109 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110 }
19111
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019112 if (eap->force_bin == FORCE_BIN)
19113 len = 6;
19114 else if (eap->force_bin == FORCE_NOBIN)
19115 len = 8;
19116 else
19117 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019118
19119 if (eap->read_edit)
19120 len += 7;
19121
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019122 if (eap->force_ff != 0)
19123 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19124# ifdef FEAT_MBYTE
19125 if (eap->force_enc != 0)
19126 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019127 if (eap->bad_char != 0)
19128 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019129# endif
19130
19131 newval = alloc(len + 1);
19132 if (newval == NULL)
19133 return NULL;
19134
19135 if (eap->force_bin == FORCE_BIN)
19136 sprintf((char *)newval, " ++bin");
19137 else if (eap->force_bin == FORCE_NOBIN)
19138 sprintf((char *)newval, " ++nobin");
19139 else
19140 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019141
19142 if (eap->read_edit)
19143 STRCAT(newval, " ++edit");
19144
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019145 if (eap->force_ff != 0)
19146 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19147 eap->cmd + eap->force_ff);
19148# ifdef FEAT_MBYTE
19149 if (eap->force_enc != 0)
19150 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19151 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019152 if (eap->bad_char == BAD_KEEP)
19153 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19154 else if (eap->bad_char == BAD_DROP)
19155 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19156 else if (eap->bad_char != 0)
19157 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019158# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019159 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019160 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161}
19162#endif
19163
19164/*
19165 * Get the value of internal variable "name".
19166 * Return OK or FAIL.
19167 */
19168 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019169get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170 char_u *name;
19171 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019172 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019173 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174{
19175 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019176 typval_T *tv = NULL;
19177 typval_T atv;
19178 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180
19181 /* truncate the name, so that we can use strcmp() */
19182 cc = name[len];
19183 name[len] = NUL;
19184
19185 /*
19186 * Check for "b:changedtick".
19187 */
19188 if (STRCMP(name, "b:changedtick") == 0)
19189 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019190 atv.v_type = VAR_NUMBER;
19191 atv.vval.v_number = curbuf->b_changedtick;
19192 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193 }
19194
19195 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 * Check for user-defined variables.
19197 */
19198 else
19199 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019200 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019202 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203 }
19204
Bram Moolenaare9a41262005-01-15 22:18:47 +000019205 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019207 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019208 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 ret = FAIL;
19210 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019211 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019212 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213
19214 name[len] = cc;
19215
19216 return ret;
19217}
19218
19219/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019220 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19221 * Also handle function call with Funcref variable: func(expr)
19222 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19223 */
19224 static int
19225handle_subscript(arg, rettv, evaluate, verbose)
19226 char_u **arg;
19227 typval_T *rettv;
19228 int evaluate; /* do more than finding the end */
19229 int verbose; /* give error messages */
19230{
19231 int ret = OK;
19232 dict_T *selfdict = NULL;
19233 char_u *s;
19234 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019235 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019236
19237 while (ret == OK
19238 && (**arg == '['
19239 || (**arg == '.' && rettv->v_type == VAR_DICT)
19240 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19241 && !vim_iswhite(*(*arg - 1)))
19242 {
19243 if (**arg == '(')
19244 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019245 /* need to copy the funcref so that we can clear rettv */
19246 functv = *rettv;
19247 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019248
19249 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019250 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019251 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019252 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19253 &len, evaluate, selfdict);
19254
19255 /* Clear the funcref afterwards, so that deleting it while
19256 * evaluating the arguments is possible (see test55). */
19257 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019258
19259 /* Stop the expression evaluation when immediately aborting on
19260 * error, or when an interrupt occurred or an exception was thrown
19261 * but not caught. */
19262 if (aborting())
19263 {
19264 if (ret == OK)
19265 clear_tv(rettv);
19266 ret = FAIL;
19267 }
19268 dict_unref(selfdict);
19269 selfdict = NULL;
19270 }
19271 else /* **arg == '[' || **arg == '.' */
19272 {
19273 dict_unref(selfdict);
19274 if (rettv->v_type == VAR_DICT)
19275 {
19276 selfdict = rettv->vval.v_dict;
19277 if (selfdict != NULL)
19278 ++selfdict->dv_refcount;
19279 }
19280 else
19281 selfdict = NULL;
19282 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19283 {
19284 clear_tv(rettv);
19285 ret = FAIL;
19286 }
19287 }
19288 }
19289 dict_unref(selfdict);
19290 return ret;
19291}
19292
19293/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019294 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019295 * value).
19296 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019297 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019298alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019299{
Bram Moolenaar33570922005-01-25 22:26:29 +000019300 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019301}
19302
19303/*
19304 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305 * The string "s" must have been allocated, it is consumed.
19306 * Return NULL for out of memory, the variable otherwise.
19307 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019308 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019309alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 char_u *s;
19311{
Bram Moolenaar33570922005-01-25 22:26:29 +000019312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019314 rettv = alloc_tv();
19315 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019317 rettv->v_type = VAR_STRING;
19318 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 }
19320 else
19321 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019322 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323}
19324
19325/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019326 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019328 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019329free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019330 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331{
19332 if (varp != NULL)
19333 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019334 switch (varp->v_type)
19335 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019336 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019337 func_unref(varp->vval.v_string);
19338 /*FALLTHROUGH*/
19339 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019340 vim_free(varp->vval.v_string);
19341 break;
19342 case VAR_LIST:
19343 list_unref(varp->vval.v_list);
19344 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019345 case VAR_DICT:
19346 dict_unref(varp->vval.v_dict);
19347 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019348 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019349#ifdef FEAT_FLOAT
19350 case VAR_FLOAT:
19351#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019352 case VAR_UNKNOWN:
19353 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019354 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019355 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019356 break;
19357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358 vim_free(varp);
19359 }
19360}
19361
19362/*
19363 * Free the memory for a variable value and set the value to NULL or 0.
19364 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019365 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019366clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019367 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368{
19369 if (varp != NULL)
19370 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019371 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019373 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019374 func_unref(varp->vval.v_string);
19375 /*FALLTHROUGH*/
19376 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019377 vim_free(varp->vval.v_string);
19378 varp->vval.v_string = NULL;
19379 break;
19380 case VAR_LIST:
19381 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019382 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019383 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019384 case VAR_DICT:
19385 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019386 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019387 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019388 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019389 varp->vval.v_number = 0;
19390 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019391#ifdef FEAT_FLOAT
19392 case VAR_FLOAT:
19393 varp->vval.v_float = 0.0;
19394 break;
19395#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019396 case VAR_UNKNOWN:
19397 break;
19398 default:
19399 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019401 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 }
19403}
19404
19405/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019406 * Set the value of a variable to NULL without freeing items.
19407 */
19408 static void
19409init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019410 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019411{
19412 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019413 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019414}
19415
19416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 * Get the number value of a variable.
19418 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019419 * For incompatible types, return 0.
19420 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19421 * caller of incompatible types: it sets *denote to TRUE if "denote"
19422 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423 */
19424 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019425get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019426 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019428 int error = FALSE;
19429
19430 return get_tv_number_chk(varp, &error); /* return 0L on error */
19431}
19432
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019433 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019434get_tv_number_chk(varp, denote)
19435 typval_T *varp;
19436 int *denote;
19437{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019438 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019440 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019442 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019443 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019444#ifdef FEAT_FLOAT
19445 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019446 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019447 break;
19448#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019449 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019450 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019451 break;
19452 case VAR_STRING:
19453 if (varp->vval.v_string != NULL)
19454 vim_str2nr(varp->vval.v_string, NULL, NULL,
19455 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019456 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019457 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019458 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019459 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019460 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019461 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019462 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019463 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019464 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019465 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019467 if (denote == NULL) /* useful for values that must be unsigned */
19468 n = -1;
19469 else
19470 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019471 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472}
19473
19474/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019475 * Get the lnum from the first argument.
19476 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019477 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 */
19479 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019480get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019481 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482{
Bram Moolenaar33570922005-01-25 22:26:29 +000019483 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 linenr_T lnum;
19485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019486 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 if (lnum == 0) /* no valid number, try using line() */
19488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019489 rettv.v_type = VAR_NUMBER;
19490 f_line(argvars, &rettv);
19491 lnum = rettv.vval.v_number;
19492 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493 }
19494 return lnum;
19495}
19496
19497/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019498 * Get the lnum from the first argument.
19499 * Also accepts "$", then "buf" is used.
19500 * Returns 0 on error.
19501 */
19502 static linenr_T
19503get_tv_lnum_buf(argvars, buf)
19504 typval_T *argvars;
19505 buf_T *buf;
19506{
19507 if (argvars[0].v_type == VAR_STRING
19508 && argvars[0].vval.v_string != NULL
19509 && argvars[0].vval.v_string[0] == '$'
19510 && buf != NULL)
19511 return buf->b_ml.ml_line_count;
19512 return get_tv_number_chk(&argvars[0], NULL);
19513}
19514
19515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 * Get the string value of a variable.
19517 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019518 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19519 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 * If the String variable has never been set, return an empty string.
19521 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019522 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19523 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 */
19525 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019526get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019527 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528{
19529 static char_u mybuf[NUMBUFLEN];
19530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019531 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532}
19533
19534 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019535get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019536 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019537 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019539 char_u *res = get_tv_string_buf_chk(varp, buf);
19540
19541 return res != NULL ? res : (char_u *)"";
19542}
19543
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019544 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019545get_tv_string_chk(varp)
19546 typval_T *varp;
19547{
19548 static char_u mybuf[NUMBUFLEN];
19549
19550 return get_tv_string_buf_chk(varp, mybuf);
19551}
19552
19553 static char_u *
19554get_tv_string_buf_chk(varp, buf)
19555 typval_T *varp;
19556 char_u *buf;
19557{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019558 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019560 case VAR_NUMBER:
19561 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19562 return buf;
19563 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019564 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019565 break;
19566 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019567 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019568 break;
19569 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019570 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019571 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019572#ifdef FEAT_FLOAT
19573 case VAR_FLOAT:
19574 EMSG(_("E806: using Float as a String"));
19575 break;
19576#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019577 case VAR_STRING:
19578 if (varp->vval.v_string != NULL)
19579 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019580 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019581 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019582 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019583 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019585 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586}
19587
19588/*
19589 * Find variable "name" in the list of variables.
19590 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019591 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019592 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019593 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019595 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019596find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019598 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019601 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602
Bram Moolenaara7043832005-01-21 11:56:39 +000019603 ht = find_var_ht(name, &varname);
19604 if (htp != NULL)
19605 *htp = ht;
19606 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019608 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609}
19610
19611/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019612 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019613 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019615 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019616find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019617 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019618 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019619 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019620{
Bram Moolenaar33570922005-01-25 22:26:29 +000019621 hashitem_T *hi;
19622
19623 if (*varname == NUL)
19624 {
19625 /* Must be something like "s:", otherwise "ht" would be NULL. */
19626 switch (varname[-2])
19627 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019628 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019629 case 'g': return &globvars_var;
19630 case 'v': return &vimvars_var;
19631 case 'b': return &curbuf->b_bufvar;
19632 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019633#ifdef FEAT_WINDOWS
19634 case 't': return &curtab->tp_winvar;
19635#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019636 case 'l': return current_funccal == NULL
19637 ? NULL : &current_funccal->l_vars_var;
19638 case 'a': return current_funccal == NULL
19639 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019640 }
19641 return NULL;
19642 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019643
19644 hi = hash_find(ht, varname);
19645 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019646 {
19647 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019648 * worked find the variable again. Don't auto-load a script if it was
19649 * loaded already, otherwise it would be loaded every time when
19650 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019651 if (ht == &globvarht && !writing)
19652 {
19653 /* Note: script_autoload() may make "hi" invalid. It must either
19654 * be obtained again or not used. */
19655 if (!script_autoload(varname, FALSE) || aborting())
19656 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019657 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019658 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019659 if (HASHITEM_EMPTY(hi))
19660 return NULL;
19661 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019662 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019663}
19664
19665/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019666 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019667 * Set "varname" to the start of name without ':'.
19668 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019669 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019670find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 char_u *name;
19672 char_u **varname;
19673{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019674 hashitem_T *hi;
19675
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 if (name[1] != ':')
19677 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019678 /* The name must not start with a colon or #. */
19679 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 return NULL;
19681 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019682
19683 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019684 hi = hash_find(&compat_hashtab, name);
19685 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019686 return &compat_hashtab;
19687
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019689 return &globvarht; /* global variable */
19690 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 }
19692 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019693 if (*name == 'g') /* global variable */
19694 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019695 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19696 */
19697 if (vim_strchr(name + 2, ':') != NULL
19698 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019699 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019701 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019703 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019704#ifdef FEAT_WINDOWS
19705 if (*name == 't') /* tab page variable */
19706 return &curtab->tp_vars.dv_hashtab;
19707#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019708 if (*name == 'v') /* v: variable */
19709 return &vimvarht;
19710 if (*name == 'a' && current_funccal != NULL) /* function argument */
19711 return &current_funccal->l_avars.dv_hashtab;
19712 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19713 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 if (*name == 's' /* script variable */
19715 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19716 return &SCRIPT_VARS(current_SID);
19717 return NULL;
19718}
19719
19720/*
19721 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019722 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723 * Returns NULL when it doesn't exist.
19724 */
19725 char_u *
19726get_var_value(name)
19727 char_u *name;
19728{
Bram Moolenaar33570922005-01-25 22:26:29 +000019729 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730
Bram Moolenaara7043832005-01-21 11:56:39 +000019731 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 if (v == NULL)
19733 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019734 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735}
19736
19737/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019738 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739 * sourcing this script and when executing functions defined in the script.
19740 */
19741 void
19742new_script_vars(id)
19743 scid_T id;
19744{
Bram Moolenaara7043832005-01-21 11:56:39 +000019745 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019746 hashtab_T *ht;
19747 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019748
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19750 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019751 /* Re-allocating ga_data means that an ht_array pointing to
19752 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019753 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019754 for (i = 1; i <= ga_scripts.ga_len; ++i)
19755 {
19756 ht = &SCRIPT_VARS(i);
19757 if (ht->ht_mask == HT_INIT_SIZE - 1)
19758 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019759 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019760 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019761 }
19762
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 while (ga_scripts.ga_len < id)
19764 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019765 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019766 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019767 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 }
19770 }
19771}
19772
19773/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019774 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19775 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 */
19777 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019778init_var_dict(dict, dict_var)
19779 dict_T *dict;
19780 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781{
Bram Moolenaar33570922005-01-25 22:26:29 +000019782 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019783 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019784 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019785 dict_var->di_tv.vval.v_dict = dict;
19786 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019787 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019788 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19789 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790}
19791
19792/*
19793 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019794 * Frees all allocated variables and the value they contain.
19795 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 */
19797 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019798vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019799 hashtab_T *ht;
19800{
19801 vars_clear_ext(ht, TRUE);
19802}
19803
19804/*
19805 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19806 */
19807 static void
19808vars_clear_ext(ht, free_val)
19809 hashtab_T *ht;
19810 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811{
Bram Moolenaara7043832005-01-21 11:56:39 +000019812 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019813 hashitem_T *hi;
19814 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815
Bram Moolenaar33570922005-01-25 22:26:29 +000019816 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019817 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019818 for (hi = ht->ht_array; todo > 0; ++hi)
19819 {
19820 if (!HASHITEM_EMPTY(hi))
19821 {
19822 --todo;
19823
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019825 * ht_array might change then. hash_clear() takes care of it
19826 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019827 v = HI2DI(hi);
19828 if (free_val)
19829 clear_tv(&v->di_tv);
19830 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19831 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019832 }
19833 }
19834 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019835 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836}
19837
Bram Moolenaara7043832005-01-21 11:56:39 +000019838/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019839 * Delete a variable from hashtab "ht" at item "hi".
19840 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019843delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019844 hashtab_T *ht;
19845 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846{
Bram Moolenaar33570922005-01-25 22:26:29 +000019847 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019848
19849 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019850 clear_tv(&di->di_tv);
19851 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852}
19853
19854/*
19855 * List the value of one internal variable.
19856 */
19857 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019858list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019859 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019861 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019863 char_u *tofree;
19864 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019865 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019866
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019867 current_copyID += COPYID_INC;
19868 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019869 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019870 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019871 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872}
19873
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019875list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 char_u *prefix;
19877 char_u *name;
19878 int type;
19879 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019880 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881{
Bram Moolenaar31859182007-08-14 20:41:13 +000019882 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19883 msg_start();
19884 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 if (name != NULL) /* "a:" vars don't have a name stored */
19886 msg_puts(name);
19887 msg_putchar(' ');
19888 msg_advance(22);
19889 if (type == VAR_NUMBER)
19890 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019891 else if (type == VAR_FUNC)
19892 msg_putchar('*');
19893 else if (type == VAR_LIST)
19894 {
19895 msg_putchar('[');
19896 if (*string == '[')
19897 ++string;
19898 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019899 else if (type == VAR_DICT)
19900 {
19901 msg_putchar('{');
19902 if (*string == '{')
19903 ++string;
19904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905 else
19906 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019907
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019909
19910 if (type == VAR_FUNC)
19911 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019912 if (*first)
19913 {
19914 msg_clr_eos();
19915 *first = FALSE;
19916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917}
19918
19919/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019920 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 * If the variable already exists, the value is updated.
19922 * Otherwise the variable is created.
19923 */
19924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019925set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019927 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019928 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929{
Bram Moolenaar33570922005-01-25 22:26:29 +000019930 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019932 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019934 ht = find_var_ht(name, &varname);
19935 if (ht == NULL || *varname == NUL)
19936 {
19937 EMSG2(_(e_illvar), name);
19938 return;
19939 }
19940 v = find_var_in_ht(ht, varname, TRUE);
19941
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019942 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
19943 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019944
Bram Moolenaar33570922005-01-25 22:26:29 +000019945 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019947 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019948 if (var_check_ro(v->di_flags, name)
19949 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019950 return;
19951 if (v->di_tv.v_type != tv->v_type
19952 && !((v->di_tv.v_type == VAR_STRING
19953 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019954 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019955 || tv->v_type == VAR_NUMBER))
19956#ifdef FEAT_FLOAT
19957 && !((v->di_tv.v_type == VAR_NUMBER
19958 || v->di_tv.v_type == VAR_FLOAT)
19959 && (tv->v_type == VAR_NUMBER
19960 || tv->v_type == VAR_FLOAT))
19961#endif
19962 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019963 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019964 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019965 return;
19966 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019967
19968 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019969 * Handle setting internal v: variables separately: we don't change
19970 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019971 */
19972 if (ht == &vimvarht)
19973 {
19974 if (v->di_tv.v_type == VAR_STRING)
19975 {
19976 vim_free(v->di_tv.vval.v_string);
19977 if (copy || tv->v_type != VAR_STRING)
19978 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19979 else
19980 {
19981 /* Take over the string to avoid an extra alloc/free. */
19982 v->di_tv.vval.v_string = tv->vval.v_string;
19983 tv->vval.v_string = NULL;
19984 }
19985 }
19986 else if (v->di_tv.v_type != VAR_NUMBER)
19987 EMSG2(_(e_intern2), "set_var()");
19988 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019989 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019990 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019991 if (STRCMP(varname, "searchforward") == 0)
19992 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19993 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019994 return;
19995 }
19996
19997 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998 }
19999 else /* add a new variable */
20000 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020001 /* Can't add "v:" variable. */
20002 if (ht == &vimvarht)
20003 {
20004 EMSG2(_(e_illvar), name);
20005 return;
20006 }
20007
Bram Moolenaar92124a32005-06-17 22:03:40 +000020008 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020009 if (!valid_varname(varname))
20010 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020011
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020012 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20013 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020014 if (v == NULL)
20015 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020016 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020017 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020019 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 return;
20021 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020022 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020024
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020025 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020026 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020027 else
20028 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020029 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020030 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020031 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033}
20034
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020035/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020036 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020037 * Also give an error message.
20038 */
20039 static int
20040var_check_ro(flags, name)
20041 int flags;
20042 char_u *name;
20043{
20044 if (flags & DI_FLAGS_RO)
20045 {
20046 EMSG2(_(e_readonlyvar), name);
20047 return TRUE;
20048 }
20049 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20050 {
20051 EMSG2(_(e_readonlysbx), name);
20052 return TRUE;
20053 }
20054 return FALSE;
20055}
20056
20057/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020058 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20059 * Also give an error message.
20060 */
20061 static int
20062var_check_fixed(flags, name)
20063 int flags;
20064 char_u *name;
20065{
20066 if (flags & DI_FLAGS_FIX)
20067 {
20068 EMSG2(_("E795: Cannot delete variable %s"), name);
20069 return TRUE;
20070 }
20071 return FALSE;
20072}
20073
20074/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020075 * Check if a funcref is assigned to a valid variable name.
20076 * Return TRUE and give an error if not.
20077 */
20078 static int
20079var_check_func_name(name, new_var)
20080 char_u *name; /* points to start of variable name */
20081 int new_var; /* TRUE when creating the variable */
20082{
20083 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20084 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20085 ? name[2] : name[0]))
20086 {
20087 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20088 name);
20089 return TRUE;
20090 }
20091 /* Don't allow hiding a function. When "v" is not NULL we might be
20092 * assigning another function to the same var, the type is checked
20093 * below. */
20094 if (new_var && function_exists(name))
20095 {
20096 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20097 name);
20098 return TRUE;
20099 }
20100 return FALSE;
20101}
20102
20103/*
20104 * Check if a variable name is valid.
20105 * Return FALSE and give an error if not.
20106 */
20107 static int
20108valid_varname(varname)
20109 char_u *varname;
20110{
20111 char_u *p;
20112
20113 for (p = varname; *p != NUL; ++p)
20114 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20115 && *p != AUTOLOAD_CHAR)
20116 {
20117 EMSG2(_(e_illvar), varname);
20118 return FALSE;
20119 }
20120 return TRUE;
20121}
20122
20123/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020124 * Return TRUE if typeval "tv" is set to be locked (immutable).
20125 * Also give an error message, using "name".
20126 */
20127 static int
20128tv_check_lock(lock, name)
20129 int lock;
20130 char_u *name;
20131{
20132 if (lock & VAR_LOCKED)
20133 {
20134 EMSG2(_("E741: Value is locked: %s"),
20135 name == NULL ? (char_u *)_("Unknown") : name);
20136 return TRUE;
20137 }
20138 if (lock & VAR_FIXED)
20139 {
20140 EMSG2(_("E742: Cannot change value of %s"),
20141 name == NULL ? (char_u *)_("Unknown") : name);
20142 return TRUE;
20143 }
20144 return FALSE;
20145}
20146
20147/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020148 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020149 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020150 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020151 * It is OK for "from" and "to" to point to the same item. This is used to
20152 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020153 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020154 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020155copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 typval_T *from;
20157 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020159 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020160 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020161 switch (from->v_type)
20162 {
20163 case VAR_NUMBER:
20164 to->vval.v_number = from->vval.v_number;
20165 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020166#ifdef FEAT_FLOAT
20167 case VAR_FLOAT:
20168 to->vval.v_float = from->vval.v_float;
20169 break;
20170#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020171 case VAR_STRING:
20172 case VAR_FUNC:
20173 if (from->vval.v_string == NULL)
20174 to->vval.v_string = NULL;
20175 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020176 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020177 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020178 if (from->v_type == VAR_FUNC)
20179 func_ref(to->vval.v_string);
20180 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020181 break;
20182 case VAR_LIST:
20183 if (from->vval.v_list == NULL)
20184 to->vval.v_list = NULL;
20185 else
20186 {
20187 to->vval.v_list = from->vval.v_list;
20188 ++to->vval.v_list->lv_refcount;
20189 }
20190 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020191 case VAR_DICT:
20192 if (from->vval.v_dict == NULL)
20193 to->vval.v_dict = NULL;
20194 else
20195 {
20196 to->vval.v_dict = from->vval.v_dict;
20197 ++to->vval.v_dict->dv_refcount;
20198 }
20199 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020200 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020201 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020202 break;
20203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204}
20205
20206/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020207 * Make a copy of an item.
20208 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020209 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20210 * reference to an already copied list/dict can be used.
20211 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020212 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020213 static int
20214item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020215 typval_T *from;
20216 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020217 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020218 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020219{
20220 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020221 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020222
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020224 {
20225 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020226 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020227 }
20228 ++recurse;
20229
20230 switch (from->v_type)
20231 {
20232 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020233#ifdef FEAT_FLOAT
20234 case VAR_FLOAT:
20235#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020236 case VAR_STRING:
20237 case VAR_FUNC:
20238 copy_tv(from, to);
20239 break;
20240 case VAR_LIST:
20241 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020242 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020243 if (from->vval.v_list == NULL)
20244 to->vval.v_list = NULL;
20245 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20246 {
20247 /* use the copy made earlier */
20248 to->vval.v_list = from->vval.v_list->lv_copylist;
20249 ++to->vval.v_list->lv_refcount;
20250 }
20251 else
20252 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20253 if (to->vval.v_list == NULL)
20254 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020255 break;
20256 case VAR_DICT:
20257 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020258 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020259 if (from->vval.v_dict == NULL)
20260 to->vval.v_dict = NULL;
20261 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20262 {
20263 /* use the copy made earlier */
20264 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20265 ++to->vval.v_dict->dv_refcount;
20266 }
20267 else
20268 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20269 if (to->vval.v_dict == NULL)
20270 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020271 break;
20272 default:
20273 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020274 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020275 }
20276 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020277 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020278}
20279
20280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281 * ":echo expr1 ..." print each argument separated with a space, add a
20282 * newline at the end.
20283 * ":echon expr1 ..." print each argument plain.
20284 */
20285 void
20286ex_echo(eap)
20287 exarg_T *eap;
20288{
20289 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020290 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020291 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 char_u *p;
20293 int needclr = TRUE;
20294 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020295 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296
20297 if (eap->skip)
20298 ++emsg_skip;
20299 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20300 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020301 /* If eval1() causes an error message the text from the command may
20302 * still need to be cleared. E.g., "echo 22,44". */
20303 need_clr_eos = needclr;
20304
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020306 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307 {
20308 /*
20309 * Report the invalid expression unless the expression evaluation
20310 * has been cancelled due to an aborting error, an interrupt, or an
20311 * exception.
20312 */
20313 if (!aborting())
20314 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020315 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 break;
20317 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020318 need_clr_eos = FALSE;
20319
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 if (!eap->skip)
20321 {
20322 if (atstart)
20323 {
20324 atstart = FALSE;
20325 /* Call msg_start() after eval1(), evaluating the expression
20326 * may cause a message to appear. */
20327 if (eap->cmdidx == CMD_echo)
20328 msg_start();
20329 }
20330 else if (eap->cmdidx == CMD_echo)
20331 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020332 current_copyID += COPYID_INC;
20333 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020334 if (p != NULL)
20335 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020337 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020339 if (*p != TAB && needclr)
20340 {
20341 /* remove any text still there from the command */
20342 msg_clr_eos();
20343 needclr = FALSE;
20344 }
20345 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346 }
20347 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020348 {
20349#ifdef FEAT_MBYTE
20350 if (has_mbyte)
20351 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020352 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020353
20354 (void)msg_outtrans_len_attr(p, i, echo_attr);
20355 p += i - 1;
20356 }
20357 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020359 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020362 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020364 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 arg = skipwhite(arg);
20366 }
20367 eap->nextcmd = check_nextcmd(arg);
20368
20369 if (eap->skip)
20370 --emsg_skip;
20371 else
20372 {
20373 /* remove text that may still be there from the command */
20374 if (needclr)
20375 msg_clr_eos();
20376 if (eap->cmdidx == CMD_echo)
20377 msg_end();
20378 }
20379}
20380
20381/*
20382 * ":echohl {name}".
20383 */
20384 void
20385ex_echohl(eap)
20386 exarg_T *eap;
20387{
20388 int id;
20389
20390 id = syn_name2id(eap->arg);
20391 if (id == 0)
20392 echo_attr = 0;
20393 else
20394 echo_attr = syn_id2attr(id);
20395}
20396
20397/*
20398 * ":execute expr1 ..." execute the result of an expression.
20399 * ":echomsg expr1 ..." Print a message
20400 * ":echoerr expr1 ..." Print an error
20401 * Each gets spaces around each argument and a newline at the end for
20402 * echo commands
20403 */
20404 void
20405ex_execute(eap)
20406 exarg_T *eap;
20407{
20408 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 int ret = OK;
20411 char_u *p;
20412 garray_T ga;
20413 int len;
20414 int save_did_emsg;
20415
20416 ga_init2(&ga, 1, 80);
20417
20418 if (eap->skip)
20419 ++emsg_skip;
20420 while (*arg != NUL && *arg != '|' && *arg != '\n')
20421 {
20422 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020423 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 {
20425 /*
20426 * Report the invalid expression unless the expression evaluation
20427 * has been cancelled due to an aborting error, an interrupt, or an
20428 * exception.
20429 */
20430 if (!aborting())
20431 EMSG2(_(e_invexpr2), p);
20432 ret = FAIL;
20433 break;
20434 }
20435
20436 if (!eap->skip)
20437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020438 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 len = (int)STRLEN(p);
20440 if (ga_grow(&ga, len + 2) == FAIL)
20441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020442 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443 ret = FAIL;
20444 break;
20445 }
20446 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449 ga.ga_len += len;
20450 }
20451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020452 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 arg = skipwhite(arg);
20454 }
20455
20456 if (ret != FAIL && ga.ga_data != NULL)
20457 {
20458 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020461 out_flush();
20462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 else if (eap->cmdidx == CMD_echoerr)
20464 {
20465 /* We don't want to abort following commands, restore did_emsg. */
20466 save_did_emsg = did_emsg;
20467 EMSG((char_u *)ga.ga_data);
20468 if (!force_abort)
20469 did_emsg = save_did_emsg;
20470 }
20471 else if (eap->cmdidx == CMD_execute)
20472 do_cmdline((char_u *)ga.ga_data,
20473 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20474 }
20475
20476 ga_clear(&ga);
20477
20478 if (eap->skip)
20479 --emsg_skip;
20480
20481 eap->nextcmd = check_nextcmd(arg);
20482}
20483
20484/*
20485 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20486 * "arg" points to the "&" or '+' when called, to "option" when returning.
20487 * Returns NULL when no option name found. Otherwise pointer to the char
20488 * after the option name.
20489 */
20490 static char_u *
20491find_option_end(arg, opt_flags)
20492 char_u **arg;
20493 int *opt_flags;
20494{
20495 char_u *p = *arg;
20496
20497 ++p;
20498 if (*p == 'g' && p[1] == ':')
20499 {
20500 *opt_flags = OPT_GLOBAL;
20501 p += 2;
20502 }
20503 else if (*p == 'l' && p[1] == ':')
20504 {
20505 *opt_flags = OPT_LOCAL;
20506 p += 2;
20507 }
20508 else
20509 *opt_flags = 0;
20510
20511 if (!ASCII_ISALPHA(*p))
20512 return NULL;
20513 *arg = p;
20514
20515 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20516 p += 4; /* termcap option */
20517 else
20518 while (ASCII_ISALPHA(*p))
20519 ++p;
20520 return p;
20521}
20522
20523/*
20524 * ":function"
20525 */
20526 void
20527ex_function(eap)
20528 exarg_T *eap;
20529{
20530 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020531 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 int j;
20533 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 char_u *name = NULL;
20536 char_u *p;
20537 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020538 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 garray_T newargs;
20540 garray_T newlines;
20541 int varargs = FALSE;
20542 int mustend = FALSE;
20543 int flags = 0;
20544 ufunc_T *fp;
20545 int indent;
20546 int nesting;
20547 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020548 dictitem_T *v;
20549 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020550 static int func_nr = 0; /* number for nameless function */
20551 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020552 hashtab_T *ht;
20553 int todo;
20554 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020555 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556
20557 /*
20558 * ":function" without argument: list functions.
20559 */
20560 if (ends_excmd(*eap->arg))
20561 {
20562 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020563 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020564 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020565 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020566 {
20567 if (!HASHITEM_EMPTY(hi))
20568 {
20569 --todo;
20570 fp = HI2UF(hi);
20571 if (!isdigit(*fp->uf_name))
20572 list_func_head(fp, FALSE);
20573 }
20574 }
20575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 eap->nextcmd = check_nextcmd(eap->arg);
20577 return;
20578 }
20579
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020580 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020581 * ":function /pat": list functions matching pattern.
20582 */
20583 if (*eap->arg == '/')
20584 {
20585 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20586 if (!eap->skip)
20587 {
20588 regmatch_T regmatch;
20589
20590 c = *p;
20591 *p = NUL;
20592 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20593 *p = c;
20594 if (regmatch.regprog != NULL)
20595 {
20596 regmatch.rm_ic = p_ic;
20597
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020598 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020599 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20600 {
20601 if (!HASHITEM_EMPTY(hi))
20602 {
20603 --todo;
20604 fp = HI2UF(hi);
20605 if (!isdigit(*fp->uf_name)
20606 && vim_regexec(&regmatch, fp->uf_name, 0))
20607 list_func_head(fp, FALSE);
20608 }
20609 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020610 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020611 }
20612 }
20613 if (*p == '/')
20614 ++p;
20615 eap->nextcmd = check_nextcmd(p);
20616 return;
20617 }
20618
20619 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020620 * Get the function name. There are these situations:
20621 * func normal function name
20622 * "name" == func, "fudi.fd_dict" == NULL
20623 * dict.func new dictionary entry
20624 * "name" == NULL, "fudi.fd_dict" set,
20625 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20626 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020627 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020628 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20629 * dict.func existing dict entry that's not a Funcref
20630 * "name" == NULL, "fudi.fd_dict" set,
20631 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020634 name = trans_function_name(&p, eap->skip, 0, &fudi);
20635 paren = (vim_strchr(p, '(') != NULL);
20636 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 {
20638 /*
20639 * Return on an invalid expression in braces, unless the expression
20640 * evaluation has been cancelled due to an aborting error, an
20641 * interrupt, or an exception.
20642 */
20643 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020644 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020645 if (!eap->skip && fudi.fd_newkey != NULL)
20646 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020647 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 else
20651 eap->skip = TRUE;
20652 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020653
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 /* An error in a function call during evaluation of an expression in magic
20655 * braces should not cause the function not to be defined. */
20656 saved_did_emsg = did_emsg;
20657 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658
20659 /*
20660 * ":function func" with only function name: list function.
20661 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020662 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 {
20664 if (!ends_excmd(*skipwhite(p)))
20665 {
20666 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020667 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020668 }
20669 eap->nextcmd = check_nextcmd(p);
20670 if (eap->nextcmd != NULL)
20671 *p = NUL;
20672 if (!eap->skip && !got_int)
20673 {
20674 fp = find_func(name);
20675 if (fp != NULL)
20676 {
20677 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020678 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020680 if (FUNCLINE(fp, j) == NULL)
20681 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 msg_putchar('\n');
20683 msg_outnum((long)(j + 1));
20684 if (j < 9)
20685 msg_putchar(' ');
20686 if (j < 99)
20687 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020688 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 out_flush(); /* show a line at a time */
20690 ui_breakcheck();
20691 }
20692 if (!got_int)
20693 {
20694 msg_putchar('\n');
20695 msg_puts((char_u *)" endfunction");
20696 }
20697 }
20698 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020699 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020701 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 }
20703
20704 /*
20705 * ":function name(arg1, arg2)" Define function.
20706 */
20707 p = skipwhite(p);
20708 if (*p != '(')
20709 {
20710 if (!eap->skip)
20711 {
20712 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020713 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 }
20715 /* attempt to continue by skipping some text */
20716 if (vim_strchr(p, '(') != NULL)
20717 p = vim_strchr(p, '(');
20718 }
20719 p = skipwhite(p + 1);
20720
20721 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20722 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20723
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020724 if (!eap->skip)
20725 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020726 /* Check the name of the function. Unless it's a dictionary function
20727 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020728 if (name != NULL)
20729 arg = name;
20730 else
20731 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020732 if (arg != NULL && (fudi.fd_di == NULL
20733 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020734 {
20735 if (*arg == K_SPECIAL)
20736 j = 3;
20737 else
20738 j = 0;
20739 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20740 : eval_isnamec(arg[j])))
20741 ++j;
20742 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020743 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020744 }
20745 }
20746
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 /*
20748 * Isolate the arguments: "arg1, arg2, ...)"
20749 */
20750 while (*p != ')')
20751 {
20752 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20753 {
20754 varargs = TRUE;
20755 p += 3;
20756 mustend = TRUE;
20757 }
20758 else
20759 {
20760 arg = p;
20761 while (ASCII_ISALNUM(*p) || *p == '_')
20762 ++p;
20763 if (arg == p || isdigit(*arg)
20764 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20765 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20766 {
20767 if (!eap->skip)
20768 EMSG2(_("E125: Illegal argument: %s"), arg);
20769 break;
20770 }
20771 if (ga_grow(&newargs, 1) == FAIL)
20772 goto erret;
20773 c = *p;
20774 *p = NUL;
20775 arg = vim_strsave(arg);
20776 if (arg == NULL)
20777 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020778
20779 /* Check for duplicate argument name. */
20780 for (i = 0; i < newargs.ga_len; ++i)
20781 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
20782 {
20783 EMSG2(_("E853: Duplicate argument name: %s"), arg);
20784 goto erret;
20785 }
20786
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20788 *p = c;
20789 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 if (*p == ',')
20791 ++p;
20792 else
20793 mustend = TRUE;
20794 }
20795 p = skipwhite(p);
20796 if (mustend && *p != ')')
20797 {
20798 if (!eap->skip)
20799 EMSG2(_(e_invarg2), eap->arg);
20800 break;
20801 }
20802 }
20803 ++p; /* skip the ')' */
20804
Bram Moolenaare9a41262005-01-15 22:18:47 +000020805 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 for (;;)
20807 {
20808 p = skipwhite(p);
20809 if (STRNCMP(p, "range", 5) == 0)
20810 {
20811 flags |= FC_RANGE;
20812 p += 5;
20813 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020814 else if (STRNCMP(p, "dict", 4) == 0)
20815 {
20816 flags |= FC_DICT;
20817 p += 4;
20818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819 else if (STRNCMP(p, "abort", 5) == 0)
20820 {
20821 flags |= FC_ABORT;
20822 p += 5;
20823 }
20824 else
20825 break;
20826 }
20827
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020828 /* When there is a line break use what follows for the function body.
20829 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20830 if (*p == '\n')
20831 line_arg = p + 1;
20832 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 EMSG(_(e_trailing));
20834
20835 /*
20836 * Read the body of the function, until ":endfunction" is found.
20837 */
20838 if (KeyTyped)
20839 {
20840 /* Check if the function already exists, don't let the user type the
20841 * whole function before telling him it doesn't work! For a script we
20842 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020843 if (!eap->skip && !eap->forceit)
20844 {
20845 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20846 EMSG(_(e_funcdict));
20847 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020848 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020851 if (!eap->skip && did_emsg)
20852 goto erret;
20853
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854 msg_putchar('\n'); /* don't overwrite the function name */
20855 cmdline_row = msg_row;
20856 }
20857
20858 indent = 2;
20859 nesting = 0;
20860 for (;;)
20861 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020020862 if (KeyTyped)
20863 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020865 sourcing_lnum_off = sourcing_lnum;
20866
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020867 if (line_arg != NULL)
20868 {
20869 /* Use eap->arg, split up in parts by line breaks. */
20870 theline = line_arg;
20871 p = vim_strchr(theline, '\n');
20872 if (p == NULL)
20873 line_arg += STRLEN(line_arg);
20874 else
20875 {
20876 *p = NUL;
20877 line_arg = p + 1;
20878 }
20879 }
20880 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 theline = getcmdline(':', 0L, indent);
20882 else
20883 theline = eap->getline(':', eap->cookie, indent);
20884 if (KeyTyped)
20885 lines_left = Rows - 1;
20886 if (theline == NULL)
20887 {
20888 EMSG(_("E126: Missing :endfunction"));
20889 goto erret;
20890 }
20891
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020892 /* Detect line continuation: sourcing_lnum increased more than one. */
20893 if (sourcing_lnum > sourcing_lnum_off + 1)
20894 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20895 else
20896 sourcing_lnum_off = 0;
20897
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 if (skip_until != NULL)
20899 {
20900 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20901 * don't check for ":endfunc". */
20902 if (STRCMP(theline, skip_until) == 0)
20903 {
20904 vim_free(skip_until);
20905 skip_until = NULL;
20906 }
20907 }
20908 else
20909 {
20910 /* skip ':' and blanks*/
20911 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20912 ;
20913
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020914 /* Check for "endfunction". */
20915 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020917 if (line_arg == NULL)
20918 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 break;
20920 }
20921
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020922 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923 * at "end". */
20924 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20925 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020926 else if (STRNCMP(p, "if", 2) == 0
20927 || STRNCMP(p, "wh", 2) == 0
20928 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 || STRNCMP(p, "try", 3) == 0)
20930 indent += 2;
20931
20932 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020933 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020935 if (*p == '!')
20936 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 p += eval_fname_script(p);
20938 if (ASCII_ISALPHA(*p))
20939 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020940 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 if (*skipwhite(p) == '(')
20942 {
20943 ++nesting;
20944 indent += 2;
20945 }
20946 }
20947 }
20948
20949 /* Check for ":append" or ":insert". */
20950 p = skip_range(p, NULL);
20951 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20952 || (p[0] == 'i'
20953 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20954 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20955 skip_until = vim_strsave((char_u *)".");
20956
20957 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20958 arg = skipwhite(skiptowhite(p));
20959 if (arg[0] == '<' && arg[1] =='<'
20960 && ((p[0] == 'p' && p[1] == 'y'
20961 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20962 || (p[0] == 'p' && p[1] == 'e'
20963 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20964 || (p[0] == 't' && p[1] == 'c'
20965 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020020966 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
20967 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20969 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020970 || (p[0] == 'm' && p[1] == 'z'
20971 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 ))
20973 {
20974 /* ":python <<" continues until a dot, like ":append" */
20975 p = skipwhite(arg + 2);
20976 if (*p == NUL)
20977 skip_until = vim_strsave((char_u *)".");
20978 else
20979 skip_until = vim_strsave(p);
20980 }
20981 }
20982
20983 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020984 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020985 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020986 if (line_arg == NULL)
20987 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020989 }
20990
20991 /* Copy the line to newly allocated memory. get_one_sourceline()
20992 * allocates 250 bytes per line, this saves 80% on average. The cost
20993 * is an extra alloc/free. */
20994 p = vim_strsave(theline);
20995 if (p != NULL)
20996 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020997 if (line_arg == NULL)
20998 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020999 theline = p;
21000 }
21001
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021002 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21003
21004 /* Add NULL lines for continuation lines, so that the line count is
21005 * equal to the index in the growarray. */
21006 while (sourcing_lnum_off-- > 0)
21007 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021008
21009 /* Check for end of eap->arg. */
21010 if (line_arg != NULL && *line_arg == NUL)
21011 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 }
21013
21014 /* Don't define the function when skipping commands or when an error was
21015 * detected. */
21016 if (eap->skip || did_emsg)
21017 goto erret;
21018
21019 /*
21020 * If there are no errors, add the function
21021 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021022 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021023 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021024 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021025 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021026 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021027 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021028 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021029 goto erret;
21030 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021031
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021032 fp = find_func(name);
21033 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021035 if (!eap->forceit)
21036 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021037 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021038 goto erret;
21039 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021040 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021041 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021042 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021043 name);
21044 goto erret;
21045 }
21046 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021047 ga_clear_strings(&(fp->uf_args));
21048 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021049 vim_free(name);
21050 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 }
21053 else
21054 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021055 char numbuf[20];
21056
21057 fp = NULL;
21058 if (fudi.fd_newkey == NULL && !eap->forceit)
21059 {
21060 EMSG(_(e_funcdict));
21061 goto erret;
21062 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021063 if (fudi.fd_di == NULL)
21064 {
21065 /* Can't add a function to a locked dictionary */
21066 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21067 goto erret;
21068 }
21069 /* Can't change an existing function if it is locked */
21070 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21071 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021072
21073 /* Give the function a sequential number. Can only be used with a
21074 * Funcref! */
21075 vim_free(name);
21076 sprintf(numbuf, "%d", ++func_nr);
21077 name = vim_strsave((char_u *)numbuf);
21078 if (name == NULL)
21079 goto erret;
21080 }
21081
21082 if (fp == NULL)
21083 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021084 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021085 {
21086 int slen, plen;
21087 char_u *scriptname;
21088
21089 /* Check that the autoload name matches the script name. */
21090 j = FAIL;
21091 if (sourcing_name != NULL)
21092 {
21093 scriptname = autoload_name(name);
21094 if (scriptname != NULL)
21095 {
21096 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021097 plen = (int)STRLEN(p);
21098 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021099 if (slen > plen && fnamecmp(p,
21100 sourcing_name + slen - plen) == 0)
21101 j = OK;
21102 vim_free(scriptname);
21103 }
21104 }
21105 if (j == FAIL)
21106 {
21107 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21108 goto erret;
21109 }
21110 }
21111
21112 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113 if (fp == NULL)
21114 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021115
21116 if (fudi.fd_dict != NULL)
21117 {
21118 if (fudi.fd_di == NULL)
21119 {
21120 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021121 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021122 if (fudi.fd_di == NULL)
21123 {
21124 vim_free(fp);
21125 goto erret;
21126 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021127 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21128 {
21129 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021130 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021131 goto erret;
21132 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021133 }
21134 else
21135 /* overwrite existing dict entry */
21136 clear_tv(&fudi.fd_di->di_tv);
21137 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021138 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021139 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021140 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021141
21142 /* behave like "dict" was used */
21143 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021144 }
21145
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021147 STRCPY(fp->uf_name, name);
21148 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021150 fp->uf_args = newargs;
21151 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021152#ifdef FEAT_PROFILE
21153 fp->uf_tml_count = NULL;
21154 fp->uf_tml_total = NULL;
21155 fp->uf_tml_self = NULL;
21156 fp->uf_profiling = FALSE;
21157 if (prof_def_func())
21158 func_do_profile(fp);
21159#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021160 fp->uf_varargs = varargs;
21161 fp->uf_flags = flags;
21162 fp->uf_calls = 0;
21163 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021164 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165
21166erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167 ga_clear_strings(&newargs);
21168 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021169ret_free:
21170 vim_free(skip_until);
21171 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174}
21175
21176/*
21177 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021178 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021180 * flags:
21181 * TFN_INT: internal function name OK
21182 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 * Advances "pp" to just after the function name (if no error).
21184 */
21185 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021186trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 char_u **pp;
21188 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021189 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021190 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021192 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 char_u *start;
21194 char_u *end;
21195 int lead;
21196 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021198 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021199
21200 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021201 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021203
21204 /* Check for hard coded <SNR>: already translated function ID (from a user
21205 * command). */
21206 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21207 && (*pp)[2] == (int)KE_SNR)
21208 {
21209 *pp += 3;
21210 len = get_id_len(pp) + 3;
21211 return vim_strnsave(start, len);
21212 }
21213
21214 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21215 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021217 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021219
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021220 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21221 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021222 if (end == start)
21223 {
21224 if (!skip)
21225 EMSG(_("E129: Function name required"));
21226 goto theend;
21227 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021228 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021229 {
21230 /*
21231 * Report an invalid expression in braces, unless the expression
21232 * evaluation has been cancelled due to an aborting error, an
21233 * interrupt, or an exception.
21234 */
21235 if (!aborting())
21236 {
21237 if (end != NULL)
21238 EMSG2(_(e_invarg2), start);
21239 }
21240 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021241 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021242 goto theend;
21243 }
21244
21245 if (lv.ll_tv != NULL)
21246 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021247 if (fdp != NULL)
21248 {
21249 fdp->fd_dict = lv.ll_dict;
21250 fdp->fd_newkey = lv.ll_newkey;
21251 lv.ll_newkey = NULL;
21252 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021253 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021254 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21255 {
21256 name = vim_strsave(lv.ll_tv->vval.v_string);
21257 *pp = end;
21258 }
21259 else
21260 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021261 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21262 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021263 EMSG(_(e_funcref));
21264 else
21265 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021266 name = NULL;
21267 }
21268 goto theend;
21269 }
21270
21271 if (lv.ll_name == NULL)
21272 {
21273 /* Error found, but continue after the function name. */
21274 *pp = end;
21275 goto theend;
21276 }
21277
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021278 /* Check if the name is a Funcref. If so, use the value. */
21279 if (lv.ll_exp_name != NULL)
21280 {
21281 len = (int)STRLEN(lv.ll_exp_name);
21282 name = deref_func_name(lv.ll_exp_name, &len);
21283 if (name == lv.ll_exp_name)
21284 name = NULL;
21285 }
21286 else
21287 {
21288 len = (int)(end - *pp);
21289 name = deref_func_name(*pp, &len);
21290 if (name == *pp)
21291 name = NULL;
21292 }
21293 if (name != NULL)
21294 {
21295 name = vim_strsave(name);
21296 *pp = end;
21297 goto theend;
21298 }
21299
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021300 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021301 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021302 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021303 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21304 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21305 {
21306 /* When there was "s:" already or the name expanded to get a
21307 * leading "s:" then remove it. */
21308 lv.ll_name += 2;
21309 len -= 2;
21310 lead = 2;
21311 }
21312 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021313 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021314 {
21315 if (lead == 2) /* skip over "s:" */
21316 lv.ll_name += 2;
21317 len = (int)(end - lv.ll_name);
21318 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021319
21320 /*
21321 * Copy the function name to allocated memory.
21322 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21323 * Accept <SNR>123_name() outside a script.
21324 */
21325 if (skip)
21326 lead = 0; /* do nothing */
21327 else if (lead > 0)
21328 {
21329 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021330 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21331 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021332 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021333 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021334 if (current_SID <= 0)
21335 {
21336 EMSG(_(e_usingsid));
21337 goto theend;
21338 }
21339 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21340 lead += (int)STRLEN(sid_buf);
21341 }
21342 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021343 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021344 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021345 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021346 goto theend;
21347 }
21348 name = alloc((unsigned)(len + lead + 1));
21349 if (name != NULL)
21350 {
21351 if (lead > 0)
21352 {
21353 name[0] = K_SPECIAL;
21354 name[1] = KS_EXTRA;
21355 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021356 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021357 STRCPY(name + 3, sid_buf);
21358 }
21359 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21360 name[len + lead] = NUL;
21361 }
21362 *pp = end;
21363
21364theend:
21365 clear_lval(&lv);
21366 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367}
21368
21369/*
21370 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21371 * Return 2 if "p" starts with "s:".
21372 * Return 0 otherwise.
21373 */
21374 static int
21375eval_fname_script(p)
21376 char_u *p;
21377{
21378 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21379 || STRNICMP(p + 1, "SNR>", 4) == 0))
21380 return 5;
21381 if (p[0] == 's' && p[1] == ':')
21382 return 2;
21383 return 0;
21384}
21385
21386/*
21387 * Return TRUE if "p" starts with "<SID>" or "s:".
21388 * Only works if eval_fname_script() returned non-zero for "p"!
21389 */
21390 static int
21391eval_fname_sid(p)
21392 char_u *p;
21393{
21394 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21395}
21396
21397/*
21398 * List the head of the function: "name(arg1, arg2)".
21399 */
21400 static void
21401list_func_head(fp, indent)
21402 ufunc_T *fp;
21403 int indent;
21404{
21405 int j;
21406
21407 msg_start();
21408 if (indent)
21409 MSG_PUTS(" ");
21410 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021411 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 {
21413 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021414 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 }
21416 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021417 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021419 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 {
21421 if (j)
21422 MSG_PUTS(", ");
21423 msg_puts(FUNCARG(fp, j));
21424 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021425 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426 {
21427 if (j)
21428 MSG_PUTS(", ");
21429 MSG_PUTS("...");
21430 }
21431 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021432 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021433 if (p_verbose > 0)
21434 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435}
21436
21437/*
21438 * Find a function by name, return pointer to it in ufuncs.
21439 * Return NULL for unknown function.
21440 */
21441 static ufunc_T *
21442find_func(name)
21443 char_u *name;
21444{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021445 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021447 hi = hash_find(&func_hashtab, name);
21448 if (!HASHITEM_EMPTY(hi))
21449 return HI2UF(hi);
21450 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451}
21452
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021453#if defined(EXITFREE) || defined(PROTO)
21454 void
21455free_all_functions()
21456{
21457 hashitem_T *hi;
21458
21459 /* Need to start all over every time, because func_free() may change the
21460 * hash table. */
21461 while (func_hashtab.ht_used > 0)
21462 for (hi = func_hashtab.ht_array; ; ++hi)
21463 if (!HASHITEM_EMPTY(hi))
21464 {
21465 func_free(HI2UF(hi));
21466 break;
21467 }
21468}
21469#endif
21470
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021471/*
21472 * Return TRUE if a function "name" exists.
21473 */
21474 static int
21475function_exists(name)
21476 char_u *name;
21477{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021478 char_u *nm = name;
21479 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021480 int n = FALSE;
21481
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021482 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021483 nm = skipwhite(nm);
21484
21485 /* Only accept "funcname", "funcname ", "funcname (..." and
21486 * "funcname(...", not "funcname!...". */
21487 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021488 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021489 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021490 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021491 else
21492 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021493 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021494 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021495 return n;
21496}
21497
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021498/*
21499 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021500 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021501 */
21502 static int
21503builtin_function(name)
21504 char_u *name;
21505{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021506 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21507 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021508}
21509
Bram Moolenaar05159a02005-02-26 23:04:13 +000021510#if defined(FEAT_PROFILE) || defined(PROTO)
21511/*
21512 * Start profiling function "fp".
21513 */
21514 static void
21515func_do_profile(fp)
21516 ufunc_T *fp;
21517{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021518 int len = fp->uf_lines.ga_len;
21519
21520 if (len == 0)
21521 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021522 fp->uf_tm_count = 0;
21523 profile_zero(&fp->uf_tm_self);
21524 profile_zero(&fp->uf_tm_total);
21525 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021526 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021527 if (fp->uf_tml_total == NULL)
21528 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021529 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021530 if (fp->uf_tml_self == NULL)
21531 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021532 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021533 fp->uf_tml_idx = -1;
21534 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21535 || fp->uf_tml_self == NULL)
21536 return; /* out of memory */
21537
21538 fp->uf_profiling = TRUE;
21539}
21540
21541/*
21542 * Dump the profiling results for all functions in file "fd".
21543 */
21544 void
21545func_dump_profile(fd)
21546 FILE *fd;
21547{
21548 hashitem_T *hi;
21549 int todo;
21550 ufunc_T *fp;
21551 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021552 ufunc_T **sorttab;
21553 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021554
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021555 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021556 if (todo == 0)
21557 return; /* nothing to dump */
21558
Bram Moolenaar73830342005-02-28 22:48:19 +000021559 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21560
Bram Moolenaar05159a02005-02-26 23:04:13 +000021561 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21562 {
21563 if (!HASHITEM_EMPTY(hi))
21564 {
21565 --todo;
21566 fp = HI2UF(hi);
21567 if (fp->uf_profiling)
21568 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021569 if (sorttab != NULL)
21570 sorttab[st_len++] = fp;
21571
Bram Moolenaar05159a02005-02-26 23:04:13 +000021572 if (fp->uf_name[0] == K_SPECIAL)
21573 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21574 else
21575 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21576 if (fp->uf_tm_count == 1)
21577 fprintf(fd, "Called 1 time\n");
21578 else
21579 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21580 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21581 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21582 fprintf(fd, "\n");
21583 fprintf(fd, "count total (s) self (s)\n");
21584
21585 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21586 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021587 if (FUNCLINE(fp, i) == NULL)
21588 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021589 prof_func_line(fd, fp->uf_tml_count[i],
21590 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021591 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21592 }
21593 fprintf(fd, "\n");
21594 }
21595 }
21596 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021597
21598 if (sorttab != NULL && st_len > 0)
21599 {
21600 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21601 prof_total_cmp);
21602 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21603 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21604 prof_self_cmp);
21605 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21606 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021607
21608 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021609}
Bram Moolenaar73830342005-02-28 22:48:19 +000021610
21611 static void
21612prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21613 FILE *fd;
21614 ufunc_T **sorttab;
21615 int st_len;
21616 char *title;
21617 int prefer_self; /* when equal print only self time */
21618{
21619 int i;
21620 ufunc_T *fp;
21621
21622 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21623 fprintf(fd, "count total (s) self (s) function\n");
21624 for (i = 0; i < 20 && i < st_len; ++i)
21625 {
21626 fp = sorttab[i];
21627 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21628 prefer_self);
21629 if (fp->uf_name[0] == K_SPECIAL)
21630 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21631 else
21632 fprintf(fd, " %s()\n", fp->uf_name);
21633 }
21634 fprintf(fd, "\n");
21635}
21636
21637/*
21638 * Print the count and times for one function or function line.
21639 */
21640 static void
21641prof_func_line(fd, count, total, self, prefer_self)
21642 FILE *fd;
21643 int count;
21644 proftime_T *total;
21645 proftime_T *self;
21646 int prefer_self; /* when equal print only self time */
21647{
21648 if (count > 0)
21649 {
21650 fprintf(fd, "%5d ", count);
21651 if (prefer_self && profile_equal(total, self))
21652 fprintf(fd, " ");
21653 else
21654 fprintf(fd, "%s ", profile_msg(total));
21655 if (!prefer_self && profile_equal(total, self))
21656 fprintf(fd, " ");
21657 else
21658 fprintf(fd, "%s ", profile_msg(self));
21659 }
21660 else
21661 fprintf(fd, " ");
21662}
21663
21664/*
21665 * Compare function for total time sorting.
21666 */
21667 static int
21668#ifdef __BORLANDC__
21669_RTLENTRYF
21670#endif
21671prof_total_cmp(s1, s2)
21672 const void *s1;
21673 const void *s2;
21674{
21675 ufunc_T *p1, *p2;
21676
21677 p1 = *(ufunc_T **)s1;
21678 p2 = *(ufunc_T **)s2;
21679 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21680}
21681
21682/*
21683 * Compare function for self time sorting.
21684 */
21685 static int
21686#ifdef __BORLANDC__
21687_RTLENTRYF
21688#endif
21689prof_self_cmp(s1, s2)
21690 const void *s1;
21691 const void *s2;
21692{
21693 ufunc_T *p1, *p2;
21694
21695 p1 = *(ufunc_T **)s1;
21696 p2 = *(ufunc_T **)s2;
21697 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21698}
21699
Bram Moolenaar05159a02005-02-26 23:04:13 +000021700#endif
21701
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021702/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021703 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021704 * Return TRUE if a package was loaded.
21705 */
21706 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021707script_autoload(name, reload)
21708 char_u *name;
21709 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021710{
21711 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021712 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021713 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021714 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021715
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021716 /* Return quickly when autoload disabled. */
21717 if (no_autoload)
21718 return FALSE;
21719
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021720 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021721 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021722 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021723 return FALSE;
21724
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021725 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021726
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021727 /* Find the name in the list of previously loaded package names. Skip
21728 * "autoload/", it's always the same. */
21729 for (i = 0; i < ga_loaded.ga_len; ++i)
21730 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21731 break;
21732 if (!reload && i < ga_loaded.ga_len)
21733 ret = FALSE; /* was loaded already */
21734 else
21735 {
21736 /* Remember the name if it wasn't loaded already. */
21737 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21738 {
21739 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21740 tofree = NULL;
21741 }
21742
21743 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021744 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021745 ret = TRUE;
21746 }
21747
21748 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021749 return ret;
21750}
21751
21752/*
21753 * Return the autoload script name for a function or variable name.
21754 * Returns NULL when out of memory.
21755 */
21756 static char_u *
21757autoload_name(name)
21758 char_u *name;
21759{
21760 char_u *p;
21761 char_u *scriptname;
21762
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021763 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021764 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21765 if (scriptname == NULL)
21766 return FALSE;
21767 STRCPY(scriptname, "autoload/");
21768 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021769 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021770 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021771 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021772 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021773 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021774}
21775
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21777
21778/*
21779 * Function given to ExpandGeneric() to obtain the list of user defined
21780 * function names.
21781 */
21782 char_u *
21783get_user_func_name(xp, idx)
21784 expand_T *xp;
21785 int idx;
21786{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021787 static long_u done;
21788 static hashitem_T *hi;
21789 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790
21791 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021793 done = 0;
21794 hi = func_hashtab.ht_array;
21795 }
21796 if (done < func_hashtab.ht_used)
21797 {
21798 if (done++ > 0)
21799 ++hi;
21800 while (HASHITEM_EMPTY(hi))
21801 ++hi;
21802 fp = HI2UF(hi);
21803
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010021804 if (fp->uf_flags & FC_DICT)
21805 return NULL; /* don't show dict functions */
21806
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021807 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21808 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809
21810 cat_func_name(IObuff, fp);
21811 if (xp->xp_context != EXPAND_USER_FUNC)
21812 {
21813 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021814 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815 STRCAT(IObuff, ")");
21816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 return IObuff;
21818 }
21819 return NULL;
21820}
21821
21822#endif /* FEAT_CMDL_COMPL */
21823
21824/*
21825 * Copy the function name of "fp" to buffer "buf".
21826 * "buf" must be able to hold the function name plus three bytes.
21827 * Takes care of script-local function names.
21828 */
21829 static void
21830cat_func_name(buf, fp)
21831 char_u *buf;
21832 ufunc_T *fp;
21833{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021834 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 {
21836 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021837 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 }
21839 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021840 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841}
21842
21843/*
21844 * ":delfunction {name}"
21845 */
21846 void
21847ex_delfunction(eap)
21848 exarg_T *eap;
21849{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021850 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 char_u *p;
21852 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021853 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854
21855 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021856 name = trans_function_name(&p, eap->skip, 0, &fudi);
21857 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021859 {
21860 if (fudi.fd_dict != NULL && !eap->skip)
21861 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 if (!ends_excmd(*skipwhite(p)))
21865 {
21866 vim_free(name);
21867 EMSG(_(e_trailing));
21868 return;
21869 }
21870 eap->nextcmd = check_nextcmd(p);
21871 if (eap->nextcmd != NULL)
21872 *p = NUL;
21873
21874 if (!eap->skip)
21875 fp = find_func(name);
21876 vim_free(name);
21877
21878 if (!eap->skip)
21879 {
21880 if (fp == NULL)
21881 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021882 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 return;
21884 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021885 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 {
21887 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21888 return;
21889 }
21890
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021891 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021893 /* Delete the dict item that refers to the function, it will
21894 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021895 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021897 else
21898 func_free(fp);
21899 }
21900}
21901
21902/*
21903 * Free a function and remove it from the list of functions.
21904 */
21905 static void
21906func_free(fp)
21907 ufunc_T *fp;
21908{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021909 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021910
21911 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021912 ga_clear_strings(&(fp->uf_args));
21913 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021914#ifdef FEAT_PROFILE
21915 vim_free(fp->uf_tml_count);
21916 vim_free(fp->uf_tml_total);
21917 vim_free(fp->uf_tml_self);
21918#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021919
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021920 /* remove the function from the function hashtable */
21921 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21922 if (HASHITEM_EMPTY(hi))
21923 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021924 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021925 hash_remove(&func_hashtab, hi);
21926
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021927 vim_free(fp);
21928}
21929
21930/*
21931 * Unreference a Function: decrement the reference count and free it when it
21932 * becomes zero. Only for numbered functions.
21933 */
21934 static void
21935func_unref(name)
21936 char_u *name;
21937{
21938 ufunc_T *fp;
21939
21940 if (name != NULL && isdigit(*name))
21941 {
21942 fp = find_func(name);
21943 if (fp == NULL)
21944 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021945 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021946 {
21947 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021948 * when "uf_calls" becomes zero. */
21949 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021950 func_free(fp);
21951 }
21952 }
21953}
21954
21955/*
21956 * Count a reference to a Function.
21957 */
21958 static void
21959func_ref(name)
21960 char_u *name;
21961{
21962 ufunc_T *fp;
21963
21964 if (name != NULL && isdigit(*name))
21965 {
21966 fp = find_func(name);
21967 if (fp == NULL)
21968 EMSG2(_(e_intern2), "func_ref()");
21969 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021970 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971 }
21972}
21973
21974/*
21975 * Call a user function.
21976 */
21977 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021978call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979 ufunc_T *fp; /* pointer to function */
21980 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021981 typval_T *argvars; /* arguments */
21982 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 linenr_T firstline; /* first line of range */
21984 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021985 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986{
Bram Moolenaar33570922005-01-25 22:26:29 +000021987 char_u *save_sourcing_name;
21988 linenr_T save_sourcing_lnum;
21989 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021990 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021991 int save_did_emsg;
21992 static int depth = 0;
21993 dictitem_T *v;
21994 int fixvar_idx = 0; /* index in fixvar[] */
21995 int i;
21996 int ai;
21997 char_u numbuf[NUMBUFLEN];
21998 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021999#ifdef FEAT_PROFILE
22000 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022001 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003
22004 /* If depth of calling is getting too high, don't execute the function */
22005 if (depth >= p_mfd)
22006 {
22007 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022008 rettv->v_type = VAR_NUMBER;
22009 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 return;
22011 }
22012 ++depth;
22013
22014 line_breakcheck(); /* check for CTRL-C hit */
22015
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022016 fc = (funccall_T *)alloc(sizeof(funccall_T));
22017 fc->caller = current_funccal;
22018 current_funccal = fc;
22019 fc->func = fp;
22020 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022021 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022022 fc->linenr = 0;
22023 fc->returned = FALSE;
22024 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022026 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22027 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028
Bram Moolenaar33570922005-01-25 22:26:29 +000022029 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022030 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022031 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22032 * each argument variable and saves a lot of time.
22033 */
22034 /*
22035 * Init l: variables.
22036 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022037 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000022038 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022039 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022040 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22041 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022042 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022043 name = v->di_key;
22044 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022045 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022046 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022047 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022048 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022049 v->di_tv.vval.v_dict = selfdict;
22050 ++selfdict->dv_refcount;
22051 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022052
Bram Moolenaar33570922005-01-25 22:26:29 +000022053 /*
22054 * Init a: variables.
22055 * Set a:0 to "argcount".
22056 * Set a:000 to a list with room for the "..." arguments.
22057 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022058 init_var_dict(&fc->l_avars, &fc->l_avars_var);
22059 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022060 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022061 /* Use "name" to avoid a warning from some compiler that checks the
22062 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022063 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022064 name = v->di_key;
22065 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022066 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022067 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022068 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022069 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022070 v->di_tv.vval.v_list = &fc->l_varlist;
22071 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22072 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22073 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022074
22075 /*
22076 * Set a:firstline to "firstline" and a:lastline to "lastline".
22077 * Set a:name to named arguments.
22078 * Set a:N to the "..." arguments.
22079 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022080 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022081 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022082 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022083 (varnumber_T)lastline);
22084 for (i = 0; i < argcount; ++i)
22085 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022086 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022087 if (ai < 0)
22088 /* named argument a:name */
22089 name = FUNCARG(fp, i);
22090 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022091 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022092 /* "..." argument a:1, a:2, etc. */
22093 sprintf((char *)numbuf, "%d", ai + 1);
22094 name = numbuf;
22095 }
22096 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22097 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022098 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022099 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22100 }
22101 else
22102 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022103 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22104 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022105 if (v == NULL)
22106 break;
22107 v->di_flags = DI_FLAGS_RO;
22108 }
22109 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022110 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022111
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022112 /* Note: the values are copied directly to avoid alloc/free.
22113 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022114 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022115 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022116
22117 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22118 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022119 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22120 fc->l_listitems[ai].li_tv = argvars[i];
22121 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022122 }
22123 }
22124
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125 /* Don't redraw while executing the function. */
22126 ++RedrawingDisabled;
22127 save_sourcing_name = sourcing_name;
22128 save_sourcing_lnum = sourcing_lnum;
22129 sourcing_lnum = 1;
22130 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022131 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 if (sourcing_name != NULL)
22133 {
22134 if (save_sourcing_name != NULL
22135 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22136 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22137 else
22138 STRCPY(sourcing_name, "function ");
22139 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22140
22141 if (p_verbose >= 12)
22142 {
22143 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022144 verbose_enter_scroll();
22145
Bram Moolenaar555b2802005-05-19 21:08:39 +000022146 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147 if (p_verbose >= 14)
22148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022150 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022151 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022152 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153
22154 msg_puts((char_u *)"(");
22155 for (i = 0; i < argcount; ++i)
22156 {
22157 if (i > 0)
22158 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022159 if (argvars[i].v_type == VAR_NUMBER)
22160 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 else
22162 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022163 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22164 if (s != NULL)
22165 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022166 if (vim_strsize(s) > MSG_BUF_CLEN)
22167 {
22168 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22169 s = buf;
22170 }
22171 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022172 vim_free(tofree);
22173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 }
22175 }
22176 msg_puts((char_u *)")");
22177 }
22178 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022179
22180 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181 --no_wait_return;
22182 }
22183 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022184#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022185 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022186 {
22187 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22188 func_do_profile(fp);
22189 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022190 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022191 {
22192 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022193 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022194 profile_zero(&fp->uf_tm_children);
22195 }
22196 script_prof_save(&wait_start);
22197 }
22198#endif
22199
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022201 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 save_did_emsg = did_emsg;
22203 did_emsg = FALSE;
22204
22205 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022206 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22208
22209 --RedrawingDisabled;
22210
22211 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022212 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022214 clear_tv(rettv);
22215 rettv->v_type = VAR_NUMBER;
22216 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 }
22218
Bram Moolenaar05159a02005-02-26 23:04:13 +000022219#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022220 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022221 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022222 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022223 profile_end(&call_start);
22224 profile_sub_wait(&wait_start, &call_start);
22225 profile_add(&fp->uf_tm_total, &call_start);
22226 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022227 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022228 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022229 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22230 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022231 }
22232 }
22233#endif
22234
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235 /* when being verbose, mention the return value */
22236 if (p_verbose >= 12)
22237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022239 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022242 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022243 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022244 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022245 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022246 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022248 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022249 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022250 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022251 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022252
Bram Moolenaar555b2802005-05-19 21:08:39 +000022253 /* The value may be very long. Skip the middle part, so that we
22254 * have some idea how it starts and ends. smsg() would always
22255 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022256 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022257 if (s != NULL)
22258 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022259 if (vim_strsize(s) > MSG_BUF_CLEN)
22260 {
22261 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22262 s = buf;
22263 }
22264 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022265 vim_free(tofree);
22266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267 }
22268 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022269
22270 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271 --no_wait_return;
22272 }
22273
22274 vim_free(sourcing_name);
22275 sourcing_name = save_sourcing_name;
22276 sourcing_lnum = save_sourcing_lnum;
22277 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022278#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022279 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022280 script_prof_restore(&wait_start);
22281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282
22283 if (p_verbose >= 12 && sourcing_name != NULL)
22284 {
22285 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022286 verbose_enter_scroll();
22287
Bram Moolenaar555b2802005-05-19 21:08:39 +000022288 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022290
22291 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 --no_wait_return;
22293 }
22294
22295 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022296 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022298
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022299 /* If the a:000 list and the l: and a: dicts are not referenced we can
22300 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022301 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22302 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22303 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22304 {
22305 free_funccal(fc, FALSE);
22306 }
22307 else
22308 {
22309 hashitem_T *hi;
22310 listitem_T *li;
22311 int todo;
22312
22313 /* "fc" is still in use. This can happen when returning "a:000" or
22314 * assigning "l:" to a global variable.
22315 * Link "fc" in the list for garbage collection later. */
22316 fc->caller = previous_funccal;
22317 previous_funccal = fc;
22318
22319 /* Make a copy of the a: variables, since we didn't do that above. */
22320 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22321 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22322 {
22323 if (!HASHITEM_EMPTY(hi))
22324 {
22325 --todo;
22326 v = HI2DI(hi);
22327 copy_tv(&v->di_tv, &v->di_tv);
22328 }
22329 }
22330
22331 /* Make a copy of the a:000 items, since we didn't do that above. */
22332 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22333 copy_tv(&li->li_tv, &li->li_tv);
22334 }
22335}
22336
22337/*
22338 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022339 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022340 */
22341 static int
22342can_free_funccal(fc, copyID)
22343 funccall_T *fc;
22344 int copyID;
22345{
22346 return (fc->l_varlist.lv_copyID != copyID
22347 && fc->l_vars.dv_copyID != copyID
22348 && fc->l_avars.dv_copyID != copyID);
22349}
22350
22351/*
22352 * Free "fc" and what it contains.
22353 */
22354 static void
22355free_funccal(fc, free_val)
22356 funccall_T *fc;
22357 int free_val; /* a: vars were allocated */
22358{
22359 listitem_T *li;
22360
22361 /* The a: variables typevals may not have been allocated, only free the
22362 * allocated variables. */
22363 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22364
22365 /* free all l: variables */
22366 vars_clear(&fc->l_vars.dv_hashtab);
22367
22368 /* Free the a:000 variables if they were allocated. */
22369 if (free_val)
22370 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22371 clear_tv(&li->li_tv);
22372
22373 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374}
22375
22376/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022377 * Add a number variable "name" to dict "dp" with value "nr".
22378 */
22379 static void
22380add_nr_var(dp, v, name, nr)
22381 dict_T *dp;
22382 dictitem_T *v;
22383 char *name;
22384 varnumber_T nr;
22385{
22386 STRCPY(v->di_key, name);
22387 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22388 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22389 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022390 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022391 v->di_tv.vval.v_number = nr;
22392}
22393
22394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 * ":return [expr]"
22396 */
22397 void
22398ex_return(eap)
22399 exarg_T *eap;
22400{
22401 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022402 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 int returning = FALSE;
22404
22405 if (current_funccal == NULL)
22406 {
22407 EMSG(_("E133: :return not inside a function"));
22408 return;
22409 }
22410
22411 if (eap->skip)
22412 ++emsg_skip;
22413
22414 eap->nextcmd = NULL;
22415 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022416 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417 {
22418 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022419 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022421 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 }
22423 /* It's safer to return also on error. */
22424 else if (!eap->skip)
22425 {
22426 /*
22427 * Return unless the expression evaluation has been cancelled due to an
22428 * aborting error, an interrupt, or an exception.
22429 */
22430 if (!aborting())
22431 returning = do_return(eap, FALSE, TRUE, NULL);
22432 }
22433
22434 /* When skipping or the return gets pending, advance to the next command
22435 * in this line (!returning). Otherwise, ignore the rest of the line.
22436 * Following lines will be ignored by get_func_line(). */
22437 if (returning)
22438 eap->nextcmd = NULL;
22439 else if (eap->nextcmd == NULL) /* no argument */
22440 eap->nextcmd = check_nextcmd(arg);
22441
22442 if (eap->skip)
22443 --emsg_skip;
22444}
22445
22446/*
22447 * Return from a function. Possibly makes the return pending. Also called
22448 * for a pending return at the ":endtry" or after returning from an extra
22449 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022450 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022451 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 * FALSE when the return gets pending.
22453 */
22454 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022455do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 exarg_T *eap;
22457 int reanimate;
22458 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022459 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460{
22461 int idx;
22462 struct condstack *cstack = eap->cstack;
22463
22464 if (reanimate)
22465 /* Undo the return. */
22466 current_funccal->returned = FALSE;
22467
22468 /*
22469 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22470 * not in its finally clause (which then is to be executed next) is found.
22471 * In this case, make the ":return" pending for execution at the ":endtry".
22472 * Otherwise, return normally.
22473 */
22474 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22475 if (idx >= 0)
22476 {
22477 cstack->cs_pending[idx] = CSTP_RETURN;
22478
22479 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022480 /* A pending return again gets pending. "rettv" points to an
22481 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022483 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 else
22485 {
22486 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022487 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022489 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022491 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 {
22493 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022494 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022495 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 else
22497 EMSG(_(e_outofmem));
22498 }
22499 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022500 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501
22502 if (reanimate)
22503 {
22504 /* The pending return value could be overwritten by a ":return"
22505 * without argument in a finally clause; reset the default
22506 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022507 current_funccal->rettv->v_type = VAR_NUMBER;
22508 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 }
22510 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022511 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 }
22513 else
22514 {
22515 current_funccal->returned = TRUE;
22516
22517 /* If the return is carried out now, store the return value. For
22518 * a return immediately after reanimation, the value is already
22519 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022520 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022522 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022523 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022524 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022525 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 }
22527 }
22528
22529 return idx < 0;
22530}
22531
22532/*
22533 * Free the variable with a pending return value.
22534 */
22535 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022536discard_pending_return(rettv)
22537 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538{
Bram Moolenaar33570922005-01-25 22:26:29 +000022539 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540}
22541
22542/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022543 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 * is an allocated string. Used by report_pending() for verbose messages.
22545 */
22546 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022547get_return_cmd(rettv)
22548 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022550 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022551 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022552 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022554 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022555 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022556 if (s == NULL)
22557 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022558
22559 STRCPY(IObuff, ":return ");
22560 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22561 if (STRLEN(s) + 8 >= IOSIZE)
22562 STRCPY(IObuff + IOSIZE - 4, "...");
22563 vim_free(tofree);
22564 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565}
22566
22567/*
22568 * Get next function line.
22569 * Called by do_cmdline() to get the next line.
22570 * Returns allocated string, or NULL for end of function.
22571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 char_u *
22573get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022574 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022576 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577{
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022579 ufunc_T *fp = fcp->func;
22580 char_u *retval;
22581 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582
22583 /* If breakpoints have been added/deleted need to check for it. */
22584 if (fcp->dbg_tick != debug_tick)
22585 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022586 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587 sourcing_lnum);
22588 fcp->dbg_tick = debug_tick;
22589 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022590#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022591 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022592 func_line_end(cookie);
22593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594
Bram Moolenaar05159a02005-02-26 23:04:13 +000022595 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022596 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22597 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 retval = NULL;
22599 else
22600 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022601 /* Skip NULL lines (continuation lines). */
22602 while (fcp->linenr < gap->ga_len
22603 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22604 ++fcp->linenr;
22605 if (fcp->linenr >= gap->ga_len)
22606 retval = NULL;
22607 else
22608 {
22609 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22610 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022611#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022612 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022613 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022614#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 }
22617
22618 /* Did we encounter a breakpoint? */
22619 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22620 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022621 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022623 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 sourcing_lnum);
22625 fcp->dbg_tick = debug_tick;
22626 }
22627
22628 return retval;
22629}
22630
Bram Moolenaar05159a02005-02-26 23:04:13 +000022631#if defined(FEAT_PROFILE) || defined(PROTO)
22632/*
22633 * Called when starting to read a function line.
22634 * "sourcing_lnum" must be correct!
22635 * When skipping lines it may not actually be executed, but we won't find out
22636 * until later and we need to store the time now.
22637 */
22638 void
22639func_line_start(cookie)
22640 void *cookie;
22641{
22642 funccall_T *fcp = (funccall_T *)cookie;
22643 ufunc_T *fp = fcp->func;
22644
22645 if (fp->uf_profiling && sourcing_lnum >= 1
22646 && sourcing_lnum <= fp->uf_lines.ga_len)
22647 {
22648 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022649 /* Skip continuation lines. */
22650 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22651 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022652 fp->uf_tml_execed = FALSE;
22653 profile_start(&fp->uf_tml_start);
22654 profile_zero(&fp->uf_tml_children);
22655 profile_get_wait(&fp->uf_tml_wait);
22656 }
22657}
22658
22659/*
22660 * Called when actually executing a function line.
22661 */
22662 void
22663func_line_exec(cookie)
22664 void *cookie;
22665{
22666 funccall_T *fcp = (funccall_T *)cookie;
22667 ufunc_T *fp = fcp->func;
22668
22669 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22670 fp->uf_tml_execed = TRUE;
22671}
22672
22673/*
22674 * Called when done with a function line.
22675 */
22676 void
22677func_line_end(cookie)
22678 void *cookie;
22679{
22680 funccall_T *fcp = (funccall_T *)cookie;
22681 ufunc_T *fp = fcp->func;
22682
22683 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22684 {
22685 if (fp->uf_tml_execed)
22686 {
22687 ++fp->uf_tml_count[fp->uf_tml_idx];
22688 profile_end(&fp->uf_tml_start);
22689 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022690 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022691 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22692 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022693 }
22694 fp->uf_tml_idx = -1;
22695 }
22696}
22697#endif
22698
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699/*
22700 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022701 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 */
22703 int
22704func_has_ended(cookie)
22705 void *cookie;
22706{
Bram Moolenaar33570922005-01-25 22:26:29 +000022707 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708
22709 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22710 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022711 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 || fcp->returned);
22713}
22714
22715/*
22716 * return TRUE if cookie indicates a function which "abort"s on errors.
22717 */
22718 int
22719func_has_abort(cookie)
22720 void *cookie;
22721{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022722 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723}
22724
22725#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22726typedef enum
22727{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022728 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22729 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22730 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731} var_flavour_T;
22732
22733static var_flavour_T var_flavour __ARGS((char_u *varname));
22734
22735 static var_flavour_T
22736var_flavour(varname)
22737 char_u *varname;
22738{
22739 char_u *p = varname;
22740
22741 if (ASCII_ISUPPER(*p))
22742 {
22743 while (*(++p))
22744 if (ASCII_ISLOWER(*p))
22745 return VAR_FLAVOUR_SESSION;
22746 return VAR_FLAVOUR_VIMINFO;
22747 }
22748 else
22749 return VAR_FLAVOUR_DEFAULT;
22750}
22751#endif
22752
22753#if defined(FEAT_VIMINFO) || defined(PROTO)
22754/*
22755 * Restore global vars that start with a capital from the viminfo file
22756 */
22757 int
22758read_viminfo_varlist(virp, writing)
22759 vir_T *virp;
22760 int writing;
22761{
22762 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022763 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022764 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765
22766 if (!writing && (find_viminfo_parameter('!') != NULL))
22767 {
22768 tab = vim_strchr(virp->vir_line + 1, '\t');
22769 if (tab != NULL)
22770 {
22771 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022772 switch (*tab)
22773 {
22774 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022775#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022776 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022777#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022778 case 'D': type = VAR_DICT; break;
22779 case 'L': type = VAR_LIST; break;
22780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781
22782 tab = vim_strchr(tab, '\t');
22783 if (tab != NULL)
22784 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022785 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022786 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022787 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022789#ifdef FEAT_FLOAT
22790 else if (type == VAR_FLOAT)
22791 (void)string2float(tab + 1, &tv.vval.v_float);
22792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022794 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022795 if (type == VAR_DICT || type == VAR_LIST)
22796 {
22797 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22798
22799 if (etv == NULL)
22800 /* Failed to parse back the dict or list, use it as a
22801 * string. */
22802 tv.v_type = VAR_STRING;
22803 else
22804 {
22805 vim_free(tv.vval.v_string);
22806 tv = *etv;
22807 }
22808 }
22809
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022810 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022811
22812 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022813 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022814 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22815 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816 }
22817 }
22818 }
22819
22820 return viminfo_readline(virp);
22821}
22822
22823/*
22824 * Write global vars that start with a capital to the viminfo file
22825 */
22826 void
22827write_viminfo_varlist(fp)
22828 FILE *fp;
22829{
Bram Moolenaar33570922005-01-25 22:26:29 +000022830 hashitem_T *hi;
22831 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022832 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022833 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022834 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022835 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022836 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837
22838 if (find_viminfo_parameter('!') == NULL)
22839 return;
22840
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022841 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022842
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022843 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022844 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022846 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022848 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022849 this_var = HI2DI(hi);
22850 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022851 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022852 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022853 {
22854 case VAR_STRING: s = "STR"; break;
22855 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022856#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022857 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022858#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022859 case VAR_DICT: s = "DIC"; break;
22860 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022861 default: continue;
22862 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022863 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022864 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022865 if (p != NULL)
22866 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022867 vim_free(tofree);
22868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 }
22870 }
22871}
22872#endif
22873
22874#if defined(FEAT_SESSION) || defined(PROTO)
22875 int
22876store_session_globals(fd)
22877 FILE *fd;
22878{
Bram Moolenaar33570922005-01-25 22:26:29 +000022879 hashitem_T *hi;
22880 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022881 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 char_u *p, *t;
22883
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022884 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022885 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022887 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022889 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022890 this_var = HI2DI(hi);
22891 if ((this_var->di_tv.v_type == VAR_NUMBER
22892 || this_var->di_tv.v_type == VAR_STRING)
22893 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022894 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022895 /* Escape special characters with a backslash. Turn a LF and
22896 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022897 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022898 (char_u *)"\\\"\n\r");
22899 if (p == NULL) /* out of memory */
22900 break;
22901 for (t = p; *t != NUL; ++t)
22902 if (*t == '\n')
22903 *t = 'n';
22904 else if (*t == '\r')
22905 *t = 'r';
22906 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022907 this_var->di_key,
22908 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22909 : ' ',
22910 p,
22911 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22912 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022913 || put_eol(fd) == FAIL)
22914 {
22915 vim_free(p);
22916 return FAIL;
22917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 vim_free(p);
22919 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022920#ifdef FEAT_FLOAT
22921 else if (this_var->di_tv.v_type == VAR_FLOAT
22922 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22923 {
22924 float_T f = this_var->di_tv.vval.v_float;
22925 int sign = ' ';
22926
22927 if (f < 0)
22928 {
22929 f = -f;
22930 sign = '-';
22931 }
22932 if ((fprintf(fd, "let %s = %c&%f",
22933 this_var->di_key, sign, f) < 0)
22934 || put_eol(fd) == FAIL)
22935 return FAIL;
22936 }
22937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 }
22939 }
22940 return OK;
22941}
22942#endif
22943
Bram Moolenaar661b1822005-07-28 22:36:45 +000022944/*
22945 * Display script name where an item was last set.
22946 * Should only be invoked when 'verbose' is non-zero.
22947 */
22948 void
22949last_set_msg(scriptID)
22950 scid_T scriptID;
22951{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022952 char_u *p;
22953
Bram Moolenaar661b1822005-07-28 22:36:45 +000022954 if (scriptID != 0)
22955 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022956 p = home_replace_save(NULL, get_scriptname(scriptID));
22957 if (p != NULL)
22958 {
22959 verbose_enter();
22960 MSG_PUTS(_("\n\tLast set from "));
22961 MSG_PUTS(p);
22962 vim_free(p);
22963 verbose_leave();
22964 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022965 }
22966}
22967
Bram Moolenaard812df62008-11-09 12:46:09 +000022968/*
22969 * List v:oldfiles in a nice way.
22970 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022971 void
22972ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022973 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022974{
22975 list_T *l = vimvars[VV_OLDFILES].vv_list;
22976 listitem_T *li;
22977 int nr = 0;
22978
22979 if (l == NULL)
22980 msg((char_u *)_("No old files"));
22981 else
22982 {
22983 msg_start();
22984 msg_scroll = TRUE;
22985 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22986 {
22987 msg_outnum((long)++nr);
22988 MSG_PUTS(": ");
22989 msg_outtrans(get_tv_string(&li->li_tv));
22990 msg_putchar('\n');
22991 out_flush(); /* output one line at a time */
22992 ui_breakcheck();
22993 }
22994 /* Assume "got_int" was set to truncate the listing. */
22995 got_int = FALSE;
22996
22997#ifdef FEAT_BROWSE_CMD
22998 if (cmdmod.browse)
22999 {
23000 quit_more = FALSE;
23001 nr = prompt_for_number(FALSE);
23002 msg_starthere();
23003 if (nr > 0)
23004 {
23005 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23006 (long)nr);
23007
23008 if (p != NULL)
23009 {
23010 p = expand_env_save(p);
23011 eap->arg = p;
23012 eap->cmdidx = CMD_edit;
23013 cmdmod.browse = FALSE;
23014 do_exedit(eap, NULL);
23015 vim_free(p);
23016 }
23017 }
23018 }
23019#endif
23020 }
23021}
23022
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023#endif /* FEAT_EVAL */
23024
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023026#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027
23028#ifdef WIN3264
23029/*
23030 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23031 */
23032static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23033static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23034static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23035
23036/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023037 * Get the short path (8.3) for the filename in "fnamep".
23038 * Only works for a valid file name.
23039 * When the path gets longer "fnamep" is changed and the allocated buffer
23040 * is put in "bufp".
23041 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23042 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 */
23044 static int
23045get_short_pathname(fnamep, bufp, fnamelen)
23046 char_u **fnamep;
23047 char_u **bufp;
23048 int *fnamelen;
23049{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023050 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051 char_u *newbuf;
23052
23053 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 l = GetShortPathName(*fnamep, *fnamep, len);
23055 if (l > len - 1)
23056 {
23057 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023058 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059 newbuf = vim_strnsave(*fnamep, l);
23060 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023061 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062
23063 vim_free(*bufp);
23064 *fnamep = *bufp = newbuf;
23065
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023066 /* Really should always succeed, as the buffer is big enough. */
23067 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 }
23069
23070 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023071 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072}
23073
23074/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023075 * Get the short path (8.3) for the filename in "fname". The converted
23076 * path is returned in "bufp".
23077 *
23078 * Some of the directories specified in "fname" may not exist. This function
23079 * will shorten the existing directories at the beginning of the path and then
23080 * append the remaining non-existing path.
23081 *
23082 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023083 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023084 * bufp - Pointer to an allocated buffer for the filename.
23085 * fnamelen - Length of the filename pointed to by fname
23086 *
23087 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088 */
23089 static int
23090shortpath_for_invalid_fname(fname, bufp, fnamelen)
23091 char_u **fname;
23092 char_u **bufp;
23093 int *fnamelen;
23094{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023095 char_u *short_fname, *save_fname, *pbuf_unused;
23096 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023098 int old_len, len;
23099 int new_len, sfx_len;
23100 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101
23102 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023103 old_len = *fnamelen;
23104 save_fname = vim_strnsave(*fname, old_len);
23105 pbuf_unused = NULL;
23106 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023107
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023108 endp = save_fname + old_len - 1; /* Find the end of the copy */
23109 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023111 /*
23112 * Try shortening the supplied path till it succeeds by removing one
23113 * directory at a time from the tail of the path.
23114 */
23115 len = 0;
23116 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023118 /* go back one path-separator */
23119 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23120 --endp;
23121 if (endp <= save_fname)
23122 break; /* processed the complete path */
23123
23124 /*
23125 * Replace the path separator with a NUL and try to shorten the
23126 * resulting path.
23127 */
23128 ch = *endp;
23129 *endp = 0;
23130 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023131 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023132 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23133 {
23134 retval = FAIL;
23135 goto theend;
23136 }
23137 *endp = ch; /* preserve the string */
23138
23139 if (len > 0)
23140 break; /* successfully shortened the path */
23141
23142 /* failed to shorten the path. Skip the path separator */
23143 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144 }
23145
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023146 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023148 /*
23149 * Succeeded in shortening the path. Now concatenate the shortened
23150 * path with the remaining path at the tail.
23151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023153 /* Compute the length of the new path. */
23154 sfx_len = (int)(save_endp - endp) + 1;
23155 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023157 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023159 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023161 /* There is not enough space in the currently allocated string,
23162 * copy it to a buffer big enough. */
23163 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023165 {
23166 retval = FAIL;
23167 goto theend;
23168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169 }
23170 else
23171 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023172 /* Transfer short_fname to the main buffer (it's big enough),
23173 * unless get_short_pathname() did its work in-place. */
23174 *fname = *bufp = save_fname;
23175 if (short_fname != save_fname)
23176 vim_strncpy(save_fname, short_fname, len);
23177 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023178 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023179
23180 /* concat the not-shortened part of the path */
23181 vim_strncpy(*fname + len, endp, sfx_len);
23182 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023184
23185theend:
23186 vim_free(pbuf_unused);
23187 vim_free(save_fname);
23188
23189 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190}
23191
23192/*
23193 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023194 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 */
23196 static int
23197shortpath_for_partial(fnamep, bufp, fnamelen)
23198 char_u **fnamep;
23199 char_u **bufp;
23200 int *fnamelen;
23201{
23202 int sepcount, len, tflen;
23203 char_u *p;
23204 char_u *pbuf, *tfname;
23205 int hasTilde;
23206
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023207 /* Count up the path separators from the RHS.. so we know which part
23208 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023210 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 if (vim_ispathsep(*p))
23212 ++sepcount;
23213
23214 /* Need full path first (use expand_env() to remove a "~/") */
23215 hasTilde = (**fnamep == '~');
23216 if (hasTilde)
23217 pbuf = tfname = expand_env_save(*fnamep);
23218 else
23219 pbuf = tfname = FullName_save(*fnamep, FALSE);
23220
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023221 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023223 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23224 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225
23226 if (len == 0)
23227 {
23228 /* Don't have a valid filename, so shorten the rest of the
23229 * path if we can. This CAN give us invalid 8.3 filenames, but
23230 * there's not a lot of point in guessing what it might be.
23231 */
23232 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023233 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23234 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235 }
23236
23237 /* Count the paths backward to find the beginning of the desired string. */
23238 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023239 {
23240#ifdef FEAT_MBYTE
23241 if (has_mbyte)
23242 p -= mb_head_off(tfname, p);
23243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244 if (vim_ispathsep(*p))
23245 {
23246 if (sepcount == 0 || (hasTilde && sepcount == 1))
23247 break;
23248 else
23249 sepcount --;
23250 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 if (hasTilde)
23253 {
23254 --p;
23255 if (p >= tfname)
23256 *p = '~';
23257 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023258 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023259 }
23260 else
23261 ++p;
23262
23263 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23264 vim_free(*bufp);
23265 *fnamelen = (int)STRLEN(p);
23266 *bufp = pbuf;
23267 *fnamep = p;
23268
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023269 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270}
23271#endif /* WIN3264 */
23272
23273/*
23274 * Adjust a filename, according to a string of modifiers.
23275 * *fnamep must be NUL terminated when called. When returning, the length is
23276 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023277 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023278 * When there is an error, *fnamep is set to NULL.
23279 */
23280 int
23281modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23282 char_u *src; /* string with modifiers */
23283 int *usedlen; /* characters after src that are used */
23284 char_u **fnamep; /* file name so far */
23285 char_u **bufp; /* buffer for allocated file name or NULL */
23286 int *fnamelen; /* length of fnamep */
23287{
23288 int valid = 0;
23289 char_u *tail;
23290 char_u *s, *p, *pbuf;
23291 char_u dirname[MAXPATHL];
23292 int c;
23293 int has_fullname = 0;
23294#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023295 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 int has_shortname = 0;
23297#endif
23298
23299repeat:
23300 /* ":p" - full path/file_name */
23301 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23302 {
23303 has_fullname = 1;
23304
23305 valid |= VALID_PATH;
23306 *usedlen += 2;
23307
23308 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23309 if ((*fnamep)[0] == '~'
23310#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23311 && ((*fnamep)[1] == '/'
23312# ifdef BACKSLASH_IN_FILENAME
23313 || (*fnamep)[1] == '\\'
23314# endif
23315 || (*fnamep)[1] == NUL)
23316
23317#endif
23318 )
23319 {
23320 *fnamep = expand_env_save(*fnamep);
23321 vim_free(*bufp); /* free any allocated file name */
23322 *bufp = *fnamep;
23323 if (*fnamep == NULL)
23324 return -1;
23325 }
23326
23327 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023328 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023329 {
23330 if (vim_ispathsep(*p)
23331 && p[1] == '.'
23332 && (p[2] == NUL
23333 || vim_ispathsep(p[2])
23334 || (p[2] == '.'
23335 && (p[3] == NUL || vim_ispathsep(p[3])))))
23336 break;
23337 }
23338
23339 /* FullName_save() is slow, don't use it when not needed. */
23340 if (*p != NUL || !vim_isAbsName(*fnamep))
23341 {
23342 *fnamep = FullName_save(*fnamep, *p != NUL);
23343 vim_free(*bufp); /* free any allocated file name */
23344 *bufp = *fnamep;
23345 if (*fnamep == NULL)
23346 return -1;
23347 }
23348
23349 /* Append a path separator to a directory. */
23350 if (mch_isdir(*fnamep))
23351 {
23352 /* Make room for one or two extra characters. */
23353 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23354 vim_free(*bufp); /* free any allocated file name */
23355 *bufp = *fnamep;
23356 if (*fnamep == NULL)
23357 return -1;
23358 add_pathsep(*fnamep);
23359 }
23360 }
23361
23362 /* ":." - path relative to the current directory */
23363 /* ":~" - path relative to the home directory */
23364 /* ":8" - shortname path - postponed till after */
23365 while (src[*usedlen] == ':'
23366 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23367 {
23368 *usedlen += 2;
23369 if (c == '8')
23370 {
23371#ifdef WIN3264
23372 has_shortname = 1; /* Postpone this. */
23373#endif
23374 continue;
23375 }
23376 pbuf = NULL;
23377 /* Need full path first (use expand_env() to remove a "~/") */
23378 if (!has_fullname)
23379 {
23380 if (c == '.' && **fnamep == '~')
23381 p = pbuf = expand_env_save(*fnamep);
23382 else
23383 p = pbuf = FullName_save(*fnamep, FALSE);
23384 }
23385 else
23386 p = *fnamep;
23387
23388 has_fullname = 0;
23389
23390 if (p != NULL)
23391 {
23392 if (c == '.')
23393 {
23394 mch_dirname(dirname, MAXPATHL);
23395 s = shorten_fname(p, dirname);
23396 if (s != NULL)
23397 {
23398 *fnamep = s;
23399 if (pbuf != NULL)
23400 {
23401 vim_free(*bufp); /* free any allocated file name */
23402 *bufp = pbuf;
23403 pbuf = NULL;
23404 }
23405 }
23406 }
23407 else
23408 {
23409 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23410 /* Only replace it when it starts with '~' */
23411 if (*dirname == '~')
23412 {
23413 s = vim_strsave(dirname);
23414 if (s != NULL)
23415 {
23416 *fnamep = s;
23417 vim_free(*bufp);
23418 *bufp = s;
23419 }
23420 }
23421 }
23422 vim_free(pbuf);
23423 }
23424 }
23425
23426 tail = gettail(*fnamep);
23427 *fnamelen = (int)STRLEN(*fnamep);
23428
23429 /* ":h" - head, remove "/file_name", can be repeated */
23430 /* Don't remove the first "/" or "c:\" */
23431 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23432 {
23433 valid |= VALID_HEAD;
23434 *usedlen += 2;
23435 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023436 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023437 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 *fnamelen = (int)(tail - *fnamep);
23439#ifdef VMS
23440 if (*fnamelen > 0)
23441 *fnamelen += 1; /* the path separator is part of the path */
23442#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023443 if (*fnamelen == 0)
23444 {
23445 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23446 p = vim_strsave((char_u *)".");
23447 if (p == NULL)
23448 return -1;
23449 vim_free(*bufp);
23450 *bufp = *fnamep = tail = p;
23451 *fnamelen = 1;
23452 }
23453 else
23454 {
23455 while (tail > s && !after_pathsep(s, tail))
23456 mb_ptr_back(*fnamep, tail);
23457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458 }
23459
23460 /* ":8" - shortname */
23461 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23462 {
23463 *usedlen += 2;
23464#ifdef WIN3264
23465 has_shortname = 1;
23466#endif
23467 }
23468
23469#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023470 /*
23471 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472 */
23473 if (has_shortname)
23474 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023475 /* Copy the string if it is shortened by :h and when it wasn't copied
23476 * yet, because we are going to change it in place. Avoids changing
23477 * the buffer name for "%:8". */
23478 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479 {
23480 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023481 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 return -1;
23483 vim_free(*bufp);
23484 *bufp = *fnamep = p;
23485 }
23486
23487 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023488 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 if (!has_fullname && !vim_isAbsName(*fnamep))
23490 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023491 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492 return -1;
23493 }
23494 else
23495 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023496 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497
Bram Moolenaardc935552011-08-17 15:23:23 +020023498 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023500 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 return -1;
23502
23503 if (l == 0)
23504 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023505 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023507 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508 return -1;
23509 }
23510 *fnamelen = l;
23511 }
23512 }
23513#endif /* WIN3264 */
23514
23515 /* ":t" - tail, just the basename */
23516 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23517 {
23518 *usedlen += 2;
23519 *fnamelen -= (int)(tail - *fnamep);
23520 *fnamep = tail;
23521 }
23522
23523 /* ":e" - extension, can be repeated */
23524 /* ":r" - root, without extension, can be repeated */
23525 while (src[*usedlen] == ':'
23526 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23527 {
23528 /* find a '.' in the tail:
23529 * - for second :e: before the current fname
23530 * - otherwise: The last '.'
23531 */
23532 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23533 s = *fnamep - 2;
23534 else
23535 s = *fnamep + *fnamelen - 1;
23536 for ( ; s > tail; --s)
23537 if (s[0] == '.')
23538 break;
23539 if (src[*usedlen + 1] == 'e') /* :e */
23540 {
23541 if (s > tail)
23542 {
23543 *fnamelen += (int)(*fnamep - (s + 1));
23544 *fnamep = s + 1;
23545#ifdef VMS
23546 /* cut version from the extension */
23547 s = *fnamep + *fnamelen - 1;
23548 for ( ; s > *fnamep; --s)
23549 if (s[0] == ';')
23550 break;
23551 if (s > *fnamep)
23552 *fnamelen = s - *fnamep;
23553#endif
23554 }
23555 else if (*fnamep <= tail)
23556 *fnamelen = 0;
23557 }
23558 else /* :r */
23559 {
23560 if (s > tail) /* remove one extension */
23561 *fnamelen = (int)(s - *fnamep);
23562 }
23563 *usedlen += 2;
23564 }
23565
23566 /* ":s?pat?foo?" - substitute */
23567 /* ":gs?pat?foo?" - global substitute */
23568 if (src[*usedlen] == ':'
23569 && (src[*usedlen + 1] == 's'
23570 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23571 {
23572 char_u *str;
23573 char_u *pat;
23574 char_u *sub;
23575 int sep;
23576 char_u *flags;
23577 int didit = FALSE;
23578
23579 flags = (char_u *)"";
23580 s = src + *usedlen + 2;
23581 if (src[*usedlen + 1] == 'g')
23582 {
23583 flags = (char_u *)"g";
23584 ++s;
23585 }
23586
23587 sep = *s++;
23588 if (sep)
23589 {
23590 /* find end of pattern */
23591 p = vim_strchr(s, sep);
23592 if (p != NULL)
23593 {
23594 pat = vim_strnsave(s, (int)(p - s));
23595 if (pat != NULL)
23596 {
23597 s = p + 1;
23598 /* find end of substitution */
23599 p = vim_strchr(s, sep);
23600 if (p != NULL)
23601 {
23602 sub = vim_strnsave(s, (int)(p - s));
23603 str = vim_strnsave(*fnamep, *fnamelen);
23604 if (sub != NULL && str != NULL)
23605 {
23606 *usedlen = (int)(p + 1 - src);
23607 s = do_string_sub(str, pat, sub, flags);
23608 if (s != NULL)
23609 {
23610 *fnamep = s;
23611 *fnamelen = (int)STRLEN(s);
23612 vim_free(*bufp);
23613 *bufp = s;
23614 didit = TRUE;
23615 }
23616 }
23617 vim_free(sub);
23618 vim_free(str);
23619 }
23620 vim_free(pat);
23621 }
23622 }
23623 /* after using ":s", repeat all the modifiers */
23624 if (didit)
23625 goto repeat;
23626 }
23627 }
23628
23629 return valid;
23630}
23631
23632/*
23633 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23634 * "flags" can be "g" to do a global substitute.
23635 * Returns an allocated string, NULL for error.
23636 */
23637 char_u *
23638do_string_sub(str, pat, sub, flags)
23639 char_u *str;
23640 char_u *pat;
23641 char_u *sub;
23642 char_u *flags;
23643{
23644 int sublen;
23645 regmatch_T regmatch;
23646 int i;
23647 int do_all;
23648 char_u *tail;
23649 garray_T ga;
23650 char_u *ret;
23651 char_u *save_cpo;
23652
23653 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23654 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023655 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656
23657 ga_init2(&ga, 1, 200);
23658
23659 do_all = (flags[0] == 'g');
23660
23661 regmatch.rm_ic = p_ic;
23662 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23663 if (regmatch.regprog != NULL)
23664 {
23665 tail = str;
23666 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23667 {
23668 /*
23669 * Get some space for a temporary buffer to do the substitution
23670 * into. It will contain:
23671 * - The text up to where the match is.
23672 * - The substituted text.
23673 * - The text after the match.
23674 */
23675 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23676 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23677 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23678 {
23679 ga_clear(&ga);
23680 break;
23681 }
23682
23683 /* copy the text up to where the match is */
23684 i = (int)(regmatch.startp[0] - tail);
23685 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23686 /* add the substituted text */
23687 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23688 + ga.ga_len + i, TRUE, TRUE, FALSE);
23689 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023690 /* avoid getting stuck on a match with an empty string */
23691 if (tail == regmatch.endp[0])
23692 {
23693 if (*tail == NUL)
23694 break;
23695 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23696 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023697 }
23698 else
23699 {
23700 tail = regmatch.endp[0];
23701 if (*tail == NUL)
23702 break;
23703 }
23704 if (!do_all)
23705 break;
23706 }
23707
23708 if (ga.ga_data != NULL)
23709 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23710
23711 vim_free(regmatch.regprog);
23712 }
23713
23714 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23715 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023716 if (p_cpo == empty_option)
23717 p_cpo = save_cpo;
23718 else
23719 /* Darn, evaluating {sub} expression changed the value. */
23720 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023721
23722 return ret;
23723}
23724
23725#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */