blob: bd2d14280edad1da0a00db295188aee12d4f19f7 [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));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100445static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000446static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000447static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000448static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
449static void set_ref_in_list __ARGS((list_T *l, int copyID));
450static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200451static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000452static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
454static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000455static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000457static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000459static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
460static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#ifdef FEAT_FLOAT
463static int string2float __ARGS((char_u *text, float_T *value));
464#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
466static int find_internal_func __ARGS((char_u *name));
467static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
468static 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 +0200469static 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 +0000470static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000471static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000472
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#ifdef FEAT_FLOAT
474static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200475static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100478static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200484static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000502static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000505static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000508static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
515static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200516static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
521static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200532#ifdef FEAT_FLOAT
533static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
534#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000537static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
544static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000548static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000557static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000559static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000573static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000574static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000575static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000576static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200579static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000580static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000588static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000602static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100607static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000609static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000610static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000621#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200622static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000623static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
624#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000629static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000630static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000631static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000633static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000637#ifdef vim_mkdir
638static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100641#ifdef FEAT_MZSCHEME
642static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
643#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100646static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000647static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000648#ifdef FEAT_FLOAT
649static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000652static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000653static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000689static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
692static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200693static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000696static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000697static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
701static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
703#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000704static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200705static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706#ifdef HAVE_STRFTIME
707static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
709static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200715static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000722static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200723static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000726static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000728static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000729static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000731static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200732#ifdef FEAT_FLOAT
733static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
740static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200743static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200744static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000754static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100758static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000760static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000761static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static int get_env_len __ARGS((char_u **arg));
763static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000765static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
766#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
767#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
768 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000769static 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 +0000770static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000771static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000772static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
773static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static typval_T *alloc_tv __ARGS((void));
775static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static void init_tv __ARGS((typval_T *varp));
777static long get_tv_number __ARGS((typval_T *varp));
778static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000779static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static char_u *get_tv_string __ARGS((typval_T *varp));
781static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000782static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000784static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000785static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
786static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
787static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000788static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
789static 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 +0000790static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
791static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000792static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200793static int var_check_func_name __ARGS((char_u *name, int new_var));
794static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000795static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000796static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
798static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
799static int eval_fname_script __ARGS((char_u *p));
800static int eval_fname_sid __ARGS((char_u *p));
801static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static ufunc_T *find_func __ARGS((char_u *name));
803static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000804static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805#ifdef FEAT_PROFILE
806static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000807static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
808static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_total_cmp __ARGS((const void *s1, const void *s2));
814static int
815# ifdef __BORLANDC__
816 _RTLENTRYF
817# endif
818 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000820static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000821static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000822static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static void func_free __ARGS((ufunc_T *fp));
824static void func_unref __ARGS((char_u *name));
825static void func_ref __ARGS((char_u *name));
826static 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 +0000827static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
828static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000830static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
831static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000832static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000833static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000834static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000835
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200836
837#ifdef EBCDIC
838static int compare_func_name __ARGS((const void *s1, const void *s2));
839static void sortFunctions __ARGS(());
840#endif
841
842
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000843/* Character used as separated in autoload function/variable names. */
844#define AUTOLOAD_CHAR '#'
845
Bram Moolenaar33570922005-01-25 22:26:29 +0000846/*
847 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000848 */
849 void
850eval_init()
851{
Bram Moolenaar33570922005-01-25 22:26:29 +0000852 int i;
853 struct vimvar *p;
854
855 init_var_dict(&globvardict, &globvars_var);
856 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200857 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000858 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000859 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000860
861 for (i = 0; i < VV_LEN; ++i)
862 {
863 p = &vimvars[i];
864 STRCPY(p->vv_di.di_key, p->vv_name);
865 if (p->vv_flags & VV_RO)
866 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
867 else if (p->vv_flags & VV_RO_SBX)
868 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
869 else
870 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000871
872 /* add to v: scope dict, unless the value is not always available */
873 if (p->vv_type != VAR_UNKNOWN)
874 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000876 /* add to compat scope dict */
877 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000879 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200880
881#ifdef EBCDIC
882 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100883 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200884 */
885 sortFunctions();
886#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000887}
888
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000889#if defined(EXITFREE) || defined(PROTO)
890 void
891eval_clear()
892{
893 int i;
894 struct vimvar *p;
895
896 for (i = 0; i < VV_LEN; ++i)
897 {
898 p = &vimvars[i];
899 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000900 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000901 vim_free(p->vv_str);
902 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000903 }
904 else if (p->vv_di.di_tv.v_type == VAR_LIST)
905 {
906 list_unref(p->vv_list);
907 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000908 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000909 }
910 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000911 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000912 hash_clear(&compat_hashtab);
913
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200915 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000916
917 /* global variables */
918 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000919
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000920 /* autoloaded script names */
921 ga_clear_strings(&ga_loaded);
922
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200923 /* script-local variables */
924 for (i = 1; i <= ga_scripts.ga_len; ++i)
925 {
926 vars_clear(&SCRIPT_VARS(i));
927 vim_free(SCRIPT_SV(i));
928 }
929 ga_clear(&ga_scripts);
930
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000931 /* unreferenced lists and dicts */
932 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000933
934 /* functions */
935 free_all_functions();
936 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000937}
938#endif
939
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 * Return the name of the executed function.
942 */
943 char_u *
944func_name(cookie)
945 void *cookie;
946{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000947 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948}
949
950/*
951 * Return the address holding the next breakpoint line for a funccall cookie.
952 */
953 linenr_T *
954func_breakpoint(cookie)
955 void *cookie;
956{
Bram Moolenaar33570922005-01-25 22:26:29 +0000957 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958}
959
960/*
961 * Return the address holding the debug tick for a funccall cookie.
962 */
963 int *
964func_dbg_tick(cookie)
965 void *cookie;
966{
Bram Moolenaar33570922005-01-25 22:26:29 +0000967 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968}
969
970/*
971 * Return the nesting level for a funccall cookie.
972 */
973 int
974func_level(cookie)
975 void *cookie;
976{
Bram Moolenaar33570922005-01-25 22:26:29 +0000977 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978}
979
980/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000981funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000983/* pointer to list of previously used funccal, still around because some
984 * item in it is still being used. */
985funccall_T *previous_funccal = NULL;
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987/*
988 * Return TRUE when a function was ended by a ":return" command.
989 */
990 int
991current_func_returned()
992{
993 return current_funccal->returned;
994}
995
996
997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 * Set an internal variable to a string value. Creates the variable if it does
999 * not already exist.
1000 */
1001 void
1002set_internal_string_var(name, value)
1003 char_u *name;
1004 char_u *value;
1005{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001006 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001007 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009 val = vim_strsave(value);
1010 if (val != NULL)
1011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001012 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001013 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001015 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001016 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 }
1018 }
1019}
1020
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001022static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023static char_u *redir_endp = NULL;
1024static char_u *redir_varname = NULL;
1025
1026/*
1027 * Start recording command output to a variable
1028 * Returns OK if successfully completed the setup. FAIL otherwise.
1029 */
1030 int
1031var_redir_start(name, append)
1032 char_u *name;
1033 int append; /* append to an existing variable */
1034{
1035 int save_emsg;
1036 int err;
1037 typval_T tv;
1038
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001039 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001040 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 {
1042 EMSG(_(e_invarg));
1043 return FAIL;
1044 }
1045
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001046 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001047 redir_varname = vim_strsave(name);
1048 if (redir_varname == NULL)
1049 return FAIL;
1050
1051 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1052 if (redir_lval == NULL)
1053 {
1054 var_redir_stop();
1055 return FAIL;
1056 }
1057
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001058 /* The output is stored in growarray "redir_ga" until redirection ends. */
1059 ga_init2(&redir_ga, (int)sizeof(char), 500);
1060
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001062 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1063 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1065 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001066 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067 if (redir_endp != NULL && *redir_endp != NUL)
1068 /* Trailing characters are present after the variable name */
1069 EMSG(_(e_trailing));
1070 else
1071 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001072 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073 var_redir_stop();
1074 return FAIL;
1075 }
1076
1077 /* check if we can write to the variable: set it to or append an empty
1078 * string */
1079 save_emsg = did_emsg;
1080 did_emsg = FALSE;
1081 tv.v_type = VAR_STRING;
1082 tv.vval.v_string = (char_u *)"";
1083 if (append)
1084 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1085 else
1086 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001087 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001089 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 if (err)
1091 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001092 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 var_redir_stop();
1094 return FAIL;
1095 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096
1097 return OK;
1098}
1099
1100/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001101 * Append "value[value_len]" to the variable set by var_redir_start().
1102 * The actual appending is postponed until redirection ends, because the value
1103 * appended may in fact be the string we write to, changing it may cause freed
1104 * memory to be used:
1105 * :redir => foo
1106 * :let foo
1107 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 */
1109 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001110var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001114 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115
1116 if (redir_lval == NULL)
1117 return;
1118
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001119 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125 {
1126 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001127 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001128 }
1129 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131}
1132
1133/*
1134 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001135 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 */
1137 void
1138var_redir_stop()
1139{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 typval_T tv;
1141
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 if (redir_lval != NULL)
1143 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001144 /* If there was no error: assign the text to the variable. */
1145 if (redir_endp != NULL)
1146 {
1147 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1148 tv.v_type = VAR_STRING;
1149 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001150 /* Call get_lval() again, if it's inside a Dict or List it may
1151 * have changed. */
1152 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1153 FALSE, FALSE, FALSE, FNE_CHECK_START);
1154 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1155 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1156 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001158
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* free the collected output */
1160 vim_free(redir_ga.ga_data);
1161 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001162
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 vim_free(redir_lval);
1164 redir_lval = NULL;
1165 }
1166 vim_free(redir_varname);
1167 redir_varname = NULL;
1168}
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170# if defined(FEAT_MBYTE) || defined(PROTO)
1171 int
1172eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1173 char_u *enc_from;
1174 char_u *enc_to;
1175 char_u *fname_from;
1176 char_u *fname_to;
1177{
1178 int err = FALSE;
1179
1180 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1181 set_vim_var_string(VV_CC_TO, enc_to, -1);
1182 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1183 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1184 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1185 err = TRUE;
1186 set_vim_var_string(VV_CC_FROM, NULL, -1);
1187 set_vim_var_string(VV_CC_TO, NULL, -1);
1188 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1189 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1190
1191 if (err)
1192 return FAIL;
1193 return OK;
1194}
1195# endif
1196
1197# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1198 int
1199eval_printexpr(fname, args)
1200 char_u *fname;
1201 char_u *args;
1202{
1203 int err = FALSE;
1204
1205 set_vim_var_string(VV_FNAME_IN, fname, -1);
1206 set_vim_var_string(VV_CMDARG, args, -1);
1207 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1208 err = TRUE;
1209 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1210 set_vim_var_string(VV_CMDARG, NULL, -1);
1211
1212 if (err)
1213 {
1214 mch_remove(fname);
1215 return FAIL;
1216 }
1217 return OK;
1218}
1219# endif
1220
1221# if defined(FEAT_DIFF) || defined(PROTO)
1222 void
1223eval_diff(origfile, newfile, outfile)
1224 char_u *origfile;
1225 char_u *newfile;
1226 char_u *outfile;
1227{
1228 int err = FALSE;
1229
1230 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1231 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1232 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1233 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1234 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1235 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1236 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1237}
1238
1239 void
1240eval_patch(origfile, difffile, outfile)
1241 char_u *origfile;
1242 char_u *difffile;
1243 char_u *outfile;
1244{
1245 int err;
1246
1247 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1248 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1249 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1250 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1251 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1252 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1253 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1254}
1255# endif
1256
1257/*
1258 * Top level evaluation function, returning a boolean.
1259 * Sets "error" to TRUE if there was an error.
1260 * Return TRUE or FALSE.
1261 */
1262 int
1263eval_to_bool(arg, error, nextcmd, skip)
1264 char_u *arg;
1265 int *error;
1266 char_u **nextcmd;
1267 int skip; /* only parse, don't execute */
1268{
Bram Moolenaar33570922005-01-25 22:26:29 +00001269 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 int retval = FALSE;
1271
1272 if (skip)
1273 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001274 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 else
1277 {
1278 *error = FALSE;
1279 if (!skip)
1280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001281 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284 }
1285 if (skip)
1286 --emsg_skip;
1287
1288 return retval;
1289}
1290
1291/*
1292 * Top level evaluation function, returning a string. If "skip" is TRUE,
1293 * only parsing to "nextcmd" is done, without reporting errors. Return
1294 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1295 */
1296 char_u *
1297eval_to_string_skip(arg, nextcmd, skip)
1298 char_u *arg;
1299 char_u **nextcmd;
1300 int skip; /* only parse, don't execute */
1301{
Bram Moolenaar33570922005-01-25 22:26:29 +00001302 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 char_u *retval;
1304
1305 if (skip)
1306 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001307 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 retval = NULL;
1309 else
1310 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 retval = vim_strsave(get_tv_string(&tv));
1312 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 if (skip)
1315 --emsg_skip;
1316
1317 return retval;
1318}
1319
1320/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001321 * Skip over an expression at "*pp".
1322 * Return FAIL for an error, OK otherwise.
1323 */
1324 int
1325skip_expr(pp)
1326 char_u **pp;
1327{
Bram Moolenaar33570922005-01-25 22:26:29 +00001328 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329
1330 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001331 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001332}
1333
1334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336 * When "convert" is TRUE convert a List into a sequence of lines and convert
1337 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 * Return pointer to allocated memory, or NULL for failure.
1339 */
1340 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *arg;
1343 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
Bram Moolenaar33570922005-01-25 22:26:29 +00001346 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001348 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001349#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001350 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 retval = NULL;
1355 else
1356 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001357 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001358 {
1359 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001360 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001361 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001362 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001363 if (tv.vval.v_list->lv_len > 0)
1364 ga_append(&ga, NL);
1365 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 ga_append(&ga, NUL);
1367 retval = (char_u *)ga.ga_data;
1368 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001369#ifdef FEAT_FLOAT
1370 else if (convert && tv.v_type == VAR_FLOAT)
1371 {
1372 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1373 retval = vim_strsave(numbuf);
1374 }
1375#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001376 else
1377 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001378 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 }
1380
1381 return retval;
1382}
1383
1384/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001385 * Call eval_to_string() without using current local variables and using
1386 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 */
1388 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001389eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 char_u *arg;
1391 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 char_u *retval;
1395 void *save_funccalp;
1396
1397 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001398 if (use_sandbox)
1399 ++sandbox;
1400 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001401 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001402 if (use_sandbox)
1403 --sandbox;
1404 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 restore_funccal(save_funccalp);
1406 return retval;
1407}
1408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409/*
1410 * Top level evaluation function, returning a number.
1411 * Evaluates "expr" silently.
1412 * Returns -1 for an error.
1413 */
1414 int
1415eval_to_number(expr)
1416 char_u *expr;
1417{
Bram Moolenaar33570922005-01-25 22:26:29 +00001418 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001420 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
1422 ++emsg_off;
1423
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 retval = -1;
1426 else
1427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001428 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001429 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
1431 --emsg_off;
1432
1433 return retval;
1434}
1435
Bram Moolenaara40058a2005-07-11 22:42:07 +00001436/*
1437 * Prepare v: variable "idx" to be used.
1438 * Save the current typeval in "save_tv".
1439 * When not used yet add the variable to the v: hashtable.
1440 */
1441 static void
1442prepare_vimvar(idx, save_tv)
1443 int idx;
1444 typval_T *save_tv;
1445{
1446 *save_tv = vimvars[idx].vv_tv;
1447 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1448 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1449}
1450
1451/*
1452 * Restore v: variable "idx" to typeval "save_tv".
1453 * When no longer defined, remove the variable from the v: hashtable.
1454 */
1455 static void
1456restore_vimvar(idx, save_tv)
1457 int idx;
1458 typval_T *save_tv;
1459{
1460 hashitem_T *hi;
1461
Bram Moolenaara40058a2005-07-11 22:42:07 +00001462 vimvars[idx].vv_tv = *save_tv;
1463 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1464 {
1465 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1466 if (HASHITEM_EMPTY(hi))
1467 EMSG2(_(e_intern2), "restore_vimvar()");
1468 else
1469 hash_remove(&vimvarht, hi);
1470 }
1471}
1472
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001473#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001474/*
1475 * Evaluate an expression to a list with suggestions.
1476 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001477 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001478 */
1479 list_T *
1480eval_spell_expr(badword, expr)
1481 char_u *badword;
1482 char_u *expr;
1483{
1484 typval_T save_val;
1485 typval_T rettv;
1486 list_T *list = NULL;
1487 char_u *p = skipwhite(expr);
1488
1489 /* Set "v:val" to the bad word. */
1490 prepare_vimvar(VV_VAL, &save_val);
1491 vimvars[VV_VAL].vv_type = VAR_STRING;
1492 vimvars[VV_VAL].vv_str = badword;
1493 if (p_verbose == 0)
1494 ++emsg_off;
1495
1496 if (eval1(&p, &rettv, TRUE) == OK)
1497 {
1498 if (rettv.v_type != VAR_LIST)
1499 clear_tv(&rettv);
1500 else
1501 list = rettv.vval.v_list;
1502 }
1503
1504 if (p_verbose == 0)
1505 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001506 restore_vimvar(VV_VAL, &save_val);
1507
1508 return list;
1509}
1510
1511/*
1512 * "list" is supposed to contain two items: a word and a number. Return the
1513 * word in "pp" and the number as the return value.
1514 * Return -1 if anything isn't right.
1515 * Used to get the good word and score from the eval_spell_expr() result.
1516 */
1517 int
1518get_spellword(list, pp)
1519 list_T *list;
1520 char_u **pp;
1521{
1522 listitem_T *li;
1523
1524 li = list->lv_first;
1525 if (li == NULL)
1526 return -1;
1527 *pp = get_tv_string(&li->li_tv);
1528
1529 li = li->li_next;
1530 if (li == NULL)
1531 return -1;
1532 return get_tv_number(&li->li_tv);
1533}
1534#endif
1535
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001536/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001537 * Top level evaluation function.
1538 * Returns an allocated typval_T with the result.
1539 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001540 */
1541 typval_T *
1542eval_expr(arg, nextcmd)
1543 char_u *arg;
1544 char_u **nextcmd;
1545{
1546 typval_T *tv;
1547
1548 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001549 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001550 {
1551 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001552 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001553 }
1554
1555 return tv;
1556}
1557
1558
Bram Moolenaar4f688582007-07-24 12:34:30 +00001559#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1560 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001563 * Uses argv[argc] for the function arguments. Only Number and String
1564 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001567 int
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 char_u *func;
1570 int argc;
1571 char_u **argv;
1572 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
Bram Moolenaar33570922005-01-25 22:26:29 +00001575 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 long n;
1577 int len;
1578 int i;
1579 int doesrange;
1580 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001581 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001583 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001585 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
1587 for (i = 0; i < argc; i++)
1588 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001589 /* Pass a NULL or empty argument as an empty string */
1590 if (argv[i] == NULL || *argv[i] == NUL)
1591 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001592 argvars[i].v_type = VAR_STRING;
1593 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001594 continue;
1595 }
1596
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 /* Recognize a number argument, the others must be strings. */
1598 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1599 if (len != 0 && len == (int)STRLEN(argv[i]))
1600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001601 argvars[i].v_type = VAR_NUMBER;
1602 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 else
1605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001606 argvars[i].v_type = VAR_STRING;
1607 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 }
1610
1611 if (safe)
1612 {
1613 save_funccalp = save_funccal();
1614 ++sandbox;
1615 }
1616
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1618 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 if (safe)
1622 {
1623 --sandbox;
1624 restore_funccal(save_funccalp);
1625 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 vim_free(argvars);
1627
1628 if (ret == FAIL)
1629 clear_tv(rettv);
1630
1631 return ret;
1632}
1633
Bram Moolenaar4f688582007-07-24 12:34:30 +00001634# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001635/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001636 * Call vimL function "func" and return the result as a string.
1637 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638 * Uses argv[argc] for the function arguments.
1639 */
1640 void *
1641call_func_retstr(func, argc, argv, safe)
1642 char_u *func;
1643 int argc;
1644 char_u **argv;
1645 int safe; /* use the sandbox */
1646{
1647 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001648 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649
1650 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1651 return NULL;
1652
1653 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 return retval;
1656}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001657# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658
Bram Moolenaar4f688582007-07-24 12:34:30 +00001659# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001660/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001661 * Call vimL function "func" and return the result as a number.
1662 * Returns -1 when calling the function fails.
1663 * Uses argv[argc] for the function arguments.
1664 */
1665 long
1666call_func_retnr(func, argc, argv, safe)
1667 char_u *func;
1668 int argc;
1669 char_u **argv;
1670 int safe; /* use the sandbox */
1671{
1672 typval_T rettv;
1673 long retval;
1674
1675 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1676 return -1;
1677
1678 retval = get_tv_number_chk(&rettv, NULL);
1679 clear_tv(&rettv);
1680 return retval;
1681}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001682# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001683
1684/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001685 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001687 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 */
1689 void *
1690call_func_retlist(func, argc, argv, safe)
1691 char_u *func;
1692 int argc;
1693 char_u **argv;
1694 int safe; /* use the sandbox */
1695{
1696 typval_T rettv;
1697
1698 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1699 return NULL;
1700
1701 if (rettv.v_type != VAR_LIST)
1702 {
1703 clear_tv(&rettv);
1704 return NULL;
1705 }
1706
1707 return rettv.vval.v_list;
1708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709#endif
1710
Bram Moolenaar4f688582007-07-24 12:34:30 +00001711
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712/*
1713 * Save the current function call pointer, and set it to NULL.
1714 * Used when executing autocommands and for ":source".
1715 */
1716 void *
1717save_funccal()
1718{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001719 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 current_funccal = NULL;
1722 return (void *)fc;
1723}
1724
1725 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726restore_funccal(vfc)
1727 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729 funccall_T *fc = (funccall_T *)vfc;
1730
1731 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732}
1733
Bram Moolenaar05159a02005-02-26 23:04:13 +00001734#if defined(FEAT_PROFILE) || defined(PROTO)
1735/*
1736 * Prepare profiling for entering a child or something else that is not
1737 * counted for the script/function itself.
1738 * Should always be called in pair with prof_child_exit().
1739 */
1740 void
1741prof_child_enter(tm)
1742 proftime_T *tm; /* place to store waittime */
1743{
1744 funccall_T *fc = current_funccal;
1745
1746 if (fc != NULL && fc->func->uf_profiling)
1747 profile_start(&fc->prof_child);
1748 script_prof_save(tm);
1749}
1750
1751/*
1752 * Take care of time spent in a child.
1753 * Should always be called after prof_child_enter().
1754 */
1755 void
1756prof_child_exit(tm)
1757 proftime_T *tm; /* where waittime was stored */
1758{
1759 funccall_T *fc = current_funccal;
1760
1761 if (fc != NULL && fc->func->uf_profiling)
1762 {
1763 profile_end(&fc->prof_child);
1764 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1765 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1766 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1767 }
1768 script_prof_restore(tm);
1769}
1770#endif
1771
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#ifdef FEAT_FOLDING
1774/*
1775 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1776 * it in "*cp". Doesn't give error messages.
1777 */
1778 int
1779eval_foldexpr(arg, cp)
1780 char_u *arg;
1781 int *cp;
1782{
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 int retval;
1785 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001786 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1787 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001790 if (use_sandbox)
1791 ++sandbox;
1792 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 retval = 0;
1796 else
1797 {
1798 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 if (tv.v_type == VAR_NUMBER)
1800 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001801 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 retval = 0;
1803 else
1804 {
1805 /* If the result is a string, check if there is a non-digit before
1806 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (!VIM_ISDIGIT(*s) && *s != '-')
1809 *cp = *s++;
1810 retval = atol((char *)s);
1811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001815 if (use_sandbox)
1816 --sandbox;
1817 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819 return retval;
1820}
1821#endif
1822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 * ":let" list all variable values
1825 * ":let var1 var2" list variable values
1826 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001827 * ":let var += expr" assignment command.
1828 * ":let var -= expr" assignment command.
1829 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 */
1832 void
1833ex_let(eap)
1834 exarg_T *eap;
1835{
1836 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 int var_count = 0;
1841 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001842 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001843 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001844 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 argend = skip_var_list(arg, &var_count, &semicolon);
1847 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001849 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1850 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001851 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001854 /*
1855 * ":let" without "=": list variables
1856 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 if (*arg == '[')
1858 EMSG(_(e_invarg));
1859 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001861 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001865 list_glob_vars(&first);
1866 list_buf_vars(&first);
1867 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001868#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001869 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001870#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001871 list_script_vars(&first);
1872 list_func_vars(&first);
1873 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 eap->nextcmd = check_nextcmd(arg);
1876 }
1877 else
1878 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 op[0] = '=';
1880 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001881 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 {
1883 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1884 op[0] = expr[-1]; /* +=, -= or .= */
1885 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (eap->skip)
1889 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (eap->skip)
1892 {
1893 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 --emsg_skip;
1896 }
1897 else if (i != FAIL)
1898 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 }
1904}
1905
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906/*
1907 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1908 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 * When "nextchars" is not NULL it points to a string with characters that
1910 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1911 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 * Returns OK or FAIL;
1913 */
1914 static int
1915ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1916 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 int copy; /* copy values from "tv", don't move */
1919 int semicolon; /* from skip_var_list() */
1920 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922{
1923 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001926 listitem_T *item;
1927 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928
1929 if (*arg != '[')
1930 {
1931 /*
1932 * ":let var = expr" or ":for var in list"
1933 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 return FAIL;
1936 return OK;
1937 }
1938
1939 /*
1940 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1941 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001942 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 {
1944 EMSG(_(e_listreq));
1945 return FAIL;
1946 }
1947
1948 i = list_len(l);
1949 if (semicolon == 0 && var_count < i)
1950 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001951 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 }
1954 if (var_count - semicolon > i)
1955 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001956 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 return FAIL;
1958 }
1959
1960 item = l->lv_first;
1961 while (*arg != ']')
1962 {
1963 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 item = item->li_next;
1966 if (arg == NULL)
1967 return FAIL;
1968
1969 arg = skipwhite(arg);
1970 if (*arg == ';')
1971 {
1972 /* Put the rest of the list (may be empty) in the var after ';'.
1973 * Create a new list for this. */
1974 l = list_alloc();
1975 if (l == NULL)
1976 return FAIL;
1977 while (item != NULL)
1978 {
1979 list_append_tv(l, &item->li_tv);
1980 item = item->li_next;
1981 }
1982
1983 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001984 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 ltv.vval.v_list = l;
1986 l->lv_refcount = 1;
1987
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001988 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1989 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 clear_tv(&ltv);
1991 if (arg == NULL)
1992 return FAIL;
1993 break;
1994 }
1995 else if (*arg != ',' && *arg != ']')
1996 {
1997 EMSG2(_(e_intern2), "ex_let_vars()");
1998 return FAIL;
1999 }
2000 }
2001
2002 return OK;
2003}
2004
2005/*
2006 * Skip over assignable variable "var" or list of variables "[var, var]".
2007 * Used for ":let varvar = expr" and ":for varvar in expr".
2008 * For "[var, var]" increment "*var_count" for each variable.
2009 * for "[var, var; var]" set "semicolon".
2010 * Return NULL for an error.
2011 */
2012 static char_u *
2013skip_var_list(arg, var_count, semicolon)
2014 char_u *arg;
2015 int *var_count;
2016 int *semicolon;
2017{
2018 char_u *p, *s;
2019
2020 if (*arg == '[')
2021 {
2022 /* "[var, var]": find the matching ']'. */
2023 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002024 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 {
2026 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2027 s = skip_var_one(p);
2028 if (s == p)
2029 {
2030 EMSG2(_(e_invarg2), p);
2031 return NULL;
2032 }
2033 ++*var_count;
2034
2035 p = skipwhite(s);
2036 if (*p == ']')
2037 break;
2038 else if (*p == ';')
2039 {
2040 if (*semicolon == 1)
2041 {
2042 EMSG(_("Double ; in list of variables"));
2043 return NULL;
2044 }
2045 *semicolon = 1;
2046 }
2047 else if (*p != ',')
2048 {
2049 EMSG2(_(e_invarg2), p);
2050 return NULL;
2051 }
2052 }
2053 return p + 1;
2054 }
2055 else
2056 return skip_var_one(arg);
2057}
2058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002059/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002060 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002061 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002063 static char_u *
2064skip_var_one(arg)
2065 char_u *arg;
2066{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002067 if (*arg == '@' && arg[1] != NUL)
2068 return arg + 2;
2069 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2070 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002071}
2072
Bram Moolenaara7043832005-01-21 11:56:39 +00002073/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 * List variables for hashtab "ht" with prefix "prefix".
2075 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002077 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002078list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002082 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083{
Bram Moolenaar33570922005-01-25 22:26:29 +00002084 hashitem_T *hi;
2085 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 int todo;
2087
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002088 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2090 {
2091 if (!HASHITEM_EMPTY(hi))
2092 {
2093 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 di = HI2DI(hi);
2095 if (empty || di->di_tv.v_type != VAR_STRING
2096 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 }
2099 }
2100}
2101
2102/*
2103 * List global variables.
2104 */
2105 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106list_glob_vars(first)
2107 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002108{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002110}
2111
2112/*
2113 * List buffer variables.
2114 */
2115 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116list_buf_vars(first)
2117 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002118{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119 char_u numbuf[NUMBUFLEN];
2120
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002121 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2122 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123
2124 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2126 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002127}
2128
2129/*
2130 * List window variables.
2131 */
2132 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133list_win_vars(first)
2134 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002135{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2137 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002138}
2139
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002140#ifdef FEAT_WINDOWS
2141/*
2142 * List tab page variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_tab_vars(first)
2146 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2149 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002150}
2151#endif
2152
Bram Moolenaara7043832005-01-21 11:56:39 +00002153/*
2154 * List Vim variables.
2155 */
2156 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157list_vim_vars(first)
2158 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161}
2162
2163/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164 * List script-local variables, if there is a script.
2165 */
2166 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167list_script_vars(first)
2168 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002169{
2170 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2172 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002173}
2174
2175/*
2176 * List function variables, if there is a function.
2177 */
2178 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179list_func_vars(first)
2180 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002181{
2182 if (current_funccal != NULL)
2183 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185}
2186
2187/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 * List variables in "arg".
2189 */
2190 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 exarg_T *eap;
2193 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195{
2196 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002199 char_u *name_start;
2200 char_u *arg_subsc;
2201 char_u *tofree;
2202 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203
2204 while (!ends_excmd(*arg) && !got_int)
2205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002208 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2210 {
2211 emsg_severe = TRUE;
2212 EMSG(_(e_trailing));
2213 break;
2214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 /* get_name_len() takes care of expanding curly braces */
2219 name_start = name = arg;
2220 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2221 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 /* This is mainly to keep test 49 working: when expanding
2224 * curly braces fails overrule the exception error message. */
2225 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 emsg_severe = TRUE;
2228 EMSG2(_(e_invarg2), arg);
2229 break;
2230 }
2231 error = TRUE;
2232 }
2233 else
2234 {
2235 if (tofree != NULL)
2236 name = tofree;
2237 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 else
2240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* handle d.key, l[idx], f(expr) */
2242 arg_subsc = arg;
2243 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002250 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 case 'g': list_glob_vars(first); break;
2252 case 'b': list_buf_vars(first); break;
2253 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002255 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002256#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002257 case 'v': list_vim_vars(first); break;
2258 case 's': list_script_vars(first); break;
2259 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 default:
2261 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 }
2264 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 {
2266 char_u numbuf[NUMBUFLEN];
2267 char_u *tf;
2268 int c;
2269 char_u *s;
2270
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002271 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 c = *arg;
2273 *arg = NUL;
2274 list_one_var_a((char_u *)"",
2275 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002276 tv.v_type,
2277 s == NULL ? (char_u *)"" : s,
2278 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 *arg = c;
2280 vim_free(tf);
2281 }
2282 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286
2287 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289
2290 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292
2293 return arg;
2294}
2295
2296/*
2297 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2298 * Returns a pointer to the char just after the var name.
2299 * Returns NULL if there is an error.
2300 */
2301 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002304 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002306 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308{
2309 int c1;
2310 char_u *name;
2311 char_u *p;
2312 char_u *arg_end = NULL;
2313 int len;
2314 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316
2317 /*
2318 * ":let $VAR = expr": Set environment variable.
2319 */
2320 if (*arg == '$')
2321 {
2322 /* Find the end of the name. */
2323 ++arg;
2324 name = arg;
2325 len = get_env_len(&arg);
2326 if (len == 0)
2327 EMSG2(_(e_invarg2), name - 1);
2328 else
2329 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 if (op != NULL && (*op == '+' || *op == '-'))
2331 EMSG2(_(e_letwrong), op);
2332 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002333 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002335 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 {
2337 c1 = name[len];
2338 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002339 p = get_tv_string_chk(tv);
2340 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 {
2342 int mustfree = FALSE;
2343 char_u *s = vim_getenv(name, &mustfree);
2344
2345 if (s != NULL)
2346 {
2347 p = tofree = concat_str(s, p);
2348 if (mustfree)
2349 vim_free(s);
2350 }
2351 }
2352 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 if (STRICMP(name, "HOME") == 0)
2356 init_homedir();
2357 else if (didset_vim && STRICMP(name, "VIM") == 0)
2358 didset_vim = FALSE;
2359 else if (didset_vimruntime
2360 && STRICMP(name, "VIMRUNTIME") == 0)
2361 didset_vimruntime = FALSE;
2362 arg_end = arg;
2363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366 }
2367 }
2368 }
2369
2370 /*
2371 * ":let &option = expr": Set option value.
2372 * ":let &l:option = expr": Set local option value.
2373 * ":let &g:option = expr": Set global option value.
2374 */
2375 else if (*arg == '&')
2376 {
2377 /* Find the end of the name. */
2378 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002379 if (p == NULL || (endchars != NULL
2380 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 EMSG(_(e_letunexp));
2382 else
2383 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 long n;
2385 int opt_type;
2386 long numval;
2387 char_u *stringval = NULL;
2388 char_u *s;
2389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 c1 = *p;
2391 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392
2393 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 s = get_tv_string_chk(tv); /* != NULL if number or string */
2395 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 {
2397 opt_type = get_option_value(arg, &numval,
2398 &stringval, opt_flags);
2399 if ((opt_type == 1 && *op == '.')
2400 || (opt_type == 0 && *op != '.'))
2401 EMSG2(_(e_letwrong), op);
2402 else
2403 {
2404 if (opt_type == 1) /* number */
2405 {
2406 if (*op == '+')
2407 n = numval + n;
2408 else
2409 n = numval - n;
2410 }
2411 else if (opt_type == 0 && stringval != NULL) /* string */
2412 {
2413 s = concat_str(stringval, s);
2414 vim_free(stringval);
2415 stringval = s;
2416 }
2417 }
2418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002419 if (s != NULL)
2420 {
2421 set_option_value(arg, n, s, opt_flags);
2422 arg_end = p;
2423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002425 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 }
2427 }
2428
2429 /*
2430 * ":let @r = expr": Set register contents.
2431 */
2432 else if (*arg == '@')
2433 {
2434 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 if (op != NULL && (*op == '+' || *op == '-'))
2436 EMSG2(_(e_letwrong), op);
2437 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002438 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 EMSG(_(e_letunexp));
2440 else
2441 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002442 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 char_u *s;
2444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 p = get_tv_string_chk(tv);
2446 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002448 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 if (s != NULL)
2450 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002451 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 vim_free(s);
2453 }
2454 }
2455 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 arg_end = arg + 1;
2459 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002460 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 }
2462 }
2463
2464 /*
2465 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002468 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002470 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002472 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2476 EMSG(_(e_letunexp));
2477 else
2478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 arg_end = p;
2481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485
2486 else
2487 EMSG2(_(e_invarg2), arg);
2488
2489 return arg_end;
2490}
2491
2492/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2494 */
2495 static int
2496check_changedtick(arg)
2497 char_u *arg;
2498{
2499 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2500 {
2501 EMSG2(_(e_readonlyvar), arg);
2502 return TRUE;
2503 }
2504 return FALSE;
2505}
2506
2507/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 * Get an lval: variable, Dict item or List item that can be assigned a value
2509 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2510 * "name.key", "name.key[expr]" etc.
2511 * Indexing only works if "name" is an existing List or Dictionary.
2512 * "name" points to the start of the name.
2513 * If "rettv" is not NULL it points to the value to be assigned.
2514 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2515 * wrong; must end in space or cmd separator.
2516 *
2517 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002518 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 */
2521 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002522get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 typval_T *rettv;
2525 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 int unlet;
2527 int skip;
2528 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002529 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 char_u *expr_start, *expr_end;
2533 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 dictitem_T *v;
2535 typval_T var1;
2536 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545
2546 if (skip)
2547 {
2548 /* When skipping just find the end of the name. */
2549 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002550 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 }
2552
2553 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002554 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (expr_start != NULL)
2556 {
2557 /* Don't expand the name when we already know there is an error. */
2558 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2559 && *p != '[' && *p != '.')
2560 {
2561 EMSG(_(e_trailing));
2562 return NULL;
2563 }
2564
2565 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2566 if (lp->ll_exp_name == NULL)
2567 {
2568 /* Report an invalid expression in braces, unless the
2569 * expression evaluation has been cancelled due to an
2570 * aborting error, an interrupt, or an exception. */
2571 if (!aborting() && !quiet)
2572 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002573 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 EMSG2(_(e_invarg2), name);
2575 return NULL;
2576 }
2577 }
2578 lp->ll_name = lp->ll_exp_name;
2579 }
2580 else
2581 lp->ll_name = name;
2582
2583 /* Without [idx] or .key we are done. */
2584 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2585 return p;
2586
2587 cc = *p;
2588 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002589 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (v == NULL && !quiet)
2591 EMSG2(_(e_undefvar), lp->ll_name);
2592 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 if (v == NULL)
2594 return NULL;
2595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 /*
2597 * Loop until no more [idx] or .key is following.
2598 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2603 && !(lp->ll_tv->v_type == VAR_DICT
2604 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_("E689: Can only index a List or Dictionary"));
2608 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (!quiet)
2613 EMSG(_("E708: [:] must come last"));
2614 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002615 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002616
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 len = -1;
2618 if (*p == '.')
2619 {
2620 key = p + 1;
2621 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2622 ;
2623 if (len == 0)
2624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (!quiet)
2626 EMSG(_(e_emptykey));
2627 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 }
2629 p = key + len;
2630 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 else
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 if (*p == ':')
2636 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 else
2638 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 empty1 = FALSE;
2640 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002642 if (get_tv_string_chk(&var1) == NULL)
2643 {
2644 /* not a number or string */
2645 clear_tv(&var1);
2646 return NULL;
2647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 }
2649
2650 /* Optionally get the second index [ :expr]. */
2651 if (*p == ':')
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002656 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657 if (!empty1)
2658 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (rettv != NULL && (rettv->v_type != VAR_LIST
2662 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 if (!empty1)
2667 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670 p = skipwhite(p + 1);
2671 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 else
2674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 if (get_tv_string_chk(&var2) == NULL)
2683 {
2684 /* not a number or string */
2685 if (!empty1)
2686 clear_tv(&var1);
2687 clear_tv(&var2);
2688 return NULL;
2689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
2699 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (!empty1)
2701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706
2707 /* Skip to past ']'. */
2708 ++p;
2709 }
2710
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
2713 if (len == -1)
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002716 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (*key == NUL)
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 lp->ll_list = NULL;
2726 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002727 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002728
2729 /* When assigning to g: check that a function and variable name is
2730 * valid. */
2731 if (rettv != NULL && lp->ll_dict == &globvardict)
2732 {
2733 if (rettv->v_type == VAR_FUNC
2734 && var_check_func_name(key, lp->ll_di == NULL))
2735 return NULL;
2736 if (!valid_varname(key))
2737 return NULL;
2738 }
2739
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002742 /* Can't add "v:" variable. */
2743 if (lp->ll_dict == &vimvardict)
2744 {
2745 EMSG2(_(e_illvar), name);
2746 return NULL;
2747 }
2748
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002749 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (len == -1)
2755 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 }
2758 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 if (len == -1)
2763 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 p = NULL;
2766 break;
2767 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768 /* existing variable, need to check if it can be changed */
2769 else if (var_check_ro(lp->ll_di->di_flags, name))
2770 return NULL;
2771
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 if (len == -1)
2773 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776 else
2777 {
2778 /*
2779 * Get the number and item for the only or first index of the List.
2780 */
2781 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 else
2784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002785 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 clear_tv(&var1);
2787 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002788 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 lp->ll_list = lp->ll_tv->vval.v_list;
2790 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2791 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002793 if (lp->ll_n1 < 0)
2794 {
2795 lp->ll_n1 = 0;
2796 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2797 }
2798 }
2799 if (lp->ll_li == NULL)
2800 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002803 if (!quiet)
2804 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 }
2807
2808 /*
2809 * May need to find the item or absolute index for the second
2810 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 * When no index given: "lp->ll_empty2" is TRUE.
2812 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002816 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002822 {
2823 if (!quiet)
2824 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002826 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 }
2829
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2831 if (lp->ll_n1 < 0)
2832 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2833 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002834 {
2835 if (!quiet)
2836 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002838 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002839 }
2840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002843 }
2844
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 return p;
2846}
2847
2848/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002849 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 */
2851 static void
2852clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002853 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854{
2855 vim_free(lp->ll_exp_name);
2856 vim_free(lp->ll_newkey);
2857}
2858
2859/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002860 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002862 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 */
2864 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002865set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002866 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002870 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871{
2872 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002873 listitem_T *ri;
2874 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875
2876 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002879 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 cc = *endp;
2881 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002882 if (op != NULL && *op != '=')
2883 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002885
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002886 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002887 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002888 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 {
2890 if (tv_op(&tv, rettv, op) == OK)
2891 set_var(lp->ll_name, &tv, FALSE);
2892 clear_tv(&tv);
2893 }
2894 }
2895 else
2896 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002898 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002900 else if (tv_check_lock(lp->ll_newkey == NULL
2901 ? lp->ll_tv->v_lock
2902 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2903 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 else if (lp->ll_range)
2905 {
2906 /*
2907 * Assign the List values to the list items.
2908 */
2909 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002910 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002911 if (op != NULL && *op != '=')
2912 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2913 else
2914 {
2915 clear_tv(&lp->ll_li->li_tv);
2916 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2917 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 ri = ri->li_next;
2919 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2920 break;
2921 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002924 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002925 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 ri = NULL;
2927 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002928 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002929 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 lp->ll_li = lp->ll_li->li_next;
2931 ++lp->ll_n1;
2932 }
2933 if (ri != NULL)
2934 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935 else if (lp->ll_empty2
2936 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 : lp->ll_n1 != lp->ll_n2)
2938 EMSG(_("E711: List value has not enough items"));
2939 }
2940 else
2941 {
2942 /*
2943 * Assign to a List or Dictionary item.
2944 */
2945 if (lp->ll_newkey != NULL)
2946 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 if (op != NULL && *op != '=')
2948 {
2949 EMSG2(_(e_letwrong), op);
2950 return;
2951 }
2952
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002954 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 if (di == NULL)
2956 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002957 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2958 {
2959 vim_free(di);
2960 return;
2961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002963 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002964 else if (op != NULL && *op != '=')
2965 {
2966 tv_op(lp->ll_tv, rettv, op);
2967 return;
2968 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002969 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002971
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 /*
2973 * Assign the value to the variable or list item.
2974 */
2975 if (copy)
2976 copy_tv(rettv, lp->ll_tv);
2977 else
2978 {
2979 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002980 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002982 }
2983 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002984}
2985
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002986/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2988 * Returns OK or FAIL.
2989 */
2990 static int
2991tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002992 typval_T *tv1;
2993 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002994 char_u *op;
2995{
2996 long n;
2997 char_u numbuf[NUMBUFLEN];
2998 char_u *s;
2999
3000 /* Can't do anything with a Funcref or a Dict on the right. */
3001 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3002 {
3003 switch (tv1->v_type)
3004 {
3005 case VAR_DICT:
3006 case VAR_FUNC:
3007 break;
3008
3009 case VAR_LIST:
3010 if (*op != '+' || tv2->v_type != VAR_LIST)
3011 break;
3012 /* List += List */
3013 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3014 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3015 return OK;
3016
3017 case VAR_NUMBER:
3018 case VAR_STRING:
3019 if (tv2->v_type == VAR_LIST)
3020 break;
3021 if (*op == '+' || *op == '-')
3022 {
3023 /* nr += nr or nr -= nr*/
3024 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003025#ifdef FEAT_FLOAT
3026 if (tv2->v_type == VAR_FLOAT)
3027 {
3028 float_T f = n;
3029
3030 if (*op == '+')
3031 f += tv2->vval.v_float;
3032 else
3033 f -= tv2->vval.v_float;
3034 clear_tv(tv1);
3035 tv1->v_type = VAR_FLOAT;
3036 tv1->vval.v_float = f;
3037 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003038 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039#endif
3040 {
3041 if (*op == '+')
3042 n += get_tv_number(tv2);
3043 else
3044 n -= get_tv_number(tv2);
3045 clear_tv(tv1);
3046 tv1->v_type = VAR_NUMBER;
3047 tv1->vval.v_number = n;
3048 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 }
3050 else
3051 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003052 if (tv2->v_type == VAR_FLOAT)
3053 break;
3054
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003055 /* str .= str */
3056 s = get_tv_string(tv1);
3057 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3058 clear_tv(tv1);
3059 tv1->v_type = VAR_STRING;
3060 tv1->vval.v_string = s;
3061 }
3062 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063
3064#ifdef FEAT_FLOAT
3065 case VAR_FLOAT:
3066 {
3067 float_T f;
3068
3069 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3070 && tv2->v_type != VAR_NUMBER
3071 && tv2->v_type != VAR_STRING))
3072 break;
3073 if (tv2->v_type == VAR_FLOAT)
3074 f = tv2->vval.v_float;
3075 else
3076 f = get_tv_number(tv2);
3077 if (*op == '+')
3078 tv1->vval.v_float += f;
3079 else
3080 tv1->vval.v_float -= f;
3081 }
3082 return OK;
3083#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 }
3085 }
3086
3087 EMSG2(_(e_letwrong), op);
3088 return FAIL;
3089}
3090
3091/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092 * Add a watcher to a list.
3093 */
3094 static void
3095list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003096 list_T *l;
3097 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003098{
3099 lw->lw_next = l->lv_watch;
3100 l->lv_watch = lw;
3101}
3102
3103/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003104 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003105 * No warning when it isn't found...
3106 */
3107 static void
3108list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003109 list_T *l;
3110 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003111{
Bram Moolenaar33570922005-01-25 22:26:29 +00003112 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003113
3114 lwp = &l->lv_watch;
3115 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3116 {
3117 if (lw == lwrem)
3118 {
3119 *lwp = lw->lw_next;
3120 break;
3121 }
3122 lwp = &lw->lw_next;
3123 }
3124}
3125
3126/*
3127 * Just before removing an item from a list: advance watchers to the next
3128 * item.
3129 */
3130 static void
3131list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003132 list_T *l;
3133 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134{
Bram Moolenaar33570922005-01-25 22:26:29 +00003135 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003136
3137 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3138 if (lw->lw_item == item)
3139 lw->lw_item = item->li_next;
3140}
3141
3142/*
3143 * Evaluate the expression used in a ":for var in expr" command.
3144 * "arg" points to "var".
3145 * Set "*errp" to TRUE for an error, FALSE otherwise;
3146 * Return a pointer that holds the info. Null when there is an error.
3147 */
3148 void *
3149eval_for_line(arg, errp, nextcmdp, skip)
3150 char_u *arg;
3151 int *errp;
3152 char_u **nextcmdp;
3153 int skip;
3154{
Bram Moolenaar33570922005-01-25 22:26:29 +00003155 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003156 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003157 typval_T tv;
3158 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003159
3160 *errp = TRUE; /* default: there is an error */
3161
Bram Moolenaar33570922005-01-25 22:26:29 +00003162 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003163 if (fi == NULL)
3164 return NULL;
3165
3166 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3167 if (expr == NULL)
3168 return fi;
3169
3170 expr = skipwhite(expr);
3171 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3172 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003173 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 return fi;
3175 }
3176
3177 if (skip)
3178 ++emsg_skip;
3179 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3180 {
3181 *errp = FALSE;
3182 if (!skip)
3183 {
3184 l = tv.vval.v_list;
3185 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003186 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003188 clear_tv(&tv);
3189 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003190 else
3191 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003192 /* No need to increment the refcount, it's already set for the
3193 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 fi->fi_list = l;
3195 list_add_watch(l, &fi->fi_lw);
3196 fi->fi_lw.lw_item = l->lv_first;
3197 }
3198 }
3199 }
3200 if (skip)
3201 --emsg_skip;
3202
3203 return fi;
3204}
3205
3206/*
3207 * Use the first item in a ":for" list. Advance to the next.
3208 * Assign the values to the variable (list). "arg" points to the first one.
3209 * Return TRUE when a valid item was found, FALSE when at end of list or
3210 * something wrong.
3211 */
3212 int
3213next_for_item(fi_void, arg)
3214 void *fi_void;
3215 char_u *arg;
3216{
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220
3221 item = fi->fi_lw.lw_item;
3222 if (item == NULL)
3223 result = FALSE;
3224 else
3225 {
3226 fi->fi_lw.lw_item = item->li_next;
3227 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3228 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3229 }
3230 return result;
3231}
3232
3233/*
3234 * Free the structure used to store info used by ":for".
3235 */
3236 void
3237free_for_info(fi_void)
3238 void *fi_void;
3239{
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003242 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003243 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003245 list_unref(fi->fi_list);
3246 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247 vim_free(fi);
3248}
3249
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3251
3252 void
3253set_context_for_expression(xp, arg, cmdidx)
3254 expand_T *xp;
3255 char_u *arg;
3256 cmdidx_T cmdidx;
3257{
3258 int got_eq = FALSE;
3259 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 if (cmdidx == CMD_let)
3263 {
3264 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003265 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 {
3267 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003268 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 {
3270 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003271 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 if (vim_iswhite(*p))
3273 break;
3274 }
3275 return;
3276 }
3277 }
3278 else
3279 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3280 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 while ((xp->xp_pattern = vim_strpbrk(arg,
3282 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3283 {
3284 c = *xp->xp_pattern;
3285 if (c == '&')
3286 {
3287 c = xp->xp_pattern[1];
3288 if (c == '&')
3289 {
3290 ++xp->xp_pattern;
3291 xp->xp_context = cmdidx != CMD_let || got_eq
3292 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3293 }
3294 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003297 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3298 xp->xp_pattern += 2;
3299
3300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302 else if (c == '$')
3303 {
3304 /* environment variable */
3305 xp->xp_context = EXPAND_ENV_VARS;
3306 }
3307 else if (c == '=')
3308 {
3309 got_eq = TRUE;
3310 xp->xp_context = EXPAND_EXPRESSION;
3311 }
3312 else if (c == '<'
3313 && xp->xp_context == EXPAND_FUNCTIONS
3314 && vim_strchr(xp->xp_pattern, '(') == NULL)
3315 {
3316 /* Function name can start with "<SNR>" */
3317 break;
3318 }
3319 else if (cmdidx != CMD_let || got_eq)
3320 {
3321 if (c == '"') /* string */
3322 {
3323 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3324 if (c == '\\' && xp->xp_pattern[1] != NUL)
3325 ++xp->xp_pattern;
3326 xp->xp_context = EXPAND_NOTHING;
3327 }
3328 else if (c == '\'') /* literal string */
3329 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003330 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3332 /* skip */ ;
3333 xp->xp_context = EXPAND_NOTHING;
3334 }
3335 else if (c == '|')
3336 {
3337 if (xp->xp_pattern[1] == '|')
3338 {
3339 ++xp->xp_pattern;
3340 xp->xp_context = EXPAND_EXPRESSION;
3341 }
3342 else
3343 xp->xp_context = EXPAND_COMMANDS;
3344 }
3345 else
3346 xp->xp_context = EXPAND_EXPRESSION;
3347 }
3348 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 /* Doesn't look like something valid, expand as an expression
3350 * anyway. */
3351 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 arg = xp->xp_pattern;
3353 if (*arg != NUL)
3354 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3355 /* skip */ ;
3356 }
3357 xp->xp_pattern = arg;
3358}
3359
3360#endif /* FEAT_CMDL_COMPL */
3361
3362/*
3363 * ":1,25call func(arg1, arg2)" function call.
3364 */
3365 void
3366ex_call(eap)
3367 exarg_T *eap;
3368{
3369 char_u *arg = eap->arg;
3370 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003372 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003374 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 linenr_T lnum;
3376 int doesrange;
3377 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003378 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003380 if (eap->skip)
3381 {
3382 /* trans_function_name() doesn't work well when skipping, use eval0()
3383 * instead to skip to any following command, e.g. for:
3384 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003385 ++emsg_skip;
3386 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3387 clear_tv(&rettv);
3388 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003389 return;
3390 }
3391
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003392 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003393 if (fudi.fd_newkey != NULL)
3394 {
3395 /* Still need to give an error message for missing key. */
3396 EMSG2(_(e_dictkey), fudi.fd_newkey);
3397 vim_free(fudi.fd_newkey);
3398 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003399 if (tofree == NULL)
3400 return;
3401
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003402 /* Increase refcount on dictionary, it could get deleted when evaluating
3403 * the arguments. */
3404 if (fudi.fd_dict != NULL)
3405 ++fudi.fd_dict->dv_refcount;
3406
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003407 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003408 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003409 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
Bram Moolenaar532c7802005-01-27 14:44:31 +00003411 /* Skip white space to allow ":call func ()". Not good, but required for
3412 * backward compatibility. */
3413 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003414 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
3416 if (*startarg != '(')
3417 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003418 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 goto end;
3420 }
3421
3422 /*
3423 * When skipping, evaluate the function once, to find the end of the
3424 * arguments.
3425 * When the function takes a range, this is discovered after the first
3426 * call, and the loop is broken.
3427 */
3428 if (eap->skip)
3429 {
3430 ++emsg_skip;
3431 lnum = eap->line2; /* do it once, also with an invalid range */
3432 }
3433 else
3434 lnum = eap->line1;
3435 for ( ; lnum <= eap->line2; ++lnum)
3436 {
3437 if (!eap->skip && eap->addr_count > 0)
3438 {
3439 curwin->w_cursor.lnum = lnum;
3440 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003441#ifdef FEAT_VIRTUALEDIT
3442 curwin->w_cursor.coladd = 0;
3443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
3445 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003446 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003447 eap->line1, eap->line2, &doesrange,
3448 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
3450 failed = TRUE;
3451 break;
3452 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003453
3454 /* Handle a function returning a Funcref, Dictionary or List. */
3455 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3456 {
3457 failed = TRUE;
3458 break;
3459 }
3460
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003461 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 if (doesrange || eap->skip)
3463 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003464
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003466 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003468 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (aborting())
3470 break;
3471 }
3472 if (eap->skip)
3473 --emsg_skip;
3474
3475 if (!failed)
3476 {
3477 /* Check for trailing illegal characters and a following command. */
3478 if (!ends_excmd(*arg))
3479 {
3480 emsg_severe = TRUE;
3481 EMSG(_(e_trailing));
3482 }
3483 else
3484 eap->nextcmd = check_nextcmd(arg);
3485 }
3486
3487end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003488 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003489 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490}
3491
3492/*
3493 * ":unlet[!] var1 ... " command.
3494 */
3495 void
3496ex_unlet(eap)
3497 exarg_T *eap;
3498{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003499 ex_unletlock(eap, eap->arg, 0);
3500}
3501
3502/*
3503 * ":lockvar" and ":unlockvar" commands
3504 */
3505 void
3506ex_lockvar(eap)
3507 exarg_T *eap;
3508{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003510 int deep = 2;
3511
3512 if (eap->forceit)
3513 deep = -1;
3514 else if (vim_isdigit(*arg))
3515 {
3516 deep = getdigits(&arg);
3517 arg = skipwhite(arg);
3518 }
3519
3520 ex_unletlock(eap, arg, deep);
3521}
3522
3523/*
3524 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3525 */
3526 static void
3527ex_unletlock(eap, argstart, deep)
3528 exarg_T *eap;
3529 char_u *argstart;
3530 int deep;
3531{
3532 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003535 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537 do
3538 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003540 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3541 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003542 if (lv.ll_name == NULL)
3543 error = TRUE; /* error but continue parsing */
3544 if (name_end == NULL || (!vim_iswhite(*name_end)
3545 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003547 if (name_end != NULL)
3548 {
3549 emsg_severe = TRUE;
3550 EMSG(_(e_trailing));
3551 }
3552 if (!(eap->skip || error))
3553 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 break;
3555 }
3556
3557 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003558 {
3559 if (eap->cmdidx == CMD_unlet)
3560 {
3561 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3562 error = TRUE;
3563 }
3564 else
3565 {
3566 if (do_lock_var(&lv, name_end, deep,
3567 eap->cmdidx == CMD_lockvar) == FAIL)
3568 error = TRUE;
3569 }
3570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003572 if (!eap->skip)
3573 clear_lval(&lv);
3574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 arg = skipwhite(name_end);
3576 } while (!ends_excmd(*arg));
3577
3578 eap->nextcmd = check_nextcmd(arg);
3579}
3580
Bram Moolenaar8c711452005-01-14 21:53:12 +00003581 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003582do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003583 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003584 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003585 int forceit;
3586{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003587 int ret = OK;
3588 int cc;
3589
3590 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003591 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003592 cc = *name_end;
3593 *name_end = NUL;
3594
3595 /* Normal name or expanded name. */
3596 if (check_changedtick(lp->ll_name))
3597 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003600 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003602 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3603 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003604 else if (lp->ll_range)
3605 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003606 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607
3608 /* Delete a range of List items. */
3609 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3610 {
3611 li = lp->ll_li->li_next;
3612 listitem_remove(lp->ll_list, lp->ll_li);
3613 lp->ll_li = li;
3614 ++lp->ll_n1;
3615 }
3616 }
3617 else
3618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 /* unlet a List item. */
3621 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003624 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 }
3626
3627 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003628}
3629
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630/*
3631 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003632 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 */
3634 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003635do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
Bram Moolenaar33570922005-01-25 22:26:29 +00003639 hashtab_T *ht;
3640 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003641 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003642 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar33570922005-01-25 22:26:29 +00003644 ht = find_var_ht(name, &varname);
3645 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003647 hi = hash_find(ht, varname);
3648 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003649 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003650 di = HI2DI(hi);
3651 if (var_check_fixed(di->di_flags, name)
3652 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003653 return FAIL;
3654 delete_var(ht, hi);
3655 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003658 if (forceit)
3659 return OK;
3660 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 return FAIL;
3662}
3663
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003664/*
3665 * Lock or unlock variable indicated by "lp".
3666 * "deep" is the levels to go (-1 for unlimited);
3667 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3668 */
3669 static int
3670do_lock_var(lp, name_end, deep, lock)
3671 lval_T *lp;
3672 char_u *name_end;
3673 int deep;
3674 int lock;
3675{
3676 int ret = OK;
3677 int cc;
3678 dictitem_T *di;
3679
3680 if (deep == 0) /* nothing to do */
3681 return OK;
3682
3683 if (lp->ll_tv == NULL)
3684 {
3685 cc = *name_end;
3686 *name_end = NUL;
3687
3688 /* Normal name or expanded name. */
3689 if (check_changedtick(lp->ll_name))
3690 ret = FAIL;
3691 else
3692 {
3693 di = find_var(lp->ll_name, NULL);
3694 if (di == NULL)
3695 ret = FAIL;
3696 else
3697 {
3698 if (lock)
3699 di->di_flags |= DI_FLAGS_LOCK;
3700 else
3701 di->di_flags &= ~DI_FLAGS_LOCK;
3702 item_lock(&di->di_tv, deep, lock);
3703 }
3704 }
3705 *name_end = cc;
3706 }
3707 else if (lp->ll_range)
3708 {
3709 listitem_T *li = lp->ll_li;
3710
3711 /* (un)lock a range of List items. */
3712 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3713 {
3714 item_lock(&li->li_tv, deep, lock);
3715 li = li->li_next;
3716 ++lp->ll_n1;
3717 }
3718 }
3719 else if (lp->ll_list != NULL)
3720 /* (un)lock a List item. */
3721 item_lock(&lp->ll_li->li_tv, deep, lock);
3722 else
3723 /* un(lock) a Dictionary item. */
3724 item_lock(&lp->ll_di->di_tv, deep, lock);
3725
3726 return ret;
3727}
3728
3729/*
3730 * Lock or unlock an item. "deep" is nr of levels to go.
3731 */
3732 static void
3733item_lock(tv, deep, lock)
3734 typval_T *tv;
3735 int deep;
3736 int lock;
3737{
3738 static int recurse = 0;
3739 list_T *l;
3740 listitem_T *li;
3741 dict_T *d;
3742 hashitem_T *hi;
3743 int todo;
3744
3745 if (recurse >= DICT_MAXNEST)
3746 {
3747 EMSG(_("E743: variable nested too deep for (un)lock"));
3748 return;
3749 }
3750 if (deep == 0)
3751 return;
3752 ++recurse;
3753
3754 /* lock/unlock the item itself */
3755 if (lock)
3756 tv->v_lock |= VAR_LOCKED;
3757 else
3758 tv->v_lock &= ~VAR_LOCKED;
3759
3760 switch (tv->v_type)
3761 {
3762 case VAR_LIST:
3763 if ((l = tv->vval.v_list) != NULL)
3764 {
3765 if (lock)
3766 l->lv_lock |= VAR_LOCKED;
3767 else
3768 l->lv_lock &= ~VAR_LOCKED;
3769 if (deep < 0 || deep > 1)
3770 /* recursive: lock/unlock the items the List contains */
3771 for (li = l->lv_first; li != NULL; li = li->li_next)
3772 item_lock(&li->li_tv, deep - 1, lock);
3773 }
3774 break;
3775 case VAR_DICT:
3776 if ((d = tv->vval.v_dict) != NULL)
3777 {
3778 if (lock)
3779 d->dv_lock |= VAR_LOCKED;
3780 else
3781 d->dv_lock &= ~VAR_LOCKED;
3782 if (deep < 0 || deep > 1)
3783 {
3784 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003785 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003786 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3787 {
3788 if (!HASHITEM_EMPTY(hi))
3789 {
3790 --todo;
3791 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3792 }
3793 }
3794 }
3795 }
3796 }
3797 --recurse;
3798}
3799
Bram Moolenaara40058a2005-07-11 22:42:07 +00003800/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003801 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3802 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003803 */
3804 static int
3805tv_islocked(tv)
3806 typval_T *tv;
3807{
3808 return (tv->v_lock & VAR_LOCKED)
3809 || (tv->v_type == VAR_LIST
3810 && tv->vval.v_list != NULL
3811 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3812 || (tv->v_type == VAR_DICT
3813 && tv->vval.v_dict != NULL
3814 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3815}
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3818/*
3819 * Delete all "menutrans_" variables.
3820 */
3821 void
3822del_menutrans_vars()
3823{
Bram Moolenaar33570922005-01-25 22:26:29 +00003824 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003825 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
Bram Moolenaar33570922005-01-25 22:26:29 +00003827 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003828 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003829 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003830 {
3831 if (!HASHITEM_EMPTY(hi))
3832 {
3833 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003834 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3835 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003836 }
3837 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839}
3840#endif
3841
3842#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3843
3844/*
3845 * Local string buffer for the next two functions to store a variable name
3846 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3847 * get_user_var_name().
3848 */
3849
3850static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3851
3852static char_u *varnamebuf = NULL;
3853static int varnamebuflen = 0;
3854
3855/*
3856 * Function to concatenate a prefix and a variable name.
3857 */
3858 static char_u *
3859cat_prefix_varname(prefix, name)
3860 int prefix;
3861 char_u *name;
3862{
3863 int len;
3864
3865 len = (int)STRLEN(name) + 3;
3866 if (len > varnamebuflen)
3867 {
3868 vim_free(varnamebuf);
3869 len += 10; /* some additional space */
3870 varnamebuf = alloc(len);
3871 if (varnamebuf == NULL)
3872 {
3873 varnamebuflen = 0;
3874 return NULL;
3875 }
3876 varnamebuflen = len;
3877 }
3878 *varnamebuf = prefix;
3879 varnamebuf[1] = ':';
3880 STRCPY(varnamebuf + 2, name);
3881 return varnamebuf;
3882}
3883
3884/*
3885 * Function given to ExpandGeneric() to obtain the list of user defined
3886 * (global/buffer/window/built-in) variable names.
3887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 char_u *
3889get_user_var_name(xp, idx)
3890 expand_T *xp;
3891 int idx;
3892{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003893 static long_u gdone;
3894 static long_u bdone;
3895 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003896#ifdef FEAT_WINDOWS
3897 static long_u tdone;
3898#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003899 static int vidx;
3900 static hashitem_T *hi;
3901 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902
3903 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003904 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003905 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003906#ifdef FEAT_WINDOWS
3907 tdone = 0;
3908#endif
3909 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003910
3911 /* Global variables */
3912 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003914 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003915 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003916 else
3917 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003918 while (HASHITEM_EMPTY(hi))
3919 ++hi;
3920 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3921 return cat_prefix_varname('g', hi->hi_key);
3922 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003924
3925 /* b: variables */
3926 ht = &curbuf->b_vars.dv_hashtab;
3927 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003931 else
3932 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003933 while (HASHITEM_EMPTY(hi))
3934 ++hi;
3935 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 return (char_u *)"b:changedtick";
3941 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003942
3943 /* w: variables */
3944 ht = &curwin->w_vars.dv_hashtab;
3945 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003947 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003949 else
3950 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003951 while (HASHITEM_EMPTY(hi))
3952 ++hi;
3953 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003955
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003956#ifdef FEAT_WINDOWS
3957 /* t: variables */
3958 ht = &curtab->tp_vars.dv_hashtab;
3959 if (tdone < ht->ht_used)
3960 {
3961 if (tdone++ == 0)
3962 hi = ht->ht_array;
3963 else
3964 ++hi;
3965 while (HASHITEM_EMPTY(hi))
3966 ++hi;
3967 return cat_prefix_varname('t', hi->hi_key);
3968 }
3969#endif
3970
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 /* v: variables */
3972 if (vidx < VV_LEN)
3973 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975 vim_free(varnamebuf);
3976 varnamebuf = NULL;
3977 varnamebuflen = 0;
3978 return NULL;
3979}
3980
3981#endif /* FEAT_CMDL_COMPL */
3982
3983/*
3984 * types for expressions.
3985 */
3986typedef enum
3987{
3988 TYPE_UNKNOWN = 0
3989 , TYPE_EQUAL /* == */
3990 , TYPE_NEQUAL /* != */
3991 , TYPE_GREATER /* > */
3992 , TYPE_GEQUAL /* >= */
3993 , TYPE_SMALLER /* < */
3994 , TYPE_SEQUAL /* <= */
3995 , TYPE_MATCH /* =~ */
3996 , TYPE_NOMATCH /* !~ */
3997} exptype_T;
3998
3999/*
4000 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4003 */
4004
4005/*
4006 * Handle zero level expression.
4007 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004008 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004009 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 * Return OK or FAIL.
4011 */
4012 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004013eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 char_u **nextcmd;
4017 int evaluate;
4018{
4019 int ret;
4020 char_u *p;
4021
4022 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 if (ret == FAIL || !ends_excmd(*p))
4025 {
4026 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 /*
4029 * Report the invalid expression unless the expression evaluation has
4030 * been cancelled due to an aborting error, an interrupt, or an
4031 * exception.
4032 */
4033 if (!aborting())
4034 EMSG2(_(e_invexpr2), arg);
4035 ret = FAIL;
4036 }
4037 if (nextcmd != NULL)
4038 *nextcmd = check_nextcmd(p);
4039
4040 return ret;
4041}
4042
4043/*
4044 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004045 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 *
4047 * "arg" must point to the first non-white of the expression.
4048 * "arg" is advanced to the next non-white after the recognized expression.
4049 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004050 * Note: "rettv.v_lock" is not set.
4051 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 * Return OK or FAIL.
4053 */
4054 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 int evaluate;
4059{
4060 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 /*
4064 * Get the first variable.
4065 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004066 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 return FAIL;
4068
4069 if ((*arg)[0] == '?')
4070 {
4071 result = FALSE;
4072 if (evaluate)
4073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004074 int error = FALSE;
4075
4076 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004079 if (error)
4080 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082
4083 /*
4084 * Get the second variable.
4085 */
4086 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 return FAIL;
4089
4090 /*
4091 * Check for the ":".
4092 */
4093 if ((*arg)[0] != ':')
4094 {
4095 EMSG(_("E109: Missing ':' after '?'"));
4096 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 return FAIL;
4099 }
4100
4101 /*
4102 * Get the third variable.
4103 */
4104 *arg = skipwhite(*arg + 1);
4105 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4106 {
4107 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 return FAIL;
4110 }
4111 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114
4115 return OK;
4116}
4117
4118/*
4119 * Handle first level expression:
4120 * expr2 || expr2 || expr2 logical OR
4121 *
4122 * "arg" must point to the first non-white of the expression.
4123 * "arg" is advanced to the next non-white after the recognized expression.
4124 *
4125 * Return OK or FAIL.
4126 */
4127 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004128eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 int evaluate;
4132{
Bram Moolenaar33570922005-01-25 22:26:29 +00004133 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 long result;
4135 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004136 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138 /*
4139 * Get the first variable.
4140 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 return FAIL;
4143
4144 /*
4145 * Repeat until there is no following "||".
4146 */
4147 first = TRUE;
4148 result = FALSE;
4149 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4150 {
4151 if (evaluate && first)
4152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004156 if (error)
4157 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 first = FALSE;
4159 }
4160
4161 /*
4162 * Get the second variable.
4163 */
4164 *arg = skipwhite(*arg + 2);
4165 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4166 return FAIL;
4167
4168 /*
4169 * Compute the result.
4170 */
4171 if (evaluate && !result)
4172 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004175 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004176 if (error)
4177 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 if (evaluate)
4180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 rettv->v_type = VAR_NUMBER;
4182 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184 }
4185
4186 return OK;
4187}
4188
4189/*
4190 * Handle second level expression:
4191 * expr3 && expr3 && expr3 logical AND
4192 *
4193 * "arg" must point to the first non-white of the expression.
4194 * "arg" is advanced to the next non-white after the recognized expression.
4195 *
4196 * Return OK or FAIL.
4197 */
4198 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 int evaluate;
4203{
Bram Moolenaar33570922005-01-25 22:26:29 +00004204 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 long result;
4206 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
4209 /*
4210 * Get the first variable.
4211 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return FAIL;
4214
4215 /*
4216 * Repeat until there is no following "&&".
4217 */
4218 first = TRUE;
4219 result = TRUE;
4220 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4221 {
4222 if (evaluate && first)
4223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004224 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004227 if (error)
4228 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 first = FALSE;
4230 }
4231
4232 /*
4233 * Get the second variable.
4234 */
4235 *arg = skipwhite(*arg + 2);
4236 if (eval4(arg, &var2, evaluate && result) == FAIL)
4237 return FAIL;
4238
4239 /*
4240 * Compute the result.
4241 */
4242 if (evaluate && result)
4243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247 if (error)
4248 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 if (evaluate)
4251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 rettv->v_type = VAR_NUMBER;
4253 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
4255 }
4256
4257 return OK;
4258}
4259
4260/*
4261 * Handle third level expression:
4262 * var1 == var2
4263 * var1 =~ var2
4264 * var1 != var2
4265 * var1 !~ var2
4266 * var1 > var2
4267 * var1 >= var2
4268 * var1 < var2
4269 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004270 * var1 is var2
4271 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 *
4273 * "arg" must point to the first non-white of the expression.
4274 * "arg" is advanced to the next non-white after the recognized expression.
4275 *
4276 * Return OK or FAIL.
4277 */
4278 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 int evaluate;
4283{
Bram Moolenaar33570922005-01-25 22:26:29 +00004284 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 char_u *p;
4286 int i;
4287 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004288 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 int len = 2;
4290 long n1, n2;
4291 char_u *s1, *s2;
4292 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4293 regmatch_T regmatch;
4294 int ic;
4295 char_u *save_cpo;
4296
4297 /*
4298 * Get the first variable.
4299 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 return FAIL;
4302
4303 p = *arg;
4304 switch (p[0])
4305 {
4306 case '=': if (p[1] == '=')
4307 type = TYPE_EQUAL;
4308 else if (p[1] == '~')
4309 type = TYPE_MATCH;
4310 break;
4311 case '!': if (p[1] == '=')
4312 type = TYPE_NEQUAL;
4313 else if (p[1] == '~')
4314 type = TYPE_NOMATCH;
4315 break;
4316 case '>': if (p[1] != '=')
4317 {
4318 type = TYPE_GREATER;
4319 len = 1;
4320 }
4321 else
4322 type = TYPE_GEQUAL;
4323 break;
4324 case '<': if (p[1] != '=')
4325 {
4326 type = TYPE_SMALLER;
4327 len = 1;
4328 }
4329 else
4330 type = TYPE_SEQUAL;
4331 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004332 case 'i': if (p[1] == 's')
4333 {
4334 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4335 len = 5;
4336 if (!vim_isIDc(p[len]))
4337 {
4338 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4339 type_is = TRUE;
4340 }
4341 }
4342 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344
4345 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004346 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 */
4348 if (type != TYPE_UNKNOWN)
4349 {
4350 /* extra question mark appended: ignore case */
4351 if (p[len] == '?')
4352 {
4353 ic = TRUE;
4354 ++len;
4355 }
4356 /* extra '#' appended: match case */
4357 else if (p[len] == '#')
4358 {
4359 ic = FALSE;
4360 ++len;
4361 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004362 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 else
4364 ic = p_ic;
4365
4366 /*
4367 * Get the second variable.
4368 */
4369 *arg = skipwhite(p + len);
4370 if (eval5(arg, &var2, evaluate) == FAIL)
4371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004372 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 return FAIL;
4374 }
4375
4376 if (evaluate)
4377 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004378 if (type_is && rettv->v_type != var2.v_type)
4379 {
4380 /* For "is" a different type always means FALSE, for "notis"
4381 * it means TRUE. */
4382 n1 = (type == TYPE_NEQUAL);
4383 }
4384 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4385 {
4386 if (type_is)
4387 {
4388 n1 = (rettv->v_type == var2.v_type
4389 && rettv->vval.v_list == var2.vval.v_list);
4390 if (type == TYPE_NEQUAL)
4391 n1 = !n1;
4392 }
4393 else if (rettv->v_type != var2.v_type
4394 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4395 {
4396 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004397 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004398 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004399 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004400 clear_tv(rettv);
4401 clear_tv(&var2);
4402 return FAIL;
4403 }
4404 else
4405 {
4406 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004407 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4408 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004409 if (type == TYPE_NEQUAL)
4410 n1 = !n1;
4411 }
4412 }
4413
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004414 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4415 {
4416 if (type_is)
4417 {
4418 n1 = (rettv->v_type == var2.v_type
4419 && rettv->vval.v_dict == var2.vval.v_dict);
4420 if (type == TYPE_NEQUAL)
4421 n1 = !n1;
4422 }
4423 else if (rettv->v_type != var2.v_type
4424 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4425 {
4426 if (rettv->v_type != var2.v_type)
4427 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4428 else
4429 EMSG(_("E736: Invalid operation for Dictionary"));
4430 clear_tv(rettv);
4431 clear_tv(&var2);
4432 return FAIL;
4433 }
4434 else
4435 {
4436 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004437 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4438 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004439 if (type == TYPE_NEQUAL)
4440 n1 = !n1;
4441 }
4442 }
4443
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004444 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4445 {
4446 if (rettv->v_type != var2.v_type
4447 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4448 {
4449 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004450 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004451 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004452 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004453 clear_tv(rettv);
4454 clear_tv(&var2);
4455 return FAIL;
4456 }
4457 else
4458 {
4459 /* Compare two Funcrefs for being equal or unequal. */
4460 if (rettv->vval.v_string == NULL
4461 || var2.vval.v_string == NULL)
4462 n1 = FALSE;
4463 else
4464 n1 = STRCMP(rettv->vval.v_string,
4465 var2.vval.v_string) == 0;
4466 if (type == TYPE_NEQUAL)
4467 n1 = !n1;
4468 }
4469 }
4470
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004471#ifdef FEAT_FLOAT
4472 /*
4473 * If one of the two variables is a float, compare as a float.
4474 * When using "=~" or "!~", always compare as string.
4475 */
4476 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4477 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4478 {
4479 float_T f1, f2;
4480
4481 if (rettv->v_type == VAR_FLOAT)
4482 f1 = rettv->vval.v_float;
4483 else
4484 f1 = get_tv_number(rettv);
4485 if (var2.v_type == VAR_FLOAT)
4486 f2 = var2.vval.v_float;
4487 else
4488 f2 = get_tv_number(&var2);
4489 n1 = FALSE;
4490 switch (type)
4491 {
4492 case TYPE_EQUAL: n1 = (f1 == f2); break;
4493 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4494 case TYPE_GREATER: n1 = (f1 > f2); break;
4495 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4496 case TYPE_SMALLER: n1 = (f1 < f2); break;
4497 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4498 case TYPE_UNKNOWN:
4499 case TYPE_MATCH:
4500 case TYPE_NOMATCH: break; /* avoid gcc warning */
4501 }
4502 }
4503#endif
4504
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 /*
4506 * If one of the two variables is a number, compare as a number.
4507 * When using "=~" or "!~", always compare as string.
4508 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4511 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004512 n1 = get_tv_number(rettv);
4513 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 switch (type)
4515 {
4516 case TYPE_EQUAL: n1 = (n1 == n2); break;
4517 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4518 case TYPE_GREATER: n1 = (n1 > n2); break;
4519 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4520 case TYPE_SMALLER: n1 = (n1 < n2); break;
4521 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4522 case TYPE_UNKNOWN:
4523 case TYPE_MATCH:
4524 case TYPE_NOMATCH: break; /* avoid gcc warning */
4525 }
4526 }
4527 else
4528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004529 s1 = get_tv_string_buf(rettv, buf1);
4530 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4532 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4533 else
4534 i = 0;
4535 n1 = FALSE;
4536 switch (type)
4537 {
4538 case TYPE_EQUAL: n1 = (i == 0); break;
4539 case TYPE_NEQUAL: n1 = (i != 0); break;
4540 case TYPE_GREATER: n1 = (i > 0); break;
4541 case TYPE_GEQUAL: n1 = (i >= 0); break;
4542 case TYPE_SMALLER: n1 = (i < 0); break;
4543 case TYPE_SEQUAL: n1 = (i <= 0); break;
4544
4545 case TYPE_MATCH:
4546 case TYPE_NOMATCH:
4547 /* avoid 'l' flag in 'cpoptions' */
4548 save_cpo = p_cpo;
4549 p_cpo = (char_u *)"";
4550 regmatch.regprog = vim_regcomp(s2,
4551 RE_MAGIC + RE_STRING);
4552 regmatch.rm_ic = ic;
4553 if (regmatch.regprog != NULL)
4554 {
4555 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4556 vim_free(regmatch.regprog);
4557 if (type == TYPE_NOMATCH)
4558 n1 = !n1;
4559 }
4560 p_cpo = save_cpo;
4561 break;
4562
4563 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4564 }
4565 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004566 clear_tv(rettv);
4567 clear_tv(&var2);
4568 rettv->v_type = VAR_NUMBER;
4569 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 }
4571 }
4572
4573 return OK;
4574}
4575
4576/*
4577 * Handle fourth level expression:
4578 * + number addition
4579 * - number subtraction
4580 * . string concatenation
4581 *
4582 * "arg" must point to the first non-white of the expression.
4583 * "arg" is advanced to the next non-white after the recognized expression.
4584 *
4585 * Return OK or FAIL.
4586 */
4587 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004588eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 int evaluate;
4592{
Bram Moolenaar33570922005-01-25 22:26:29 +00004593 typval_T var2;
4594 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 int op;
4596 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004597#ifdef FEAT_FLOAT
4598 float_T f1 = 0, f2 = 0;
4599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 char_u *s1, *s2;
4601 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4602 char_u *p;
4603
4604 /*
4605 * Get the first variable.
4606 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004607 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 return FAIL;
4609
4610 /*
4611 * Repeat computing, until no '+', '-' or '.' is following.
4612 */
4613 for (;;)
4614 {
4615 op = **arg;
4616 if (op != '+' && op != '-' && op != '.')
4617 break;
4618
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004619 if ((op != '+' || rettv->v_type != VAR_LIST)
4620#ifdef FEAT_FLOAT
4621 && (op == '.' || rettv->v_type != VAR_FLOAT)
4622#endif
4623 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004624 {
4625 /* For "list + ...", an illegal use of the first operand as
4626 * a number cannot be determined before evaluating the 2nd
4627 * operand: if this is also a list, all is ok.
4628 * For "something . ...", "something - ..." or "non-list + ...",
4629 * we know that the first operand needs to be a string or number
4630 * without evaluating the 2nd operand. So check before to avoid
4631 * side effects after an error. */
4632 if (evaluate && get_tv_string_chk(rettv) == NULL)
4633 {
4634 clear_tv(rettv);
4635 return FAIL;
4636 }
4637 }
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 /*
4640 * Get the second variable.
4641 */
4642 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004643 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004645 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 return FAIL;
4647 }
4648
4649 if (evaluate)
4650 {
4651 /*
4652 * Compute the result.
4653 */
4654 if (op == '.')
4655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004656 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4657 s2 = get_tv_string_buf_chk(&var2, buf2);
4658 if (s2 == NULL) /* type error ? */
4659 {
4660 clear_tv(rettv);
4661 clear_tv(&var2);
4662 return FAIL;
4663 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004664 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 clear_tv(rettv);
4666 rettv->v_type = VAR_STRING;
4667 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004669 else if (op == '+' && rettv->v_type == VAR_LIST
4670 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004671 {
4672 /* concatenate Lists */
4673 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4674 &var3) == FAIL)
4675 {
4676 clear_tv(rettv);
4677 clear_tv(&var2);
4678 return FAIL;
4679 }
4680 clear_tv(rettv);
4681 *rettv = var3;
4682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 else
4684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004685 int error = FALSE;
4686
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004687#ifdef FEAT_FLOAT
4688 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004689 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004690 f1 = rettv->vval.v_float;
4691 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004692 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004693 else
4694#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004695 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696 n1 = get_tv_number_chk(rettv, &error);
4697 if (error)
4698 {
4699 /* This can only happen for "list + non-list". For
4700 * "non-list + ..." or "something - ...", we returned
4701 * before evaluating the 2nd operand. */
4702 clear_tv(rettv);
4703 return FAIL;
4704 }
4705#ifdef FEAT_FLOAT
4706 if (var2.v_type == VAR_FLOAT)
4707 f1 = n1;
4708#endif
4709 }
4710#ifdef FEAT_FLOAT
4711 if (var2.v_type == VAR_FLOAT)
4712 {
4713 f2 = var2.vval.v_float;
4714 n2 = 0;
4715 }
4716 else
4717#endif
4718 {
4719 n2 = get_tv_number_chk(&var2, &error);
4720 if (error)
4721 {
4722 clear_tv(rettv);
4723 clear_tv(&var2);
4724 return FAIL;
4725 }
4726#ifdef FEAT_FLOAT
4727 if (rettv->v_type == VAR_FLOAT)
4728 f2 = n2;
4729#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004730 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004731 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004732
4733#ifdef FEAT_FLOAT
4734 /* If there is a float on either side the result is a float. */
4735 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4736 {
4737 if (op == '+')
4738 f1 = f1 + f2;
4739 else
4740 f1 = f1 - f2;
4741 rettv->v_type = VAR_FLOAT;
4742 rettv->vval.v_float = f1;
4743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004745#endif
4746 {
4747 if (op == '+')
4748 n1 = n1 + n2;
4749 else
4750 n1 = n1 - n2;
4751 rettv->v_type = VAR_NUMBER;
4752 rettv->vval.v_number = n1;
4753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004755 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 }
4758 return OK;
4759}
4760
4761/*
4762 * Handle fifth level expression:
4763 * * number multiplication
4764 * / number division
4765 * % number modulo
4766 *
4767 * "arg" must point to the first non-white of the expression.
4768 * "arg" is advanced to the next non-white after the recognized expression.
4769 *
4770 * Return OK or FAIL.
4771 */
4772 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004773eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004777 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
Bram Moolenaar33570922005-01-25 22:26:29 +00004779 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 int op;
4781 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004782#ifdef FEAT_FLOAT
4783 int use_float = FALSE;
4784 float_T f1 = 0, f2;
4785#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004786 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
4788 /*
4789 * Get the first variable.
4790 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004791 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 return FAIL;
4793
4794 /*
4795 * Repeat computing, until no '*', '/' or '%' is following.
4796 */
4797 for (;;)
4798 {
4799 op = **arg;
4800 if (op != '*' && op != '/' && op != '%')
4801 break;
4802
4803 if (evaluate)
4804 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805#ifdef FEAT_FLOAT
4806 if (rettv->v_type == VAR_FLOAT)
4807 {
4808 f1 = rettv->vval.v_float;
4809 use_float = TRUE;
4810 n1 = 0;
4811 }
4812 else
4813#endif
4814 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004816 if (error)
4817 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819 else
4820 n1 = 0;
4821
4822 /*
4823 * Get the second variable.
4824 */
4825 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004826 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 return FAIL;
4828
4829 if (evaluate)
4830 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831#ifdef FEAT_FLOAT
4832 if (var2.v_type == VAR_FLOAT)
4833 {
4834 if (!use_float)
4835 {
4836 f1 = n1;
4837 use_float = TRUE;
4838 }
4839 f2 = var2.vval.v_float;
4840 n2 = 0;
4841 }
4842 else
4843#endif
4844 {
4845 n2 = get_tv_number_chk(&var2, &error);
4846 clear_tv(&var2);
4847 if (error)
4848 return FAIL;
4849#ifdef FEAT_FLOAT
4850 if (use_float)
4851 f2 = n2;
4852#endif
4853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
4855 /*
4856 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004859#ifdef FEAT_FLOAT
4860 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004862 if (op == '*')
4863 f1 = f1 * f2;
4864 else if (op == '/')
4865 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004866# ifdef VMS
4867 /* VMS crashes on divide by zero, work around it */
4868 if (f2 == 0.0)
4869 {
4870 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004871 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004872 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004873 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004874 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004875 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004876 }
4877 else
4878 f1 = f1 / f2;
4879# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880 /* We rely on the floating point library to handle divide
4881 * by zero to result in "inf" and not a crash. */
4882 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004883# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004886 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004887 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004888 return FAIL;
4889 }
4890 rettv->v_type = VAR_FLOAT;
4891 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
4893 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004896 if (op == '*')
4897 n1 = n1 * n2;
4898 else if (op == '/')
4899 {
4900 if (n2 == 0) /* give an error message? */
4901 {
4902 if (n1 == 0)
4903 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4904 else if (n1 < 0)
4905 n1 = -0x7fffffffL;
4906 else
4907 n1 = 0x7fffffffL;
4908 }
4909 else
4910 n1 = n1 / n2;
4911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913 {
4914 if (n2 == 0) /* give an error message? */
4915 n1 = 0;
4916 else
4917 n1 = n1 % n2;
4918 }
4919 rettv->v_type = VAR_NUMBER;
4920 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 }
4924
4925 return OK;
4926}
4927
4928/*
4929 * Handle sixth level expression:
4930 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004931 * "string" string constant
4932 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 * &option-name option value
4934 * @r register contents
4935 * identifier variable value
4936 * function() function call
4937 * $VAR environment variable
4938 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004939 * [expr, expr] List
4940 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 *
4942 * Also handle:
4943 * ! in front logical NOT
4944 * - in front unary minus
4945 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004946 * trailing [] subscript in String or List
4947 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 *
4949 * "arg" must point to the first non-white of the expression.
4950 * "arg" is advanced to the next non-white after the recognized expression.
4951 *
4952 * Return OK or FAIL.
4953 */
4954 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004955eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004959 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 long n;
4962 int len;
4963 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 char_u *start_leader, *end_leader;
4965 int ret = OK;
4966 char_u *alias;
4967
4968 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004969 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004970 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004972 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974 /*
4975 * Skip '!' and '-' characters. They are handled later.
4976 */
4977 start_leader = *arg;
4978 while (**arg == '!' || **arg == '-' || **arg == '+')
4979 *arg = skipwhite(*arg + 1);
4980 end_leader = *arg;
4981
4982 switch (**arg)
4983 {
4984 /*
4985 * Number constant.
4986 */
4987 case '0':
4988 case '1':
4989 case '2':
4990 case '3':
4991 case '4':
4992 case '5':
4993 case '6':
4994 case '7':
4995 case '8':
4996 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 {
4998#ifdef FEAT_FLOAT
4999 char_u *p = skipdigits(*arg + 1);
5000 int get_float = FALSE;
5001
5002 /* We accept a float when the format matches
5003 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005004 * strict to avoid backwards compatibility problems.
5005 * Don't look for a float after the "." operator, so that
5006 * ":let vers = 1.2.3" doesn't fail. */
5007 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 get_float = TRUE;
5010 p = skipdigits(p + 2);
5011 if (*p == 'e' || *p == 'E')
5012 {
5013 ++p;
5014 if (*p == '-' || *p == '+')
5015 ++p;
5016 if (!vim_isdigit(*p))
5017 get_float = FALSE;
5018 else
5019 p = skipdigits(p + 1);
5020 }
5021 if (ASCII_ISALPHA(*p) || *p == '.')
5022 get_float = FALSE;
5023 }
5024 if (get_float)
5025 {
5026 float_T f;
5027
5028 *arg += string2float(*arg, &f);
5029 if (evaluate)
5030 {
5031 rettv->v_type = VAR_FLOAT;
5032 rettv->vval.v_float = f;
5033 }
5034 }
5035 else
5036#endif
5037 {
5038 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5039 *arg += len;
5040 if (evaluate)
5041 {
5042 rettv->v_type = VAR_NUMBER;
5043 rettv->vval.v_number = n;
5044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
5049 /*
5050 * String constant: "string".
5051 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 break;
5054
5055 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005058 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005059 break;
5060
5061 /*
5062 * List: [expr, expr]
5063 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 break;
5066
5067 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 * Dictionary: {key: val, key: val}
5069 */
5070 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5071 break;
5072
5073 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005074 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005076 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 break;
5078
5079 /*
5080 * Environment variable: $VAR.
5081 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 break;
5084
5085 /*
5086 * Register contents: @r.
5087 */
5088 case '@': ++*arg;
5089 if (evaluate)
5090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005091 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005092 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094 if (**arg != NUL)
5095 ++*arg;
5096 break;
5097
5098 /*
5099 * nested expression: (expression).
5100 */
5101 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005102 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (**arg == ')')
5104 ++*arg;
5105 else if (ret == OK)
5106 {
5107 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005108 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 ret = FAIL;
5110 }
5111 break;
5112
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 break;
5115 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116
5117 if (ret == NOTDONE)
5118 {
5119 /*
5120 * Must be a variable or function name.
5121 * Can also be a curly-braces kind of name: {expr}.
5122 */
5123 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005124 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 if (alias != NULL)
5126 s = alias;
5127
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005128 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 ret = FAIL;
5130 else
5131 {
5132 if (**arg == '(') /* recursive! */
5133 {
5134 /* If "s" is the name of a variable of type VAR_FUNC
5135 * use its contents. */
5136 s = deref_func_name(s, &len);
5137
5138 /* Invoke the function. */
5139 ret = get_func_tv(s, len, rettv, arg,
5140 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005141 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 /* Stop the expression evaluation when immediately
5143 * aborting on error, or when an interrupt occurred or
5144 * an exception was thrown but not caught. */
5145 if (aborting())
5146 {
5147 if (ret == OK)
5148 clear_tv(rettv);
5149 ret = FAIL;
5150 }
5151 }
5152 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005153 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005154 else
5155 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005157 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 }
5159
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 *arg = skipwhite(*arg);
5161
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005162 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5163 * expr(expr). */
5164 if (ret == OK)
5165 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166
5167 /*
5168 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5169 */
5170 if (ret == OK && evaluate && end_leader > start_leader)
5171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005172 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005173 int val = 0;
5174#ifdef FEAT_FLOAT
5175 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005176
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005177 if (rettv->v_type == VAR_FLOAT)
5178 f = rettv->vval.v_float;
5179 else
5180#endif
5181 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005182 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005184 clear_tv(rettv);
5185 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005187 else
5188 {
5189 while (end_leader > start_leader)
5190 {
5191 --end_leader;
5192 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005193 {
5194#ifdef FEAT_FLOAT
5195 if (rettv->v_type == VAR_FLOAT)
5196 f = !f;
5197 else
5198#endif
5199 val = !val;
5200 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005202 {
5203#ifdef FEAT_FLOAT
5204 if (rettv->v_type == VAR_FLOAT)
5205 f = -f;
5206 else
5207#endif
5208 val = -val;
5209 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005210 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005211#ifdef FEAT_FLOAT
5212 if (rettv->v_type == VAR_FLOAT)
5213 {
5214 clear_tv(rettv);
5215 rettv->vval.v_float = f;
5216 }
5217 else
5218#endif
5219 {
5220 clear_tv(rettv);
5221 rettv->v_type = VAR_NUMBER;
5222 rettv->vval.v_number = val;
5223 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226
5227 return ret;
5228}
5229
5230/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005231 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5232 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5234 */
5235 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005236eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005238 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005240 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241{
5242 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005243 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 long len = -1;
5246 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005250 if (rettv->v_type == VAR_FUNC
5251#ifdef FEAT_FLOAT
5252 || rettv->v_type == VAR_FLOAT
5253#endif
5254 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005256 if (verbose)
5257 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 return FAIL;
5259 }
5260
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 /*
5264 * dict.name
5265 */
5266 key = *arg + 1;
5267 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5268 ;
5269 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 }
5273 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 /*
5276 * something[idx]
5277 *
5278 * Get the (first) variable from inside the [].
5279 */
5280 *arg = skipwhite(*arg + 1);
5281 if (**arg == ':')
5282 empty1 = TRUE;
5283 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5284 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5286 {
5287 /* not a number or string */
5288 clear_tv(&var1);
5289 return FAIL;
5290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291
5292 /*
5293 * Get the second variable from inside the [:].
5294 */
5295 if (**arg == ':')
5296 {
5297 range = TRUE;
5298 *arg = skipwhite(*arg + 1);
5299 if (**arg == ']')
5300 empty2 = TRUE;
5301 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 if (!empty1)
5304 clear_tv(&var1);
5305 return FAIL;
5306 }
5307 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5308 {
5309 /* not a number or string */
5310 if (!empty1)
5311 clear_tv(&var1);
5312 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313 return FAIL;
5314 }
5315 }
5316
5317 /* Check for the ']'. */
5318 if (**arg != ']')
5319 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005320 if (verbose)
5321 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 clear_tv(&var1);
5323 if (range)
5324 clear_tv(&var2);
5325 return FAIL;
5326 }
5327 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 }
5329
5330 if (evaluate)
5331 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332 n1 = 0;
5333 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005335 n1 = get_tv_number(&var1);
5336 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 }
5338 if (range)
5339 {
5340 if (empty2)
5341 n2 = -1;
5342 else
5343 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005344 n2 = get_tv_number(&var2);
5345 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 }
5347 }
5348
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005349 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 {
5351 case VAR_NUMBER:
5352 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005353 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 len = (long)STRLEN(s);
5355 if (range)
5356 {
5357 /* The resulting variable is a substring. If the indexes
5358 * are out of range the result is empty. */
5359 if (n1 < 0)
5360 {
5361 n1 = len + n1;
5362 if (n1 < 0)
5363 n1 = 0;
5364 }
5365 if (n2 < 0)
5366 n2 = len + n2;
5367 else if (n2 >= len)
5368 n2 = len;
5369 if (n1 >= len || n2 < 0 || n1 > n2)
5370 s = NULL;
5371 else
5372 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5373 }
5374 else
5375 {
5376 /* The resulting variable is a string of a single
5377 * character. If the index is too big or negative the
5378 * result is empty. */
5379 if (n1 >= len || n1 < 0)
5380 s = NULL;
5381 else
5382 s = vim_strnsave(s + n1, 1);
5383 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 clear_tv(rettv);
5385 rettv->v_type = VAR_STRING;
5386 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 break;
5388
5389 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005390 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 if (n1 < 0)
5392 n1 = len + n1;
5393 if (!empty1 && (n1 < 0 || n1 >= len))
5394 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005395 /* For a range we allow invalid values and return an empty
5396 * list. A list index out of range is an error. */
5397 if (!range)
5398 {
5399 if (verbose)
5400 EMSGN(_(e_listidx), n1);
5401 return FAIL;
5402 }
5403 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 }
5405 if (range)
5406 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005407 list_T *l;
5408 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409
5410 if (n2 < 0)
5411 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005412 else if (n2 >= len)
5413 n2 = len - 1;
5414 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005415 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 l = list_alloc();
5417 if (l == NULL)
5418 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005419 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 n1 <= n2; ++n1)
5421 {
5422 if (list_append_tv(l, &item->li_tv) == FAIL)
5423 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005424 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425 return FAIL;
5426 }
5427 item = item->li_next;
5428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 clear_tv(rettv);
5430 rettv->v_type = VAR_LIST;
5431 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005432 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 }
5434 else
5435 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005436 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005437 clear_tv(rettv);
5438 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 }
5440 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441
5442 case VAR_DICT:
5443 if (range)
5444 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005445 if (verbose)
5446 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 if (len == -1)
5448 clear_tv(&var1);
5449 return FAIL;
5450 }
5451 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453
5454 if (len == -1)
5455 {
5456 key = get_tv_string(&var1);
5457 if (*key == NUL)
5458 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005459 if (verbose)
5460 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461 clear_tv(&var1);
5462 return FAIL;
5463 }
5464 }
5465
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005466 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005467
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005468 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005469 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005470 if (len == -1)
5471 clear_tv(&var1);
5472 if (item == NULL)
5473 return FAIL;
5474
5475 copy_tv(&item->di_tv, &var1);
5476 clear_tv(rettv);
5477 *rettv = var1;
5478 }
5479 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 }
5481 }
5482
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 return OK;
5484}
5485
5486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 * Get an option value.
5488 * "arg" points to the '&' or '+' before the option name.
5489 * "arg" is advanced to character after the option name.
5490 * Return OK or FAIL.
5491 */
5492 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005495 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 int evaluate;
5497{
5498 char_u *option_end;
5499 long numval;
5500 char_u *stringval;
5501 int opt_type;
5502 int c;
5503 int working = (**arg == '+'); /* has("+option") */
5504 int ret = OK;
5505 int opt_flags;
5506
5507 /*
5508 * Isolate the option name and find its value.
5509 */
5510 option_end = find_option_end(arg, &opt_flags);
5511 if (option_end == NULL)
5512 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005513 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 EMSG2(_("E112: Option name missing: %s"), *arg);
5515 return FAIL;
5516 }
5517
5518 if (!evaluate)
5519 {
5520 *arg = option_end;
5521 return OK;
5522 }
5523
5524 c = *option_end;
5525 *option_end = NUL;
5526 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528
5529 if (opt_type == -3) /* invalid name */
5530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005531 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 EMSG2(_("E113: Unknown option: %s"), *arg);
5533 ret = FAIL;
5534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
5537 if (opt_type == -2) /* hidden string option */
5538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 rettv->v_type = VAR_STRING;
5540 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
5542 else if (opt_type == -1) /* hidden number option */
5543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 rettv->v_type = VAR_NUMBER;
5545 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 }
5547 else if (opt_type == 1) /* number option */
5548 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549 rettv->v_type = VAR_NUMBER;
5550 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 }
5552 else /* string option */
5553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 rettv->v_type = VAR_STRING;
5555 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 }
5557 }
5558 else if (working && (opt_type == -2 || opt_type == -1))
5559 ret = FAIL;
5560
5561 *option_end = c; /* put back for error messages */
5562 *arg = option_end;
5563
5564 return ret;
5565}
5566
5567/*
5568 * Allocate a variable for a string constant.
5569 * Return OK or FAIL.
5570 */
5571 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 int evaluate;
5576{
5577 char_u *p;
5578 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 int extra = 0;
5580
5581 /*
5582 * Find the end of the string, skipping backslashed characters.
5583 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005584 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
5586 if (*p == '\\' && p[1] != NUL)
5587 {
5588 ++p;
5589 /* A "\<x>" form occupies at least 4 characters, and produces up
5590 * to 6 characters: reserve space for 2 extra */
5591 if (*p == '<')
5592 extra += 2;
5593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595
5596 if (*p != '"')
5597 {
5598 EMSG2(_("E114: Missing quote: %s"), *arg);
5599 return FAIL;
5600 }
5601
5602 /* If only parsing, set *arg and return here */
5603 if (!evaluate)
5604 {
5605 *arg = p + 1;
5606 return OK;
5607 }
5608
5609 /*
5610 * Copy the string into allocated memory, handling backslashed
5611 * characters.
5612 */
5613 name = alloc((unsigned)(p - *arg + extra));
5614 if (name == NULL)
5615 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616 rettv->v_type = VAR_STRING;
5617 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 if (*p == '\\')
5622 {
5623 switch (*++p)
5624 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005625 case 'b': *name++ = BS; ++p; break;
5626 case 'e': *name++ = ESC; ++p; break;
5627 case 'f': *name++ = FF; ++p; break;
5628 case 'n': *name++ = NL; ++p; break;
5629 case 'r': *name++ = CAR; ++p; break;
5630 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
5632 case 'X': /* hex: "\x1", "\x12" */
5633 case 'x':
5634 case 'u': /* Unicode: "\u0023" */
5635 case 'U':
5636 if (vim_isxdigit(p[1]))
5637 {
5638 int n, nr;
5639 int c = toupper(*p);
5640
5641 if (c == 'X')
5642 n = 2;
5643 else
5644 n = 4;
5645 nr = 0;
5646 while (--n >= 0 && vim_isxdigit(p[1]))
5647 {
5648 ++p;
5649 nr = (nr << 4) + hex2nr(*p);
5650 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652#ifdef FEAT_MBYTE
5653 /* For "\u" store the number according to
5654 * 'encoding'. */
5655 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 else
5658#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 break;
5662
5663 /* octal: "\1", "\12", "\123" */
5664 case '0':
5665 case '1':
5666 case '2':
5667 case '3':
5668 case '4':
5669 case '5':
5670 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 case '7': *name = *p++ - '0';
5672 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 *name = (*name << 3) + *p++ - '0';
5675 if (*p >= '0' && *p <= '7')
5676 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 break;
5680
5681 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 if (extra != 0)
5684 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 break;
5687 }
5688 /* FALLTHROUGH */
5689
Bram Moolenaar8c711452005-01-14 21:53:12 +00005690 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 break;
5692 }
5693 }
5694 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 *arg = p + 1;
5700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 return OK;
5702}
5703
5704/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 * Return OK or FAIL.
5707 */
5708 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005709get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 int evaluate;
5713{
5714 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005715 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716 int reduce = 0;
5717
5718 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005720 */
5721 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005726 break;
5727 ++reduce;
5728 ++p;
5729 }
5730 }
5731
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005733 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005735 return FAIL;
5736 }
5737
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005739 if (!evaluate)
5740 {
5741 *arg = p + 1;
5742 return OK;
5743 }
5744
5745 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 */
5748 str = alloc((unsigned)((p - *arg) - reduce));
5749 if (str == NULL)
5750 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 rettv->v_type = VAR_STRING;
5752 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 break;
5760 ++p;
5761 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765 *arg = p + 1;
5766
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005767 return OK;
5768}
5769
5770/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771 * Allocate a variable for a List and fill it from "*arg".
5772 * Return OK or FAIL.
5773 */
5774 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005775get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005777 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778 int evaluate;
5779{
Bram Moolenaar33570922005-01-25 22:26:29 +00005780 list_T *l = NULL;
5781 typval_T tv;
5782 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783
5784 if (evaluate)
5785 {
5786 l = list_alloc();
5787 if (l == NULL)
5788 return FAIL;
5789 }
5790
5791 *arg = skipwhite(*arg + 1);
5792 while (**arg != ']' && **arg != NUL)
5793 {
5794 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5795 goto failret;
5796 if (evaluate)
5797 {
5798 item = listitem_alloc();
5799 if (item != NULL)
5800 {
5801 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005802 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 list_append(l, item);
5804 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005805 else
5806 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 }
5808
5809 if (**arg == ']')
5810 break;
5811 if (**arg != ',')
5812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814 goto failret;
5815 }
5816 *arg = skipwhite(*arg + 1);
5817 }
5818
5819 if (**arg != ']')
5820 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822failret:
5823 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005824 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825 return FAIL;
5826 }
5827
5828 *arg = skipwhite(*arg + 1);
5829 if (evaluate)
5830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005831 rettv->v_type = VAR_LIST;
5832 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 ++l->lv_refcount;
5834 }
5835
5836 return OK;
5837}
5838
5839/*
5840 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005841 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005843 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844list_alloc()
5845{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005846 list_T *l;
5847
5848 l = (list_T *)alloc_clear(sizeof(list_T));
5849 if (l != NULL)
5850 {
5851 /* Prepend the list to the list of lists for garbage collection. */
5852 if (first_list != NULL)
5853 first_list->lv_used_prev = l;
5854 l->lv_used_prev = NULL;
5855 l->lv_used_next = first_list;
5856 first_list = l;
5857 }
5858 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859}
5860
5861/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005862 * Allocate an empty list for a return value.
5863 * Returns OK or FAIL.
5864 */
5865 static int
5866rettv_list_alloc(rettv)
5867 typval_T *rettv;
5868{
5869 list_T *l = list_alloc();
5870
5871 if (l == NULL)
5872 return FAIL;
5873
5874 rettv->vval.v_list = l;
5875 rettv->v_type = VAR_LIST;
5876 ++l->lv_refcount;
5877 return OK;
5878}
5879
5880/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 * Unreference a list: decrement the reference count and free it when it
5882 * becomes zero.
5883 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005884 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005886 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005888 if (l != NULL && --l->lv_refcount <= 0)
5889 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890}
5891
5892/*
5893 * Free a list, including all items it points to.
5894 * Ignores the reference count.
5895 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005896 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005897list_free(l, recurse)
5898 list_T *l;
5899 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900{
Bram Moolenaar33570922005-01-25 22:26:29 +00005901 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005903 /* Remove the list from the list of lists for garbage collection. */
5904 if (l->lv_used_prev == NULL)
5905 first_list = l->lv_used_next;
5906 else
5907 l->lv_used_prev->lv_used_next = l->lv_used_next;
5908 if (l->lv_used_next != NULL)
5909 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5910
Bram Moolenaard9fba312005-06-26 22:34:35 +00005911 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005913 /* Remove the item before deleting it. */
5914 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005915 if (recurse || (item->li_tv.v_type != VAR_LIST
5916 && item->li_tv.v_type != VAR_DICT))
5917 clear_tv(&item->li_tv);
5918 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 }
5920 vim_free(l);
5921}
5922
5923/*
5924 * Allocate a list item.
5925 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927listitem_alloc()
5928{
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930}
5931
5932/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005933 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 */
5935 static void
5936listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005939 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 vim_free(item);
5941}
5942
5943/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005944 * Remove a list item from a List and free it. Also clears the value.
5945 */
5946 static void
5947listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 list_T *l;
5949 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005950{
5951 list_remove(l, item, item);
5952 listitem_free(item);
5953}
5954
5955/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956 * Get the number of items in a list.
5957 */
5958 static long
5959list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005960 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 if (l == NULL)
5963 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005964 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965}
5966
5967/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005968 * Return TRUE when two lists have exactly the same values.
5969 */
5970 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005971list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 list_T *l1;
5973 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005974 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005975 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005976{
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005978
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005979 if (l1 == NULL || l2 == NULL)
5980 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005981 if (l1 == l2)
5982 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005983 if (list_len(l1) != list_len(l2))
5984 return FALSE;
5985
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005986 for (item1 = l1->lv_first, item2 = l2->lv_first;
5987 item1 != NULL && item2 != NULL;
5988 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005989 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005990 return FALSE;
5991 return item1 == NULL && item2 == NULL;
5992}
5993
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005994#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5995 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005996/*
5997 * Return the dictitem that an entry in a hashtable points to.
5998 */
5999 dictitem_T *
6000dict_lookup(hi)
6001 hashitem_T *hi;
6002{
6003 return HI2DI(hi);
6004}
6005#endif
6006
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006008 * Return TRUE when two dictionaries have exactly the same key/values.
6009 */
6010 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006011dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 dict_T *d1;
6013 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006014 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006015 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006016{
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 hashitem_T *hi;
6018 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006019 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006020
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006021 if (d1 == NULL || d2 == NULL)
6022 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006023 if (d1 == d2)
6024 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006025 if (dict_len(d1) != dict_len(d2))
6026 return FALSE;
6027
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006028 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006030 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006031 if (!HASHITEM_EMPTY(hi))
6032 {
6033 item2 = dict_find(d2, hi->hi_key, -1);
6034 if (item2 == NULL)
6035 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006036 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006037 return FALSE;
6038 --todo;
6039 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006040 }
6041 return TRUE;
6042}
6043
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006044static int tv_equal_recurse_limit;
6045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006046/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006047 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006048 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006049 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006050 */
6051 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006052tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 typval_T *tv1;
6054 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 int ic; /* ignore case */
6056 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006057{
6058 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006059 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006060 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006061 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006062
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006063 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006064 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006066 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006067 * recursiveness to a limit. We guess they are equal then.
6068 * A fixed limit has the problem of still taking an awful long time.
6069 * Reduce the limit every time running into it. That should work fine for
6070 * deeply linked structures that are not recursively linked and catch
6071 * recursiveness quickly. */
6072 if (!recursive)
6073 tv_equal_recurse_limit = 1000;
6074 if (recursive_cnt >= tv_equal_recurse_limit)
6075 {
6076 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006077 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006078 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006079
6080 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006081 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006082 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006083 ++recursive_cnt;
6084 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6085 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006086 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006087
6088 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006089 ++recursive_cnt;
6090 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6091 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006092 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006093
6094 case VAR_FUNC:
6095 return (tv1->vval.v_string != NULL
6096 && tv2->vval.v_string != NULL
6097 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6098
6099 case VAR_NUMBER:
6100 return tv1->vval.v_number == tv2->vval.v_number;
6101
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006102#ifdef FEAT_FLOAT
6103 case VAR_FLOAT:
6104 return tv1->vval.v_float == tv2->vval.v_float;
6105#endif
6106
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006107 case VAR_STRING:
6108 s1 = get_tv_string_buf(tv1, buf1);
6109 s2 = get_tv_string_buf(tv2, buf2);
6110 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006112
6113 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 return TRUE;
6115}
6116
6117/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006118 * Locate item with index "n" in list "l" and return it.
6119 * A negative index is counted from the end; -1 is the last item.
6120 * Returns NULL when "n" is out of range.
6121 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006123list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006124 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006125 long n;
6126{
Bram Moolenaar33570922005-01-25 22:26:29 +00006127 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006128 long idx;
6129
6130 if (l == NULL)
6131 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006132
6133 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006134 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006135 n = l->lv_len + n;
6136
6137 /* Check for index out of range. */
6138 if (n < 0 || n >= l->lv_len)
6139 return NULL;
6140
6141 /* When there is a cached index may start search from there. */
6142 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006144 if (n < l->lv_idx / 2)
6145 {
6146 /* closest to the start of the list */
6147 item = l->lv_first;
6148 idx = 0;
6149 }
6150 else if (n > (l->lv_idx + l->lv_len) / 2)
6151 {
6152 /* closest to the end of the list */
6153 item = l->lv_last;
6154 idx = l->lv_len - 1;
6155 }
6156 else
6157 {
6158 /* closest to the cached index */
6159 item = l->lv_idx_item;
6160 idx = l->lv_idx;
6161 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162 }
6163 else
6164 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006165 if (n < l->lv_len / 2)
6166 {
6167 /* closest to the start of the list */
6168 item = l->lv_first;
6169 idx = 0;
6170 }
6171 else
6172 {
6173 /* closest to the end of the list */
6174 item = l->lv_last;
6175 idx = l->lv_len - 1;
6176 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006178
6179 while (n > idx)
6180 {
6181 /* search forward */
6182 item = item->li_next;
6183 ++idx;
6184 }
6185 while (n < idx)
6186 {
6187 /* search backward */
6188 item = item->li_prev;
6189 --idx;
6190 }
6191
6192 /* cache the used index */
6193 l->lv_idx = idx;
6194 l->lv_idx_item = item;
6195
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196 return item;
6197}
6198
6199/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006200 * Get list item "l[idx]" as a number.
6201 */
6202 static long
6203list_find_nr(l, idx, errorp)
6204 list_T *l;
6205 long idx;
6206 int *errorp; /* set to TRUE when something wrong */
6207{
6208 listitem_T *li;
6209
6210 li = list_find(l, idx);
6211 if (li == NULL)
6212 {
6213 if (errorp != NULL)
6214 *errorp = TRUE;
6215 return -1L;
6216 }
6217 return get_tv_number_chk(&li->li_tv, errorp);
6218}
6219
6220/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006221 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6222 */
6223 char_u *
6224list_find_str(l, idx)
6225 list_T *l;
6226 long idx;
6227{
6228 listitem_T *li;
6229
6230 li = list_find(l, idx - 1);
6231 if (li == NULL)
6232 {
6233 EMSGN(_(e_listidx), idx);
6234 return NULL;
6235 }
6236 return get_tv_string(&li->li_tv);
6237}
6238
6239/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006240 * Locate "item" list "l" and return its index.
6241 * Returns -1 when "item" is not in the list.
6242 */
6243 static long
6244list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006245 list_T *l;
6246 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006247{
6248 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006249 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006250
6251 if (l == NULL)
6252 return -1;
6253 idx = 0;
6254 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6255 ++idx;
6256 if (li == NULL)
6257 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006258 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006259}
6260
6261/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 * Append item "item" to the end of list "l".
6263 */
6264 static void
6265list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 list_T *l;
6267 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006268{
6269 if (l->lv_last == NULL)
6270 {
6271 /* empty list */
6272 l->lv_first = item;
6273 l->lv_last = item;
6274 item->li_prev = NULL;
6275 }
6276 else
6277 {
6278 l->lv_last->li_next = item;
6279 item->li_prev = l->lv_last;
6280 l->lv_last = item;
6281 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006282 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 item->li_next = NULL;
6284}
6285
6286/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006288 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006290 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006292 list_T *l;
6293 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006295 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296
Bram Moolenaar05159a02005-02-26 23:04:13 +00006297 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006298 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006299 copy_tv(tv, &li->li_tv);
6300 list_append(l, li);
6301 return OK;
6302}
6303
6304/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006305 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006306 * Return FAIL when out of memory.
6307 */
6308 int
6309list_append_dict(list, dict)
6310 list_T *list;
6311 dict_T *dict;
6312{
6313 listitem_T *li = listitem_alloc();
6314
6315 if (li == NULL)
6316 return FAIL;
6317 li->li_tv.v_type = VAR_DICT;
6318 li->li_tv.v_lock = 0;
6319 li->li_tv.vval.v_dict = dict;
6320 list_append(list, li);
6321 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322 return OK;
6323}
6324
6325/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006326 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006327 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006328 * Returns FAIL when out of memory.
6329 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006330 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006331list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006332 list_T *l;
6333 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006334 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006335{
6336 listitem_T *li = listitem_alloc();
6337
6338 if (li == NULL)
6339 return FAIL;
6340 list_append(l, li);
6341 li->li_tv.v_type = VAR_STRING;
6342 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006343 if (str == NULL)
6344 li->li_tv.vval.v_string = NULL;
6345 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006346 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006347 return FAIL;
6348 return OK;
6349}
6350
6351/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006352 * Append "n" to list "l".
6353 * Returns FAIL when out of memory.
6354 */
6355 static int
6356list_append_number(l, n)
6357 list_T *l;
6358 varnumber_T n;
6359{
6360 listitem_T *li;
6361
6362 li = listitem_alloc();
6363 if (li == NULL)
6364 return FAIL;
6365 li->li_tv.v_type = VAR_NUMBER;
6366 li->li_tv.v_lock = 0;
6367 li->li_tv.vval.v_number = n;
6368 list_append(l, li);
6369 return OK;
6370}
6371
6372/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006374 * If "item" is NULL append at the end.
6375 * Return FAIL when out of memory.
6376 */
6377 static int
6378list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 list_T *l;
6380 typval_T *tv;
6381 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006382{
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006384
6385 if (ni == NULL)
6386 return FAIL;
6387 copy_tv(tv, &ni->li_tv);
6388 if (item == NULL)
6389 /* Append new item at end of list. */
6390 list_append(l, ni);
6391 else
6392 {
6393 /* Insert new item before existing item. */
6394 ni->li_prev = item->li_prev;
6395 ni->li_next = item;
6396 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006397 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006398 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006399 ++l->lv_idx;
6400 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006401 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006402 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006404 l->lv_idx_item = NULL;
6405 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006407 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006408 }
6409 return OK;
6410}
6411
6412/*
6413 * Extend "l1" with "l2".
6414 * If "bef" is NULL append at the end, otherwise insert before this item.
6415 * Returns FAIL when out of memory.
6416 */
6417 static int
6418list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 list_T *l1;
6420 list_T *l2;
6421 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422{
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006424 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006426 /* We also quit the loop when we have inserted the original item count of
6427 * the list, avoid a hang when we extend a list with itself. */
6428 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6430 return FAIL;
6431 return OK;
6432}
6433
6434/*
6435 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6436 * Return FAIL when out of memory.
6437 */
6438 static int
6439list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 list_T *l1;
6441 list_T *l2;
6442 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443{
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006445
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006446 if (l1 == NULL || l2 == NULL)
6447 return FAIL;
6448
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006449 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006450 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006451 if (l == NULL)
6452 return FAIL;
6453 tv->v_type = VAR_LIST;
6454 tv->vval.v_list = l;
6455
6456 /* append all items from the second list */
6457 return list_extend(l, l2, NULL);
6458}
6459
6460/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006461 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006462 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006463 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006464 * Returns NULL when out of memory.
6465 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006466 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006467list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006470 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006471{
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 list_T *copy;
6473 listitem_T *item;
6474 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006475
6476 if (orig == NULL)
6477 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478
6479 copy = list_alloc();
6480 if (copy != NULL)
6481 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006482 if (copyID != 0)
6483 {
6484 /* Do this before adding the items, because one of the items may
6485 * refer back to this list. */
6486 orig->lv_copyID = copyID;
6487 orig->lv_copylist = copy;
6488 }
6489 for (item = orig->lv_first; item != NULL && !got_int;
6490 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 {
6492 ni = listitem_alloc();
6493 if (ni == NULL)
6494 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006495 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 {
6497 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6498 {
6499 vim_free(ni);
6500 break;
6501 }
6502 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006504 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006505 list_append(copy, ni);
6506 }
6507 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006508 if (item != NULL)
6509 {
6510 list_unref(copy);
6511 copy = NULL;
6512 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513 }
6514
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515 return copy;
6516}
6517
6518/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006520 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006521 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006523list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006524 list_T *l;
6525 listitem_T *item;
6526 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527{
Bram Moolenaar33570922005-01-25 22:26:29 +00006528 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006529
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 /* notify watchers */
6531 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006533 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534 list_fix_watch(l, ip);
6535 if (ip == item2)
6536 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538
6539 if (item2->li_next == NULL)
6540 l->lv_last = item->li_prev;
6541 else
6542 item2->li_next->li_prev = item->li_prev;
6543 if (item->li_prev == NULL)
6544 l->lv_first = item2->li_next;
6545 else
6546 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006547 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548}
6549
6550/*
6551 * Return an allocated string with the string representation of a list.
6552 * May return NULL.
6553 */
6554 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006555list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006557 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558{
6559 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560
6561 if (tv->vval.v_list == NULL)
6562 return NULL;
6563 ga_init2(&ga, (int)sizeof(char), 80);
6564 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006565 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006566 {
6567 vim_free(ga.ga_data);
6568 return NULL;
6569 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 ga_append(&ga, ']');
6571 ga_append(&ga, NUL);
6572 return (char_u *)ga.ga_data;
6573}
6574
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006575typedef struct join_S {
6576 char_u *s;
6577 char_u *tofree;
6578} join_T;
6579
6580 static int
6581list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6582 garray_T *gap; /* to store the result in */
6583 list_T *l;
6584 char_u *sep;
6585 int echo_style;
6586 int copyID;
6587 garray_T *join_gap; /* to keep each list item string */
6588{
6589 int i;
6590 join_T *p;
6591 int len;
6592 int sumlen = 0;
6593 int first = TRUE;
6594 char_u *tofree;
6595 char_u numbuf[NUMBUFLEN];
6596 listitem_T *item;
6597 char_u *s;
6598
6599 /* Stringify each item in the list. */
6600 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6601 {
6602 if (echo_style)
6603 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6604 else
6605 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6606 if (s == NULL)
6607 return FAIL;
6608
6609 len = (int)STRLEN(s);
6610 sumlen += len;
6611
6612 ga_grow(join_gap, 1);
6613 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6614 if (tofree != NULL || s != numbuf)
6615 {
6616 p->s = s;
6617 p->tofree = tofree;
6618 }
6619 else
6620 {
6621 p->s = vim_strnsave(s, len);
6622 p->tofree = p->s;
6623 }
6624
6625 line_breakcheck();
6626 }
6627
6628 /* Allocate result buffer with its total size, avoid re-allocation and
6629 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6630 if (join_gap->ga_len >= 2)
6631 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6632 if (ga_grow(gap, sumlen + 2) == FAIL)
6633 return FAIL;
6634
6635 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6636 {
6637 if (first)
6638 first = FALSE;
6639 else
6640 ga_concat(gap, sep);
6641 p = ((join_T *)join_gap->ga_data) + i;
6642
6643 if (p->s != NULL)
6644 ga_concat(gap, p->s);
6645 line_breakcheck();
6646 }
6647
6648 return OK;
6649}
6650
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006652 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006653 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006654 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006655 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006656 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006657list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006658 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006659 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006660 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006661 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006662 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006663{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006664 garray_T join_ga;
6665 int retval;
6666 join_T *p;
6667 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006668
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006669 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6670 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6671
6672 /* Dispose each item in join_ga. */
6673 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006674 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006675 p = (join_T *)join_ga.ga_data;
6676 for (i = 0; i < join_ga.ga_len; ++i)
6677 {
6678 vim_free(p->tofree);
6679 ++p;
6680 }
6681 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006682 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006683
6684 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006685}
6686
6687/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006688 * Garbage collection for lists and dictionaries.
6689 *
6690 * We use reference counts to be able to free most items right away when they
6691 * are no longer used. But for composite items it's possible that it becomes
6692 * unused while the reference count is > 0: When there is a recursive
6693 * reference. Example:
6694 * :let l = [1, 2, 3]
6695 * :let d = {9: l}
6696 * :let l[1] = d
6697 *
6698 * Since this is quite unusual we handle this with garbage collection: every
6699 * once in a while find out which lists and dicts are not referenced from any
6700 * variable.
6701 *
6702 * Here is a good reference text about garbage collection (refers to Python
6703 * but it applies to all reference-counting mechanisms):
6704 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006705 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006706
6707/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006708 * Do garbage collection for lists and dicts.
6709 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006710 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006711 int
6712garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006713{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006714 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006715 buf_T *buf;
6716 win_T *wp;
6717 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006718 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006719 int did_free;
6720 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006721#ifdef FEAT_WINDOWS
6722 tabpage_T *tp;
6723#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006724
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006725 /* Only do this once. */
6726 want_garbage_collect = FALSE;
6727 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006728 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006729
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006730 /* We advance by two because we add one for items referenced through
6731 * previous_funccal. */
6732 current_copyID += COPYID_INC;
6733 copyID = current_copyID;
6734
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006735 /*
6736 * 1. Go through all accessible variables and mark all lists and dicts
6737 * with copyID.
6738 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006739
6740 /* Don't free variables in the previous_funccal list unless they are only
6741 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006742 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006743 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6744 {
6745 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6746 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6747 }
6748
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006749 /* script-local variables */
6750 for (i = 1; i <= ga_scripts.ga_len; ++i)
6751 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6752
6753 /* buffer-local variables */
6754 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6755 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6756
6757 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006758 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006759 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6760
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006761#ifdef FEAT_WINDOWS
6762 /* tabpage-local variables */
6763 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6764 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6765#endif
6766
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 /* global variables */
6768 set_ref_in_ht(&globvarht, copyID);
6769
6770 /* function-local variables */
6771 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6772 {
6773 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6774 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6775 }
6776
Bram Moolenaard812df62008-11-09 12:46:09 +00006777 /* v: vars */
6778 set_ref_in_ht(&vimvarht, copyID);
6779
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006780 /*
6781 * 2. Free lists and dictionaries that are not referenced.
6782 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006783 did_free = free_unref_items(copyID);
6784
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006785 /*
6786 * 3. Check if any funccal can be freed now.
6787 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006788 for (pfc = &previous_funccal; *pfc != NULL; )
6789 {
6790 if (can_free_funccal(*pfc, copyID))
6791 {
6792 fc = *pfc;
6793 *pfc = fc->caller;
6794 free_funccal(fc, TRUE);
6795 did_free = TRUE;
6796 did_free_funccal = TRUE;
6797 }
6798 else
6799 pfc = &(*pfc)->caller;
6800 }
6801 if (did_free_funccal)
6802 /* When a funccal was freed some more items might be garbage
6803 * collected, so run again. */
6804 (void)garbage_collect();
6805
6806 return did_free;
6807}
6808
6809/*
6810 * Free lists and dictionaries that are no longer referenced.
6811 */
6812 static int
6813free_unref_items(copyID)
6814 int copyID;
6815{
6816 dict_T *dd;
6817 list_T *ll;
6818 int did_free = FALSE;
6819
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006821 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006822 */
6823 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006824 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006826 /* Free the Dictionary and ordinary items it contains, but don't
6827 * recurse into Lists and Dictionaries, they will be in the list
6828 * of dicts or list of lists. */
6829 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830 did_free = TRUE;
6831
6832 /* restart, next dict may also have been freed */
6833 dd = first_dict;
6834 }
6835 else
6836 dd = dd->dv_used_next;
6837
6838 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006839 * Go through the list of lists and free items without the copyID.
6840 * But don't free a list that has a watcher (used in a for loop), these
6841 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 */
6843 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006844 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6845 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006846 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006847 /* Free the List and ordinary items it contains, but don't recurse
6848 * into Lists and Dictionaries, they will be in the list of dicts
6849 * or list of lists. */
6850 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851 did_free = TRUE;
6852
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006853 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 ll = first_list;
6855 }
6856 else
6857 ll = ll->lv_used_next;
6858
6859 return did_free;
6860}
6861
6862/*
6863 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6864 */
6865 static void
6866set_ref_in_ht(ht, copyID)
6867 hashtab_T *ht;
6868 int copyID;
6869{
6870 int todo;
6871 hashitem_T *hi;
6872
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006873 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 for (hi = ht->ht_array; todo > 0; ++hi)
6875 if (!HASHITEM_EMPTY(hi))
6876 {
6877 --todo;
6878 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6879 }
6880}
6881
6882/*
6883 * Mark all lists and dicts referenced through list "l" with "copyID".
6884 */
6885 static void
6886set_ref_in_list(l, copyID)
6887 list_T *l;
6888 int copyID;
6889{
6890 listitem_T *li;
6891
6892 for (li = l->lv_first; li != NULL; li = li->li_next)
6893 set_ref_in_item(&li->li_tv, copyID);
6894}
6895
6896/*
6897 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6898 */
6899 static void
6900set_ref_in_item(tv, copyID)
6901 typval_T *tv;
6902 int copyID;
6903{
6904 dict_T *dd;
6905 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006906
6907 switch (tv->v_type)
6908 {
6909 case VAR_DICT:
6910 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006911 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006912 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913 /* Didn't see this dict yet. */
6914 dd->dv_copyID = copyID;
6915 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006916 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006918
6919 case VAR_LIST:
6920 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006921 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006922 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923 /* Didn't see this list yet. */
6924 ll->lv_copyID = copyID;
6925 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006926 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006928 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006930}
6931
6932/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006933 * Allocate an empty header for a dictionary.
6934 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006935 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006936dict_alloc()
6937{
Bram Moolenaar33570922005-01-25 22:26:29 +00006938 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006939
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006941 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006942 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006943 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944 if (first_dict != NULL)
6945 first_dict->dv_used_prev = d;
6946 d->dv_used_next = first_dict;
6947 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006948 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006949
Bram Moolenaar33570922005-01-25 22:26:29 +00006950 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006951 d->dv_lock = 0;
6952 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006953 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006954 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006955 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006956}
6957
6958/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006959 * Allocate an empty dict for a return value.
6960 * Returns OK or FAIL.
6961 */
6962 static int
6963rettv_dict_alloc(rettv)
6964 typval_T *rettv;
6965{
6966 dict_T *d = dict_alloc();
6967
6968 if (d == NULL)
6969 return FAIL;
6970
6971 rettv->vval.v_dict = d;
6972 rettv->v_type = VAR_DICT;
6973 ++d->dv_refcount;
6974 return OK;
6975}
6976
6977
6978/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006979 * Unreference a Dictionary: decrement the reference count and free it when it
6980 * becomes zero.
6981 */
Bram Moolenaar82139082011-09-14 16:52:09 +02006982 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006983dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006985{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006986 if (d != NULL && --d->dv_refcount <= 0)
6987 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006988}
6989
6990/*
6991 * Free a Dictionary, including all items it contains.
6992 * Ignores the reference count.
6993 */
6994 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006995dict_free(d, recurse)
6996 dict_T *d;
6997 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006998{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006999 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007000 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007001 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007002
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 /* Remove the dict from the list of dicts for garbage collection. */
7004 if (d->dv_used_prev == NULL)
7005 first_dict = d->dv_used_next;
7006 else
7007 d->dv_used_prev->dv_used_next = d->dv_used_next;
7008 if (d->dv_used_next != NULL)
7009 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7010
7011 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007012 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007013 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007014 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007016 if (!HASHITEM_EMPTY(hi))
7017 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007018 /* Remove the item before deleting it, just in case there is
7019 * something recursive causing trouble. */
7020 di = HI2DI(hi);
7021 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007022 if (recurse || (di->di_tv.v_type != VAR_LIST
7023 && di->di_tv.v_type != VAR_DICT))
7024 clear_tv(&di->di_tv);
7025 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007026 --todo;
7027 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007028 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007029 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007030 vim_free(d);
7031}
7032
7033/*
7034 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007035 * The "key" is copied to the new item.
7036 * Note that the value of the item "di_tv" still needs to be initialized!
7037 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007038 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007039 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007040dictitem_alloc(key)
7041 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007042{
Bram Moolenaar33570922005-01-25 22:26:29 +00007043 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007044
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007045 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007046 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007047 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007048 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007049 di->di_flags = 0;
7050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052}
7053
7054/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007055 * Make a copy of a Dictionary item.
7056 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007057 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007058dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007059 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007060{
Bram Moolenaar33570922005-01-25 22:26:29 +00007061 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007062
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007063 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7064 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007065 if (di != NULL)
7066 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007067 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007068 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007069 copy_tv(&org->di_tv, &di->di_tv);
7070 }
7071 return di;
7072}
7073
7074/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075 * Remove item "item" from Dictionary "dict" and free it.
7076 */
7077 static void
7078dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007079 dict_T *dict;
7080 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007081{
Bram Moolenaar33570922005-01-25 22:26:29 +00007082 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007083
Bram Moolenaar33570922005-01-25 22:26:29 +00007084 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085 if (HASHITEM_EMPTY(hi))
7086 EMSG2(_(e_intern2), "dictitem_remove()");
7087 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007088 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089 dictitem_free(item);
7090}
7091
7092/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007093 * Free a dict item. Also clears the value.
7094 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007095 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007098{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099 clear_tv(&item->di_tv);
7100 vim_free(item);
7101}
7102
7103/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007104 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7105 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007106 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107 * Returns NULL when out of memory.
7108 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007110dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007111 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007112 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007113 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114{
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 dict_T *copy;
7116 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007119
7120 if (orig == NULL)
7121 return NULL;
7122
7123 copy = dict_alloc();
7124 if (copy != NULL)
7125 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007126 if (copyID != 0)
7127 {
7128 orig->dv_copyID = copyID;
7129 orig->dv_copydict = copy;
7130 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007131 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007132 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007133 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007134 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007135 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007136 --todo;
7137
7138 di = dictitem_alloc(hi->hi_key);
7139 if (di == NULL)
7140 break;
7141 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007142 {
7143 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7144 copyID) == FAIL)
7145 {
7146 vim_free(di);
7147 break;
7148 }
7149 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007150 else
7151 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7152 if (dict_add(copy, di) == FAIL)
7153 {
7154 dictitem_free(di);
7155 break;
7156 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007158 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159
Bram Moolenaare9a41262005-01-15 22:18:47 +00007160 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007161 if (todo > 0)
7162 {
7163 dict_unref(copy);
7164 copy = NULL;
7165 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007166 }
7167
7168 return copy;
7169}
7170
7171/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007173 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007175 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007177 dict_T *d;
7178 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179{
Bram Moolenaar33570922005-01-25 22:26:29 +00007180 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181}
7182
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007184 * Add a number or string entry to dictionary "d".
7185 * When "str" is NULL use number "nr", otherwise use "str".
7186 * Returns FAIL when out of memory and when key already exists.
7187 */
7188 int
7189dict_add_nr_str(d, key, nr, str)
7190 dict_T *d;
7191 char *key;
7192 long nr;
7193 char_u *str;
7194{
7195 dictitem_T *item;
7196
7197 item = dictitem_alloc((char_u *)key);
7198 if (item == NULL)
7199 return FAIL;
7200 item->di_tv.v_lock = 0;
7201 if (str == NULL)
7202 {
7203 item->di_tv.v_type = VAR_NUMBER;
7204 item->di_tv.vval.v_number = nr;
7205 }
7206 else
7207 {
7208 item->di_tv.v_type = VAR_STRING;
7209 item->di_tv.vval.v_string = vim_strsave(str);
7210 }
7211 if (dict_add(d, item) == FAIL)
7212 {
7213 dictitem_free(item);
7214 return FAIL;
7215 }
7216 return OK;
7217}
7218
7219/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007220 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007221 * Returns FAIL when out of memory and when key already exists.
7222 */
7223 int
7224dict_add_list(d, key, list)
7225 dict_T *d;
7226 char *key;
7227 list_T *list;
7228{
7229 dictitem_T *item;
7230
7231 item = dictitem_alloc((char_u *)key);
7232 if (item == NULL)
7233 return FAIL;
7234 item->di_tv.v_lock = 0;
7235 item->di_tv.v_type = VAR_LIST;
7236 item->di_tv.vval.v_list = list;
7237 if (dict_add(d, item) == FAIL)
7238 {
7239 dictitem_free(item);
7240 return FAIL;
7241 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007242 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007243 return OK;
7244}
7245
7246/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007247 * Get the number of items in a Dictionary.
7248 */
7249 static long
7250dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007252{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007253 if (d == NULL)
7254 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007255 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007256}
7257
7258/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 * Find item "key[len]" in Dictionary "d".
7260 * If "len" is negative use strlen(key).
7261 * Returns NULL when not found.
7262 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007263 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007264dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007265 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 char_u *key;
7267 int len;
7268{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269#define AKEYLEN 200
7270 char_u buf[AKEYLEN];
7271 char_u *akey;
7272 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007275 if (len < 0)
7276 akey = key;
7277 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007278 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007279 tofree = akey = vim_strnsave(key, len);
7280 if (akey == NULL)
7281 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007282 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283 else
7284 {
7285 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007286 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 akey = buf;
7288 }
7289
Bram Moolenaar33570922005-01-25 22:26:29 +00007290 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007291 vim_free(tofree);
7292 if (HASHITEM_EMPTY(hi))
7293 return NULL;
7294 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295}
7296
7297/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007298 * Get a string item from a dictionary.
7299 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007300 * Returns NULL if the entry doesn't exist or out of memory.
7301 */
7302 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007303get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007304 dict_T *d;
7305 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007306 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007307{
7308 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007309 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007310
7311 di = dict_find(d, key, -1);
7312 if (di == NULL)
7313 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007314 s = get_tv_string(&di->di_tv);
7315 if (save && s != NULL)
7316 s = vim_strsave(s);
7317 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007318}
7319
7320/*
7321 * Get a number item from a dictionary.
7322 * Returns 0 if the entry doesn't exist or out of memory.
7323 */
7324 long
7325get_dict_number(d, key)
7326 dict_T *d;
7327 char_u *key;
7328{
7329 dictitem_T *di;
7330
7331 di = dict_find(d, key, -1);
7332 if (di == NULL)
7333 return 0;
7334 return get_tv_number(&di->di_tv);
7335}
7336
7337/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 * Return an allocated string with the string representation of a Dictionary.
7339 * May return NULL.
7340 */
7341 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007342dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007344 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345{
7346 garray_T ga;
7347 int first = TRUE;
7348 char_u *tofree;
7349 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356 return NULL;
7357 ga_init2(&ga, (int)sizeof(char), 80);
7358 ga_append(&ga, '{');
7359
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007360 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007362 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007364 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007365 --todo;
7366
7367 if (first)
7368 first = FALSE;
7369 else
7370 ga_concat(&ga, (char_u *)", ");
7371
7372 tofree = string_quote(hi->hi_key, FALSE);
7373 if (tofree != NULL)
7374 {
7375 ga_concat(&ga, tofree);
7376 vim_free(tofree);
7377 }
7378 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007379 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007380 if (s != NULL)
7381 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007383 if (s == NULL)
7384 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007387 if (todo > 0)
7388 {
7389 vim_free(ga.ga_data);
7390 return NULL;
7391 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392
7393 ga_append(&ga, '}');
7394 ga_append(&ga, NUL);
7395 return (char_u *)ga.ga_data;
7396}
7397
7398/*
7399 * Allocate a variable for a Dictionary and fill it from "*arg".
7400 * Return OK or FAIL. Returns NOTDONE for {expr}.
7401 */
7402 static int
7403get_dict_tv(arg, rettv, evaluate)
7404 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 int evaluate;
7407{
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 dict_T *d = NULL;
7409 typval_T tvkey;
7410 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007411 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007412 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007414 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415
7416 /*
7417 * First check if it's not a curly-braces thing: {expr}.
7418 * Must do this without evaluating, otherwise a function may be called
7419 * twice. Unfortunately this means we need to call eval1() twice for the
7420 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007421 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 if (*start != '}')
7424 {
7425 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7426 return FAIL;
7427 if (*start == '}')
7428 return NOTDONE;
7429 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430
7431 if (evaluate)
7432 {
7433 d = dict_alloc();
7434 if (d == NULL)
7435 return FAIL;
7436 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437 tvkey.v_type = VAR_UNKNOWN;
7438 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439
7440 *arg = skipwhite(*arg + 1);
7441 while (**arg != '}' && **arg != NUL)
7442 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007443 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444 goto failret;
7445 if (**arg != ':')
7446 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007447 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007448 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 goto failret;
7450 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007451 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007453 key = get_tv_string_buf_chk(&tvkey, buf);
7454 if (key == NULL || *key == NUL)
7455 {
7456 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7457 if (key != NULL)
7458 EMSG(_(e_emptykey));
7459 clear_tv(&tvkey);
7460 goto failret;
7461 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463
7464 *arg = skipwhite(*arg + 1);
7465 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7466 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007467 if (evaluate)
7468 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007469 goto failret;
7470 }
7471 if (evaluate)
7472 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007473 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007474 if (item != NULL)
7475 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007476 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007477 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478 clear_tv(&tv);
7479 goto failret;
7480 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007481 item = dictitem_alloc(key);
7482 clear_tv(&tvkey);
7483 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007486 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007487 if (dict_add(d, item) == FAIL)
7488 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 }
7490 }
7491
7492 if (**arg == '}')
7493 break;
7494 if (**arg != ',')
7495 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007496 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497 goto failret;
7498 }
7499 *arg = skipwhite(*arg + 1);
7500 }
7501
7502 if (**arg != '}')
7503 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007504 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505failret:
7506 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007507 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 return FAIL;
7509 }
7510
7511 *arg = skipwhite(*arg + 1);
7512 if (evaluate)
7513 {
7514 rettv->v_type = VAR_DICT;
7515 rettv->vval.v_dict = d;
7516 ++d->dv_refcount;
7517 }
7518
7519 return OK;
7520}
7521
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007523 * Return a string with the string representation of a variable.
7524 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007525 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007526 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007527 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007528 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007529 */
7530 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007531echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007532 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007533 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007534 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007535 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007536{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007537 static int recurse = 0;
7538 char_u *r = NULL;
7539
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007542 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007543 *tofree = NULL;
7544 return NULL;
7545 }
7546 ++recurse;
7547
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007548 switch (tv->v_type)
7549 {
7550 case VAR_FUNC:
7551 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 r = tv->vval.v_string;
7553 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007554
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007555 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007556 if (tv->vval.v_list == NULL)
7557 {
7558 *tofree = NULL;
7559 r = NULL;
7560 }
7561 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7562 {
7563 *tofree = NULL;
7564 r = (char_u *)"[...]";
7565 }
7566 else
7567 {
7568 tv->vval.v_list->lv_copyID = copyID;
7569 *tofree = list2string(tv, copyID);
7570 r = *tofree;
7571 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007572 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007573
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007575 if (tv->vval.v_dict == NULL)
7576 {
7577 *tofree = NULL;
7578 r = NULL;
7579 }
7580 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7581 {
7582 *tofree = NULL;
7583 r = (char_u *)"{...}";
7584 }
7585 else
7586 {
7587 tv->vval.v_dict->dv_copyID = copyID;
7588 *tofree = dict2string(tv, copyID);
7589 r = *tofree;
7590 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007591 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007592
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007593 case VAR_STRING:
7594 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007595 *tofree = NULL;
7596 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007597 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007598
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007599#ifdef FEAT_FLOAT
7600 case VAR_FLOAT:
7601 *tofree = NULL;
7602 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7603 r = numbuf;
7604 break;
7605#endif
7606
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007607 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007608 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007609 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007610 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007611
7612 --recurse;
7613 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007614}
7615
7616/*
7617 * Return a string with the string representation of a variable.
7618 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7619 * "numbuf" is used for a number.
7620 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007621 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007622 */
7623 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007624tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007626 char_u **tofree;
7627 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007628 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007629{
7630 switch (tv->v_type)
7631 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007632 case VAR_FUNC:
7633 *tofree = string_quote(tv->vval.v_string, TRUE);
7634 return *tofree;
7635 case VAR_STRING:
7636 *tofree = string_quote(tv->vval.v_string, FALSE);
7637 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007638#ifdef FEAT_FLOAT
7639 case VAR_FLOAT:
7640 *tofree = NULL;
7641 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7642 return numbuf;
7643#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007644 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007645 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007648 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007650 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007651 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007652}
7653
7654/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007655 * Return string "str" in ' quotes, doubling ' characters.
7656 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007658 */
7659 static char_u *
7660string_quote(str, function)
7661 char_u *str;
7662 int function;
7663{
Bram Moolenaar33570922005-01-25 22:26:29 +00007664 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007665 char_u *p, *r, *s;
7666
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 len = (function ? 13 : 3);
7668 if (str != NULL)
7669 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007670 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 for (p = str; *p != NUL; mb_ptr_adv(p))
7672 if (*p == '\'')
7673 ++len;
7674 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007675 s = r = alloc(len);
7676 if (r != NULL)
7677 {
7678 if (function)
7679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007681 r += 10;
7682 }
7683 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 if (str != NULL)
7686 for (p = str; *p != NUL; )
7687 {
7688 if (*p == '\'')
7689 *r++ = '\'';
7690 MB_COPY_CHAR(p, r);
7691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693 if (function)
7694 *r++ = ')';
7695 *r++ = NUL;
7696 }
7697 return s;
7698}
7699
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007700#ifdef FEAT_FLOAT
7701/*
7702 * Convert the string "text" to a floating point number.
7703 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7704 * this always uses a decimal point.
7705 * Returns the length of the text that was consumed.
7706 */
7707 static int
7708string2float(text, value)
7709 char_u *text;
7710 float_T *value; /* result stored here */
7711{
7712 char *s = (char *)text;
7713 float_T f;
7714
7715 f = strtod(s, &s);
7716 *value = f;
7717 return (int)((char_u *)s - text);
7718}
7719#endif
7720
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 * Get the value of an environment variable.
7723 * "arg" is pointing to the '$'. It is advanced to after the name.
7724 * If the environment variable was not set, silently assume it is empty.
7725 * Always return OK.
7726 */
7727 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007728get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 int evaluate;
7732{
7733 char_u *string = NULL;
7734 int len;
7735 int cc;
7736 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007737 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738
7739 ++*arg;
7740 name = *arg;
7741 len = get_env_len(arg);
7742 if (evaluate)
7743 {
7744 if (len != 0)
7745 {
7746 cc = name[len];
7747 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007748 /* first try vim_getenv(), fast for normal environment vars */
7749 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007751 {
7752 if (!mustfree)
7753 string = vim_strsave(string);
7754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 else
7756 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007757 if (mustfree)
7758 vim_free(string);
7759
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 /* next try expanding things like $VIM and ${HOME} */
7761 string = expand_env_save(name - 1);
7762 if (string != NULL && *string == '$')
7763 {
7764 vim_free(string);
7765 string = NULL;
7766 }
7767 }
7768 name[len] = cc;
7769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770 rettv->v_type = VAR_STRING;
7771 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 }
7773
7774 return OK;
7775}
7776
7777/*
7778 * Array with names and number of arguments of all internal functions
7779 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7780 */
7781static struct fst
7782{
7783 char *f_name; /* function name */
7784 char f_min_argc; /* minimal number of arguments */
7785 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007787 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788} functions[] =
7789{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007790#ifdef FEAT_FLOAT
7791 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007792 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007793#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007794 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007795 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 {"append", 2, 2, f_append},
7797 {"argc", 0, 0, f_argc},
7798 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007799 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007800#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007801 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007802 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007803 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007806 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 {"bufexists", 1, 1, f_bufexists},
7808 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7809 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7810 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7811 {"buflisted", 1, 1, f_buflisted},
7812 {"bufloaded", 1, 1, f_bufloaded},
7813 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007814 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {"bufwinnr", 1, 1, f_bufwinnr},
7816 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007817 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007819#ifdef FEAT_FLOAT
7820 {"ceil", 1, 1, f_ceil},
7821#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007822 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {"char2nr", 1, 1, f_char2nr},
7824 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007825 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007827#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007828 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007829 {"complete_add", 1, 1, f_complete_add},
7830 {"complete_check", 0, 0, f_complete_check},
7831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007833 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007834#ifdef FEAT_FLOAT
7835 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007836 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007837#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007838 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007840 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007841 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"delete", 1, 1, f_delete},
7843 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007844 {"diff_filler", 1, 1, f_diff_filler},
7845 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007846 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007848 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {"eventhandler", 0, 0, f_eventhandler},
7850 {"executable", 1, 1, f_executable},
7851 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007852#ifdef FEAT_FLOAT
7853 {"exp", 1, 1, f_exp},
7854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007856 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007857 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7859 {"filereadable", 1, 1, f_filereadable},
7860 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007861 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007862 {"finddir", 1, 3, f_finddir},
7863 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007864#ifdef FEAT_FLOAT
7865 {"float2nr", 1, 1, f_float2nr},
7866 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007867 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007869 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {"fnamemodify", 2, 2, f_fnamemodify},
7871 {"foldclosed", 1, 1, f_foldclosed},
7872 {"foldclosedend", 1, 1, f_foldclosedend},
7873 {"foldlevel", 1, 1, f_foldlevel},
7874 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007875 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007877 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007878 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007879 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007880 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"getbufvar", 2, 2, f_getbufvar},
7882 {"getchar", 0, 1, f_getchar},
7883 {"getcharmod", 0, 0, f_getcharmod},
7884 {"getcmdline", 0, 0, f_getcmdline},
7885 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007886 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007888 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007889 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {"getfsize", 1, 1, f_getfsize},
7891 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007892 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007893 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007894 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007895 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007896 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007897 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007898 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007899 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007901 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007902 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"getwinposx", 0, 0, f_getwinposx},
7904 {"getwinposy", 0, 0, f_getwinposy},
7905 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007906 {"glob", 1, 2, f_glob},
7907 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007909 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007910 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007911 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7913 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7914 {"histadd", 2, 2, f_histadd},
7915 {"histdel", 1, 2, f_histdel},
7916 {"histget", 1, 2, f_histget},
7917 {"histnr", 1, 1, f_histnr},
7918 {"hlID", 1, 1, f_hlID},
7919 {"hlexists", 1, 1, f_hlexists},
7920 {"hostname", 0, 0, f_hostname},
7921 {"iconv", 3, 3, f_iconv},
7922 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007923 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007924 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007926 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"inputrestore", 0, 0, f_inputrestore},
7928 {"inputsave", 0, 0, f_inputsave},
7929 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007930 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007931 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007933 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007935 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007936 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"libcall", 3, 3, f_libcall},
7940 {"libcallnr", 3, 3, f_libcallnr},
7941 {"line", 1, 1, f_line},
7942 {"line2byte", 1, 1, f_line2byte},
7943 {"lispindent", 1, 1, f_lispindent},
7944 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007945#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007946 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007947 {"log10", 1, 1, f_log10},
7948#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007949 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007950 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007951 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007952 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007953 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007954 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007955 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007956 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007957 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007958 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007959 {"max", 1, 1, f_max},
7960 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007961#ifdef vim_mkdir
7962 {"mkdir", 1, 3, f_mkdir},
7963#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007964 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007965#ifdef FEAT_MZSCHEME
7966 {"mzeval", 1, 1, f_mzeval},
7967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"nextnonblank", 1, 1, f_nextnonblank},
7969 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007970 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007971 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007972#ifdef FEAT_FLOAT
7973 {"pow", 2, 2, f_pow},
7974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007976 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007977 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007979 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007980 {"reltime", 0, 2, f_reltime},
7981 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"remote_expr", 2, 3, f_remote_expr},
7983 {"remote_foreground", 1, 1, f_remote_foreground},
7984 {"remote_peek", 1, 2, f_remote_peek},
7985 {"remote_read", 1, 1, f_remote_read},
7986 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007987 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007989 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007991 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007992#ifdef FEAT_FLOAT
7993 {"round", 1, 1, f_round},
7994#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007995 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007996 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007997 {"searchpair", 3, 7, f_searchpair},
7998 {"searchpairpos", 3, 7, f_searchpairpos},
7999 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {"server2client", 2, 2, f_server2client},
8001 {"serverlist", 0, 0, f_serverlist},
8002 {"setbufvar", 3, 3, f_setbufvar},
8003 {"setcmdpos", 1, 1, f_setcmdpos},
8004 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008005 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008006 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008007 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008008 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008010 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008011 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008013 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008015#ifdef FEAT_FLOAT
8016 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008017 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008018#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008019 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008020 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008021 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008022 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008023 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008024#ifdef FEAT_FLOAT
8025 {"sqrt", 1, 1, f_sqrt},
8026 {"str2float", 1, 1, f_str2float},
8027#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008028 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008029 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008030 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031#ifdef HAVE_STRFTIME
8032 {"strftime", 1, 2, f_strftime},
8033#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008034 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008035 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {"strlen", 1, 1, f_strlen},
8037 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008038 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008040 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {"submatch", 1, 1, f_submatch},
8042 {"substitute", 4, 4, f_substitute},
8043 {"synID", 3, 3, f_synID},
8044 {"synIDattr", 2, 3, f_synIDattr},
8045 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008046 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008047 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008048 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008049 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008050 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008051 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008052 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008053 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008054#ifdef FEAT_FLOAT
8055 {"tan", 1, 1, f_tan},
8056 {"tanh", 1, 1, f_tanh},
8057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008059 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"tolower", 1, 1, f_tolower},
8061 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008062 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008063#ifdef FEAT_FLOAT
8064 {"trunc", 1, 1, f_trunc},
8065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008067 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008068 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"virtcol", 1, 1, f_virtcol},
8071 {"visualmode", 0, 1, f_visualmode},
8072 {"winbufnr", 1, 1, f_winbufnr},
8073 {"wincol", 0, 0, f_wincol},
8074 {"winheight", 1, 1, f_winheight},
8075 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008076 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008078 {"winrestview", 1, 1, f_winrestview},
8079 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008081 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008082 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083};
8084
8085#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8086
8087/*
8088 * Function given to ExpandGeneric() to obtain the list of internal
8089 * or user defined function names.
8090 */
8091 char_u *
8092get_function_name(xp, idx)
8093 expand_T *xp;
8094 int idx;
8095{
8096 static int intidx = -1;
8097 char_u *name;
8098
8099 if (idx == 0)
8100 intidx = -1;
8101 if (intidx < 0)
8102 {
8103 name = get_user_func_name(xp, idx);
8104 if (name != NULL)
8105 return name;
8106 }
8107 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8108 {
8109 STRCPY(IObuff, functions[intidx].f_name);
8110 STRCAT(IObuff, "(");
8111 if (functions[intidx].f_max_argc == 0)
8112 STRCAT(IObuff, ")");
8113 return IObuff;
8114 }
8115
8116 return NULL;
8117}
8118
8119/*
8120 * Function given to ExpandGeneric() to obtain the list of internal or
8121 * user defined variable or function names.
8122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 char_u *
8124get_expr_name(xp, idx)
8125 expand_T *xp;
8126 int idx;
8127{
8128 static int intidx = -1;
8129 char_u *name;
8130
8131 if (idx == 0)
8132 intidx = -1;
8133 if (intidx < 0)
8134 {
8135 name = get_function_name(xp, idx);
8136 if (name != NULL)
8137 return name;
8138 }
8139 return get_user_var_name(xp, ++intidx);
8140}
8141
8142#endif /* FEAT_CMDL_COMPL */
8143
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008144#if defined(EBCDIC) || defined(PROTO)
8145/*
8146 * Compare struct fst by function name.
8147 */
8148 static int
8149compare_func_name(s1, s2)
8150 const void *s1;
8151 const void *s2;
8152{
8153 struct fst *p1 = (struct fst *)s1;
8154 struct fst *p2 = (struct fst *)s2;
8155
8156 return STRCMP(p1->f_name, p2->f_name);
8157}
8158
8159/*
8160 * Sort the function table by function name.
8161 * The sorting of the table above is ASCII dependant.
8162 * On machines using EBCDIC we have to sort it.
8163 */
8164 static void
8165sortFunctions()
8166{
8167 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8168
8169 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8170}
8171#endif
8172
8173
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174/*
8175 * Find internal function in table above.
8176 * Return index, or -1 if not found
8177 */
8178 static int
8179find_internal_func(name)
8180 char_u *name; /* name of the function */
8181{
8182 int first = 0;
8183 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8184 int cmp;
8185 int x;
8186
8187 /*
8188 * Find the function name in the table. Binary search.
8189 */
8190 while (first <= last)
8191 {
8192 x = first + ((unsigned)(last - first) >> 1);
8193 cmp = STRCMP(name, functions[x].f_name);
8194 if (cmp < 0)
8195 last = x - 1;
8196 else if (cmp > 0)
8197 first = x + 1;
8198 else
8199 return x;
8200 }
8201 return -1;
8202}
8203
8204/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008205 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8206 * name it contains, otherwise return "name".
8207 */
8208 static char_u *
8209deref_func_name(name, lenp)
8210 char_u *name;
8211 int *lenp;
8212{
Bram Moolenaar33570922005-01-25 22:26:29 +00008213 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008214 int cc;
8215
8216 cc = name[*lenp];
8217 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008218 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008219 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008220 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008221 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008222 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008223 {
8224 *lenp = 0;
8225 return (char_u *)""; /* just in case */
8226 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008227 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008229 }
8230
8231 return name;
8232}
8233
8234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 * Allocate a variable for the result of a function.
8236 * Return OK or FAIL.
8237 */
8238 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008239get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8240 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 char_u *name; /* name of the function */
8242 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 char_u **arg; /* argument, pointing to the '(' */
8245 linenr_T firstline; /* first line of range */
8246 linenr_T lastline; /* last line of range */
8247 int *doesrange; /* return: function handled range */
8248 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008249 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250{
8251 char_u *argp;
8252 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008253 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 int argcount = 0; /* number of arguments found */
8255
8256 /*
8257 * Get the arguments.
8258 */
8259 argp = *arg;
8260 while (argcount < MAX_FUNC_ARGS)
8261 {
8262 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8263 if (*argp == ')' || *argp == ',' || *argp == NUL)
8264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8266 {
8267 ret = FAIL;
8268 break;
8269 }
8270 ++argcount;
8271 if (*argp != ',')
8272 break;
8273 }
8274 if (*argp == ')')
8275 ++argp;
8276 else
8277 ret = FAIL;
8278
8279 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008280 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008281 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008283 {
8284 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008285 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008287 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
8290 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008291 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292
8293 *arg = skipwhite(argp);
8294 return ret;
8295}
8296
8297
8298/*
8299 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008300 * Return OK when the function can't be called, FAIL otherwise.
8301 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 */
8303 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008304call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008305 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008306 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008308 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008310 typval_T *argvars; /* vars for arguments, must have "argcount"
8311 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 linenr_T firstline; /* first line of range */
8313 linenr_T lastline; /* last line of range */
8314 int *doesrange; /* return: function handled range */
8315 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008316 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317{
8318 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319#define ERROR_UNKNOWN 0
8320#define ERROR_TOOMANY 1
8321#define ERROR_TOOFEW 2
8322#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008323#define ERROR_DICT 4
8324#define ERROR_NONE 5
8325#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 int error = ERROR_NONE;
8327 int i;
8328 int llen;
8329 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330#define FLEN_FIXED 40
8331 char_u fname_buf[FLEN_FIXED + 1];
8332 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008333 char_u *name;
8334
8335 /* Make a copy of the name, if it comes from a funcref variable it could
8336 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008337 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008338 if (name == NULL)
8339 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340
8341 /*
8342 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8343 * Change <SNR>123_name() to K_SNR 123_name().
8344 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 llen = eval_fname_script(name);
8347 if (llen > 0)
8348 {
8349 fname_buf[0] = K_SPECIAL;
8350 fname_buf[1] = KS_EXTRA;
8351 fname_buf[2] = (int)KE_SNR;
8352 i = 3;
8353 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8354 {
8355 if (current_SID <= 0)
8356 error = ERROR_SCRIPT;
8357 else
8358 {
8359 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8360 i = (int)STRLEN(fname_buf);
8361 }
8362 }
8363 if (i + STRLEN(name + llen) < FLEN_FIXED)
8364 {
8365 STRCPY(fname_buf + i, name + llen);
8366 fname = fname_buf;
8367 }
8368 else
8369 {
8370 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8371 if (fname == NULL)
8372 error = ERROR_OTHER;
8373 else
8374 {
8375 mch_memmove(fname, fname_buf, (size_t)i);
8376 STRCPY(fname + i, name + llen);
8377 }
8378 }
8379 }
8380 else
8381 fname = name;
8382
8383 *doesrange = FALSE;
8384
8385
8386 /* execute the function if no errors detected and executing */
8387 if (evaluate && error == ERROR_NONE)
8388 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008389 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8390 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 error = ERROR_UNKNOWN;
8392
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008393 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {
8395 /*
8396 * User defined function.
8397 */
8398 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008399
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008401 /* Trigger FuncUndefined event, may load the function. */
8402 if (fp == NULL
8403 && apply_autocmds(EVENT_FUNCUNDEFINED,
8404 fname, fname, TRUE, NULL)
8405 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008407 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 fp = find_func(fname);
8409 }
8410#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008411 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008412 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008413 {
8414 /* loaded a package, search for the function again */
8415 fp = find_func(fname);
8416 }
8417
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 if (fp != NULL)
8419 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008420 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008422 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008424 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008426 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008427 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 else
8429 {
8430 /*
8431 * Call the user function.
8432 * Save and restore search patterns, script variables and
8433 * redo buffer.
8434 */
8435 save_search_patterns();
8436 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008437 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008438 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008439 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008440 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8441 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8442 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008443 /* Function was unreferenced while being used, free it
8444 * now. */
8445 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 restoreRedobuff();
8447 restore_search_patterns();
8448 error = ERROR_NONE;
8449 }
8450 }
8451 }
8452 else
8453 {
8454 /*
8455 * Find the function name in the table, call its implementation.
8456 */
8457 i = find_internal_func(fname);
8458 if (i >= 0)
8459 {
8460 if (argcount < functions[i].f_min_argc)
8461 error = ERROR_TOOFEW;
8462 else if (argcount > functions[i].f_max_argc)
8463 error = ERROR_TOOMANY;
8464 else
8465 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008466 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008467 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 error = ERROR_NONE;
8469 }
8470 }
8471 }
8472 /*
8473 * The function call (or "FuncUndefined" autocommand sequence) might
8474 * have been aborted by an error, an interrupt, or an explicitly thrown
8475 * exception that has not been caught so far. This situation can be
8476 * tested for by calling aborting(). For an error in an internal
8477 * function or for the "E132" error in call_user_func(), however, the
8478 * throw point at which the "force_abort" flag (temporarily reset by
8479 * emsg()) is normally updated has not been reached yet. We need to
8480 * update that flag first to make aborting() reliable.
8481 */
8482 update_force_abort();
8483 }
8484 if (error == ERROR_NONE)
8485 ret = OK;
8486
8487 /*
8488 * Report an error unless the argument evaluation or function call has been
8489 * cancelled due to an aborting error, an interrupt, or an exception.
8490 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008491 if (!aborting())
8492 {
8493 switch (error)
8494 {
8495 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008496 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008497 break;
8498 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008499 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008500 break;
8501 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008502 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008503 name);
8504 break;
8505 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008506 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008507 name);
8508 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008509 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008510 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008511 name);
8512 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008513 }
8514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 if (fname != name && fname != fname_buf)
8517 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008518 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519
8520 return ret;
8521}
8522
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008523/*
8524 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008525 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008526 */
8527 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008528emsg_funcname(ermsg, name)
8529 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008530 char_u *name;
8531{
8532 char_u *p;
8533
8534 if (*name == K_SPECIAL)
8535 p = concat_str((char_u *)"<SNR>", name + 3);
8536 else
8537 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008538 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008539 if (p != name)
8540 vim_free(p);
8541}
8542
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008543/*
8544 * Return TRUE for a non-zero Number and a non-empty String.
8545 */
8546 static int
8547non_zero_arg(argvars)
8548 typval_T *argvars;
8549{
8550 return ((argvars[0].v_type == VAR_NUMBER
8551 && argvars[0].vval.v_number != 0)
8552 || (argvars[0].v_type == VAR_STRING
8553 && argvars[0].vval.v_string != NULL
8554 && *argvars[0].vval.v_string != NUL));
8555}
8556
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557/*********************************************
8558 * Implementation of the built-in functions
8559 */
8560
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008561#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008562static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8563
8564/*
8565 * Get the float value of "argvars[0]" into "f".
8566 * Returns FAIL when the argument is not a Number or Float.
8567 */
8568 static int
8569get_float_arg(argvars, f)
8570 typval_T *argvars;
8571 float_T *f;
8572{
8573 if (argvars[0].v_type == VAR_FLOAT)
8574 {
8575 *f = argvars[0].vval.v_float;
8576 return OK;
8577 }
8578 if (argvars[0].v_type == VAR_NUMBER)
8579 {
8580 *f = (float_T)argvars[0].vval.v_number;
8581 return OK;
8582 }
8583 EMSG(_("E808: Number or Float required"));
8584 return FAIL;
8585}
8586
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008587/*
8588 * "abs(expr)" function
8589 */
8590 static void
8591f_abs(argvars, rettv)
8592 typval_T *argvars;
8593 typval_T *rettv;
8594{
8595 if (argvars[0].v_type == VAR_FLOAT)
8596 {
8597 rettv->v_type = VAR_FLOAT;
8598 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8599 }
8600 else
8601 {
8602 varnumber_T n;
8603 int error = FALSE;
8604
8605 n = get_tv_number_chk(&argvars[0], &error);
8606 if (error)
8607 rettv->vval.v_number = -1;
8608 else if (n > 0)
8609 rettv->vval.v_number = n;
8610 else
8611 rettv->vval.v_number = -n;
8612 }
8613}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008614
8615/*
8616 * "acos()" function
8617 */
8618 static void
8619f_acos(argvars, rettv)
8620 typval_T *argvars;
8621 typval_T *rettv;
8622{
8623 float_T f;
8624
8625 rettv->v_type = VAR_FLOAT;
8626 if (get_float_arg(argvars, &f) == OK)
8627 rettv->vval.v_float = acos(f);
8628 else
8629 rettv->vval.v_float = 0.0;
8630}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008631#endif
8632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008634 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 */
8636 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008637f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008638 typval_T *argvars;
8639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640{
Bram Moolenaar33570922005-01-25 22:26:29 +00008641 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008643 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008644 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008646 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008647 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008648 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650 }
8651 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008652 EMSG(_(e_listreq));
8653}
8654
8655/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008656 * "and(expr, expr)" function
8657 */
8658 static void
8659f_and(argvars, rettv)
8660 typval_T *argvars;
8661 typval_T *rettv;
8662{
8663 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8664 & get_tv_number_chk(&argvars[1], NULL);
8665}
8666
8667/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008668 * "append(lnum, string/list)" function
8669 */
8670 static void
8671f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008672 typval_T *argvars;
8673 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008674{
8675 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008676 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008677 list_T *l = NULL;
8678 listitem_T *li = NULL;
8679 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008680 long added = 0;
8681
Bram Moolenaar0d660222005-01-07 21:51:51 +00008682 lnum = get_tv_lnum(argvars);
8683 if (lnum >= 0
8684 && lnum <= curbuf->b_ml.ml_line_count
8685 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008686 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008687 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008688 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008689 l = argvars[1].vval.v_list;
8690 if (l == NULL)
8691 return;
8692 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008693 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008694 for (;;)
8695 {
8696 if (l == NULL)
8697 tv = &argvars[1]; /* append a string */
8698 else if (li == NULL)
8699 break; /* end of list */
8700 else
8701 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008702 line = get_tv_string_chk(tv);
8703 if (line == NULL) /* type error */
8704 {
8705 rettv->vval.v_number = 1; /* Failed */
8706 break;
8707 }
8708 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008709 ++added;
8710 if (l == NULL)
8711 break;
8712 li = li->li_next;
8713 }
8714
8715 appended_lines_mark(lnum, added);
8716 if (curwin->w_cursor.lnum > lnum)
8717 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008719 else
8720 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721}
8722
8723/*
8724 * "argc()" function
8725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008727f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008728 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008731 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732}
8733
8734/*
8735 * "argidx()" function
8736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008738f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008739 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743}
8744
8745/*
8746 * "argv(nr)" function
8747 */
8748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008750 typval_T *argvars;
8751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752{
8753 int idx;
8754
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008755 if (argvars[0].v_type != VAR_UNKNOWN)
8756 {
8757 idx = get_tv_number_chk(&argvars[0], NULL);
8758 if (idx >= 0 && idx < ARGCOUNT)
8759 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8760 else
8761 rettv->vval.v_string = NULL;
8762 rettv->v_type = VAR_STRING;
8763 }
8764 else if (rettv_list_alloc(rettv) == OK)
8765 for (idx = 0; idx < ARGCOUNT; ++idx)
8766 list_append_string(rettv->vval.v_list,
8767 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768}
8769
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008770#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008771/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008772 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008773 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008774 static void
8775f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008776 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008777 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008778{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008779 float_T f;
8780
8781 rettv->v_type = VAR_FLOAT;
8782 if (get_float_arg(argvars, &f) == OK)
8783 rettv->vval.v_float = asin(f);
8784 else
8785 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008786}
8787
8788/*
8789 * "atan()" function
8790 */
8791 static void
8792f_atan(argvars, rettv)
8793 typval_T *argvars;
8794 typval_T *rettv;
8795{
8796 float_T f;
8797
8798 rettv->v_type = VAR_FLOAT;
8799 if (get_float_arg(argvars, &f) == OK)
8800 rettv->vval.v_float = atan(f);
8801 else
8802 rettv->vval.v_float = 0.0;
8803}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008804
8805/*
8806 * "atan2()" function
8807 */
8808 static void
8809f_atan2(argvars, rettv)
8810 typval_T *argvars;
8811 typval_T *rettv;
8812{
8813 float_T fx, fy;
8814
8815 rettv->v_type = VAR_FLOAT;
8816 if (get_float_arg(argvars, &fx) == OK
8817 && get_float_arg(&argvars[1], &fy) == OK)
8818 rettv->vval.v_float = atan2(fx, fy);
8819 else
8820 rettv->vval.v_float = 0.0;
8821}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008822#endif
8823
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824/*
8825 * "browse(save, title, initdir, default)" function
8826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008828f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008829 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831{
8832#ifdef FEAT_BROWSE
8833 int save;
8834 char_u *title;
8835 char_u *initdir;
8836 char_u *defname;
8837 char_u buf[NUMBUFLEN];
8838 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008839 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008841 save = get_tv_number_chk(&argvars[0], &error);
8842 title = get_tv_string_chk(&argvars[1]);
8843 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8844 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008846 if (error || title == NULL || initdir == NULL || defname == NULL)
8847 rettv->vval.v_string = NULL;
8848 else
8849 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008850 do_browse(save ? BROWSE_SAVE : 0,
8851 title, defname, NULL, initdir, NULL, curbuf);
8852#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008854#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008856}
8857
8858/*
8859 * "browsedir(title, initdir)" function
8860 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008863 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008864 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008865{
8866#ifdef FEAT_BROWSE
8867 char_u *title;
8868 char_u *initdir;
8869 char_u buf[NUMBUFLEN];
8870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008871 title = get_tv_string_chk(&argvars[0]);
8872 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008874 if (title == NULL || initdir == NULL)
8875 rettv->vval.v_string = NULL;
8876 else
8877 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008878 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883}
8884
Bram Moolenaar33570922005-01-25 22:26:29 +00008885static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008886
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887/*
8888 * Find a buffer by number or exact name.
8889 */
8890 static buf_T *
8891find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008892 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893{
8894 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008896 if (avar->v_type == VAR_NUMBER)
8897 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008898 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008900 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008901 if (buf == NULL)
8902 {
8903 /* No full path name match, try a match with a URL or a "nofile"
8904 * buffer, these don't use the full path. */
8905 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8906 if (buf->b_fname != NULL
8907 && (path_with_url(buf->b_fname)
8908#ifdef FEAT_QUICKFIX
8909 || bt_nofile(buf)
8910#endif
8911 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008912 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008913 break;
8914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 }
8916 return buf;
8917}
8918
8919/*
8920 * "bufexists(expr)" function
8921 */
8922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008924 typval_T *argvars;
8925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928}
8929
8930/*
8931 * "buflisted(expr)" function
8932 */
8933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008935 typval_T *argvars;
8936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937{
8938 buf_T *buf;
8939
8940 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008941 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942}
8943
8944/*
8945 * "bufloaded(expr)" function
8946 */
8947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008949 typval_T *argvars;
8950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951{
8952 buf_T *buf;
8953
8954 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956}
8957
Bram Moolenaar33570922005-01-25 22:26:29 +00008958static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008959
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960/*
8961 * Get buffer by number or pattern.
8962 */
8963 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008964get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008965 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 int save_magic;
8969 char_u *save_cpo;
8970 buf_T *buf;
8971
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008972 if (tv->v_type == VAR_NUMBER)
8973 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008974 if (tv->v_type != VAR_STRING)
8975 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 if (name == NULL || *name == NUL)
8977 return curbuf;
8978 if (name[0] == '$' && name[1] == NUL)
8979 return lastbuf;
8980
8981 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8982 save_magic = p_magic;
8983 p_magic = TRUE;
8984 save_cpo = p_cpo;
8985 p_cpo = (char_u *)"";
8986
8987 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8988 TRUE, FALSE));
8989
8990 p_magic = save_magic;
8991 p_cpo = save_cpo;
8992
8993 /* If not found, try expanding the name, like done for bufexists(). */
8994 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996
8997 return buf;
8998}
8999
9000/*
9001 * "bufname(expr)" function
9002 */
9003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009005 typval_T *argvars;
9006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 buf_T *buf;
9009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009010 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012 buf = get_buf_tv(&argvars[0]);
9013 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009017 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 --emsg_off;
9019}
9020
9021/*
9022 * "bufnr(expr)" function
9023 */
9024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009026 typval_T *argvars;
9027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028{
9029 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009030 int error = FALSE;
9031 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009033 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009036 --emsg_off;
9037
9038 /* If the buffer isn't found and the second argument is not zero create a
9039 * new buffer. */
9040 if (buf == NULL
9041 && argvars[1].v_type != VAR_UNKNOWN
9042 && get_tv_number_chk(&argvars[1], &error) != 0
9043 && !error
9044 && (name = get_tv_string_chk(&argvars[0])) != NULL
9045 && !error)
9046 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9047
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052}
9053
9054/*
9055 * "bufwinnr(nr)" function
9056 */
9057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *argvars;
9060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
9062#ifdef FEAT_WINDOWS
9063 win_T *wp;
9064 int winnr = 0;
9065#endif
9066 buf_T *buf;
9067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009068 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071#ifdef FEAT_WINDOWS
9072 for (wp = firstwin; wp; wp = wp->w_next)
9073 {
9074 ++winnr;
9075 if (wp->w_buffer == buf)
9076 break;
9077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081#endif
9082 --emsg_off;
9083}
9084
9085/*
9086 * "byte2line(byte)" function
9087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009090 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092{
9093#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095#else
9096 long boff = 0;
9097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009098 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 (linenr_T)0, &boff);
9104#endif
9105}
9106
9107/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009108 * "byteidx()" function
9109 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009112 typval_T *argvars;
9113 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009114{
9115#ifdef FEAT_MBYTE
9116 char_u *t;
9117#endif
9118 char_u *str;
9119 long idx;
9120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009121 str = get_tv_string_chk(&argvars[0]);
9122 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009124 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009125 return;
9126
9127#ifdef FEAT_MBYTE
9128 t = str;
9129 for ( ; idx > 0; idx--)
9130 {
9131 if (*t == NUL) /* EOL reached */
9132 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009133 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009134 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009135 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009136#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009137 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009139#endif
9140}
9141
9142/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009143 * "call(func, arglist)" function
9144 */
9145 static void
9146f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009147 typval_T *argvars;
9148 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009149{
9150 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009151 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009152 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009154 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009156
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009157 if (argvars[1].v_type != VAR_LIST)
9158 {
9159 EMSG(_(e_listreq));
9160 return;
9161 }
9162 if (argvars[1].vval.v_list == NULL)
9163 return;
9164
9165 if (argvars[0].v_type == VAR_FUNC)
9166 func = argvars[0].vval.v_string;
9167 else
9168 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009169 if (*func == NUL)
9170 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009171
Bram Moolenaare9a41262005-01-15 22:18:47 +00009172 if (argvars[2].v_type != VAR_UNKNOWN)
9173 {
9174 if (argvars[2].v_type != VAR_DICT)
9175 {
9176 EMSG(_(e_dictreq));
9177 return;
9178 }
9179 selfdict = argvars[2].vval.v_dict;
9180 }
9181
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009182 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9183 item = item->li_next)
9184 {
9185 if (argc == MAX_FUNC_ARGS)
9186 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009187 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009188 break;
9189 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009190 /* Make a copy of each argument. This is needed to be able to set
9191 * v_lock to VAR_FIXED in the copy without changing the original list.
9192 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009193 copy_tv(&item->li_tv, &argv[argc++]);
9194 }
9195
9196 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009197 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009198 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9199 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009200
9201 /* Free the arguments. */
9202 while (argc > 0)
9203 clear_tv(&argv[--argc]);
9204}
9205
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009206#ifdef FEAT_FLOAT
9207/*
9208 * "ceil({float})" function
9209 */
9210 static void
9211f_ceil(argvars, rettv)
9212 typval_T *argvars;
9213 typval_T *rettv;
9214{
9215 float_T f;
9216
9217 rettv->v_type = VAR_FLOAT;
9218 if (get_float_arg(argvars, &f) == OK)
9219 rettv->vval.v_float = ceil(f);
9220 else
9221 rettv->vval.v_float = 0.0;
9222}
9223#endif
9224
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009225/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009226 * "changenr()" function
9227 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009228 static void
9229f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009230 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009231 typval_T *rettv;
9232{
9233 rettv->vval.v_number = curbuf->b_u_seq_cur;
9234}
9235
9236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 * "char2nr(string)" function
9238 */
9239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009240f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009241 typval_T *argvars;
9242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243{
9244#ifdef FEAT_MBYTE
9245 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009246 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 else
9248#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009249 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250}
9251
9252/*
9253 * "cindent(lnum)" function
9254 */
9255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009256f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009257 typval_T *argvars;
9258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259{
9260#ifdef FEAT_CINDENT
9261 pos_T pos;
9262 linenr_T lnum;
9263
9264 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009265 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9267 {
9268 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009269 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 curwin->w_cursor = pos;
9271 }
9272 else
9273#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009274 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275}
9276
9277/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009278 * "clearmatches()" function
9279 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009280 static void
9281f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009282 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009283 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009284{
9285#ifdef FEAT_SEARCH_EXTRA
9286 clear_matches(curwin);
9287#endif
9288}
9289
9290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 * "col(string)" function
9292 */
9293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009295 typval_T *argvars;
9296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297{
9298 colnr_T col = 0;
9299 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009300 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009302 fp = var2fpos(&argvars[0], FALSE, &fnum);
9303 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 {
9305 if (fp->col == MAXCOL)
9306 {
9307 /* '> can be MAXCOL, get the length of the line then */
9308 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009309 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 else
9311 col = MAXCOL;
9312 }
9313 else
9314 {
9315 col = fp->col + 1;
9316#ifdef FEAT_VIRTUALEDIT
9317 /* col(".") when the cursor is on the NUL at the end of the line
9318 * because of "coladd" can be seen as an extra column. */
9319 if (virtual_active() && fp == &curwin->w_cursor)
9320 {
9321 char_u *p = ml_get_cursor();
9322
9323 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9324 curwin->w_virtcol - curwin->w_cursor.coladd))
9325 {
9326# ifdef FEAT_MBYTE
9327 int l;
9328
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009329 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 col += l;
9331# else
9332 if (*p != NUL && p[1] == NUL)
9333 ++col;
9334# endif
9335 }
9336 }
9337#endif
9338 }
9339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009340 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341}
9342
Bram Moolenaar572cb562005-08-05 21:35:02 +00009343#if defined(FEAT_INS_EXPAND)
9344/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009345 * "complete()" function
9346 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009347 static void
9348f_complete(argvars, rettv)
9349 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009350 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009351{
9352 int startcol;
9353
9354 if ((State & INSERT) == 0)
9355 {
9356 EMSG(_("E785: complete() can only be used in Insert mode"));
9357 return;
9358 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009359
9360 /* Check for undo allowed here, because if something was already inserted
9361 * the line was already saved for undo and this check isn't done. */
9362 if (!undo_allowed())
9363 return;
9364
Bram Moolenaarade00832006-03-10 21:46:58 +00009365 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9366 {
9367 EMSG(_(e_invarg));
9368 return;
9369 }
9370
9371 startcol = get_tv_number_chk(&argvars[0], NULL);
9372 if (startcol <= 0)
9373 return;
9374
9375 set_completion(startcol - 1, argvars[1].vval.v_list);
9376}
9377
9378/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009379 * "complete_add()" function
9380 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009381 static void
9382f_complete_add(argvars, rettv)
9383 typval_T *argvars;
9384 typval_T *rettv;
9385{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009386 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009387}
9388
9389/*
9390 * "complete_check()" function
9391 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009392 static void
9393f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009394 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009395 typval_T *rettv;
9396{
9397 int saved = RedrawingDisabled;
9398
9399 RedrawingDisabled = 0;
9400 ins_compl_check_keys(0);
9401 rettv->vval.v_number = compl_interrupted;
9402 RedrawingDisabled = saved;
9403}
9404#endif
9405
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406/*
9407 * "confirm(message, buttons[, default [, type]])" function
9408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009411 typval_T *argvars UNUSED;
9412 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413{
9414#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9415 char_u *message;
9416 char_u *buttons = NULL;
9417 char_u buf[NUMBUFLEN];
9418 char_u buf2[NUMBUFLEN];
9419 int def = 1;
9420 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009421 char_u *typestr;
9422 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009424 message = get_tv_string_chk(&argvars[0]);
9425 if (message == NULL)
9426 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009427 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009429 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9430 if (buttons == NULL)
9431 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009432 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009434 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009435 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009437 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9438 if (typestr == NULL)
9439 error = TRUE;
9440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009442 switch (TOUPPER_ASC(*typestr))
9443 {
9444 case 'E': type = VIM_ERROR; break;
9445 case 'Q': type = VIM_QUESTION; break;
9446 case 'I': type = VIM_INFO; break;
9447 case 'W': type = VIM_WARNING; break;
9448 case 'G': type = VIM_GENERIC; break;
9449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 }
9451 }
9452 }
9453 }
9454
9455 if (buttons == NULL || *buttons == NUL)
9456 buttons = (char_u *)_("&Ok");
9457
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009458 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009459 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009460 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461#endif
9462}
9463
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009464/*
9465 * "copy()" function
9466 */
9467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009468f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009469 typval_T *argvars;
9470 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009471{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009472 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009475#ifdef FEAT_FLOAT
9476/*
9477 * "cos()" function
9478 */
9479 static void
9480f_cos(argvars, rettv)
9481 typval_T *argvars;
9482 typval_T *rettv;
9483{
9484 float_T f;
9485
9486 rettv->v_type = VAR_FLOAT;
9487 if (get_float_arg(argvars, &f) == OK)
9488 rettv->vval.v_float = cos(f);
9489 else
9490 rettv->vval.v_float = 0.0;
9491}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009492
9493/*
9494 * "cosh()" function
9495 */
9496 static void
9497f_cosh(argvars, rettv)
9498 typval_T *argvars;
9499 typval_T *rettv;
9500{
9501 float_T f;
9502
9503 rettv->v_type = VAR_FLOAT;
9504 if (get_float_arg(argvars, &f) == OK)
9505 rettv->vval.v_float = cosh(f);
9506 else
9507 rettv->vval.v_float = 0.0;
9508}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009509#endif
9510
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009512 * "count()" function
9513 */
9514 static void
9515f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009516 typval_T *argvars;
9517 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009518{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009519 long n = 0;
9520 int ic = FALSE;
9521
Bram Moolenaare9a41262005-01-15 22:18:47 +00009522 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009523 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009524 listitem_T *li;
9525 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009526 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009527
Bram Moolenaare9a41262005-01-15 22:18:47 +00009528 if ((l = argvars[0].vval.v_list) != NULL)
9529 {
9530 li = l->lv_first;
9531 if (argvars[2].v_type != VAR_UNKNOWN)
9532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009533 int error = FALSE;
9534
9535 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009536 if (argvars[3].v_type != VAR_UNKNOWN)
9537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009538 idx = get_tv_number_chk(&argvars[3], &error);
9539 if (!error)
9540 {
9541 li = list_find(l, idx);
9542 if (li == NULL)
9543 EMSGN(_(e_listidx), idx);
9544 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009545 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009546 if (error)
9547 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009548 }
9549
9550 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009551 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009552 ++n;
9553 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009554 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009555 else if (argvars[0].v_type == VAR_DICT)
9556 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009557 int todo;
9558 dict_T *d;
9559 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009560
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009561 if ((d = argvars[0].vval.v_dict) != NULL)
9562 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009563 int error = FALSE;
9564
Bram Moolenaare9a41262005-01-15 22:18:47 +00009565 if (argvars[2].v_type != VAR_UNKNOWN)
9566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009567 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009568 if (argvars[3].v_type != VAR_UNKNOWN)
9569 EMSG(_(e_invarg));
9570 }
9571
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009572 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009573 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009574 {
9575 if (!HASHITEM_EMPTY(hi))
9576 {
9577 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009578 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009579 ++n;
9580 }
9581 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009582 }
9583 }
9584 else
9585 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009586 rettv->vval.v_number = n;
9587}
9588
9589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9591 *
9592 * Checks the existence of a cscope connection.
9593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009596 typval_T *argvars UNUSED;
9597 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598{
9599#ifdef FEAT_CSCOPE
9600 int num = 0;
9601 char_u *dbpath = NULL;
9602 char_u *prepend = NULL;
9603 char_u buf[NUMBUFLEN];
9604
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009605 if (argvars[0].v_type != VAR_UNKNOWN
9606 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 num = (int)get_tv_number(&argvars[0]);
9609 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009610 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 }
9613
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615#endif
9616}
9617
9618/*
9619 * "cursor(lnum, col)" function
9620 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009621 * Moves the cursor to the specified line and column.
9622 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009626 typval_T *argvars;
9627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628{
9629 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009630#ifdef FEAT_VIRTUALEDIT
9631 long coladd = 0;
9632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009634 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009635 if (argvars[1].v_type == VAR_UNKNOWN)
9636 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009637 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009638
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009639 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009640 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009641 line = pos.lnum;
9642 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009643#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009644 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009645#endif
9646 }
9647 else
9648 {
9649 line = get_tv_lnum(argvars);
9650 col = get_tv_number_chk(&argvars[1], NULL);
9651#ifdef FEAT_VIRTUALEDIT
9652 if (argvars[2].v_type != VAR_UNKNOWN)
9653 coladd = get_tv_number_chk(&argvars[2], NULL);
9654#endif
9655 }
9656 if (line < 0 || col < 0
9657#ifdef FEAT_VIRTUALEDIT
9658 || coladd < 0
9659#endif
9660 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009661 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 if (line > 0)
9663 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (col > 0)
9665 curwin->w_cursor.col = col - 1;
9666#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009667 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668#endif
9669
9670 /* Make sure the cursor is in a valid position. */
9671 check_cursor();
9672#ifdef FEAT_MBYTE
9673 /* Correct cursor for multi-byte character. */
9674 if (has_mbyte)
9675 mb_adjust_cursor();
9676#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009677
9678 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009679 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680}
9681
9682/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009683 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 */
9685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009687 typval_T *argvars;
9688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009690 int noref = 0;
9691
9692 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009694 if (noref < 0 || noref > 1)
9695 EMSG(_(e_invarg));
9696 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009697 {
9698 current_copyID += COPYID_INC;
9699 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701}
9702
9703/*
9704 * "delete()" function
9705 */
9706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009708 typval_T *argvars;
9709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
9711 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715}
9716
9717/*
9718 * "did_filetype()" function
9719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009722 typval_T *argvars UNUSED;
9723 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727#endif
9728}
9729
9730/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009731 * "diff_filler()" function
9732 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009734f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009735 typval_T *argvars UNUSED;
9736 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009737{
9738#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009739 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009740#endif
9741}
9742
9743/*
9744 * "diff_hlID()" function
9745 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009747f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009748 typval_T *argvars UNUSED;
9749 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009750{
9751#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009752 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009753 static linenr_T prev_lnum = 0;
9754 static int changedtick = 0;
9755 static int fnum = 0;
9756 static int change_start = 0;
9757 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009758 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009759 int filler_lines;
9760 int col;
9761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009762 if (lnum < 0) /* ignore type error in {lnum} arg */
9763 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009764 if (lnum != prev_lnum
9765 || changedtick != curbuf->b_changedtick
9766 || fnum != curbuf->b_fnum)
9767 {
9768 /* New line, buffer, change: need to get the values. */
9769 filler_lines = diff_check(curwin, lnum);
9770 if (filler_lines < 0)
9771 {
9772 if (filler_lines == -1)
9773 {
9774 change_start = MAXCOL;
9775 change_end = -1;
9776 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9777 hlID = HLF_ADD; /* added line */
9778 else
9779 hlID = HLF_CHD; /* changed line */
9780 }
9781 else
9782 hlID = HLF_ADD; /* added line */
9783 }
9784 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009785 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009786 prev_lnum = lnum;
9787 changedtick = curbuf->b_changedtick;
9788 fnum = curbuf->b_fnum;
9789 }
9790
9791 if (hlID == HLF_CHD || hlID == HLF_TXD)
9792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009793 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009794 if (col >= change_start && col <= change_end)
9795 hlID = HLF_TXD; /* changed text */
9796 else
9797 hlID = HLF_CHD; /* changed line */
9798 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009799 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009800#endif
9801}
9802
9803/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009804 * "empty({expr})" function
9805 */
9806 static void
9807f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009808 typval_T *argvars;
9809 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009810{
9811 int n;
9812
9813 switch (argvars[0].v_type)
9814 {
9815 case VAR_STRING:
9816 case VAR_FUNC:
9817 n = argvars[0].vval.v_string == NULL
9818 || *argvars[0].vval.v_string == NUL;
9819 break;
9820 case VAR_NUMBER:
9821 n = argvars[0].vval.v_number == 0;
9822 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009823#ifdef FEAT_FLOAT
9824 case VAR_FLOAT:
9825 n = argvars[0].vval.v_float == 0.0;
9826 break;
9827#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009828 case VAR_LIST:
9829 n = argvars[0].vval.v_list == NULL
9830 || argvars[0].vval.v_list->lv_first == NULL;
9831 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009832 case VAR_DICT:
9833 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009834 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009835 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009836 default:
9837 EMSG2(_(e_intern2), "f_empty()");
9838 n = 0;
9839 }
9840
9841 rettv->vval.v_number = n;
9842}
9843
9844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 * "escape({string}, {chars})" function
9846 */
9847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009849 typval_T *argvars;
9850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851{
9852 char_u buf[NUMBUFLEN];
9853
Bram Moolenaar758711c2005-02-02 23:11:38 +00009854 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9855 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857}
9858
9859/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009860 * "eval()" function
9861 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009862 static void
9863f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 typval_T *argvars;
9865 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009866{
9867 char_u *s;
9868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009869 s = get_tv_string_chk(&argvars[0]);
9870 if (s != NULL)
9871 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009873 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9874 {
9875 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009876 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009877 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009878 else if (*s != NUL)
9879 EMSG(_(e_trailing));
9880}
9881
9882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 * "eventhandler()" function
9884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009886f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009887 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891}
9892
9893/*
9894 * "executable()" function
9895 */
9896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009897f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009898 typval_T *argvars;
9899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902}
9903
9904/*
9905 * "exists()" function
9906 */
9907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009909 typval_T *argvars;
9910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911{
9912 char_u *p;
9913 char_u *name;
9914 int n = FALSE;
9915 int len = 0;
9916
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009917 no_autoload = TRUE;
9918
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 if (*p == '$') /* environment variable */
9921 {
9922 /* first try "normal" environment variables (fast) */
9923 if (mch_getenv(p + 1) != NULL)
9924 n = TRUE;
9925 else
9926 {
9927 /* try expanding things like $VIM and ${HOME} */
9928 p = expand_env_save(p);
9929 if (p != NULL && *p != '$')
9930 n = TRUE;
9931 vim_free(p);
9932 }
9933 }
9934 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009937 if (*skipwhite(p) != NUL)
9938 n = FALSE; /* trailing garbage */
9939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 else if (*p == '*') /* internal or user defined function */
9941 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009942 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 }
9944 else if (*p == ':')
9945 {
9946 n = cmd_exists(p + 1);
9947 }
9948 else if (*p == '#')
9949 {
9950#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009951 if (p[1] == '#')
9952 n = autocmd_supported(p + 2);
9953 else
9954 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955#endif
9956 }
9957 else /* internal variable */
9958 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009959 char_u *tofree;
9960 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009962 /* get_name_len() takes care of expanding curly braces */
9963 name = p;
9964 len = get_name_len(&p, &tofree, TRUE, FALSE);
9965 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009967 if (tofree != NULL)
9968 name = tofree;
9969 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9970 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009972 /* handle d.key, l[idx], f(expr) */
9973 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9974 if (n)
9975 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 }
9977 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009978 if (*p != NUL)
9979 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009981 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 }
9983
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009984 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009985
9986 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987}
9988
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009989#ifdef FEAT_FLOAT
9990/*
9991 * "exp()" function
9992 */
9993 static void
9994f_exp(argvars, rettv)
9995 typval_T *argvars;
9996 typval_T *rettv;
9997{
9998 float_T f;
9999
10000 rettv->v_type = VAR_FLOAT;
10001 if (get_float_arg(argvars, &f) == OK)
10002 rettv->vval.v_float = exp(f);
10003 else
10004 rettv->vval.v_float = 0.0;
10005}
10006#endif
10007
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008/*
10009 * "expand()" function
10010 */
10011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010013 typval_T *argvars;
10014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015{
10016 char_u *s;
10017 int len;
10018 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010019 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010021 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023 rettv->v_type = VAR_STRING;
10024 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 if (*s == '%' || *s == '#' || *s == '<')
10026 {
10027 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010028 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 --emsg_off;
10030 }
10031 else
10032 {
10033 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010034 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010035 if (argvars[1].v_type != VAR_UNKNOWN
10036 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010037 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010038 if (!error)
10039 {
10040 ExpandInit(&xpc);
10041 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010042 if (p_wic)
10043 options += WILD_ICASE;
10044 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010045 }
10046 else
10047 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 }
10049}
10050
10051/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010052 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010053 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010054 */
10055 static void
10056f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010057 typval_T *argvars;
10058 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010059{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010060 char *arg_errmsg = N_("extend() argument");
10061
Bram Moolenaare9a41262005-01-15 22:18:47 +000010062 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010063 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010064 list_T *l1, *l2;
10065 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010066 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010067 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010068
Bram Moolenaare9a41262005-01-15 22:18:47 +000010069 l1 = argvars[0].vval.v_list;
10070 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010071 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010072 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010073 {
10074 if (argvars[2].v_type != VAR_UNKNOWN)
10075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010076 before = get_tv_number_chk(&argvars[2], &error);
10077 if (error)
10078 return; /* type error; errmsg already given */
10079
Bram Moolenaar758711c2005-02-02 23:11:38 +000010080 if (before == l1->lv_len)
10081 item = NULL;
10082 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010083 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010084 item = list_find(l1, before);
10085 if (item == NULL)
10086 {
10087 EMSGN(_(e_listidx), before);
10088 return;
10089 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010090 }
10091 }
10092 else
10093 item = NULL;
10094 list_extend(l1, l2, item);
10095
Bram Moolenaare9a41262005-01-15 22:18:47 +000010096 copy_tv(&argvars[0], rettv);
10097 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010098 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010099 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10100 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010101 dict_T *d1, *d2;
10102 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010103 char_u *action;
10104 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010105 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010106 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010107
10108 d1 = argvars[0].vval.v_dict;
10109 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010110 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010111 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010112 {
10113 /* Check the third argument. */
10114 if (argvars[2].v_type != VAR_UNKNOWN)
10115 {
10116 static char *(av[]) = {"keep", "force", "error"};
10117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010118 action = get_tv_string_chk(&argvars[2]);
10119 if (action == NULL)
10120 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010121 for (i = 0; i < 3; ++i)
10122 if (STRCMP(action, av[i]) == 0)
10123 break;
10124 if (i == 3)
10125 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010126 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010127 return;
10128 }
10129 }
10130 else
10131 action = (char_u *)"force";
10132
10133 /* Go over all entries in the second dict and add them to the
10134 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010135 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010136 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010137 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010138 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010139 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010140 --todo;
10141 di1 = dict_find(d1, hi2->hi_key, -1);
10142 if (di1 == NULL)
10143 {
10144 di1 = dictitem_copy(HI2DI(hi2));
10145 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10146 dictitem_free(di1);
10147 }
10148 else if (*action == 'e')
10149 {
10150 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10151 break;
10152 }
10153 else if (*action == 'f')
10154 {
10155 clear_tv(&di1->di_tv);
10156 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10157 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010158 }
10159 }
10160
Bram Moolenaare9a41262005-01-15 22:18:47 +000010161 copy_tv(&argvars[0], rettv);
10162 }
10163 }
10164 else
10165 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010166}
10167
10168/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010169 * "feedkeys()" function
10170 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010171 static void
10172f_feedkeys(argvars, rettv)
10173 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010174 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010175{
10176 int remap = TRUE;
10177 char_u *keys, *flags;
10178 char_u nbuf[NUMBUFLEN];
10179 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010180 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010181
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010182 /* This is not allowed in the sandbox. If the commands would still be
10183 * executed in the sandbox it would be OK, but it probably happens later,
10184 * when "sandbox" is no longer set. */
10185 if (check_secure())
10186 return;
10187
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010188 keys = get_tv_string(&argvars[0]);
10189 if (*keys != NUL)
10190 {
10191 if (argvars[1].v_type != VAR_UNKNOWN)
10192 {
10193 flags = get_tv_string_buf(&argvars[1], nbuf);
10194 for ( ; *flags != NUL; ++flags)
10195 {
10196 switch (*flags)
10197 {
10198 case 'n': remap = FALSE; break;
10199 case 'm': remap = TRUE; break;
10200 case 't': typed = TRUE; break;
10201 }
10202 }
10203 }
10204
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010205 /* Need to escape K_SPECIAL and CSI before putting the string in the
10206 * typeahead buffer. */
10207 keys_esc = vim_strsave_escape_csi(keys);
10208 if (keys_esc != NULL)
10209 {
10210 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010211 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010212 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010213 if (vgetc_busy)
10214 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010215 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010216 }
10217}
10218
10219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 * "filereadable()" function
10221 */
10222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010224 typval_T *argvars;
10225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010227 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 char_u *p;
10229 int n;
10230
Bram Moolenaarc236c162008-07-13 17:41:49 +000010231#ifndef O_NONBLOCK
10232# define O_NONBLOCK 0
10233#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010234 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010235 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10236 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 {
10238 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010239 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 }
10241 else
10242 n = FALSE;
10243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010244 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245}
10246
10247/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010248 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 * rights to write into.
10250 */
10251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010252f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010253 typval_T *argvars;
10254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010256 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257}
10258
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010259static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010260
10261 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010262findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010263 typval_T *argvars;
10264 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010265 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010266{
10267#ifdef FEAT_SEARCHPATH
10268 char_u *fname;
10269 char_u *fresult = NULL;
10270 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10271 char_u *p;
10272 char_u pathbuf[NUMBUFLEN];
10273 int count = 1;
10274 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010275 int error = FALSE;
10276#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010277
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010278 rettv->vval.v_string = NULL;
10279 rettv->v_type = VAR_STRING;
10280
10281#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010282 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010283
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010284 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010286 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10287 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010288 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 else
10290 {
10291 if (*p != NUL)
10292 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010294 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010295 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010296 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010297 }
10298
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010299 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10300 error = TRUE;
10301
10302 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010304 do
10305 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010306 if (rettv->v_type == VAR_STRING)
10307 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010308 fresult = find_file_in_path_option(first ? fname : NULL,
10309 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010310 0, first, path,
10311 find_what,
10312 curbuf->b_ffname,
10313 find_what == FINDFILE_DIR
10314 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010315 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010316
10317 if (fresult != NULL && rettv->v_type == VAR_LIST)
10318 list_append_string(rettv->vval.v_list, fresult, -1);
10319
10320 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010321 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010322
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010323 if (rettv->v_type == VAR_STRING)
10324 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010325#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010326}
10327
Bram Moolenaar33570922005-01-25 22:26:29 +000010328static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10329static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010330
10331/*
10332 * Implementation of map() and filter().
10333 */
10334 static void
10335filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010336 typval_T *argvars;
10337 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010338 int map;
10339{
10340 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010341 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010342 listitem_T *li, *nli;
10343 list_T *l = NULL;
10344 dictitem_T *di;
10345 hashtab_T *ht;
10346 hashitem_T *hi;
10347 dict_T *d = NULL;
10348 typval_T save_val;
10349 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010350 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010351 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010352 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10353 char *arg_errmsg = (map ? N_("map() argument")
10354 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010355 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010356 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010357
Bram Moolenaare9a41262005-01-15 22:18:47 +000010358 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010359 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010360 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010361 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010362 return;
10363 }
10364 else if (argvars[0].v_type == VAR_DICT)
10365 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010366 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010367 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010368 return;
10369 }
10370 else
10371 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010372 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010373 return;
10374 }
10375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010376 expr = get_tv_string_buf_chk(&argvars[1], buf);
10377 /* On type errors, the preceding call has already displayed an error
10378 * message. Avoid a misleading error message for an empty string that
10379 * was not passed as argument. */
10380 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010382 prepare_vimvar(VV_VAL, &save_val);
10383 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010384
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010385 /* We reset "did_emsg" to be able to detect whether an error
10386 * occurred during evaluation of the expression. */
10387 save_did_emsg = did_emsg;
10388 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010389
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010390 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010391 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010393 vimvars[VV_KEY].vv_type = VAR_STRING;
10394
10395 ht = &d->dv_hashtab;
10396 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010397 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010398 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010400 if (!HASHITEM_EMPTY(hi))
10401 {
10402 --todo;
10403 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010404 if (tv_check_lock(di->di_tv.v_lock,
10405 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010406 break;
10407 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010408 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010409 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010410 break;
10411 if (!map && rem)
10412 dictitem_remove(d, di);
10413 clear_tv(&vimvars[VV_KEY].vv_tv);
10414 }
10415 }
10416 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 }
10418 else
10419 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010420 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010422 for (li = l->lv_first; li != NULL; li = nli)
10423 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010424 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010425 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010426 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010427 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010428 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010429 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010430 break;
10431 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010433 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010434 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010435 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010436
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010437 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010438 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010439
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010440 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010441 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010442
10443 copy_tv(&argvars[0], rettv);
10444}
10445
10446 static int
10447filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010448 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010449 char_u *expr;
10450 int map;
10451 int *remp;
10452{
Bram Moolenaar33570922005-01-25 22:26:29 +000010453 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010455 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010456
Bram Moolenaar33570922005-01-25 22:26:29 +000010457 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010458 s = expr;
10459 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010460 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010461 if (*s != NUL) /* check for trailing chars after expr */
10462 {
10463 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010464 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010465 }
10466 if (map)
10467 {
10468 /* map(): replace the list item value */
10469 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010470 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010471 *tv = rettv;
10472 }
10473 else
10474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010475 int error = FALSE;
10476
Bram Moolenaare9a41262005-01-15 22:18:47 +000010477 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010478 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010479 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010480 /* On type error, nothing has been removed; return FAIL to stop the
10481 * loop. The error message was given by get_tv_number_chk(). */
10482 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010483 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010485 retval = OK;
10486theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010487 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010488 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010489}
10490
10491/*
10492 * "filter()" function
10493 */
10494 static void
10495f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 typval_T *argvars;
10497 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010498{
10499 filter_map(argvars, rettv, FALSE);
10500}
10501
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010502/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010503 * "finddir({fname}[, {path}[, {count}]])" function
10504 */
10505 static void
10506f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010507 typval_T *argvars;
10508 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010509{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010510 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010511}
10512
10513/*
10514 * "findfile({fname}[, {path}[, {count}]])" function
10515 */
10516 static void
10517f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010518 typval_T *argvars;
10519 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010520{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010521 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010522}
10523
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010524#ifdef FEAT_FLOAT
10525/*
10526 * "float2nr({float})" function
10527 */
10528 static void
10529f_float2nr(argvars, rettv)
10530 typval_T *argvars;
10531 typval_T *rettv;
10532{
10533 float_T f;
10534
10535 if (get_float_arg(argvars, &f) == OK)
10536 {
10537 if (f < -0x7fffffff)
10538 rettv->vval.v_number = -0x7fffffff;
10539 else if (f > 0x7fffffff)
10540 rettv->vval.v_number = 0x7fffffff;
10541 else
10542 rettv->vval.v_number = (varnumber_T)f;
10543 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010544}
10545
10546/*
10547 * "floor({float})" function
10548 */
10549 static void
10550f_floor(argvars, rettv)
10551 typval_T *argvars;
10552 typval_T *rettv;
10553{
10554 float_T f;
10555
10556 rettv->v_type = VAR_FLOAT;
10557 if (get_float_arg(argvars, &f) == OK)
10558 rettv->vval.v_float = floor(f);
10559 else
10560 rettv->vval.v_float = 0.0;
10561}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010562
10563/*
10564 * "fmod()" function
10565 */
10566 static void
10567f_fmod(argvars, rettv)
10568 typval_T *argvars;
10569 typval_T *rettv;
10570{
10571 float_T fx, fy;
10572
10573 rettv->v_type = VAR_FLOAT;
10574 if (get_float_arg(argvars, &fx) == OK
10575 && get_float_arg(&argvars[1], &fy) == OK)
10576 rettv->vval.v_float = fmod(fx, fy);
10577 else
10578 rettv->vval.v_float = 0.0;
10579}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010580#endif
10581
Bram Moolenaar0d660222005-01-07 21:51:51 +000010582/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010583 * "fnameescape({string})" function
10584 */
10585 static void
10586f_fnameescape(argvars, rettv)
10587 typval_T *argvars;
10588 typval_T *rettv;
10589{
10590 rettv->vval.v_string = vim_strsave_fnameescape(
10591 get_tv_string(&argvars[0]), FALSE);
10592 rettv->v_type = VAR_STRING;
10593}
10594
10595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 * "fnamemodify({fname}, {mods})" function
10597 */
10598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010600 typval_T *argvars;
10601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602{
10603 char_u *fname;
10604 char_u *mods;
10605 int usedlen = 0;
10606 int len;
10607 char_u *fbuf = NULL;
10608 char_u buf[NUMBUFLEN];
10609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 fname = get_tv_string_chk(&argvars[0]);
10611 mods = get_tv_string_buf_chk(&argvars[1], buf);
10612 if (fname == NULL || mods == NULL)
10613 fname = NULL;
10614 else
10615 {
10616 len = (int)STRLEN(fname);
10617 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010620 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 vim_free(fbuf);
10626}
10627
Bram Moolenaar33570922005-01-25 22:26:29 +000010628static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629
10630/*
10631 * "foldclosed()" function
10632 */
10633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010635 typval_T *argvars;
10636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 int end;
10638{
10639#ifdef FEAT_FOLDING
10640 linenr_T lnum;
10641 linenr_T first, last;
10642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10645 {
10646 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10647 {
10648 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010649 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010651 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 return;
10653 }
10654 }
10655#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657}
10658
10659/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010660 * "foldclosed()" function
10661 */
10662 static void
10663f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010664 typval_T *argvars;
10665 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010666{
10667 foldclosed_both(argvars, rettv, FALSE);
10668}
10669
10670/*
10671 * "foldclosedend()" function
10672 */
10673 static void
10674f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010675 typval_T *argvars;
10676 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010677{
10678 foldclosed_both(argvars, rettv, TRUE);
10679}
10680
10681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 * "foldlevel()" function
10683 */
10684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010686 typval_T *argvars;
10687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
10689#ifdef FEAT_FOLDING
10690 linenr_T lnum;
10691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010692 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010694 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696}
10697
10698/*
10699 * "foldtext()" function
10700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010702f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010703 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705{
10706#ifdef FEAT_FOLDING
10707 linenr_T lnum;
10708 char_u *s;
10709 char_u *r;
10710 int len;
10711 char *txt;
10712#endif
10713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714 rettv->v_type = VAR_STRING;
10715 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010717 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10718 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10719 <= curbuf->b_ml.ml_line_count
10720 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 {
10722 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010723 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10724 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 {
10726 if (!linewhite(lnum))
10727 break;
10728 ++lnum;
10729 }
10730
10731 /* Find interesting text in this line. */
10732 s = skipwhite(ml_get(lnum));
10733 /* skip C comment-start */
10734 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010737 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010738 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010739 {
10740 s = skipwhite(ml_get(lnum + 1));
10741 if (*s == '*')
10742 s = skipwhite(s + 1);
10743 }
10744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 txt = _("+-%s%3ld lines: ");
10746 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010747 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 + 20 /* for %3ld */
10749 + STRLEN(s))); /* concatenated */
10750 if (r != NULL)
10751 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010752 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10753 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10754 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 len = (int)STRLEN(r);
10756 STRCAT(r, s);
10757 /* remove 'foldmarker' and 'commentstring' */
10758 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010759 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 }
10761 }
10762#endif
10763}
10764
10765/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010766 * "foldtextresult(lnum)" function
10767 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010770 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010771 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010772{
10773#ifdef FEAT_FOLDING
10774 linenr_T lnum;
10775 char_u *text;
10776 char_u buf[51];
10777 foldinfo_T foldinfo;
10778 int fold_count;
10779#endif
10780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010781 rettv->v_type = VAR_STRING;
10782 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010783#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010784 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010785 /* treat illegal types and illegal string values for {lnum} the same */
10786 if (lnum < 0)
10787 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010788 fold_count = foldedCount(curwin, lnum, &foldinfo);
10789 if (fold_count > 0)
10790 {
10791 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10792 &foldinfo, buf);
10793 if (text == buf)
10794 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010795 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010796 }
10797#endif
10798}
10799
10800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 * "foreground()" function
10802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010804f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010805 typval_T *argvars UNUSED;
10806 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808#ifdef FEAT_GUI
10809 if (gui.in_use)
10810 gui_mch_set_foreground();
10811#else
10812# ifdef WIN32
10813 win32_set_foreground();
10814# endif
10815#endif
10816}
10817
10818/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010819 * "function()" function
10820 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010822f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010823 typval_T *argvars;
10824 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010825{
10826 char_u *s;
10827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010829 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010830 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010831 /* Don't check an autoload name for existence here. */
10832 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010833 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010834 else
10835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836 rettv->vval.v_string = vim_strsave(s);
10837 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010838 }
10839}
10840
10841/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010842 * "garbagecollect()" function
10843 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010844 static void
10845f_garbagecollect(argvars, rettv)
10846 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010847 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010848{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010849 /* This is postponed until we are back at the toplevel, because we may be
10850 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10851 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010852
10853 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10854 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010855}
10856
10857/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010858 * "get()" function
10859 */
10860 static void
10861f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010862 typval_T *argvars;
10863 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010864{
Bram Moolenaar33570922005-01-25 22:26:29 +000010865 listitem_T *li;
10866 list_T *l;
10867 dictitem_T *di;
10868 dict_T *d;
10869 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010870
Bram Moolenaare9a41262005-01-15 22:18:47 +000010871 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010872 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010873 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010874 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 int error = FALSE;
10876
10877 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10878 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010879 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010880 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010881 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010882 else if (argvars[0].v_type == VAR_DICT)
10883 {
10884 if ((d = argvars[0].vval.v_dict) != NULL)
10885 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010886 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010887 if (di != NULL)
10888 tv = &di->di_tv;
10889 }
10890 }
10891 else
10892 EMSG2(_(e_listdictarg), "get()");
10893
10894 if (tv == NULL)
10895 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010896 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010897 copy_tv(&argvars[2], rettv);
10898 }
10899 else
10900 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010901}
10902
Bram Moolenaar342337a2005-07-21 21:11:17 +000010903static 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 +000010904
10905/*
10906 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010907 * Return a range (from start to end) of lines in rettv from the specified
10908 * buffer.
10909 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010910 */
10911 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010912get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010913 buf_T *buf;
10914 linenr_T start;
10915 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010916 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010917 typval_T *rettv;
10918{
10919 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010920
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010921 if (retlist && rettv_list_alloc(rettv) == FAIL)
10922 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010923
10924 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10925 return;
10926
10927 if (!retlist)
10928 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010929 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10930 p = ml_get_buf(buf, start, FALSE);
10931 else
10932 p = (char_u *)"";
10933
10934 rettv->v_type = VAR_STRING;
10935 rettv->vval.v_string = vim_strsave(p);
10936 }
10937 else
10938 {
10939 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010940 return;
10941
10942 if (start < 1)
10943 start = 1;
10944 if (end > buf->b_ml.ml_line_count)
10945 end = buf->b_ml.ml_line_count;
10946 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010947 if (list_append_string(rettv->vval.v_list,
10948 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010949 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010950 }
10951}
10952
10953/*
10954 * "getbufline()" function
10955 */
10956 static void
10957f_getbufline(argvars, rettv)
10958 typval_T *argvars;
10959 typval_T *rettv;
10960{
10961 linenr_T lnum;
10962 linenr_T end;
10963 buf_T *buf;
10964
10965 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10966 ++emsg_off;
10967 buf = get_buf_tv(&argvars[0]);
10968 --emsg_off;
10969
Bram Moolenaar661b1822005-07-28 22:36:45 +000010970 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010971 if (argvars[2].v_type == VAR_UNKNOWN)
10972 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010973 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010974 end = get_tv_lnum_buf(&argvars[2], buf);
10975
Bram Moolenaar342337a2005-07-21 21:11:17 +000010976 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010977}
10978
Bram Moolenaar0d660222005-01-07 21:51:51 +000010979/*
10980 * "getbufvar()" function
10981 */
10982 static void
10983f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010984 typval_T *argvars;
10985 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010986{
10987 buf_T *buf;
10988 buf_T *save_curbuf;
10989 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010990 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010992 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10993 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010994 ++emsg_off;
10995 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010996
10997 rettv->v_type = VAR_STRING;
10998 rettv->vval.v_string = NULL;
10999
11000 if (buf != NULL && varname != NULL)
11001 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011002 /* set curbuf to be our buf, temporarily */
11003 save_curbuf = curbuf;
11004 curbuf = buf;
11005
Bram Moolenaar0d660222005-01-07 21:51:51 +000011006 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011007 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011008 else if (STRCMP(varname, "changedtick") == 0)
11009 {
11010 rettv->v_type = VAR_NUMBER;
11011 rettv->vval.v_number = curbuf->b_changedtick;
11012 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011013 else
11014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011015 if (*varname == NUL)
11016 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11017 * scope prefix before the NUL byte is required by
11018 * find_var_in_ht(). */
11019 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011020 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011021 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011022 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011023 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011024 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011025
11026 /* restore previous notion of curbuf */
11027 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011028 }
11029
11030 --emsg_off;
11031}
11032
11033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 * "getchar()" function
11035 */
11036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011038 typval_T *argvars;
11039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040{
11041 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011042 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011044 /* Position the cursor. Needed after a message that ends in a space. */
11045 windgoto(msg_row, msg_col);
11046
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 ++no_mapping;
11048 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011049 for (;;)
11050 {
11051 if (argvars[0].v_type == VAR_UNKNOWN)
11052 /* getchar(): blocking wait. */
11053 n = safe_vgetc();
11054 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11055 /* getchar(1): only check if char avail */
11056 n = vpeekc();
11057 else if (error || vpeekc() == NUL)
11058 /* illegal argument or getchar(0) and no char avail: return zero */
11059 n = 0;
11060 else
11061 /* getchar(0) and char avail: return char */
11062 n = safe_vgetc();
11063 if (n == K_IGNORE)
11064 continue;
11065 break;
11066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 --no_mapping;
11068 --allow_keys;
11069
Bram Moolenaar219b8702006-11-01 14:32:36 +000011070 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11071 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11072 vimvars[VV_MOUSE_COL].vv_nr = 0;
11073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 if (IS_SPECIAL(n) || mod_mask != 0)
11076 {
11077 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11078 int i = 0;
11079
11080 /* Turn a special key into three bytes, plus modifier. */
11081 if (mod_mask != 0)
11082 {
11083 temp[i++] = K_SPECIAL;
11084 temp[i++] = KS_MODIFIER;
11085 temp[i++] = mod_mask;
11086 }
11087 if (IS_SPECIAL(n))
11088 {
11089 temp[i++] = K_SPECIAL;
11090 temp[i++] = K_SECOND(n);
11091 temp[i++] = K_THIRD(n);
11092 }
11093#ifdef FEAT_MBYTE
11094 else if (has_mbyte)
11095 i += (*mb_char2bytes)(n, temp + i);
11096#endif
11097 else
11098 temp[i++] = n;
11099 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011100 rettv->v_type = VAR_STRING;
11101 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011102
11103#ifdef FEAT_MOUSE
11104 if (n == K_LEFTMOUSE
11105 || n == K_LEFTMOUSE_NM
11106 || n == K_LEFTDRAG
11107 || n == K_LEFTRELEASE
11108 || n == K_LEFTRELEASE_NM
11109 || n == K_MIDDLEMOUSE
11110 || n == K_MIDDLEDRAG
11111 || n == K_MIDDLERELEASE
11112 || n == K_RIGHTMOUSE
11113 || n == K_RIGHTDRAG
11114 || n == K_RIGHTRELEASE
11115 || n == K_X1MOUSE
11116 || n == K_X1DRAG
11117 || n == K_X1RELEASE
11118 || n == K_X2MOUSE
11119 || n == K_X2DRAG
11120 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011121 || n == K_MOUSELEFT
11122 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011123 || n == K_MOUSEDOWN
11124 || n == K_MOUSEUP)
11125 {
11126 int row = mouse_row;
11127 int col = mouse_col;
11128 win_T *win;
11129 linenr_T lnum;
11130# ifdef FEAT_WINDOWS
11131 win_T *wp;
11132# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011133 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011134
11135 if (row >= 0 && col >= 0)
11136 {
11137 /* Find the window at the mouse coordinates and compute the
11138 * text position. */
11139 win = mouse_find_win(&row, &col);
11140 (void)mouse_comp_pos(win, &row, &col, &lnum);
11141# ifdef FEAT_WINDOWS
11142 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011143 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011144# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011145 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011146 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11147 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11148 }
11149 }
11150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 }
11152}
11153
11154/*
11155 * "getcharmod()" function
11156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011159 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011162 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163}
11164
11165/*
11166 * "getcmdline()" function
11167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011169f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011170 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 rettv->v_type = VAR_STRING;
11174 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175}
11176
11177/*
11178 * "getcmdpos()" function
11179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011181f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011182 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186}
11187
11188/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011189 * "getcmdtype()" function
11190 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011191 static void
11192f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011193 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011194 typval_T *rettv;
11195{
11196 rettv->v_type = VAR_STRING;
11197 rettv->vval.v_string = alloc(2);
11198 if (rettv->vval.v_string != NULL)
11199 {
11200 rettv->vval.v_string[0] = get_cmdline_type();
11201 rettv->vval.v_string[1] = NUL;
11202 }
11203}
11204
11205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 * "getcwd()" function
11207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011210 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011213 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011216 rettv->vval.v_string = NULL;
11217 cwd = alloc(MAXPATHL);
11218 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011220 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11221 {
11222 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011224 if (rettv->vval.v_string != NULL)
11225 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011227 }
11228 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 }
11230}
11231
11232/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011233 * "getfontname()" function
11234 */
11235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011237 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011238 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011239{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011240 rettv->v_type = VAR_STRING;
11241 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011242#ifdef FEAT_GUI
11243 if (gui.in_use)
11244 {
11245 GuiFont font;
11246 char_u *name = NULL;
11247
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011248 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011249 {
11250 /* Get the "Normal" font. Either the name saved by
11251 * hl_set_font_name() or from the font ID. */
11252 font = gui.norm_font;
11253 name = hl_get_font_name();
11254 }
11255 else
11256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011258 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11259 return;
11260 font = gui_mch_get_font(name, FALSE);
11261 if (font == NOFONT)
11262 return; /* Invalid font name, return empty string. */
11263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011265 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011266 gui_mch_free_font(font);
11267 }
11268#endif
11269}
11270
11271/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011272 * "getfperm({fname})" function
11273 */
11274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011276 typval_T *argvars;
11277 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011278{
11279 char_u *fname;
11280 struct stat st;
11281 char_u *perm = NULL;
11282 char_u flags[] = "rwx";
11283 int i;
11284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011288 if (mch_stat((char *)fname, &st) >= 0)
11289 {
11290 perm = vim_strsave((char_u *)"---------");
11291 if (perm != NULL)
11292 {
11293 for (i = 0; i < 9; i++)
11294 {
11295 if (st.st_mode & (1 << (8 - i)))
11296 perm[i] = flags[i % 3];
11297 }
11298 }
11299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011301}
11302
11303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 * "getfsize({fname})" function
11305 */
11306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011307f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011308 typval_T *argvars;
11309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310{
11311 char_u *fname;
11312 struct stat st;
11313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317
11318 if (mch_stat((char *)fname, &st) >= 0)
11319 {
11320 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011323 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011325
11326 /* non-perfect check for overflow */
11327 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11328 rettv->vval.v_number = -2;
11329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 }
11331 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333}
11334
11335/*
11336 * "getftime({fname})" function
11337 */
11338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011340 typval_T *argvars;
11341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342{
11343 char_u *fname;
11344 struct stat st;
11345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347
11348 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352}
11353
11354/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011355 * "getftype({fname})" function
11356 */
11357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011359 typval_T *argvars;
11360 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011361{
11362 char_u *fname;
11363 struct stat st;
11364 char_u *type = NULL;
11365 char *t;
11366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011370 if (mch_lstat((char *)fname, &st) >= 0)
11371 {
11372#ifdef S_ISREG
11373 if (S_ISREG(st.st_mode))
11374 t = "file";
11375 else if (S_ISDIR(st.st_mode))
11376 t = "dir";
11377# ifdef S_ISLNK
11378 else if (S_ISLNK(st.st_mode))
11379 t = "link";
11380# endif
11381# ifdef S_ISBLK
11382 else if (S_ISBLK(st.st_mode))
11383 t = "bdev";
11384# endif
11385# ifdef S_ISCHR
11386 else if (S_ISCHR(st.st_mode))
11387 t = "cdev";
11388# endif
11389# ifdef S_ISFIFO
11390 else if (S_ISFIFO(st.st_mode))
11391 t = "fifo";
11392# endif
11393# ifdef S_ISSOCK
11394 else if (S_ISSOCK(st.st_mode))
11395 t = "fifo";
11396# endif
11397 else
11398 t = "other";
11399#else
11400# ifdef S_IFMT
11401 switch (st.st_mode & S_IFMT)
11402 {
11403 case S_IFREG: t = "file"; break;
11404 case S_IFDIR: t = "dir"; break;
11405# ifdef S_IFLNK
11406 case S_IFLNK: t = "link"; break;
11407# endif
11408# ifdef S_IFBLK
11409 case S_IFBLK: t = "bdev"; break;
11410# endif
11411# ifdef S_IFCHR
11412 case S_IFCHR: t = "cdev"; break;
11413# endif
11414# ifdef S_IFIFO
11415 case S_IFIFO: t = "fifo"; break;
11416# endif
11417# ifdef S_IFSOCK
11418 case S_IFSOCK: t = "socket"; break;
11419# endif
11420 default: t = "other";
11421 }
11422# else
11423 if (mch_isdir(fname))
11424 t = "dir";
11425 else
11426 t = "file";
11427# endif
11428#endif
11429 type = vim_strsave((char_u *)t);
11430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011432}
11433
11434/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011435 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011436 */
11437 static void
11438f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011439 typval_T *argvars;
11440 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011441{
11442 linenr_T lnum;
11443 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011444 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011445
11446 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011447 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011448 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011449 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011450 retlist = FALSE;
11451 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011452 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011453 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011454 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011455 retlist = TRUE;
11456 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011457
Bram Moolenaar342337a2005-07-21 21:11:17 +000011458 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011459}
11460
11461/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011462 * "getmatches()" function
11463 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011464 static void
11465f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011466 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011467 typval_T *rettv;
11468{
11469#ifdef FEAT_SEARCH_EXTRA
11470 dict_T *dict;
11471 matchitem_T *cur = curwin->w_match_head;
11472
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011473 if (rettv_list_alloc(rettv) == OK)
11474 {
11475 while (cur != NULL)
11476 {
11477 dict = dict_alloc();
11478 if (dict == NULL)
11479 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011480 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11481 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11482 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11483 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11484 list_append_dict(rettv->vval.v_list, dict);
11485 cur = cur->next;
11486 }
11487 }
11488#endif
11489}
11490
11491/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011492 * "getpid()" function
11493 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011494 static void
11495f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011496 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011497 typval_T *rettv;
11498{
11499 rettv->vval.v_number = mch_get_pid();
11500}
11501
11502/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011503 * "getpos(string)" function
11504 */
11505 static void
11506f_getpos(argvars, rettv)
11507 typval_T *argvars;
11508 typval_T *rettv;
11509{
11510 pos_T *fp;
11511 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011512 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011513
11514 if (rettv_list_alloc(rettv) == OK)
11515 {
11516 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011517 fp = var2fpos(&argvars[0], TRUE, &fnum);
11518 if (fnum != -1)
11519 list_append_number(l, (varnumber_T)fnum);
11520 else
11521 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011522 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11523 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011524 list_append_number(l, (fp != NULL)
11525 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011526 : (varnumber_T)0);
11527 list_append_number(l,
11528#ifdef FEAT_VIRTUALEDIT
11529 (fp != NULL) ? (varnumber_T)fp->coladd :
11530#endif
11531 (varnumber_T)0);
11532 }
11533 else
11534 rettv->vval.v_number = FALSE;
11535}
11536
11537/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011538 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011539 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011540 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011541f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011542 typval_T *argvars UNUSED;
11543 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011544{
11545#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011546 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011547#endif
11548
Bram Moolenaar2641f772005-03-25 21:58:17 +000011549#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011550 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011551 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011552 wp = NULL;
11553 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11554 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011555 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011556 if (wp == NULL)
11557 return;
11558 }
11559
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011560 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011561 }
11562#endif
11563}
11564
11565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 * "getreg()" function
11567 */
11568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011569f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011570 typval_T *argvars;
11571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572{
11573 char_u *strregname;
11574 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011575 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011576 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011578 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011580 strregname = get_tv_string_chk(&argvars[0]);
11581 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011582 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011583 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011586 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587 regname = (strregname == NULL ? '"' : *strregname);
11588 if (regname == 0)
11589 regname = '"';
11590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011591 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011592 rettv->vval.v_string = error ? NULL :
11593 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594}
11595
11596/*
11597 * "getregtype()" function
11598 */
11599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011600f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011601 typval_T *argvars;
11602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603{
11604 char_u *strregname;
11605 int regname;
11606 char_u buf[NUMBUFLEN + 2];
11607 long reglen = 0;
11608
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011609 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011610 {
11611 strregname = get_tv_string_chk(&argvars[0]);
11612 if (strregname == NULL) /* type error; errmsg already given */
11613 {
11614 rettv->v_type = VAR_STRING;
11615 rettv->vval.v_string = NULL;
11616 return;
11617 }
11618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619 else
11620 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011621 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622
11623 regname = (strregname == NULL ? '"' : *strregname);
11624 if (regname == 0)
11625 regname = '"';
11626
11627 buf[0] = NUL;
11628 buf[1] = NUL;
11629 switch (get_reg_type(regname, &reglen))
11630 {
11631 case MLINE: buf[0] = 'V'; break;
11632 case MCHAR: buf[0] = 'v'; break;
11633#ifdef FEAT_VISUAL
11634 case MBLOCK:
11635 buf[0] = Ctrl_V;
11636 sprintf((char *)buf + 1, "%ld", reglen + 1);
11637 break;
11638#endif
11639 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011640 rettv->v_type = VAR_STRING;
11641 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642}
11643
11644/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011645 * "gettabvar()" function
11646 */
11647 static void
11648f_gettabvar(argvars, rettv)
11649 typval_T *argvars;
11650 typval_T *rettv;
11651{
11652 tabpage_T *tp;
11653 dictitem_T *v;
11654 char_u *varname;
11655
11656 rettv->v_type = VAR_STRING;
11657 rettv->vval.v_string = NULL;
11658
11659 varname = get_tv_string_chk(&argvars[1]);
11660 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11661 if (tp != NULL && varname != NULL)
11662 {
11663 /* look up the variable */
11664 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11665 if (v != NULL)
11666 copy_tv(&v->di_tv, rettv);
11667 }
11668}
11669
11670/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011671 * "gettabwinvar()" function
11672 */
11673 static void
11674f_gettabwinvar(argvars, rettv)
11675 typval_T *argvars;
11676 typval_T *rettv;
11677{
11678 getwinvar(argvars, rettv, 1);
11679}
11680
11681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 * "getwinposx()" function
11683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011685f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011686 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690#ifdef FEAT_GUI
11691 if (gui.in_use)
11692 {
11693 int x, y;
11694
11695 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 }
11698#endif
11699}
11700
11701/*
11702 * "getwinposy()" function
11703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011705f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011706 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710#ifdef FEAT_GUI
11711 if (gui.in_use)
11712 {
11713 int x, y;
11714
11715 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011716 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 }
11718#endif
11719}
11720
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011721/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011722 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011723 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011724 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011725find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011726 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011727 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011728{
11729#ifdef FEAT_WINDOWS
11730 win_T *wp;
11731#endif
11732 int nr;
11733
11734 nr = get_tv_number_chk(vp, NULL);
11735
11736#ifdef FEAT_WINDOWS
11737 if (nr < 0)
11738 return NULL;
11739 if (nr == 0)
11740 return curwin;
11741
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011742 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11743 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011744 if (--nr <= 0)
11745 break;
11746 return wp;
11747#else
11748 if (nr == 0 || nr == 1)
11749 return curwin;
11750 return NULL;
11751#endif
11752}
11753
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754/*
11755 * "getwinvar()" function
11756 */
11757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011758f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011759 typval_T *argvars;
11760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011762 getwinvar(argvars, rettv, 0);
11763}
11764
11765/*
11766 * getwinvar() and gettabwinvar()
11767 */
11768 static void
11769getwinvar(argvars, rettv, off)
11770 typval_T *argvars;
11771 typval_T *rettv;
11772 int off; /* 1 for gettabwinvar() */
11773{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 win_T *win, *oldcurwin;
11775 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011776 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011777 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011779#ifdef FEAT_WINDOWS
11780 if (off == 1)
11781 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11782 else
11783 tp = curtab;
11784#endif
11785 win = find_win_by_nr(&argvars[off], tp);
11786 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011787 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011789 rettv->v_type = VAR_STRING;
11790 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791
11792 if (win != NULL && varname != NULL)
11793 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011794 /* Set curwin to be our win, temporarily. Also set curbuf, so
11795 * that we can get buffer-local options. */
11796 oldcurwin = curwin;
11797 curwin = win;
11798 curbuf = win->w_buffer;
11799
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 else
11803 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011804 if (*varname == NUL)
11805 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11806 * scope prefix before the NUL byte is required by
11807 * find_var_in_ht(). */
11808 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011810 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011812 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011814
11815 /* restore previous notion of curwin */
11816 curwin = oldcurwin;
11817 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 }
11819
11820 --emsg_off;
11821}
11822
11823/*
11824 * "glob()" function
11825 */
11826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011827f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011828 typval_T *argvars;
11829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011831 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011833 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011835 /* When the optional second argument is non-zero, don't remove matches
11836 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11837 if (argvars[1].v_type != VAR_UNKNOWN
11838 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011839 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011840 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011841 if (!error)
11842 {
11843 ExpandInit(&xpc);
11844 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011845 if (p_wic)
11846 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011847 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011848 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011849 }
11850 else
11851 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852}
11853
11854/*
11855 * "globpath()" function
11856 */
11857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011858f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011859 typval_T *argvars;
11860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011862 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011864 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011865 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011867 /* When the optional second argument is non-zero, don't remove matches
11868 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11869 if (argvars[2].v_type != VAR_UNKNOWN
11870 && get_tv_number_chk(&argvars[2], &error))
11871 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011872 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011873 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011874 rettv->vval.v_string = NULL;
11875 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011876 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11877 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878}
11879
11880/*
11881 * "has()" function
11882 */
11883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011884f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011885 typval_T *argvars;
11886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887{
11888 int i;
11889 char_u *name;
11890 int n = FALSE;
11891 static char *(has_list[]) =
11892 {
11893#ifdef AMIGA
11894 "amiga",
11895# ifdef FEAT_ARP
11896 "arp",
11897# endif
11898#endif
11899#ifdef __BEOS__
11900 "beos",
11901#endif
11902#ifdef MSDOS
11903# ifdef DJGPP
11904 "dos32",
11905# else
11906 "dos16",
11907# endif
11908#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011909#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910 "mac",
11911#endif
11912#if defined(MACOS_X_UNIX)
11913 "macunix",
11914#endif
11915#ifdef OS2
11916 "os2",
11917#endif
11918#ifdef __QNX__
11919 "qnx",
11920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921#ifdef UNIX
11922 "unix",
11923#endif
11924#ifdef VMS
11925 "vms",
11926#endif
11927#ifdef WIN16
11928 "win16",
11929#endif
11930#ifdef WIN32
11931 "win32",
11932#endif
11933#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11934 "win32unix",
11935#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011936#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937 "win64",
11938#endif
11939#ifdef EBCDIC
11940 "ebcdic",
11941#endif
11942#ifndef CASE_INSENSITIVE_FILENAME
11943 "fname_case",
11944#endif
11945#ifdef FEAT_ARABIC
11946 "arabic",
11947#endif
11948#ifdef FEAT_AUTOCMD
11949 "autocmd",
11950#endif
11951#ifdef FEAT_BEVAL
11952 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011953# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11954 "balloon_multiline",
11955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956#endif
11957#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11958 "builtin_terms",
11959# ifdef ALL_BUILTIN_TCAPS
11960 "all_builtin_terms",
11961# endif
11962#endif
11963#ifdef FEAT_BYTEOFF
11964 "byte_offset",
11965#endif
11966#ifdef FEAT_CINDENT
11967 "cindent",
11968#endif
11969#ifdef FEAT_CLIENTSERVER
11970 "clientserver",
11971#endif
11972#ifdef FEAT_CLIPBOARD
11973 "clipboard",
11974#endif
11975#ifdef FEAT_CMDL_COMPL
11976 "cmdline_compl",
11977#endif
11978#ifdef FEAT_CMDHIST
11979 "cmdline_hist",
11980#endif
11981#ifdef FEAT_COMMENTS
11982 "comments",
11983#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011984#ifdef FEAT_CONCEAL
11985 "conceal",
11986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987#ifdef FEAT_CRYPT
11988 "cryptv",
11989#endif
11990#ifdef FEAT_CSCOPE
11991 "cscope",
11992#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011993#ifdef FEAT_CURSORBIND
11994 "cursorbind",
11995#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011996#ifdef CURSOR_SHAPE
11997 "cursorshape",
11998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999#ifdef DEBUG
12000 "debug",
12001#endif
12002#ifdef FEAT_CON_DIALOG
12003 "dialog_con",
12004#endif
12005#ifdef FEAT_GUI_DIALOG
12006 "dialog_gui",
12007#endif
12008#ifdef FEAT_DIFF
12009 "diff",
12010#endif
12011#ifdef FEAT_DIGRAPHS
12012 "digraphs",
12013#endif
12014#ifdef FEAT_DND
12015 "dnd",
12016#endif
12017#ifdef FEAT_EMACS_TAGS
12018 "emacs_tags",
12019#endif
12020 "eval", /* always present, of course! */
12021#ifdef FEAT_EX_EXTRA
12022 "ex_extra",
12023#endif
12024#ifdef FEAT_SEARCH_EXTRA
12025 "extra_search",
12026#endif
12027#ifdef FEAT_FKMAP
12028 "farsi",
12029#endif
12030#ifdef FEAT_SEARCHPATH
12031 "file_in_path",
12032#endif
Bram Moolenaar4b9669f2011-07-07 16:20:52 +020012033#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012034 "filterpipe",
12035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036#ifdef FEAT_FIND_ID
12037 "find_in_path",
12038#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012039#ifdef FEAT_FLOAT
12040 "float",
12041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042#ifdef FEAT_FOLDING
12043 "folding",
12044#endif
12045#ifdef FEAT_FOOTER
12046 "footer",
12047#endif
12048#if !defined(USE_SYSTEM) && defined(UNIX)
12049 "fork",
12050#endif
12051#ifdef FEAT_GETTEXT
12052 "gettext",
12053#endif
12054#ifdef FEAT_GUI
12055 "gui",
12056#endif
12057#ifdef FEAT_GUI_ATHENA
12058# ifdef FEAT_GUI_NEXTAW
12059 "gui_neXtaw",
12060# else
12061 "gui_athena",
12062# endif
12063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064#ifdef FEAT_GUI_GTK
12065 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012068#ifdef FEAT_GUI_GNOME
12069 "gui_gnome",
12070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071#ifdef FEAT_GUI_MAC
12072 "gui_mac",
12073#endif
12074#ifdef FEAT_GUI_MOTIF
12075 "gui_motif",
12076#endif
12077#ifdef FEAT_GUI_PHOTON
12078 "gui_photon",
12079#endif
12080#ifdef FEAT_GUI_W16
12081 "gui_win16",
12082#endif
12083#ifdef FEAT_GUI_W32
12084 "gui_win32",
12085#endif
12086#ifdef FEAT_HANGULIN
12087 "hangul_input",
12088#endif
12089#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12090 "iconv",
12091#endif
12092#ifdef FEAT_INS_EXPAND
12093 "insert_expand",
12094#endif
12095#ifdef FEAT_JUMPLIST
12096 "jumplist",
12097#endif
12098#ifdef FEAT_KEYMAP
12099 "keymap",
12100#endif
12101#ifdef FEAT_LANGMAP
12102 "langmap",
12103#endif
12104#ifdef FEAT_LIBCALL
12105 "libcall",
12106#endif
12107#ifdef FEAT_LINEBREAK
12108 "linebreak",
12109#endif
12110#ifdef FEAT_LISP
12111 "lispindent",
12112#endif
12113#ifdef FEAT_LISTCMDS
12114 "listcmds",
12115#endif
12116#ifdef FEAT_LOCALMAP
12117 "localmap",
12118#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012119#ifdef FEAT_LUA
12120# ifndef DYNAMIC_LUA
12121 "lua",
12122# endif
12123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124#ifdef FEAT_MENU
12125 "menu",
12126#endif
12127#ifdef FEAT_SESSION
12128 "mksession",
12129#endif
12130#ifdef FEAT_MODIFY_FNAME
12131 "modify_fname",
12132#endif
12133#ifdef FEAT_MOUSE
12134 "mouse",
12135#endif
12136#ifdef FEAT_MOUSESHAPE
12137 "mouseshape",
12138#endif
12139#if defined(UNIX) || defined(VMS)
12140# ifdef FEAT_MOUSE_DEC
12141 "mouse_dec",
12142# endif
12143# ifdef FEAT_MOUSE_GPM
12144 "mouse_gpm",
12145# endif
12146# ifdef FEAT_MOUSE_JSB
12147 "mouse_jsbterm",
12148# endif
12149# ifdef FEAT_MOUSE_NET
12150 "mouse_netterm",
12151# endif
12152# ifdef FEAT_MOUSE_PTERM
12153 "mouse_pterm",
12154# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012155# ifdef FEAT_SYSMOUSE
12156 "mouse_sysmouse",
12157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158# ifdef FEAT_MOUSE_XTERM
12159 "mouse_xterm",
12160# endif
12161#endif
12162#ifdef FEAT_MBYTE
12163 "multi_byte",
12164#endif
12165#ifdef FEAT_MBYTE_IME
12166 "multi_byte_ime",
12167#endif
12168#ifdef FEAT_MULTI_LANG
12169 "multi_lang",
12170#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012171#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012172#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012173 "mzscheme",
12174#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176#ifdef FEAT_OLE
12177 "ole",
12178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179#ifdef FEAT_PATH_EXTRA
12180 "path_extra",
12181#endif
12182#ifdef FEAT_PERL
12183#ifndef DYNAMIC_PERL
12184 "perl",
12185#endif
12186#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012187#ifdef FEAT_PERSISTENT_UNDO
12188 "persistent_undo",
12189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190#ifdef FEAT_PYTHON
12191#ifndef DYNAMIC_PYTHON
12192 "python",
12193#endif
12194#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012195#ifdef FEAT_PYTHON3
12196#ifndef DYNAMIC_PYTHON3
12197 "python3",
12198#endif
12199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200#ifdef FEAT_POSTSCRIPT
12201 "postscript",
12202#endif
12203#ifdef FEAT_PRINTER
12204 "printer",
12205#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012206#ifdef FEAT_PROFILE
12207 "profile",
12208#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012209#ifdef FEAT_RELTIME
12210 "reltime",
12211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212#ifdef FEAT_QUICKFIX
12213 "quickfix",
12214#endif
12215#ifdef FEAT_RIGHTLEFT
12216 "rightleft",
12217#endif
12218#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12219 "ruby",
12220#endif
12221#ifdef FEAT_SCROLLBIND
12222 "scrollbind",
12223#endif
12224#ifdef FEAT_CMDL_INFO
12225 "showcmd",
12226 "cmdline_info",
12227#endif
12228#ifdef FEAT_SIGNS
12229 "signs",
12230#endif
12231#ifdef FEAT_SMARTINDENT
12232 "smartindent",
12233#endif
12234#ifdef FEAT_SNIFF
12235 "sniff",
12236#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012237#ifdef STARTUPTIME
12238 "startuptime",
12239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240#ifdef FEAT_STL_OPT
12241 "statusline",
12242#endif
12243#ifdef FEAT_SUN_WORKSHOP
12244 "sun_workshop",
12245#endif
12246#ifdef FEAT_NETBEANS_INTG
12247 "netbeans_intg",
12248#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012249#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012250 "spell",
12251#endif
12252#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 "syntax",
12254#endif
12255#if defined(USE_SYSTEM) || !defined(UNIX)
12256 "system",
12257#endif
12258#ifdef FEAT_TAG_BINS
12259 "tag_binary",
12260#endif
12261#ifdef FEAT_TAG_OLDSTATIC
12262 "tag_old_static",
12263#endif
12264#ifdef FEAT_TAG_ANYWHITE
12265 "tag_any_white",
12266#endif
12267#ifdef FEAT_TCL
12268# ifndef DYNAMIC_TCL
12269 "tcl",
12270# endif
12271#endif
12272#ifdef TERMINFO
12273 "terminfo",
12274#endif
12275#ifdef FEAT_TERMRESPONSE
12276 "termresponse",
12277#endif
12278#ifdef FEAT_TEXTOBJ
12279 "textobjects",
12280#endif
12281#ifdef HAVE_TGETENT
12282 "tgetent",
12283#endif
12284#ifdef FEAT_TITLE
12285 "title",
12286#endif
12287#ifdef FEAT_TOOLBAR
12288 "toolbar",
12289#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012290#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12291 "unnamedplus",
12292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293#ifdef FEAT_USR_CMDS
12294 "user-commands", /* was accidentally included in 5.4 */
12295 "user_commands",
12296#endif
12297#ifdef FEAT_VIMINFO
12298 "viminfo",
12299#endif
12300#ifdef FEAT_VERTSPLIT
12301 "vertsplit",
12302#endif
12303#ifdef FEAT_VIRTUALEDIT
12304 "virtualedit",
12305#endif
12306#ifdef FEAT_VISUAL
12307 "visual",
12308#endif
12309#ifdef FEAT_VISUALEXTRA
12310 "visualextra",
12311#endif
12312#ifdef FEAT_VREPLACE
12313 "vreplace",
12314#endif
12315#ifdef FEAT_WILDIGN
12316 "wildignore",
12317#endif
12318#ifdef FEAT_WILDMENU
12319 "wildmenu",
12320#endif
12321#ifdef FEAT_WINDOWS
12322 "windows",
12323#endif
12324#ifdef FEAT_WAK
12325 "winaltkeys",
12326#endif
12327#ifdef FEAT_WRITEBACKUP
12328 "writebackup",
12329#endif
12330#ifdef FEAT_XIM
12331 "xim",
12332#endif
12333#ifdef FEAT_XFONTSET
12334 "xfontset",
12335#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012336#ifdef FEAT_XPM_W32
12337 "xpm_w32",
12338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339#ifdef USE_XSMP
12340 "xsmp",
12341#endif
12342#ifdef USE_XSMP_INTERACT
12343 "xsmp_interact",
12344#endif
12345#ifdef FEAT_XCLIPBOARD
12346 "xterm_clipboard",
12347#endif
12348#ifdef FEAT_XTERM_SAVE
12349 "xterm_save",
12350#endif
12351#if defined(UNIX) && defined(FEAT_X11)
12352 "X11",
12353#endif
12354 NULL
12355 };
12356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012357 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 for (i = 0; has_list[i] != NULL; ++i)
12359 if (STRICMP(name, has_list[i]) == 0)
12360 {
12361 n = TRUE;
12362 break;
12363 }
12364
12365 if (n == FALSE)
12366 {
12367 if (STRNICMP(name, "patch", 5) == 0)
12368 n = has_patch(atoi((char *)name + 5));
12369 else if (STRICMP(name, "vim_starting") == 0)
12370 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012371#ifdef FEAT_MBYTE
12372 else if (STRICMP(name, "multi_byte_encoding") == 0)
12373 n = has_mbyte;
12374#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012375#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12376 else if (STRICMP(name, "balloon_multiline") == 0)
12377 n = multiline_balloon_available();
12378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379#ifdef DYNAMIC_TCL
12380 else if (STRICMP(name, "tcl") == 0)
12381 n = tcl_enabled(FALSE);
12382#endif
12383#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12384 else if (STRICMP(name, "iconv") == 0)
12385 n = iconv_enabled(FALSE);
12386#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012387#ifdef DYNAMIC_LUA
12388 else if (STRICMP(name, "lua") == 0)
12389 n = lua_enabled(FALSE);
12390#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012391#ifdef DYNAMIC_MZSCHEME
12392 else if (STRICMP(name, "mzscheme") == 0)
12393 n = mzscheme_enabled(FALSE);
12394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395#ifdef DYNAMIC_RUBY
12396 else if (STRICMP(name, "ruby") == 0)
12397 n = ruby_enabled(FALSE);
12398#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012399#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400#ifdef DYNAMIC_PYTHON
12401 else if (STRICMP(name, "python") == 0)
12402 n = python_enabled(FALSE);
12403#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012404#endif
12405#ifdef FEAT_PYTHON3
12406#ifdef DYNAMIC_PYTHON3
12407 else if (STRICMP(name, "python3") == 0)
12408 n = python3_enabled(FALSE);
12409#endif
12410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411#ifdef DYNAMIC_PERL
12412 else if (STRICMP(name, "perl") == 0)
12413 n = perl_enabled(FALSE);
12414#endif
12415#ifdef FEAT_GUI
12416 else if (STRICMP(name, "gui_running") == 0)
12417 n = (gui.in_use || gui.starting);
12418# ifdef FEAT_GUI_W32
12419 else if (STRICMP(name, "gui_win32s") == 0)
12420 n = gui_is_win32s();
12421# endif
12422# ifdef FEAT_BROWSE
12423 else if (STRICMP(name, "browse") == 0)
12424 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12425# endif
12426#endif
12427#ifdef FEAT_SYN_HL
12428 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012429 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430#endif
12431#if defined(WIN3264)
12432 else if (STRICMP(name, "win95") == 0)
12433 n = mch_windows95();
12434#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012435#ifdef FEAT_NETBEANS_INTG
12436 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012437 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439 }
12440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442}
12443
12444/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012445 * "has_key()" function
12446 */
12447 static void
12448f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012449 typval_T *argvars;
12450 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012451{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012452 if (argvars[0].v_type != VAR_DICT)
12453 {
12454 EMSG(_(e_dictreq));
12455 return;
12456 }
12457 if (argvars[0].vval.v_dict == NULL)
12458 return;
12459
12460 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012461 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012462}
12463
12464/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012465 * "haslocaldir()" function
12466 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012467 static void
12468f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012469 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012470 typval_T *rettv;
12471{
12472 rettv->vval.v_number = (curwin->w_localdir != NULL);
12473}
12474
12475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476 * "hasmapto()" function
12477 */
12478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012480 typval_T *argvars;
12481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482{
12483 char_u *name;
12484 char_u *mode;
12485 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012486 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012488 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012489 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490 mode = (char_u *)"nvo";
12491 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012492 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012493 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012494 if (argvars[2].v_type != VAR_UNKNOWN)
12495 abbr = get_tv_number(&argvars[2]);
12496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497
Bram Moolenaar2c932302006-03-18 21:42:09 +000012498 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012499 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502}
12503
12504/*
12505 * "histadd()" function
12506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012508f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012509 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511{
12512#ifdef FEAT_CMDHIST
12513 int histype;
12514 char_u *str;
12515 char_u buf[NUMBUFLEN];
12516#endif
12517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012518 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 if (check_restricted() || check_secure())
12520 return;
12521#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012522 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12523 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524 if (histype >= 0)
12525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012526 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527 if (*str != NUL)
12528 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012529 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 return;
12533 }
12534 }
12535#endif
12536}
12537
12538/*
12539 * "histdel()" function
12540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012542f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012543 typval_T *argvars UNUSED;
12544 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545{
12546#ifdef FEAT_CMDHIST
12547 int n;
12548 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012549 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012551 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12552 if (str == NULL)
12553 n = 0;
12554 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012556 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012557 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012559 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561 else
12562 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012563 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564 get_tv_string_buf(&argvars[1], buf));
12565 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566#endif
12567}
12568
12569/*
12570 * "histget()" function
12571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012573f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012574 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576{
12577#ifdef FEAT_CMDHIST
12578 int type;
12579 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012580 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012582 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12583 if (str == NULL)
12584 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012586 {
12587 type = get_histtype(str);
12588 if (argvars[1].v_type == VAR_UNKNOWN)
12589 idx = get_history_idx(type);
12590 else
12591 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12592 /* -1 on type error */
12593 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012596 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012598 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599}
12600
12601/*
12602 * "histnr()" function
12603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012605f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012606 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608{
12609 int i;
12610
12611#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012612 char_u *history = get_tv_string_chk(&argvars[0]);
12613
12614 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 if (i >= HIST_CMD && i < HIST_COUNT)
12616 i = get_history_idx(i);
12617 else
12618#endif
12619 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012620 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621}
12622
12623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624 * "highlightID(name)" function
12625 */
12626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012627f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012628 typval_T *argvars;
12629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012631 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632}
12633
12634/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012635 * "highlight_exists()" function
12636 */
12637 static void
12638f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 typval_T *argvars;
12640 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012641{
12642 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12643}
12644
12645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646 * "hostname()" function
12647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012650 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652{
12653 char_u hostname[256];
12654
12655 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656 rettv->v_type = VAR_STRING;
12657 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658}
12659
12660/*
12661 * iconv() function
12662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012664f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012665 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667{
12668#ifdef FEAT_MBYTE
12669 char_u buf1[NUMBUFLEN];
12670 char_u buf2[NUMBUFLEN];
12671 char_u *from, *to, *str;
12672 vimconv_T vimconv;
12673#endif
12674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675 rettv->v_type = VAR_STRING;
12676 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677
12678#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012679 str = get_tv_string(&argvars[0]);
12680 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12681 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 vimconv.vc_type = CONV_NONE;
12683 convert_setup(&vimconv, from, to);
12684
12685 /* If the encodings are equal, no conversion needed. */
12686 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012687 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690
12691 convert_setup(&vimconv, NULL, NULL);
12692 vim_free(from);
12693 vim_free(to);
12694#endif
12695}
12696
12697/*
12698 * "indent()" function
12699 */
12700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012702 typval_T *argvars;
12703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704{
12705 linenr_T lnum;
12706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012707 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012709 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712}
12713
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012714/*
12715 * "index()" function
12716 */
12717 static void
12718f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012719 typval_T *argvars;
12720 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012721{
Bram Moolenaar33570922005-01-25 22:26:29 +000012722 list_T *l;
12723 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012724 long idx = 0;
12725 int ic = FALSE;
12726
12727 rettv->vval.v_number = -1;
12728 if (argvars[0].v_type != VAR_LIST)
12729 {
12730 EMSG(_(e_listreq));
12731 return;
12732 }
12733 l = argvars[0].vval.v_list;
12734 if (l != NULL)
12735 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012736 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012737 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012739 int error = FALSE;
12740
Bram Moolenaar758711c2005-02-02 23:11:38 +000012741 /* Start at specified item. Use the cached index that list_find()
12742 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012743 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012744 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012745 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012746 ic = get_tv_number_chk(&argvars[3], &error);
12747 if (error)
12748 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012749 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012750
Bram Moolenaar758711c2005-02-02 23:11:38 +000012751 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012752 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012753 {
12754 rettv->vval.v_number = idx;
12755 break;
12756 }
12757 }
12758}
12759
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760static int inputsecret_flag = 0;
12761
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012762static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12763
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012765 * This function is used by f_input() and f_inputdialog() functions. The third
12766 * argument to f_input() specifies the type of completion to use at the
12767 * prompt. The third argument to f_inputdialog() specifies the value to return
12768 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 */
12770 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012771get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012772 typval_T *argvars;
12773 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012774 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012776 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 char_u *p = NULL;
12778 int c;
12779 char_u buf[NUMBUFLEN];
12780 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012781 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012782 int xp_type = EXPAND_NOTHING;
12783 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012786 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787
12788#ifdef NO_CONSOLE_INPUT
12789 /* While starting up, there is no place to enter text. */
12790 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792#endif
12793
12794 cmd_silent = FALSE; /* Want to see the prompt. */
12795 if (prompt != NULL)
12796 {
12797 /* Only the part of the message after the last NL is considered as
12798 * prompt for the command line */
12799 p = vim_strrchr(prompt, '\n');
12800 if (p == NULL)
12801 p = prompt;
12802 else
12803 {
12804 ++p;
12805 c = *p;
12806 *p = NUL;
12807 msg_start();
12808 msg_clr_eos();
12809 msg_puts_attr(prompt, echo_attr);
12810 msg_didout = FALSE;
12811 msg_starthere();
12812 *p = c;
12813 }
12814 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012816 if (argvars[1].v_type != VAR_UNKNOWN)
12817 {
12818 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12819 if (defstr != NULL)
12820 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012822 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012823 {
12824 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012825 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012826 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012827
Bram Moolenaar4463f292005-09-25 22:20:24 +000012828 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012829
Bram Moolenaar4463f292005-09-25 22:20:24 +000012830 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12831 if (xp_name == NULL)
12832 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012833
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012834 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012835
Bram Moolenaar4463f292005-09-25 22:20:24 +000012836 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12837 &xp_arg) == FAIL)
12838 return;
12839 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012840 }
12841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012842 if (defstr != NULL)
12843 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012844 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12845 xp_type, xp_arg);
12846
12847 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012849 /* since the user typed this, no need to wait for return */
12850 need_wait_return = FALSE;
12851 msg_didout = FALSE;
12852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853 cmd_silent = cmd_silent_save;
12854}
12855
12856/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012857 * "input()" function
12858 * Also handles inputsecret() when inputsecret is set.
12859 */
12860 static void
12861f_input(argvars, rettv)
12862 typval_T *argvars;
12863 typval_T *rettv;
12864{
12865 get_user_input(argvars, rettv, FALSE);
12866}
12867
12868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 * "inputdialog()" function
12870 */
12871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012873 typval_T *argvars;
12874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875{
12876#if defined(FEAT_GUI_TEXTDIALOG)
12877 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12878 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12879 {
12880 char_u *message;
12881 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012882 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012884 message = get_tv_string_chk(&argvars[0]);
12885 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012886 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012887 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888 else
12889 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012890 if (message != NULL && defstr != NULL
12891 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012892 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894 else
12895 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012896 if (message != NULL && defstr != NULL
12897 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012898 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899 rettv->vval.v_string = vim_strsave(
12900 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012902 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012904 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905 }
12906 else
12907#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012908 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909}
12910
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012911/*
12912 * "inputlist()" function
12913 */
12914 static void
12915f_inputlist(argvars, rettv)
12916 typval_T *argvars;
12917 typval_T *rettv;
12918{
12919 listitem_T *li;
12920 int selected;
12921 int mouse_used;
12922
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012923#ifdef NO_CONSOLE_INPUT
12924 /* While starting up, there is no place to enter text. */
12925 if (no_console_input())
12926 return;
12927#endif
12928 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12929 {
12930 EMSG2(_(e_listarg), "inputlist()");
12931 return;
12932 }
12933
12934 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012935 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012936 lines_left = Rows; /* avoid more prompt */
12937 msg_scroll = TRUE;
12938 msg_clr_eos();
12939
12940 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12941 {
12942 msg_puts(get_tv_string(&li->li_tv));
12943 msg_putchar('\n');
12944 }
12945
12946 /* Ask for choice. */
12947 selected = prompt_for_number(&mouse_used);
12948 if (mouse_used)
12949 selected -= lines_left;
12950
12951 rettv->vval.v_number = selected;
12952}
12953
12954
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12956
12957/*
12958 * "inputrestore()" function
12959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012961f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012962 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964{
12965 if (ga_userinput.ga_len > 0)
12966 {
12967 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12969 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012970 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971 }
12972 else if (p_verbose > 1)
12973 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012974 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012975 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976 }
12977}
12978
12979/*
12980 * "inputsave()" function
12981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012983f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012984 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012987 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988 if (ga_grow(&ga_userinput, 1) == OK)
12989 {
12990 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12991 + ga_userinput.ga_len);
12992 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012993 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994 }
12995 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012996 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997}
12998
12999/*
13000 * "inputsecret()" function
13001 */
13002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013003f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013004 typval_T *argvars;
13005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013006{
13007 ++cmdline_star;
13008 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013009 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010 --cmdline_star;
13011 --inputsecret_flag;
13012}
13013
13014/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013015 * "insert()" function
13016 */
13017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013018f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013019 typval_T *argvars;
13020 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013021{
13022 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013023 listitem_T *item;
13024 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013025 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013026
13027 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013028 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013029 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013030 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013031 {
13032 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013033 before = get_tv_number_chk(&argvars[2], &error);
13034 if (error)
13035 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013036
Bram Moolenaar758711c2005-02-02 23:11:38 +000013037 if (before == l->lv_len)
13038 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013039 else
13040 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013041 item = list_find(l, before);
13042 if (item == NULL)
13043 {
13044 EMSGN(_(e_listidx), before);
13045 l = NULL;
13046 }
13047 }
13048 if (l != NULL)
13049 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013050 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013051 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013052 }
13053 }
13054}
13055
13056/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013057 * "invert(expr)" function
13058 */
13059 static void
13060f_invert(argvars, rettv)
13061 typval_T *argvars;
13062 typval_T *rettv;
13063{
13064 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13065}
13066
13067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 * "isdirectory()" function
13069 */
13070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013071f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013072 typval_T *argvars;
13073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013075 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076}
13077
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013078/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013079 * "islocked()" function
13080 */
13081 static void
13082f_islocked(argvars, rettv)
13083 typval_T *argvars;
13084 typval_T *rettv;
13085{
13086 lval_T lv;
13087 char_u *end;
13088 dictitem_T *di;
13089
13090 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013091 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13092 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013093 if (end != NULL && lv.ll_name != NULL)
13094 {
13095 if (*end != NUL)
13096 EMSG(_(e_trailing));
13097 else
13098 {
13099 if (lv.ll_tv == NULL)
13100 {
13101 if (check_changedtick(lv.ll_name))
13102 rettv->vval.v_number = 1; /* always locked */
13103 else
13104 {
13105 di = find_var(lv.ll_name, NULL);
13106 if (di != NULL)
13107 {
13108 /* Consider a variable locked when:
13109 * 1. the variable itself is locked
13110 * 2. the value of the variable is locked.
13111 * 3. the List or Dict value is locked.
13112 */
13113 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13114 || tv_islocked(&di->di_tv));
13115 }
13116 }
13117 }
13118 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013119 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013120 else if (lv.ll_newkey != NULL)
13121 EMSG2(_(e_dictkey), lv.ll_newkey);
13122 else if (lv.ll_list != NULL)
13123 /* List item. */
13124 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13125 else
13126 /* Dictionary item. */
13127 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13128 }
13129 }
13130
13131 clear_lval(&lv);
13132}
13133
Bram Moolenaar33570922005-01-25 22:26:29 +000013134static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013135
13136/*
13137 * Turn a dict into a list:
13138 * "what" == 0: list of keys
13139 * "what" == 1: list of values
13140 * "what" == 2: list of items
13141 */
13142 static void
13143dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013144 typval_T *argvars;
13145 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013146 int what;
13147{
Bram Moolenaar33570922005-01-25 22:26:29 +000013148 list_T *l2;
13149 dictitem_T *di;
13150 hashitem_T *hi;
13151 listitem_T *li;
13152 listitem_T *li2;
13153 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013154 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013155
Bram Moolenaar8c711452005-01-14 21:53:12 +000013156 if (argvars[0].v_type != VAR_DICT)
13157 {
13158 EMSG(_(e_dictreq));
13159 return;
13160 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013161 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013162 return;
13163
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013164 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013165 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013166
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013167 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013168 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013169 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013170 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013171 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013172 --todo;
13173 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013174
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013175 li = listitem_alloc();
13176 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013177 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013178 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013179
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013180 if (what == 0)
13181 {
13182 /* keys() */
13183 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013184 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013185 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13186 }
13187 else if (what == 1)
13188 {
13189 /* values() */
13190 copy_tv(&di->di_tv, &li->li_tv);
13191 }
13192 else
13193 {
13194 /* items() */
13195 l2 = list_alloc();
13196 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013197 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013198 li->li_tv.vval.v_list = l2;
13199 if (l2 == NULL)
13200 break;
13201 ++l2->lv_refcount;
13202
13203 li2 = listitem_alloc();
13204 if (li2 == NULL)
13205 break;
13206 list_append(l2, li2);
13207 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013208 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013209 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13210
13211 li2 = listitem_alloc();
13212 if (li2 == NULL)
13213 break;
13214 list_append(l2, li2);
13215 copy_tv(&di->di_tv, &li2->li_tv);
13216 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013217 }
13218 }
13219}
13220
13221/*
13222 * "items(dict)" function
13223 */
13224 static void
13225f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013226 typval_T *argvars;
13227 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013228{
13229 dict_list(argvars, rettv, 2);
13230}
13231
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013233 * "join()" function
13234 */
13235 static void
13236f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013237 typval_T *argvars;
13238 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013239{
13240 garray_T ga;
13241 char_u *sep;
13242
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013243 if (argvars[0].v_type != VAR_LIST)
13244 {
13245 EMSG(_(e_listreq));
13246 return;
13247 }
13248 if (argvars[0].vval.v_list == NULL)
13249 return;
13250 if (argvars[1].v_type == VAR_UNKNOWN)
13251 sep = (char_u *)" ";
13252 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013253 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013254
13255 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013256
13257 if (sep != NULL)
13258 {
13259 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013260 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013261 ga_append(&ga, NUL);
13262 rettv->vval.v_string = (char_u *)ga.ga_data;
13263 }
13264 else
13265 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013266}
13267
13268/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013269 * "keys()" function
13270 */
13271 static void
13272f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013273 typval_T *argvars;
13274 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013275{
13276 dict_list(argvars, rettv, 0);
13277}
13278
13279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 * "last_buffer_nr()" function.
13281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013283f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013284 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286{
13287 int n = 0;
13288 buf_T *buf;
13289
13290 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13291 if (n < buf->b_fnum)
13292 n = buf->b_fnum;
13293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013294 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013295}
13296
13297/*
13298 * "len()" function
13299 */
13300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013301f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013302 typval_T *argvars;
13303 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013304{
13305 switch (argvars[0].v_type)
13306 {
13307 case VAR_STRING:
13308 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013309 rettv->vval.v_number = (varnumber_T)STRLEN(
13310 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013311 break;
13312 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013313 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013314 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013315 case VAR_DICT:
13316 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13317 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013318 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013319 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013320 break;
13321 }
13322}
13323
Bram Moolenaar33570922005-01-25 22:26:29 +000013324static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013325
13326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013327libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013328 typval_T *argvars;
13329 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013330 int type;
13331{
13332#ifdef FEAT_LIBCALL
13333 char_u *string_in;
13334 char_u **string_result;
13335 int nr_result;
13336#endif
13337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013338 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013339 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013340 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013341
13342 if (check_restricted() || check_secure())
13343 return;
13344
13345#ifdef FEAT_LIBCALL
13346 /* The first two args must be strings, otherwise its meaningless */
13347 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13348 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013349 string_in = NULL;
13350 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013351 string_in = argvars[2].vval.v_string;
13352 if (type == VAR_NUMBER)
13353 string_result = NULL;
13354 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013355 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013356 if (mch_libcall(argvars[0].vval.v_string,
13357 argvars[1].vval.v_string,
13358 string_in,
13359 argvars[2].vval.v_number,
13360 string_result,
13361 &nr_result) == OK
13362 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013364 }
13365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366}
13367
13368/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013369 * "libcall()" function
13370 */
13371 static void
13372f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013373 typval_T *argvars;
13374 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013375{
13376 libcall_common(argvars, rettv, VAR_STRING);
13377}
13378
13379/*
13380 * "libcallnr()" function
13381 */
13382 static void
13383f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013384 typval_T *argvars;
13385 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013386{
13387 libcall_common(argvars, rettv, VAR_NUMBER);
13388}
13389
13390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 * "line(string)" function
13392 */
13393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013394f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013395 typval_T *argvars;
13396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397{
13398 linenr_T lnum = 0;
13399 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013400 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013402 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 if (fp != NULL)
13404 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013405 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406}
13407
13408/*
13409 * "line2byte(lnum)" function
13410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013412f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013413 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415{
13416#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013417 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418#else
13419 linenr_T lnum;
13420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013421 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013423 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013425 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13426 if (rettv->vval.v_number >= 0)
13427 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428#endif
13429}
13430
13431/*
13432 * "lispindent(lnum)" function
13433 */
13434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013435f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013436 typval_T *argvars;
13437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438{
13439#ifdef FEAT_LISP
13440 pos_T pos;
13441 linenr_T lnum;
13442
13443 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13446 {
13447 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013448 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449 curwin->w_cursor = pos;
13450 }
13451 else
13452#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013453 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454}
13455
13456/*
13457 * "localtime()" function
13458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013460f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013461 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013464 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465}
13466
Bram Moolenaar33570922005-01-25 22:26:29 +000013467static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468
13469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013470get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013471 typval_T *argvars;
13472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473 int exact;
13474{
13475 char_u *keys;
13476 char_u *which;
13477 char_u buf[NUMBUFLEN];
13478 char_u *keys_buf = NULL;
13479 char_u *rhs;
13480 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013481 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013482 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013483 mapblock_T *mp;
13484 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485
13486 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013487 rettv->v_type = VAR_STRING;
13488 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013490 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 if (*keys == NUL)
13492 return;
13493
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013494 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013495 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013496 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013497 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013498 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013499 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013500 if (argvars[3].v_type != VAR_UNKNOWN)
13501 get_dict = get_tv_number(&argvars[3]);
13502 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 else
13505 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013506 if (which == NULL)
13507 return;
13508
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 mode = get_map_mode(&which, 0);
13510
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013511 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013512 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013514
13515 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013517 /* Return a string. */
13518 if (rhs != NULL)
13519 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520
Bram Moolenaarbd743252010-10-20 21:23:33 +020013521 }
13522 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13523 {
13524 /* Return a dictionary. */
13525 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13526 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13527 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528
Bram Moolenaarbd743252010-10-20 21:23:33 +020013529 dict_add_nr_str(dict, "lhs", 0L, lhs);
13530 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13531 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13532 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13533 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13534 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13535 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13536 dict_add_nr_str(dict, "mode", 0L, mapmode);
13537
13538 vim_free(lhs);
13539 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540 }
13541}
13542
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013543#ifdef FEAT_FLOAT
13544/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013545 * "log()" function
13546 */
13547 static void
13548f_log(argvars, rettv)
13549 typval_T *argvars;
13550 typval_T *rettv;
13551{
13552 float_T f;
13553
13554 rettv->v_type = VAR_FLOAT;
13555 if (get_float_arg(argvars, &f) == OK)
13556 rettv->vval.v_float = log(f);
13557 else
13558 rettv->vval.v_float = 0.0;
13559}
13560
13561/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013562 * "log10()" function
13563 */
13564 static void
13565f_log10(argvars, rettv)
13566 typval_T *argvars;
13567 typval_T *rettv;
13568{
13569 float_T f;
13570
13571 rettv->v_type = VAR_FLOAT;
13572 if (get_float_arg(argvars, &f) == OK)
13573 rettv->vval.v_float = log10(f);
13574 else
13575 rettv->vval.v_float = 0.0;
13576}
13577#endif
13578
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013580 * "map()" function
13581 */
13582 static void
13583f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013584 typval_T *argvars;
13585 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013586{
13587 filter_map(argvars, rettv, TRUE);
13588}
13589
13590/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013591 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592 */
13593 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013594f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013595 typval_T *argvars;
13596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013598 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599}
13600
13601/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013602 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603 */
13604 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013605f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013606 typval_T *argvars;
13607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013609 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610}
13611
Bram Moolenaar33570922005-01-25 22:26:29 +000013612static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613
13614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013616 typval_T *argvars;
13617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 int type;
13619{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013620 char_u *str = NULL;
13621 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622 char_u *pat;
13623 regmatch_T regmatch;
13624 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013625 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 char_u *save_cpo;
13627 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013628 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013629 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013630 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013631 list_T *l = NULL;
13632 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013633 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013634 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635
13636 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13637 save_cpo = p_cpo;
13638 p_cpo = (char_u *)"";
13639
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013640 rettv->vval.v_number = -1;
13641 if (type == 3)
13642 {
13643 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013644 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013645 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013646 }
13647 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 rettv->v_type = VAR_STRING;
13650 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013653 if (argvars[0].v_type == VAR_LIST)
13654 {
13655 if ((l = argvars[0].vval.v_list) == NULL)
13656 goto theend;
13657 li = l->lv_first;
13658 }
13659 else
13660 expr = str = get_tv_string(&argvars[0]);
13661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013662 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13663 if (pat == NULL)
13664 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013665
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013666 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013668 int error = FALSE;
13669
13670 start = get_tv_number_chk(&argvars[2], &error);
13671 if (error)
13672 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013673 if (l != NULL)
13674 {
13675 li = list_find(l, start);
13676 if (li == NULL)
13677 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013678 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013679 }
13680 else
13681 {
13682 if (start < 0)
13683 start = 0;
13684 if (start > (long)STRLEN(str))
13685 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013686 /* When "count" argument is there ignore matches before "start",
13687 * otherwise skip part of the string. Differs when pattern is "^"
13688 * or "\<". */
13689 if (argvars[3].v_type != VAR_UNKNOWN)
13690 startcol = start;
13691 else
13692 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013693 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013694
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013695 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696 nth = get_tv_number_chk(&argvars[3], &error);
13697 if (error)
13698 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699 }
13700
13701 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13702 if (regmatch.regprog != NULL)
13703 {
13704 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013705
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013706 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013707 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013708 if (l != NULL)
13709 {
13710 if (li == NULL)
13711 {
13712 match = FALSE;
13713 break;
13714 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013715 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013716 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013717 if (str == NULL)
13718 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013719 }
13720
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013721 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013722
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013723 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013724 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013725 if (l == NULL && !match)
13726 break;
13727
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013728 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013729 if (l != NULL)
13730 {
13731 li = li->li_next;
13732 ++idx;
13733 }
13734 else
13735 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013736#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013737 startcol = (colnr_T)(regmatch.startp[0]
13738 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013739#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013740 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013741#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013742 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013743 }
13744
13745 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013747 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013748 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013749 int i;
13750
13751 /* return list with matched string and submatches */
13752 for (i = 0; i < NSUBEXP; ++i)
13753 {
13754 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013755 {
13756 if (list_append_string(rettv->vval.v_list,
13757 (char_u *)"", 0) == FAIL)
13758 break;
13759 }
13760 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013761 regmatch.startp[i],
13762 (int)(regmatch.endp[i] - regmatch.startp[i]))
13763 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013764 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013765 }
13766 }
13767 else if (type == 2)
13768 {
13769 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013770 if (l != NULL)
13771 copy_tv(&li->li_tv, rettv);
13772 else
13773 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013775 }
13776 else if (l != NULL)
13777 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778 else
13779 {
13780 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013781 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782 (varnumber_T)(regmatch.startp[0] - str);
13783 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013784 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013786 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787 }
13788 }
13789 vim_free(regmatch.regprog);
13790 }
13791
13792theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013793 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 p_cpo = save_cpo;
13795}
13796
13797/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013798 * "match()" function
13799 */
13800 static void
13801f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013802 typval_T *argvars;
13803 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013804{
13805 find_some_match(argvars, rettv, 1);
13806}
13807
13808/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013809 * "matchadd()" function
13810 */
13811 static void
13812f_matchadd(argvars, rettv)
13813 typval_T *argvars;
13814 typval_T *rettv;
13815{
13816#ifdef FEAT_SEARCH_EXTRA
13817 char_u buf[NUMBUFLEN];
13818 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13819 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13820 int prio = 10; /* default priority */
13821 int id = -1;
13822 int error = FALSE;
13823
13824 rettv->vval.v_number = -1;
13825
13826 if (grp == NULL || pat == NULL)
13827 return;
13828 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013829 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013830 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013831 if (argvars[3].v_type != VAR_UNKNOWN)
13832 id = get_tv_number_chk(&argvars[3], &error);
13833 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013834 if (error == TRUE)
13835 return;
13836 if (id >= 1 && id <= 3)
13837 {
13838 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13839 return;
13840 }
13841
13842 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13843#endif
13844}
13845
13846/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013847 * "matcharg()" function
13848 */
13849 static void
13850f_matcharg(argvars, rettv)
13851 typval_T *argvars;
13852 typval_T *rettv;
13853{
13854 if (rettv_list_alloc(rettv) == OK)
13855 {
13856#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013857 int id = get_tv_number(&argvars[0]);
13858 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013859
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013860 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013861 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013862 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13863 {
13864 list_append_string(rettv->vval.v_list,
13865 syn_id2name(m->hlg_id), -1);
13866 list_append_string(rettv->vval.v_list, m->pattern, -1);
13867 }
13868 else
13869 {
13870 list_append_string(rettv->vval.v_list, NUL, -1);
13871 list_append_string(rettv->vval.v_list, NUL, -1);
13872 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013873 }
13874#endif
13875 }
13876}
13877
13878/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013879 * "matchdelete()" function
13880 */
13881 static void
13882f_matchdelete(argvars, rettv)
13883 typval_T *argvars;
13884 typval_T *rettv;
13885{
13886#ifdef FEAT_SEARCH_EXTRA
13887 rettv->vval.v_number = match_delete(curwin,
13888 (int)get_tv_number(&argvars[0]), TRUE);
13889#endif
13890}
13891
13892/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013893 * "matchend()" function
13894 */
13895 static void
13896f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013897 typval_T *argvars;
13898 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013899{
13900 find_some_match(argvars, rettv, 0);
13901}
13902
13903/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013904 * "matchlist()" function
13905 */
13906 static void
13907f_matchlist(argvars, rettv)
13908 typval_T *argvars;
13909 typval_T *rettv;
13910{
13911 find_some_match(argvars, rettv, 3);
13912}
13913
13914/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013915 * "matchstr()" function
13916 */
13917 static void
13918f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013919 typval_T *argvars;
13920 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013921{
13922 find_some_match(argvars, rettv, 2);
13923}
13924
Bram Moolenaar33570922005-01-25 22:26:29 +000013925static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013926
13927 static void
13928max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013929 typval_T *argvars;
13930 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013931 int domax;
13932{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013933 long n = 0;
13934 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013935 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013936
13937 if (argvars[0].v_type == VAR_LIST)
13938 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013939 list_T *l;
13940 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013941
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013942 l = argvars[0].vval.v_list;
13943 if (l != NULL)
13944 {
13945 li = l->lv_first;
13946 if (li != NULL)
13947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013948 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013949 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013950 {
13951 li = li->li_next;
13952 if (li == NULL)
13953 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013954 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013955 if (domax ? i > n : i < n)
13956 n = i;
13957 }
13958 }
13959 }
13960 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013961 else if (argvars[0].v_type == VAR_DICT)
13962 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013963 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013964 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013965 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013966 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013967
13968 d = argvars[0].vval.v_dict;
13969 if (d != NULL)
13970 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013971 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013972 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013973 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013974 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013975 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013976 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013977 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013978 if (first)
13979 {
13980 n = i;
13981 first = FALSE;
13982 }
13983 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013984 n = i;
13985 }
13986 }
13987 }
13988 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013989 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013990 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013991 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013992}
13993
13994/*
13995 * "max()" function
13996 */
13997 static void
13998f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013999 typval_T *argvars;
14000 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014001{
14002 max_min(argvars, rettv, TRUE);
14003}
14004
14005/*
14006 * "min()" function
14007 */
14008 static void
14009f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014010 typval_T *argvars;
14011 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014012{
14013 max_min(argvars, rettv, FALSE);
14014}
14015
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014016static int mkdir_recurse __ARGS((char_u *dir, int prot));
14017
14018/*
14019 * Create the directory in which "dir" is located, and higher levels when
14020 * needed.
14021 */
14022 static int
14023mkdir_recurse(dir, prot)
14024 char_u *dir;
14025 int prot;
14026{
14027 char_u *p;
14028 char_u *updir;
14029 int r = FAIL;
14030
14031 /* Get end of directory name in "dir".
14032 * We're done when it's "/" or "c:/". */
14033 p = gettail_sep(dir);
14034 if (p <= get_past_head(dir))
14035 return OK;
14036
14037 /* If the directory exists we're done. Otherwise: create it.*/
14038 updir = vim_strnsave(dir, (int)(p - dir));
14039 if (updir == NULL)
14040 return FAIL;
14041 if (mch_isdir(updir))
14042 r = OK;
14043 else if (mkdir_recurse(updir, prot) == OK)
14044 r = vim_mkdir_emsg(updir, prot);
14045 vim_free(updir);
14046 return r;
14047}
14048
14049#ifdef vim_mkdir
14050/*
14051 * "mkdir()" function
14052 */
14053 static void
14054f_mkdir(argvars, rettv)
14055 typval_T *argvars;
14056 typval_T *rettv;
14057{
14058 char_u *dir;
14059 char_u buf[NUMBUFLEN];
14060 int prot = 0755;
14061
14062 rettv->vval.v_number = FAIL;
14063 if (check_restricted() || check_secure())
14064 return;
14065
14066 dir = get_tv_string_buf(&argvars[0], buf);
14067 if (argvars[1].v_type != VAR_UNKNOWN)
14068 {
14069 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014070 prot = get_tv_number_chk(&argvars[2], NULL);
14071 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014072 mkdir_recurse(dir, prot);
14073 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014074 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014075}
14076#endif
14077
Bram Moolenaar0d660222005-01-07 21:51:51 +000014078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079 * "mode()" function
14080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014083 typval_T *argvars;
14084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014086 char_u buf[3];
14087
14088 buf[1] = NUL;
14089 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090
14091#ifdef FEAT_VISUAL
14092 if (VIsual_active)
14093 {
14094 if (VIsual_select)
14095 buf[0] = VIsual_mode + 's' - 'v';
14096 else
14097 buf[0] = VIsual_mode;
14098 }
14099 else
14100#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014101 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14102 || State == CONFIRM)
14103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014105 if (State == ASKMORE)
14106 buf[1] = 'm';
14107 else if (State == CONFIRM)
14108 buf[1] = '?';
14109 }
14110 else if (State == EXTERNCMD)
14111 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112 else if (State & INSERT)
14113 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014114#ifdef FEAT_VREPLACE
14115 if (State & VREPLACE_FLAG)
14116 {
14117 buf[0] = 'R';
14118 buf[1] = 'v';
14119 }
14120 else
14121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 if (State & REPLACE_FLAG)
14123 buf[0] = 'R';
14124 else
14125 buf[0] = 'i';
14126 }
14127 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014130 if (exmode_active)
14131 buf[1] = 'v';
14132 }
14133 else if (exmode_active)
14134 {
14135 buf[0] = 'c';
14136 buf[1] = 'e';
14137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014141 if (finish_op)
14142 buf[1] = 'o';
14143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014145 /* Clear out the minor mode when the argument is not a non-zero number or
14146 * non-empty string. */
14147 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014148 buf[1] = NUL;
14149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014150 rettv->vval.v_string = vim_strsave(buf);
14151 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152}
14153
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014154#ifdef FEAT_MZSCHEME
14155/*
14156 * "mzeval()" function
14157 */
14158 static void
14159f_mzeval(argvars, rettv)
14160 typval_T *argvars;
14161 typval_T *rettv;
14162{
14163 char_u *str;
14164 char_u buf[NUMBUFLEN];
14165
14166 str = get_tv_string_buf(&argvars[0], buf);
14167 do_mzeval(str, rettv);
14168}
14169#endif
14170
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014172 * "nextnonblank()" function
14173 */
14174 static void
14175f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014176 typval_T *argvars;
14177 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014178{
14179 linenr_T lnum;
14180
14181 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014183 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014184 {
14185 lnum = 0;
14186 break;
14187 }
14188 if (*skipwhite(ml_get(lnum)) != NUL)
14189 break;
14190 }
14191 rettv->vval.v_number = lnum;
14192}
14193
14194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195 * "nr2char()" function
14196 */
14197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014198f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014199 typval_T *argvars;
14200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201{
14202 char_u buf[NUMBUFLEN];
14203
14204#ifdef FEAT_MBYTE
14205 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014206 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207 else
14208#endif
14209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014210 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 buf[1] = NUL;
14212 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014213 rettv->v_type = VAR_STRING;
14214 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014215}
14216
14217/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014218 * "or(expr, expr)" function
14219 */
14220 static void
14221f_or(argvars, rettv)
14222 typval_T *argvars;
14223 typval_T *rettv;
14224{
14225 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14226 | get_tv_number_chk(&argvars[1], NULL);
14227}
14228
14229/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014230 * "pathshorten()" function
14231 */
14232 static void
14233f_pathshorten(argvars, rettv)
14234 typval_T *argvars;
14235 typval_T *rettv;
14236{
14237 char_u *p;
14238
14239 rettv->v_type = VAR_STRING;
14240 p = get_tv_string_chk(&argvars[0]);
14241 if (p == NULL)
14242 rettv->vval.v_string = NULL;
14243 else
14244 {
14245 p = vim_strsave(p);
14246 rettv->vval.v_string = p;
14247 if (p != NULL)
14248 shorten_dir(p);
14249 }
14250}
14251
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014252#ifdef FEAT_FLOAT
14253/*
14254 * "pow()" function
14255 */
14256 static void
14257f_pow(argvars, rettv)
14258 typval_T *argvars;
14259 typval_T *rettv;
14260{
14261 float_T fx, fy;
14262
14263 rettv->v_type = VAR_FLOAT;
14264 if (get_float_arg(argvars, &fx) == OK
14265 && get_float_arg(&argvars[1], &fy) == OK)
14266 rettv->vval.v_float = pow(fx, fy);
14267 else
14268 rettv->vval.v_float = 0.0;
14269}
14270#endif
14271
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014272/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014273 * "prevnonblank()" function
14274 */
14275 static void
14276f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014277 typval_T *argvars;
14278 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014279{
14280 linenr_T lnum;
14281
14282 lnum = get_tv_lnum(argvars);
14283 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14284 lnum = 0;
14285 else
14286 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14287 --lnum;
14288 rettv->vval.v_number = lnum;
14289}
14290
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014291#ifdef HAVE_STDARG_H
14292/* This dummy va_list is here because:
14293 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14294 * - locally in the function results in a "used before set" warning
14295 * - using va_start() to initialize it gives "function with fixed args" error */
14296static va_list ap;
14297#endif
14298
Bram Moolenaar8c711452005-01-14 21:53:12 +000014299/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014300 * "printf()" function
14301 */
14302 static void
14303f_printf(argvars, rettv)
14304 typval_T *argvars;
14305 typval_T *rettv;
14306{
14307 rettv->v_type = VAR_STRING;
14308 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014309#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014310 {
14311 char_u buf[NUMBUFLEN];
14312 int len;
14313 char_u *s;
14314 int saved_did_emsg = did_emsg;
14315 char *fmt;
14316
14317 /* Get the required length, allocate the buffer and do it for real. */
14318 did_emsg = FALSE;
14319 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014320 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014321 if (!did_emsg)
14322 {
14323 s = alloc(len + 1);
14324 if (s != NULL)
14325 {
14326 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014327 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014328 }
14329 }
14330 did_emsg |= saved_did_emsg;
14331 }
14332#endif
14333}
14334
14335/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014336 * "pumvisible()" function
14337 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014338 static void
14339f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014340 typval_T *argvars UNUSED;
14341 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014342{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014343#ifdef FEAT_INS_EXPAND
14344 if (pum_visible())
14345 rettv->vval.v_number = 1;
14346#endif
14347}
14348
14349/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014350 * "range()" function
14351 */
14352 static void
14353f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014354 typval_T *argvars;
14355 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014356{
14357 long start;
14358 long end;
14359 long stride = 1;
14360 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014361 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014364 if (argvars[1].v_type == VAR_UNKNOWN)
14365 {
14366 end = start - 1;
14367 start = 0;
14368 }
14369 else
14370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014371 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014372 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014373 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014374 }
14375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014376 if (error)
14377 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014378 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014379 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014380 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014381 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014382 else
14383 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014384 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014385 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014386 if (list_append_number(rettv->vval.v_list,
14387 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014388 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014389 }
14390}
14391
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014392/*
14393 * "readfile()" function
14394 */
14395 static void
14396f_readfile(argvars, rettv)
14397 typval_T *argvars;
14398 typval_T *rettv;
14399{
14400 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014401 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014402 char_u *fname;
14403 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014404 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14405 int io_size = sizeof(buf);
14406 int readlen; /* size of last fread() */
14407 char_u *prev = NULL; /* previously read bytes, if any */
14408 long prevlen = 0; /* length of data in prev */
14409 long prevsize = 0; /* size of prev buffer */
14410 long maxline = MAXLNUM;
14411 long cnt = 0;
14412 char_u *p; /* position in buf */
14413 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014414
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014415 if (argvars[1].v_type != VAR_UNKNOWN)
14416 {
14417 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14418 binary = TRUE;
14419 if (argvars[2].v_type != VAR_UNKNOWN)
14420 maxline = get_tv_number(&argvars[2]);
14421 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014422
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014423 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014424 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014425
14426 /* Always open the file in binary mode, library functions have a mind of
14427 * their own about CR-LF conversion. */
14428 fname = get_tv_string(&argvars[0]);
14429 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14430 {
14431 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14432 return;
14433 }
14434
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014435 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014436 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014437 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014438
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014439 /* This for loop processes what was read, but is also entered at end
14440 * of file so that either:
14441 * - an incomplete line gets written
14442 * - a "binary" file gets an empty line at the end if it ends in a
14443 * newline. */
14444 for (p = buf, start = buf;
14445 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14446 ++p)
14447 {
14448 if (*p == '\n' || readlen <= 0)
14449 {
14450 listitem_T *li;
14451 char_u *s = NULL;
14452 long_u len = p - start;
14453
14454 /* Finished a line. Remove CRs before NL. */
14455 if (readlen > 0 && !binary)
14456 {
14457 while (len > 0 && start[len - 1] == '\r')
14458 --len;
14459 /* removal may cross back to the "prev" string */
14460 if (len == 0)
14461 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14462 --prevlen;
14463 }
14464 if (prevlen == 0)
14465 s = vim_strnsave(start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014466 else
14467 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014468 /* Change "prev" buffer to be the right size. This way
14469 * the bytes are only copied once, and very long lines are
14470 * allocated only once. */
14471 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014472 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014473 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014474 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014475 prev = NULL; /* the list will own the string */
14476 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014477 }
14478 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014479 if (s == NULL)
14480 {
14481 do_outofmem_msg((long_u) prevlen + len + 1);
14482 failed = TRUE;
14483 break;
14484 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014485
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014486 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014487 {
14488 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014489 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014490 break;
14491 }
14492 li->li_tv.v_type = VAR_STRING;
14493 li->li_tv.v_lock = 0;
14494 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014495 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014496
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014497 start = p + 1; /* step over newline */
14498 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014499 break;
14500 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014501 else if (*p == NUL)
14502 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014503#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014504 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14505 * when finding the BF and check the previous two bytes. */
14506 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014507 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014508 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14509 * + 1, these may be in the "prev" string. */
14510 char_u back1 = p >= buf + 1 ? p[-1]
14511 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14512 char_u back2 = p >= buf + 2 ? p[-2]
14513 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14514 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014515
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014516 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014517 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014518 char_u *dest = p - 2;
14519
14520 /* Usually a BOM is at the beginning of a file, and so at
14521 * the beginning of a line; then we can just step over it.
14522 */
14523 if (start == dest)
14524 start = p + 1;
14525 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014526 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014527 /* have to shuffle buf to close gap */
14528 int adjust_prevlen = 0;
14529
14530 if (dest < buf)
14531 {
14532 adjust_prevlen = buf - dest; /* must be 1 or 2 */
14533 dest = buf;
14534 }
14535 if (readlen > p - buf + 1)
14536 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14537 readlen -= 3 - adjust_prevlen;
14538 prevlen -= adjust_prevlen;
14539 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014540 }
14541 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014542 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014543#endif
14544 } /* for */
14545
14546 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14547 break;
14548 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014549 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014550 /* There's part of a line in buf, store it in "prev". */
14551 if (p - start + prevlen >= prevsize)
14552 {
14553 /* need bigger "prev" buffer */
14554 char_u *newprev;
14555
14556 /* A common use case is ordinary text files and "prev" gets a
14557 * fragment of a line, so the first allocation is made
14558 * small, to avoid repeatedly 'allocing' large and
14559 * 'reallocing' small. */
14560 if (prevsize == 0)
14561 prevsize = p - start;
14562 else
14563 {
14564 long grow50pc = (prevsize * 3) / 2;
14565 long growmin = (p - start) * 2 + prevlen;
14566 prevsize = grow50pc > growmin ? grow50pc : growmin;
14567 }
14568 if ((newprev = vim_realloc(prev, prevsize)) == NULL)
14569 {
14570 do_outofmem_msg((long_u)prevsize);
14571 failed = TRUE;
14572 break;
14573 }
14574 prev = newprev;
14575 }
14576 /* Add the line part to end of "prev". */
14577 mch_memmove(prev + prevlen, start, p - start);
14578 prevlen += p - start;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014579 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014580 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014581
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014582 /*
14583 * For a negative line count use only the lines at the end of the file,
14584 * free the rest.
14585 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014586 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014587 while (cnt > -maxline)
14588 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014589 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014590 --cnt;
14591 }
14592
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014593 if (failed)
14594 {
14595 list_free(rettv->vval.v_list, TRUE);
14596 /* readfile doc says an empty list is returned on error */
14597 rettv->vval.v_list = list_alloc();
14598 }
14599
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014600 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014601 fclose(fd);
14602}
14603
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014604#if defined(FEAT_RELTIME)
14605static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14606
14607/*
14608 * Convert a List to proftime_T.
14609 * Return FAIL when there is something wrong.
14610 */
14611 static int
14612list2proftime(arg, tm)
14613 typval_T *arg;
14614 proftime_T *tm;
14615{
14616 long n1, n2;
14617 int error = FALSE;
14618
14619 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14620 || arg->vval.v_list->lv_len != 2)
14621 return FAIL;
14622 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14623 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14624# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014625 tm->HighPart = n1;
14626 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014627# else
14628 tm->tv_sec = n1;
14629 tm->tv_usec = n2;
14630# endif
14631 return error ? FAIL : OK;
14632}
14633#endif /* FEAT_RELTIME */
14634
14635/*
14636 * "reltime()" function
14637 */
14638 static void
14639f_reltime(argvars, rettv)
14640 typval_T *argvars;
14641 typval_T *rettv;
14642{
14643#ifdef FEAT_RELTIME
14644 proftime_T res;
14645 proftime_T start;
14646
14647 if (argvars[0].v_type == VAR_UNKNOWN)
14648 {
14649 /* No arguments: get current time. */
14650 profile_start(&res);
14651 }
14652 else if (argvars[1].v_type == VAR_UNKNOWN)
14653 {
14654 if (list2proftime(&argvars[0], &res) == FAIL)
14655 return;
14656 profile_end(&res);
14657 }
14658 else
14659 {
14660 /* Two arguments: compute the difference. */
14661 if (list2proftime(&argvars[0], &start) == FAIL
14662 || list2proftime(&argvars[1], &res) == FAIL)
14663 return;
14664 profile_sub(&res, &start);
14665 }
14666
14667 if (rettv_list_alloc(rettv) == OK)
14668 {
14669 long n1, n2;
14670
14671# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014672 n1 = res.HighPart;
14673 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014674# else
14675 n1 = res.tv_sec;
14676 n2 = res.tv_usec;
14677# endif
14678 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14679 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14680 }
14681#endif
14682}
14683
14684/*
14685 * "reltimestr()" function
14686 */
14687 static void
14688f_reltimestr(argvars, rettv)
14689 typval_T *argvars;
14690 typval_T *rettv;
14691{
14692#ifdef FEAT_RELTIME
14693 proftime_T tm;
14694#endif
14695
14696 rettv->v_type = VAR_STRING;
14697 rettv->vval.v_string = NULL;
14698#ifdef FEAT_RELTIME
14699 if (list2proftime(&argvars[0], &tm) == OK)
14700 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14701#endif
14702}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014703
Bram Moolenaar0d660222005-01-07 21:51:51 +000014704#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14705static void make_connection __ARGS((void));
14706static int check_connection __ARGS((void));
14707
14708 static void
14709make_connection()
14710{
14711 if (X_DISPLAY == NULL
14712# ifdef FEAT_GUI
14713 && !gui.in_use
14714# endif
14715 )
14716 {
14717 x_force_connect = TRUE;
14718 setup_term_clip();
14719 x_force_connect = FALSE;
14720 }
14721}
14722
14723 static int
14724check_connection()
14725{
14726 make_connection();
14727 if (X_DISPLAY == NULL)
14728 {
14729 EMSG(_("E240: No connection to Vim server"));
14730 return FAIL;
14731 }
14732 return OK;
14733}
14734#endif
14735
14736#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014737static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014738
14739 static void
14740remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014741 typval_T *argvars;
14742 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014743 int expr;
14744{
14745 char_u *server_name;
14746 char_u *keys;
14747 char_u *r = NULL;
14748 char_u buf[NUMBUFLEN];
14749# ifdef WIN32
14750 HWND w;
14751# else
14752 Window w;
14753# endif
14754
14755 if (check_restricted() || check_secure())
14756 return;
14757
14758# ifdef FEAT_X11
14759 if (check_connection() == FAIL)
14760 return;
14761# endif
14762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014763 server_name = get_tv_string_chk(&argvars[0]);
14764 if (server_name == NULL)
14765 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014766 keys = get_tv_string_buf(&argvars[1], buf);
14767# ifdef WIN32
14768 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14769# else
14770 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14771 < 0)
14772# endif
14773 {
14774 if (r != NULL)
14775 EMSG(r); /* sending worked but evaluation failed */
14776 else
14777 EMSG2(_("E241: Unable to send to %s"), server_name);
14778 return;
14779 }
14780
14781 rettv->vval.v_string = r;
14782
14783 if (argvars[2].v_type != VAR_UNKNOWN)
14784 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014785 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014786 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014787 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014788
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014789 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014790 v.di_tv.v_type = VAR_STRING;
14791 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014792 idvar = get_tv_string_chk(&argvars[2]);
14793 if (idvar != NULL)
14794 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014795 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014796 }
14797}
14798#endif
14799
14800/*
14801 * "remote_expr()" function
14802 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014803 static void
14804f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014805 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014806 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014807{
14808 rettv->v_type = VAR_STRING;
14809 rettv->vval.v_string = NULL;
14810#ifdef FEAT_CLIENTSERVER
14811 remote_common(argvars, rettv, TRUE);
14812#endif
14813}
14814
14815/*
14816 * "remote_foreground()" function
14817 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014818 static void
14819f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014820 typval_T *argvars UNUSED;
14821 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014822{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014823#ifdef FEAT_CLIENTSERVER
14824# ifdef WIN32
14825 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014826 {
14827 char_u *server_name = get_tv_string_chk(&argvars[0]);
14828
14829 if (server_name != NULL)
14830 serverForeground(server_name);
14831 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014832# else
14833 /* Send a foreground() expression to the server. */
14834 argvars[1].v_type = VAR_STRING;
14835 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14836 argvars[2].v_type = VAR_UNKNOWN;
14837 remote_common(argvars, rettv, TRUE);
14838 vim_free(argvars[1].vval.v_string);
14839# endif
14840#endif
14841}
14842
Bram Moolenaar0d660222005-01-07 21:51:51 +000014843 static void
14844f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014845 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014846 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014847{
14848#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014849 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014850 char_u *s = NULL;
14851# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014852 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014853# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014854 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014855
14856 if (check_restricted() || check_secure())
14857 {
14858 rettv->vval.v_number = -1;
14859 return;
14860 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014861 serverid = get_tv_string_chk(&argvars[0]);
14862 if (serverid == NULL)
14863 {
14864 rettv->vval.v_number = -1;
14865 return; /* type error; errmsg already given */
14866 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014867# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014868 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014869 if (n == 0)
14870 rettv->vval.v_number = -1;
14871 else
14872 {
14873 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14874 rettv->vval.v_number = (s != NULL);
14875 }
14876# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014877 if (check_connection() == FAIL)
14878 return;
14879
14880 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014881 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014882# endif
14883
14884 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014886 char_u *retvar;
14887
Bram Moolenaar33570922005-01-25 22:26:29 +000014888 v.di_tv.v_type = VAR_STRING;
14889 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014890 retvar = get_tv_string_chk(&argvars[1]);
14891 if (retvar != NULL)
14892 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014893 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014894 }
14895#else
14896 rettv->vval.v_number = -1;
14897#endif
14898}
14899
Bram Moolenaar0d660222005-01-07 21:51:51 +000014900 static void
14901f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014902 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014903 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014904{
14905 char_u *r = NULL;
14906
14907#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014908 char_u *serverid = get_tv_string_chk(&argvars[0]);
14909
14910 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014911 {
14912# ifdef WIN32
14913 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014914 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014915
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014916 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014917 if (n != 0)
14918 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14919 if (r == NULL)
14920# else
14921 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014922 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014923# endif
14924 EMSG(_("E277: Unable to read a server reply"));
14925 }
14926#endif
14927 rettv->v_type = VAR_STRING;
14928 rettv->vval.v_string = r;
14929}
14930
14931/*
14932 * "remote_send()" function
14933 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014934 static void
14935f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014936 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014937 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014938{
14939 rettv->v_type = VAR_STRING;
14940 rettv->vval.v_string = NULL;
14941#ifdef FEAT_CLIENTSERVER
14942 remote_common(argvars, rettv, FALSE);
14943#endif
14944}
14945
14946/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014947 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014948 */
14949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014950f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014951 typval_T *argvars;
14952 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014953{
Bram Moolenaar33570922005-01-25 22:26:29 +000014954 list_T *l;
14955 listitem_T *item, *item2;
14956 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014957 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014958 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014959 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014960 dict_T *d;
14961 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014962 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014963
Bram Moolenaar8c711452005-01-14 21:53:12 +000014964 if (argvars[0].v_type == VAR_DICT)
14965 {
14966 if (argvars[2].v_type != VAR_UNKNOWN)
14967 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014968 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014969 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014971 key = get_tv_string_chk(&argvars[1]);
14972 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014974 di = dict_find(d, key, -1);
14975 if (di == NULL)
14976 EMSG2(_(e_dictkey), key);
14977 else
14978 {
14979 *rettv = di->di_tv;
14980 init_tv(&di->di_tv);
14981 dictitem_remove(d, di);
14982 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014983 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014984 }
14985 }
14986 else if (argvars[0].v_type != VAR_LIST)
14987 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014988 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014989 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014990 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014991 int error = FALSE;
14992
14993 idx = get_tv_number_chk(&argvars[1], &error);
14994 if (error)
14995 ; /* type error: do nothing, errmsg already given */
14996 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014997 EMSGN(_(e_listidx), idx);
14998 else
14999 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015000 if (argvars[2].v_type == VAR_UNKNOWN)
15001 {
15002 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015003 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015004 *rettv = item->li_tv;
15005 vim_free(item);
15006 }
15007 else
15008 {
15009 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015010 end = get_tv_number_chk(&argvars[2], &error);
15011 if (error)
15012 ; /* type error: do nothing */
15013 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015014 EMSGN(_(e_listidx), end);
15015 else
15016 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015017 int cnt = 0;
15018
15019 for (li = item; li != NULL; li = li->li_next)
15020 {
15021 ++cnt;
15022 if (li == item2)
15023 break;
15024 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015025 if (li == NULL) /* didn't find "item2" after "item" */
15026 EMSG(_(e_invrange));
15027 else
15028 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015029 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015030 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015031 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015032 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015033 l->lv_first = item;
15034 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015035 item->li_prev = NULL;
15036 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015037 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015038 }
15039 }
15040 }
15041 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015042 }
15043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044}
15045
15046/*
15047 * "rename({from}, {to})" function
15048 */
15049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015051 typval_T *argvars;
15052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053{
15054 char_u buf[NUMBUFLEN];
15055
15056 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015057 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015059 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15060 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061}
15062
15063/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015064 * "repeat()" function
15065 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015066 static void
15067f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015068 typval_T *argvars;
15069 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015070{
15071 char_u *p;
15072 int n;
15073 int slen;
15074 int len;
15075 char_u *r;
15076 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015077
15078 n = get_tv_number(&argvars[1]);
15079 if (argvars[0].v_type == VAR_LIST)
15080 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015081 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015082 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015083 if (list_extend(rettv->vval.v_list,
15084 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015085 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015086 }
15087 else
15088 {
15089 p = get_tv_string(&argvars[0]);
15090 rettv->v_type = VAR_STRING;
15091 rettv->vval.v_string = NULL;
15092
15093 slen = (int)STRLEN(p);
15094 len = slen * n;
15095 if (len <= 0)
15096 return;
15097
15098 r = alloc(len + 1);
15099 if (r != NULL)
15100 {
15101 for (i = 0; i < n; i++)
15102 mch_memmove(r + i * slen, p, (size_t)slen);
15103 r[len] = NUL;
15104 }
15105
15106 rettv->vval.v_string = r;
15107 }
15108}
15109
15110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111 * "resolve()" function
15112 */
15113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015114f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015115 typval_T *argvars;
15116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117{
15118 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015119#ifdef HAVE_READLINK
15120 char_u *buf = NULL;
15121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015123 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124#ifdef FEAT_SHORTCUT
15125 {
15126 char_u *v = NULL;
15127
15128 v = mch_resolve_shortcut(p);
15129 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015130 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 }
15134#else
15135# ifdef HAVE_READLINK
15136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137 char_u *cpy;
15138 int len;
15139 char_u *remain = NULL;
15140 char_u *q;
15141 int is_relative_to_current = FALSE;
15142 int has_trailing_pathsep = FALSE;
15143 int limit = 100;
15144
15145 p = vim_strsave(p);
15146
15147 if (p[0] == '.' && (vim_ispathsep(p[1])
15148 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15149 is_relative_to_current = TRUE;
15150
15151 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015152 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015154 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015155 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157
15158 q = getnextcomp(p);
15159 if (*q != NUL)
15160 {
15161 /* Separate the first path component in "p", and keep the
15162 * remainder (beginning with the path separator). */
15163 remain = vim_strsave(q - 1);
15164 q[-1] = NUL;
15165 }
15166
Bram Moolenaard9462e32011-04-11 21:35:11 +020015167 buf = alloc(MAXPATHL + 1);
15168 if (buf == NULL)
15169 goto fail;
15170
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171 for (;;)
15172 {
15173 for (;;)
15174 {
15175 len = readlink((char *)p, (char *)buf, MAXPATHL);
15176 if (len <= 0)
15177 break;
15178 buf[len] = NUL;
15179
15180 if (limit-- == 0)
15181 {
15182 vim_free(p);
15183 vim_free(remain);
15184 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015185 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186 goto fail;
15187 }
15188
15189 /* Ensure that the result will have a trailing path separator
15190 * if the argument has one. */
15191 if (remain == NULL && has_trailing_pathsep)
15192 add_pathsep(buf);
15193
15194 /* Separate the first path component in the link value and
15195 * concatenate the remainders. */
15196 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15197 if (*q != NUL)
15198 {
15199 if (remain == NULL)
15200 remain = vim_strsave(q - 1);
15201 else
15202 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015203 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204 if (cpy != NULL)
15205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 vim_free(remain);
15207 remain = cpy;
15208 }
15209 }
15210 q[-1] = NUL;
15211 }
15212
15213 q = gettail(p);
15214 if (q > p && *q == NUL)
15215 {
15216 /* Ignore trailing path separator. */
15217 q[-1] = NUL;
15218 q = gettail(p);
15219 }
15220 if (q > p && !mch_isFullName(buf))
15221 {
15222 /* symlink is relative to directory of argument */
15223 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15224 if (cpy != NULL)
15225 {
15226 STRCPY(cpy, p);
15227 STRCPY(gettail(cpy), buf);
15228 vim_free(p);
15229 p = cpy;
15230 }
15231 }
15232 else
15233 {
15234 vim_free(p);
15235 p = vim_strsave(buf);
15236 }
15237 }
15238
15239 if (remain == NULL)
15240 break;
15241
15242 /* Append the first path component of "remain" to "p". */
15243 q = getnextcomp(remain + 1);
15244 len = q - remain - (*q != NUL);
15245 cpy = vim_strnsave(p, STRLEN(p) + len);
15246 if (cpy != NULL)
15247 {
15248 STRNCAT(cpy, remain, len);
15249 vim_free(p);
15250 p = cpy;
15251 }
15252 /* Shorten "remain". */
15253 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015254 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255 else
15256 {
15257 vim_free(remain);
15258 remain = NULL;
15259 }
15260 }
15261
15262 /* If the result is a relative path name, make it explicitly relative to
15263 * the current directory if and only if the argument had this form. */
15264 if (!vim_ispathsep(*p))
15265 {
15266 if (is_relative_to_current
15267 && *p != NUL
15268 && !(p[0] == '.'
15269 && (p[1] == NUL
15270 || vim_ispathsep(p[1])
15271 || (p[1] == '.'
15272 && (p[2] == NUL
15273 || vim_ispathsep(p[2]))))))
15274 {
15275 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015276 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 if (cpy != NULL)
15278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279 vim_free(p);
15280 p = cpy;
15281 }
15282 }
15283 else if (!is_relative_to_current)
15284 {
15285 /* Strip leading "./". */
15286 q = p;
15287 while (q[0] == '.' && vim_ispathsep(q[1]))
15288 q += 2;
15289 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015290 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291 }
15292 }
15293
15294 /* Ensure that the result will have no trailing path separator
15295 * if the argument had none. But keep "/" or "//". */
15296 if (!has_trailing_pathsep)
15297 {
15298 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015299 if (after_pathsep(p, q))
15300 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 }
15302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015303 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304 }
15305# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015306 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307# endif
15308#endif
15309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015310 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311
15312#ifdef HAVE_READLINK
15313fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015314 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015316 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317}
15318
15319/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015320 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321 */
15322 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015323f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015324 typval_T *argvars;
15325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326{
Bram Moolenaar33570922005-01-25 22:26:29 +000015327 list_T *l;
15328 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329
Bram Moolenaar0d660222005-01-07 21:51:51 +000015330 if (argvars[0].v_type != VAR_LIST)
15331 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015332 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015333 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015334 {
15335 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015336 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015337 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015338 while (li != NULL)
15339 {
15340 ni = li->li_prev;
15341 list_append(l, li);
15342 li = ni;
15343 }
15344 rettv->vval.v_list = l;
15345 rettv->v_type = VAR_LIST;
15346 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015347 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349}
15350
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015351#define SP_NOMOVE 0x01 /* don't move cursor */
15352#define SP_REPEAT 0x02 /* repeat to find outer pair */
15353#define SP_RETCOUNT 0x04 /* return matchcount */
15354#define SP_SETPCMARK 0x08 /* set previous context mark */
15355#define SP_START 0x10 /* accept match at start position */
15356#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15357#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015358
Bram Moolenaar33570922005-01-25 22:26:29 +000015359static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015360
15361/*
15362 * Get flags for a search function.
15363 * Possibly sets "p_ws".
15364 * Returns BACKWARD, FORWARD or zero (for an error).
15365 */
15366 static int
15367get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015368 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015369 int *flagsp;
15370{
15371 int dir = FORWARD;
15372 char_u *flags;
15373 char_u nbuf[NUMBUFLEN];
15374 int mask;
15375
15376 if (varp->v_type != VAR_UNKNOWN)
15377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015378 flags = get_tv_string_buf_chk(varp, nbuf);
15379 if (flags == NULL)
15380 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015381 while (*flags != NUL)
15382 {
15383 switch (*flags)
15384 {
15385 case 'b': dir = BACKWARD; break;
15386 case 'w': p_ws = TRUE; break;
15387 case 'W': p_ws = FALSE; break;
15388 default: mask = 0;
15389 if (flagsp != NULL)
15390 switch (*flags)
15391 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015392 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015393 case 'e': mask = SP_END; break;
15394 case 'm': mask = SP_RETCOUNT; break;
15395 case 'n': mask = SP_NOMOVE; break;
15396 case 'p': mask = SP_SUBPAT; break;
15397 case 'r': mask = SP_REPEAT; break;
15398 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015399 }
15400 if (mask == 0)
15401 {
15402 EMSG2(_(e_invarg2), flags);
15403 dir = 0;
15404 }
15405 else
15406 *flagsp |= mask;
15407 }
15408 if (dir == 0)
15409 break;
15410 ++flags;
15411 }
15412 }
15413 return dir;
15414}
15415
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015417 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015419 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015420search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015421 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015422 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015423 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015425 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426 char_u *pat;
15427 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015428 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 int save_p_ws = p_ws;
15430 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015431 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015432 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015433 proftime_T tm;
15434#ifdef FEAT_RELTIME
15435 long time_limit = 0;
15436#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015437 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015438 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015440 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015441 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015442 if (dir == 0)
15443 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015444 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015445 if (flags & SP_START)
15446 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015447 if (flags & SP_END)
15448 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015449
Bram Moolenaar76929292008-01-06 19:07:36 +000015450 /* Optional arguments: line number to stop searching and timeout. */
15451 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015452 {
15453 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15454 if (lnum_stop < 0)
15455 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015456#ifdef FEAT_RELTIME
15457 if (argvars[3].v_type != VAR_UNKNOWN)
15458 {
15459 time_limit = get_tv_number_chk(&argvars[3], NULL);
15460 if (time_limit < 0)
15461 goto theend;
15462 }
15463#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015464 }
15465
Bram Moolenaar76929292008-01-06 19:07:36 +000015466#ifdef FEAT_RELTIME
15467 /* Set the time limit, if there is one. */
15468 profile_setlimit(time_limit, &tm);
15469#endif
15470
Bram Moolenaar231334e2005-07-25 20:46:57 +000015471 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015472 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015473 * Check to make sure only those flags are set.
15474 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15475 * flags cannot be set. Check for that condition also.
15476 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015477 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015478 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015480 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015481 goto theend;
15482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015484 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015485 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015486 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015487 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015489 if (flags & SP_SUBPAT)
15490 retval = subpatnum;
15491 else
15492 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015493 if (flags & SP_SETPCMARK)
15494 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015496 if (match_pos != NULL)
15497 {
15498 /* Store the match cursor position */
15499 match_pos->lnum = pos.lnum;
15500 match_pos->col = pos.col + 1;
15501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 /* "/$" will put the cursor after the end of the line, may need to
15503 * correct that here */
15504 check_cursor();
15505 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015506
15507 /* If 'n' flag is used: restore cursor position. */
15508 if (flags & SP_NOMOVE)
15509 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015510 else
15511 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015512theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015514
15515 return retval;
15516}
15517
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015518#ifdef FEAT_FLOAT
15519/*
15520 * "round({float})" function
15521 */
15522 static void
15523f_round(argvars, rettv)
15524 typval_T *argvars;
15525 typval_T *rettv;
15526{
15527 float_T f;
15528
15529 rettv->v_type = VAR_FLOAT;
15530 if (get_float_arg(argvars, &f) == OK)
15531 /* round() is not in C90, use ceil() or floor() instead. */
15532 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15533 else
15534 rettv->vval.v_float = 0.0;
15535}
15536#endif
15537
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015538/*
15539 * "search()" function
15540 */
15541 static void
15542f_search(argvars, rettv)
15543 typval_T *argvars;
15544 typval_T *rettv;
15545{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015546 int flags = 0;
15547
15548 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549}
15550
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015552 * "searchdecl()" function
15553 */
15554 static void
15555f_searchdecl(argvars, rettv)
15556 typval_T *argvars;
15557 typval_T *rettv;
15558{
15559 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015560 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015561 int error = FALSE;
15562 char_u *name;
15563
15564 rettv->vval.v_number = 1; /* default: FAIL */
15565
15566 name = get_tv_string_chk(&argvars[0]);
15567 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015568 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015569 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015570 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15571 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15572 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015573 if (!error && name != NULL)
15574 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015575 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015576}
15577
15578/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015579 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015581 static int
15582searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015583 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015584 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585{
15586 char_u *spat, *mpat, *epat;
15587 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 int dir;
15590 int flags = 0;
15591 char_u nbuf1[NUMBUFLEN];
15592 char_u nbuf2[NUMBUFLEN];
15593 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015594 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015595 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015596 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015599 spat = get_tv_string_chk(&argvars[0]);
15600 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15601 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15602 if (spat == NULL || mpat == NULL || epat == NULL)
15603 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605 /* Handle the optional fourth argument: flags */
15606 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015607 if (dir == 0)
15608 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015609
15610 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015611 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15612 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015613 if ((flags & (SP_END | SP_SUBPAT)) != 0
15614 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015615 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015616 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015617 goto theend;
15618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015620 /* Using 'r' implies 'W', otherwise it doesn't work. */
15621 if (flags & SP_REPEAT)
15622 p_ws = FALSE;
15623
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015624 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015625 if (argvars[3].v_type == VAR_UNKNOWN
15626 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 skip = (char_u *)"";
15628 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015630 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015631 if (argvars[5].v_type != VAR_UNKNOWN)
15632 {
15633 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15634 if (lnum_stop < 0)
15635 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015636#ifdef FEAT_RELTIME
15637 if (argvars[6].v_type != VAR_UNKNOWN)
15638 {
15639 time_limit = get_tv_number_chk(&argvars[6], NULL);
15640 if (time_limit < 0)
15641 goto theend;
15642 }
15643#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015644 }
15645 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015646 if (skip == NULL)
15647 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015649 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015650 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015651
15652theend:
15653 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015654
15655 return retval;
15656}
15657
15658/*
15659 * "searchpair()" function
15660 */
15661 static void
15662f_searchpair(argvars, rettv)
15663 typval_T *argvars;
15664 typval_T *rettv;
15665{
15666 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15667}
15668
15669/*
15670 * "searchpairpos()" function
15671 */
15672 static void
15673f_searchpairpos(argvars, rettv)
15674 typval_T *argvars;
15675 typval_T *rettv;
15676{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015677 pos_T match_pos;
15678 int lnum = 0;
15679 int col = 0;
15680
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015681 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015682 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015683
15684 if (searchpair_cmn(argvars, &match_pos) > 0)
15685 {
15686 lnum = match_pos.lnum;
15687 col = match_pos.col;
15688 }
15689
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015690 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15691 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015692}
15693
15694/*
15695 * Search for a start/middle/end thing.
15696 * Used by searchpair(), see its documentation for the details.
15697 * Returns 0 or -1 for no match,
15698 */
15699 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015700do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15701 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015702 char_u *spat; /* start pattern */
15703 char_u *mpat; /* middle pattern */
15704 char_u *epat; /* end pattern */
15705 int dir; /* BACKWARD or FORWARD */
15706 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015707 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015708 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015709 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015710 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015711{
15712 char_u *save_cpo;
15713 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15714 long retval = 0;
15715 pos_T pos;
15716 pos_T firstpos;
15717 pos_T foundpos;
15718 pos_T save_cursor;
15719 pos_T save_pos;
15720 int n;
15721 int r;
15722 int nest = 1;
15723 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015724 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015725 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015726
15727 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15728 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015729 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015730
Bram Moolenaar76929292008-01-06 19:07:36 +000015731#ifdef FEAT_RELTIME
15732 /* Set the time limit, if there is one. */
15733 profile_setlimit(time_limit, &tm);
15734#endif
15735
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015736 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15737 * start/middle/end (pat3, for the top pair). */
15738 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15739 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15740 if (pat2 == NULL || pat3 == NULL)
15741 goto theend;
15742 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15743 if (*mpat == NUL)
15744 STRCPY(pat3, pat2);
15745 else
15746 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15747 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015748 if (flags & SP_START)
15749 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015750
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751 save_cursor = curwin->w_cursor;
15752 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015753 clearpos(&firstpos);
15754 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 pat = pat3;
15756 for (;;)
15757 {
15758 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015759 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15761 /* didn't find it or found the first match again: FAIL */
15762 break;
15763
15764 if (firstpos.lnum == 0)
15765 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015766 if (equalpos(pos, foundpos))
15767 {
15768 /* Found the same position again. Can happen with a pattern that
15769 * has "\zs" at the end and searching backwards. Advance one
15770 * character and try again. */
15771 if (dir == BACKWARD)
15772 decl(&pos);
15773 else
15774 incl(&pos);
15775 }
15776 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015778 /* clear the start flag to avoid getting stuck here */
15779 options &= ~SEARCH_START;
15780
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 /* If the skip pattern matches, ignore this match. */
15782 if (*skip != NUL)
15783 {
15784 save_pos = curwin->w_cursor;
15785 curwin->w_cursor = pos;
15786 r = eval_to_bool(skip, &err, NULL, FALSE);
15787 curwin->w_cursor = save_pos;
15788 if (err)
15789 {
15790 /* Evaluating {skip} caused an error, break here. */
15791 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015792 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793 break;
15794 }
15795 if (r)
15796 continue;
15797 }
15798
15799 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15800 {
15801 /* Found end when searching backwards or start when searching
15802 * forward: nested pair. */
15803 ++nest;
15804 pat = pat2; /* nested, don't search for middle */
15805 }
15806 else
15807 {
15808 /* Found end when searching forward or start when searching
15809 * backward: end of (nested) pair; or found middle in outer pair. */
15810 if (--nest == 1)
15811 pat = pat3; /* outer level, search for middle */
15812 }
15813
15814 if (nest == 0)
15815 {
15816 /* Found the match: return matchcount or line number. */
15817 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015818 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015820 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015821 if (flags & SP_SETPCMARK)
15822 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 curwin->w_cursor = pos;
15824 if (!(flags & SP_REPEAT))
15825 break;
15826 nest = 1; /* search for next unmatched */
15827 }
15828 }
15829
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015830 if (match_pos != NULL)
15831 {
15832 /* Store the match cursor position */
15833 match_pos->lnum = curwin->w_cursor.lnum;
15834 match_pos->col = curwin->w_cursor.col + 1;
15835 }
15836
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015838 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 curwin->w_cursor = save_cursor;
15840
15841theend:
15842 vim_free(pat2);
15843 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015844 if (p_cpo == empty_option)
15845 p_cpo = save_cpo;
15846 else
15847 /* Darn, evaluating the {skip} expression changed the value. */
15848 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015849
15850 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851}
15852
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015853/*
15854 * "searchpos()" function
15855 */
15856 static void
15857f_searchpos(argvars, rettv)
15858 typval_T *argvars;
15859 typval_T *rettv;
15860{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015861 pos_T match_pos;
15862 int lnum = 0;
15863 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015864 int n;
15865 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015866
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015867 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015868 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015869
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015870 n = search_cmn(argvars, &match_pos, &flags);
15871 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015872 {
15873 lnum = match_pos.lnum;
15874 col = match_pos.col;
15875 }
15876
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015877 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15878 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015879 if (flags & SP_SUBPAT)
15880 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015881}
15882
15883
Bram Moolenaar0d660222005-01-07 21:51:51 +000015884 static void
15885f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015886 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015889#ifdef FEAT_CLIENTSERVER
15890 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015891 char_u *server = get_tv_string_chk(&argvars[0]);
15892 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893
Bram Moolenaar0d660222005-01-07 21:51:51 +000015894 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015895 if (server == NULL || reply == NULL)
15896 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015897 if (check_restricted() || check_secure())
15898 return;
15899# ifdef FEAT_X11
15900 if (check_connection() == FAIL)
15901 return;
15902# endif
15903
15904 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015906 EMSG(_("E258: Unable to send to client"));
15907 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015909 rettv->vval.v_number = 0;
15910#else
15911 rettv->vval.v_number = -1;
15912#endif
15913}
15914
Bram Moolenaar0d660222005-01-07 21:51:51 +000015915 static void
15916f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015917 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015918 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015919{
15920 char_u *r = NULL;
15921
15922#ifdef FEAT_CLIENTSERVER
15923# ifdef WIN32
15924 r = serverGetVimNames();
15925# else
15926 make_connection();
15927 if (X_DISPLAY != NULL)
15928 r = serverGetVimNames(X_DISPLAY);
15929# endif
15930#endif
15931 rettv->v_type = VAR_STRING;
15932 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933}
15934
15935/*
15936 * "setbufvar()" function
15937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015939f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015940 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015941 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015942{
15943 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015946 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947 char_u nbuf[NUMBUFLEN];
15948
15949 if (check_restricted() || check_secure())
15950 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015951 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15952 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015953 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954 varp = &argvars[2];
15955
15956 if (buf != NULL && varname != NULL && varp != NULL)
15957 {
15958 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015959 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960
15961 if (*varname == '&')
15962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015963 long numval;
15964 char_u *strval;
15965 int error = FALSE;
15966
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015968 numval = get_tv_number_chk(varp, &error);
15969 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015970 if (!error && strval != NULL)
15971 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972 }
15973 else
15974 {
15975 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15976 if (bufvarname != NULL)
15977 {
15978 STRCPY(bufvarname, "b:");
15979 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015980 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981 vim_free(bufvarname);
15982 }
15983 }
15984
15985 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988}
15989
15990/*
15991 * "setcmdpos()" function
15992 */
15993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015994f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015995 typval_T *argvars;
15996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015998 int pos = (int)get_tv_number(&argvars[0]) - 1;
15999
16000 if (pos >= 0)
16001 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002}
16003
16004/*
16005 * "setline()" function
16006 */
16007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016008f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016009 typval_T *argvars;
16010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011{
16012 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016013 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016014 list_T *l = NULL;
16015 listitem_T *li = NULL;
16016 long added = 0;
16017 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016019 lnum = get_tv_lnum(&argvars[0]);
16020 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016022 l = argvars[1].vval.v_list;
16023 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016025 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016026 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016027
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016028 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016029 for (;;)
16030 {
16031 if (l != NULL)
16032 {
16033 /* list argument, get next string */
16034 if (li == NULL)
16035 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016036 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016037 li = li->li_next;
16038 }
16039
16040 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016041 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016042 break;
16043 if (lnum <= curbuf->b_ml.ml_line_count)
16044 {
16045 /* existing line, replace it */
16046 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16047 {
16048 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016049 if (lnum == curwin->w_cursor.lnum)
16050 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016051 rettv->vval.v_number = 0; /* OK */
16052 }
16053 }
16054 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16055 {
16056 /* lnum is one past the last line, append the line */
16057 ++added;
16058 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16059 rettv->vval.v_number = 0; /* OK */
16060 }
16061
16062 if (l == NULL) /* only one string argument */
16063 break;
16064 ++lnum;
16065 }
16066
16067 if (added > 0)
16068 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069}
16070
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016071static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16072
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016074 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016075 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016076 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016077set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016078 win_T *wp UNUSED;
16079 typval_T *list_arg UNUSED;
16080 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016081 typval_T *rettv;
16082{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016083#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016084 char_u *act;
16085 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016086#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016087
Bram Moolenaar2641f772005-03-25 21:58:17 +000016088 rettv->vval.v_number = -1;
16089
16090#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016091 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016092 EMSG(_(e_listreq));
16093 else
16094 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016095 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016096
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016097 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016098 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016099 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016100 if (act == NULL)
16101 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016102 if (*act == 'a' || *act == 'r')
16103 action = *act;
16104 }
16105
Bram Moolenaarbc226b62010-08-09 22:14:48 +020016106 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016107 rettv->vval.v_number = 0;
16108 }
16109#endif
16110}
16111
16112/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016113 * "setloclist()" function
16114 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016115 static void
16116f_setloclist(argvars, rettv)
16117 typval_T *argvars;
16118 typval_T *rettv;
16119{
16120 win_T *win;
16121
16122 rettv->vval.v_number = -1;
16123
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016124 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016125 if (win != NULL)
16126 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16127}
16128
16129/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016130 * "setmatches()" function
16131 */
16132 static void
16133f_setmatches(argvars, rettv)
16134 typval_T *argvars;
16135 typval_T *rettv;
16136{
16137#ifdef FEAT_SEARCH_EXTRA
16138 list_T *l;
16139 listitem_T *li;
16140 dict_T *d;
16141
16142 rettv->vval.v_number = -1;
16143 if (argvars[0].v_type != VAR_LIST)
16144 {
16145 EMSG(_(e_listreq));
16146 return;
16147 }
16148 if ((l = argvars[0].vval.v_list) != NULL)
16149 {
16150
16151 /* To some extent make sure that we are dealing with a list from
16152 * "getmatches()". */
16153 li = l->lv_first;
16154 while (li != NULL)
16155 {
16156 if (li->li_tv.v_type != VAR_DICT
16157 || (d = li->li_tv.vval.v_dict) == NULL)
16158 {
16159 EMSG(_(e_invarg));
16160 return;
16161 }
16162 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16163 && dict_find(d, (char_u *)"pattern", -1) != NULL
16164 && dict_find(d, (char_u *)"priority", -1) != NULL
16165 && dict_find(d, (char_u *)"id", -1) != NULL))
16166 {
16167 EMSG(_(e_invarg));
16168 return;
16169 }
16170 li = li->li_next;
16171 }
16172
16173 clear_matches(curwin);
16174 li = l->lv_first;
16175 while (li != NULL)
16176 {
16177 d = li->li_tv.vval.v_dict;
16178 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16179 get_dict_string(d, (char_u *)"pattern", FALSE),
16180 (int)get_dict_number(d, (char_u *)"priority"),
16181 (int)get_dict_number(d, (char_u *)"id"));
16182 li = li->li_next;
16183 }
16184 rettv->vval.v_number = 0;
16185 }
16186#endif
16187}
16188
16189/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016190 * "setpos()" function
16191 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016192 static void
16193f_setpos(argvars, rettv)
16194 typval_T *argvars;
16195 typval_T *rettv;
16196{
16197 pos_T pos;
16198 int fnum;
16199 char_u *name;
16200
Bram Moolenaar08250432008-02-13 11:42:46 +000016201 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016202 name = get_tv_string_chk(argvars);
16203 if (name != NULL)
16204 {
16205 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16206 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016207 if (--pos.col < 0)
16208 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016209 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016210 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016211 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016212 if (fnum == curbuf->b_fnum)
16213 {
16214 curwin->w_cursor = pos;
16215 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016216 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016217 }
16218 else
16219 EMSG(_(e_invarg));
16220 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016221 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16222 {
16223 /* set mark */
16224 if (setmark_pos(name[1], &pos, fnum) == OK)
16225 rettv->vval.v_number = 0;
16226 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016227 else
16228 EMSG(_(e_invarg));
16229 }
16230 }
16231}
16232
16233/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016234 * "setqflist()" function
16235 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016236 static void
16237f_setqflist(argvars, rettv)
16238 typval_T *argvars;
16239 typval_T *rettv;
16240{
16241 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16242}
16243
16244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245 * "setreg()" function
16246 */
16247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016248f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016249 typval_T *argvars;
16250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251{
16252 int regname;
16253 char_u *strregname;
16254 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016255 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 int append;
16257 char_u yank_type;
16258 long block_len;
16259
16260 block_len = -1;
16261 yank_type = MAUTO;
16262 append = FALSE;
16263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016264 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016265 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016267 if (strregname == NULL)
16268 return; /* type error; errmsg already given */
16269 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270 if (regname == 0 || regname == '@')
16271 regname = '"';
16272 else if (regname == '=')
16273 return;
16274
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016275 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016277 stropt = get_tv_string_chk(&argvars[2]);
16278 if (stropt == NULL)
16279 return; /* type error */
16280 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 switch (*stropt)
16282 {
16283 case 'a': case 'A': /* append */
16284 append = TRUE;
16285 break;
16286 case 'v': case 'c': /* character-wise selection */
16287 yank_type = MCHAR;
16288 break;
16289 case 'V': case 'l': /* line-wise selection */
16290 yank_type = MLINE;
16291 break;
16292#ifdef FEAT_VISUAL
16293 case 'b': case Ctrl_V: /* block-wise selection */
16294 yank_type = MBLOCK;
16295 if (VIM_ISDIGIT(stropt[1]))
16296 {
16297 ++stropt;
16298 block_len = getdigits(&stropt) - 1;
16299 --stropt;
16300 }
16301 break;
16302#endif
16303 }
16304 }
16305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016306 strval = get_tv_string_chk(&argvars[1]);
16307 if (strval != NULL)
16308 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016310 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311}
16312
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016313/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016314 * "settabvar()" function
16315 */
16316 static void
16317f_settabvar(argvars, rettv)
16318 typval_T *argvars;
16319 typval_T *rettv;
16320{
16321 tabpage_T *save_curtab;
16322 char_u *varname, *tabvarname;
16323 typval_T *varp;
16324 tabpage_T *tp;
16325
16326 rettv->vval.v_number = 0;
16327
16328 if (check_restricted() || check_secure())
16329 return;
16330
16331 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16332 varname = get_tv_string_chk(&argvars[1]);
16333 varp = &argvars[2];
16334
16335 if (tp != NULL && varname != NULL && varp != NULL)
16336 {
16337 save_curtab = curtab;
16338 goto_tabpage_tp(tp);
16339
16340 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16341 if (tabvarname != NULL)
16342 {
16343 STRCPY(tabvarname, "t:");
16344 STRCPY(tabvarname + 2, varname);
16345 set_var(tabvarname, varp, TRUE);
16346 vim_free(tabvarname);
16347 }
16348
16349 /* Restore current tabpage */
16350 if (valid_tabpage(save_curtab))
16351 goto_tabpage_tp(save_curtab);
16352 }
16353}
16354
16355/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016356 * "settabwinvar()" function
16357 */
16358 static void
16359f_settabwinvar(argvars, rettv)
16360 typval_T *argvars;
16361 typval_T *rettv;
16362{
16363 setwinvar(argvars, rettv, 1);
16364}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365
16366/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016367 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016370f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016371 typval_T *argvars;
16372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016374 setwinvar(argvars, rettv, 0);
16375}
16376
16377/*
16378 * "setwinvar()" and "settabwinvar()" functions
16379 */
16380 static void
16381setwinvar(argvars, rettv, off)
16382 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016383 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016384 int off;
16385{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 win_T *win;
16387#ifdef FEAT_WINDOWS
16388 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016389 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390#endif
16391 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016392 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016394 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395
16396 if (check_restricted() || check_secure())
16397 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016398
16399#ifdef FEAT_WINDOWS
16400 if (off == 1)
16401 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16402 else
16403 tp = curtab;
16404#endif
16405 win = find_win_by_nr(&argvars[off], tp);
16406 varname = get_tv_string_chk(&argvars[off + 1]);
16407 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408
16409 if (win != NULL && varname != NULL && varp != NULL)
16410 {
16411#ifdef FEAT_WINDOWS
16412 /* set curwin to be our win, temporarily */
16413 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016414 save_curtab = curtab;
16415 goto_tabpage_tp(tp);
16416 if (!win_valid(win))
16417 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 curwin = win;
16419 curbuf = curwin->w_buffer;
16420#endif
16421
16422 if (*varname == '&')
16423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016424 long numval;
16425 char_u *strval;
16426 int error = FALSE;
16427
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016429 numval = get_tv_number_chk(varp, &error);
16430 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016431 if (!error && strval != NULL)
16432 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433 }
16434 else
16435 {
16436 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16437 if (winvarname != NULL)
16438 {
16439 STRCPY(winvarname, "w:");
16440 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016441 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442 vim_free(winvarname);
16443 }
16444 }
16445
16446#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016447 /* Restore current tabpage and window, if still valid (autocomands can
16448 * make them invalid). */
16449 if (valid_tabpage(save_curtab))
16450 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 if (win_valid(save_curwin))
16452 {
16453 curwin = save_curwin;
16454 curbuf = curwin->w_buffer;
16455 }
16456#endif
16457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458}
16459
16460/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016461 * "shellescape({string})" function
16462 */
16463 static void
16464f_shellescape(argvars, rettv)
16465 typval_T *argvars;
16466 typval_T *rettv;
16467{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016468 rettv->vval.v_string = vim_strsave_shellescape(
16469 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016470 rettv->v_type = VAR_STRING;
16471}
16472
16473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475 */
16476 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016478 typval_T *argvars;
16479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016481 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482
Bram Moolenaar0d660222005-01-07 21:51:51 +000016483 p = get_tv_string(&argvars[0]);
16484 rettv->vval.v_string = vim_strsave(p);
16485 simplify_filename(rettv->vval.v_string); /* simplify in place */
16486 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487}
16488
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016489#ifdef FEAT_FLOAT
16490/*
16491 * "sin()" function
16492 */
16493 static void
16494f_sin(argvars, rettv)
16495 typval_T *argvars;
16496 typval_T *rettv;
16497{
16498 float_T f;
16499
16500 rettv->v_type = VAR_FLOAT;
16501 if (get_float_arg(argvars, &f) == OK)
16502 rettv->vval.v_float = sin(f);
16503 else
16504 rettv->vval.v_float = 0.0;
16505}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016506
16507/*
16508 * "sinh()" function
16509 */
16510 static void
16511f_sinh(argvars, rettv)
16512 typval_T *argvars;
16513 typval_T *rettv;
16514{
16515 float_T f;
16516
16517 rettv->v_type = VAR_FLOAT;
16518 if (get_float_arg(argvars, &f) == OK)
16519 rettv->vval.v_float = sinh(f);
16520 else
16521 rettv->vval.v_float = 0.0;
16522}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016523#endif
16524
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525static int
16526#ifdef __BORLANDC__
16527 _RTLENTRYF
16528#endif
16529 item_compare __ARGS((const void *s1, const void *s2));
16530static int
16531#ifdef __BORLANDC__
16532 _RTLENTRYF
16533#endif
16534 item_compare2 __ARGS((const void *s1, const void *s2));
16535
16536static int item_compare_ic;
16537static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016538static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016539static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016540#define ITEM_COMPARE_FAIL 999
16541
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016543 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016545 static int
16546#ifdef __BORLANDC__
16547_RTLENTRYF
16548#endif
16549item_compare(s1, s2)
16550 const void *s1;
16551 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016553 char_u *p1, *p2;
16554 char_u *tofree1, *tofree2;
16555 int res;
16556 char_u numbuf1[NUMBUFLEN];
16557 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016559 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16560 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016561 if (p1 == NULL)
16562 p1 = (char_u *)"";
16563 if (p2 == NULL)
16564 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016565 if (item_compare_ic)
16566 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016568 res = STRCMP(p1, p2);
16569 vim_free(tofree1);
16570 vim_free(tofree2);
16571 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572}
16573
16574 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016575#ifdef __BORLANDC__
16576_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016577#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016578item_compare2(s1, s2)
16579 const void *s1;
16580 const void *s2;
16581{
16582 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016583 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016584 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016585 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016587 /* shortcut after failure in previous call; compare all items equal */
16588 if (item_compare_func_err)
16589 return 0;
16590
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016591 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16592 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016593 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16594 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016595
16596 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016597 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016598 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16599 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016600 clear_tv(&argv[0]);
16601 clear_tv(&argv[1]);
16602
16603 if (res == FAIL)
16604 res = ITEM_COMPARE_FAIL;
16605 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016606 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16607 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016608 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016609 clear_tv(&rettv);
16610 return res;
16611}
16612
16613/*
16614 * "sort({list})" function
16615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016617f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016618 typval_T *argvars;
16619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620{
Bram Moolenaar33570922005-01-25 22:26:29 +000016621 list_T *l;
16622 listitem_T *li;
16623 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016624 long len;
16625 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626
Bram Moolenaar0d660222005-01-07 21:51:51 +000016627 if (argvars[0].v_type != VAR_LIST)
16628 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 else
16630 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016631 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016632 if (l == NULL || tv_check_lock(l->lv_lock,
16633 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016634 return;
16635 rettv->vval.v_list = l;
16636 rettv->v_type = VAR_LIST;
16637 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638
Bram Moolenaar0d660222005-01-07 21:51:51 +000016639 len = list_len(l);
16640 if (len <= 1)
16641 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642
Bram Moolenaar0d660222005-01-07 21:51:51 +000016643 item_compare_ic = FALSE;
16644 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016645 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016646 if (argvars[1].v_type != VAR_UNKNOWN)
16647 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016648 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016649 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016650 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016651 else
16652 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016653 int error = FALSE;
16654
16655 i = get_tv_number_chk(&argvars[1], &error);
16656 if (error)
16657 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016658 if (i == 1)
16659 item_compare_ic = TRUE;
16660 else
16661 item_compare_func = get_tv_string(&argvars[1]);
16662 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016663
16664 if (argvars[2].v_type != VAR_UNKNOWN)
16665 {
16666 /* optional third argument: {dict} */
16667 if (argvars[2].v_type != VAR_DICT)
16668 {
16669 EMSG(_(e_dictreq));
16670 return;
16671 }
16672 item_compare_selfdict = argvars[2].vval.v_dict;
16673 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675
Bram Moolenaar0d660222005-01-07 21:51:51 +000016676 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016677 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016678 if (ptrs == NULL)
16679 return;
16680 i = 0;
16681 for (li = l->lv_first; li != NULL; li = li->li_next)
16682 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016684 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016685 /* test the compare function */
16686 if (item_compare_func != NULL
16687 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16688 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016689 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016690 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016691 {
16692 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016693 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016694 item_compare_func == NULL ? item_compare : item_compare2);
16695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016696 if (!item_compare_func_err)
16697 {
16698 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016699 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016700 l->lv_len = 0;
16701 for (i = 0; i < len; ++i)
16702 list_append(l, ptrs[i]);
16703 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016704 }
16705
16706 vim_free(ptrs);
16707 }
16708}
16709
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016710/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016711 * "soundfold({word})" function
16712 */
16713 static void
16714f_soundfold(argvars, rettv)
16715 typval_T *argvars;
16716 typval_T *rettv;
16717{
16718 char_u *s;
16719
16720 rettv->v_type = VAR_STRING;
16721 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016722#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016723 rettv->vval.v_string = eval_soundfold(s);
16724#else
16725 rettv->vval.v_string = vim_strsave(s);
16726#endif
16727}
16728
16729/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016730 * "spellbadword()" function
16731 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016732 static void
16733f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016734 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016735 typval_T *rettv;
16736{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016737 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016738 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016739 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016740
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016741 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016742 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016743
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016744#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016745 if (argvars[0].v_type == VAR_UNKNOWN)
16746 {
16747 /* Find the start and length of the badly spelled word. */
16748 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16749 if (len != 0)
16750 word = ml_get_cursor();
16751 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016752 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016753 {
16754 char_u *str = get_tv_string_chk(&argvars[0]);
16755 int capcol = -1;
16756
16757 if (str != NULL)
16758 {
16759 /* Check the argument for spelling. */
16760 while (*str != NUL)
16761 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016762 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016763 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016764 {
16765 word = str;
16766 break;
16767 }
16768 str += len;
16769 }
16770 }
16771 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016772#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016773
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016774 list_append_string(rettv->vval.v_list, word, len);
16775 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016776 attr == HLF_SPB ? "bad" :
16777 attr == HLF_SPR ? "rare" :
16778 attr == HLF_SPL ? "local" :
16779 attr == HLF_SPC ? "caps" :
16780 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016781}
16782
16783/*
16784 * "spellsuggest()" function
16785 */
16786 static void
16787f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016788 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016789 typval_T *rettv;
16790{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016791#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016792 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016793 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016794 int maxcount;
16795 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016796 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016797 listitem_T *li;
16798 int need_capital = FALSE;
16799#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016800
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016801 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016802 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016803
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016804#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016805 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016806 {
16807 str = get_tv_string(&argvars[0]);
16808 if (argvars[1].v_type != VAR_UNKNOWN)
16809 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016810 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016811 if (maxcount <= 0)
16812 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016813 if (argvars[2].v_type != VAR_UNKNOWN)
16814 {
16815 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16816 if (typeerr)
16817 return;
16818 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016819 }
16820 else
16821 maxcount = 25;
16822
Bram Moolenaar4770d092006-01-12 23:22:24 +000016823 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016824
16825 for (i = 0; i < ga.ga_len; ++i)
16826 {
16827 str = ((char_u **)ga.ga_data)[i];
16828
16829 li = listitem_alloc();
16830 if (li == NULL)
16831 vim_free(str);
16832 else
16833 {
16834 li->li_tv.v_type = VAR_STRING;
16835 li->li_tv.v_lock = 0;
16836 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016837 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016838 }
16839 }
16840 ga_clear(&ga);
16841 }
16842#endif
16843}
16844
Bram Moolenaar0d660222005-01-07 21:51:51 +000016845 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016846f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016847 typval_T *argvars;
16848 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016849{
16850 char_u *str;
16851 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016852 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016853 regmatch_T regmatch;
16854 char_u patbuf[NUMBUFLEN];
16855 char_u *save_cpo;
16856 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016857 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016858 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016859 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016860
16861 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16862 save_cpo = p_cpo;
16863 p_cpo = (char_u *)"";
16864
16865 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016866 if (argvars[1].v_type != VAR_UNKNOWN)
16867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016868 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16869 if (pat == NULL)
16870 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016871 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016872 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016873 }
16874 if (pat == NULL || *pat == NUL)
16875 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016876
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016877 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016879 if (typeerr)
16880 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881
Bram Moolenaar0d660222005-01-07 21:51:51 +000016882 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16883 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016885 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016886 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016887 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016888 if (*str == NUL)
16889 match = FALSE; /* empty item at the end */
16890 else
16891 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016892 if (match)
16893 end = regmatch.startp[0];
16894 else
16895 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016896 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16897 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016898 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016899 if (list_append_string(rettv->vval.v_list, str,
16900 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016901 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016902 }
16903 if (!match)
16904 break;
16905 /* Advance to just after the match. */
16906 if (regmatch.endp[0] > str)
16907 col = 0;
16908 else
16909 {
16910 /* Don't get stuck at the same match. */
16911#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016912 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016913#else
16914 col = 1;
16915#endif
16916 }
16917 str = regmatch.endp[0];
16918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919
Bram Moolenaar0d660222005-01-07 21:51:51 +000016920 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924}
16925
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016926#ifdef FEAT_FLOAT
16927/*
16928 * "sqrt()" function
16929 */
16930 static void
16931f_sqrt(argvars, rettv)
16932 typval_T *argvars;
16933 typval_T *rettv;
16934{
16935 float_T f;
16936
16937 rettv->v_type = VAR_FLOAT;
16938 if (get_float_arg(argvars, &f) == OK)
16939 rettv->vval.v_float = sqrt(f);
16940 else
16941 rettv->vval.v_float = 0.0;
16942}
16943
16944/*
16945 * "str2float()" function
16946 */
16947 static void
16948f_str2float(argvars, rettv)
16949 typval_T *argvars;
16950 typval_T *rettv;
16951{
16952 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16953
16954 if (*p == '+')
16955 p = skipwhite(p + 1);
16956 (void)string2float(p, &rettv->vval.v_float);
16957 rettv->v_type = VAR_FLOAT;
16958}
16959#endif
16960
Bram Moolenaar2c932302006-03-18 21:42:09 +000016961/*
16962 * "str2nr()" function
16963 */
16964 static void
16965f_str2nr(argvars, rettv)
16966 typval_T *argvars;
16967 typval_T *rettv;
16968{
16969 int base = 10;
16970 char_u *p;
16971 long n;
16972
16973 if (argvars[1].v_type != VAR_UNKNOWN)
16974 {
16975 base = get_tv_number(&argvars[1]);
16976 if (base != 8 && base != 10 && base != 16)
16977 {
16978 EMSG(_(e_invarg));
16979 return;
16980 }
16981 }
16982
16983 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016984 if (*p == '+')
16985 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016986 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16987 rettv->vval.v_number = n;
16988}
16989
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990#ifdef HAVE_STRFTIME
16991/*
16992 * "strftime({format}[, {time}])" function
16993 */
16994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016995f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016996 typval_T *argvars;
16997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998{
16999 char_u result_buf[256];
17000 struct tm *curtime;
17001 time_t seconds;
17002 char_u *p;
17003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017004 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017006 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017007 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 seconds = time(NULL);
17009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017010 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 curtime = localtime(&seconds);
17012 /* MSVC returns NULL for an invalid value of seconds. */
17013 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017014 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 else
17016 {
17017# ifdef FEAT_MBYTE
17018 vimconv_T conv;
17019 char_u *enc;
17020
17021 conv.vc_type = CONV_NONE;
17022 enc = enc_locale();
17023 convert_setup(&conv, p_enc, enc);
17024 if (conv.vc_type != CONV_NONE)
17025 p = string_convert(&conv, p, NULL);
17026# endif
17027 if (p != NULL)
17028 (void)strftime((char *)result_buf, sizeof(result_buf),
17029 (char *)p, curtime);
17030 else
17031 result_buf[0] = NUL;
17032
17033# ifdef FEAT_MBYTE
17034 if (conv.vc_type != CONV_NONE)
17035 vim_free(p);
17036 convert_setup(&conv, enc, p_enc);
17037 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017038 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 else
17040# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017041 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042
17043# ifdef FEAT_MBYTE
17044 /* Release conversion descriptors */
17045 convert_setup(&conv, NULL, NULL);
17046 vim_free(enc);
17047# endif
17048 }
17049}
17050#endif
17051
17052/*
17053 * "stridx()" function
17054 */
17055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017056f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017057 typval_T *argvars;
17058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059{
17060 char_u buf[NUMBUFLEN];
17061 char_u *needle;
17062 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017063 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017065 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017067 needle = get_tv_string_chk(&argvars[1]);
17068 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017069 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017070 if (needle == NULL || haystack == NULL)
17071 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072
Bram Moolenaar33570922005-01-25 22:26:29 +000017073 if (argvars[2].v_type != VAR_UNKNOWN)
17074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017075 int error = FALSE;
17076
17077 start_idx = get_tv_number_chk(&argvars[2], &error);
17078 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017079 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017080 if (start_idx >= 0)
17081 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017082 }
17083
17084 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17085 if (pos != NULL)
17086 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087}
17088
17089/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017090 * "string()" function
17091 */
17092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017093f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017094 typval_T *argvars;
17095 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017096{
17097 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017098 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017100 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017101 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017102 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017103 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017104 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105}
17106
17107/*
17108 * "strlen()" function
17109 */
17110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017111f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017112 typval_T *argvars;
17113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017115 rettv->vval.v_number = (varnumber_T)(STRLEN(
17116 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117}
17118
17119/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017120 * "strchars()" function
17121 */
17122 static void
17123f_strchars(argvars, rettv)
17124 typval_T *argvars;
17125 typval_T *rettv;
17126{
17127 char_u *s = get_tv_string(&argvars[0]);
17128#ifdef FEAT_MBYTE
17129 varnumber_T len = 0;
17130
17131 while (*s != NUL)
17132 {
17133 mb_cptr2char_adv(&s);
17134 ++len;
17135 }
17136 rettv->vval.v_number = len;
17137#else
17138 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17139#endif
17140}
17141
17142/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017143 * "strdisplaywidth()" function
17144 */
17145 static void
17146f_strdisplaywidth(argvars, rettv)
17147 typval_T *argvars;
17148 typval_T *rettv;
17149{
17150 char_u *s = get_tv_string(&argvars[0]);
17151 int col = 0;
17152
17153 if (argvars[1].v_type != VAR_UNKNOWN)
17154 col = get_tv_number(&argvars[1]);
17155
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017156 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017157}
17158
17159/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017160 * "strwidth()" function
17161 */
17162 static void
17163f_strwidth(argvars, rettv)
17164 typval_T *argvars;
17165 typval_T *rettv;
17166{
17167 char_u *s = get_tv_string(&argvars[0]);
17168
17169 rettv->vval.v_number = (varnumber_T)(
17170#ifdef FEAT_MBYTE
17171 mb_string2cells(s, -1)
17172#else
17173 STRLEN(s)
17174#endif
17175 );
17176}
17177
17178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 * "strpart()" function
17180 */
17181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017182f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017183 typval_T *argvars;
17184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185{
17186 char_u *p;
17187 int n;
17188 int len;
17189 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017190 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017192 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 slen = (int)STRLEN(p);
17194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017195 n = get_tv_number_chk(&argvars[1], &error);
17196 if (error)
17197 len = 0;
17198 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017199 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 else
17201 len = slen - n; /* default len: all bytes that are available. */
17202
17203 /*
17204 * Only return the overlap between the specified part and the actual
17205 * string.
17206 */
17207 if (n < 0)
17208 {
17209 len += n;
17210 n = 0;
17211 }
17212 else if (n > slen)
17213 n = slen;
17214 if (len < 0)
17215 len = 0;
17216 else if (n + len > slen)
17217 len = slen - n;
17218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017219 rettv->v_type = VAR_STRING;
17220 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221}
17222
17223/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017224 * "strridx()" function
17225 */
17226 static void
17227f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017228 typval_T *argvars;
17229 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017230{
17231 char_u buf[NUMBUFLEN];
17232 char_u *needle;
17233 char_u *haystack;
17234 char_u *rest;
17235 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017236 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017238 needle = get_tv_string_chk(&argvars[1]);
17239 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017240
17241 rettv->vval.v_number = -1;
17242 if (needle == NULL || haystack == NULL)
17243 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017244
17245 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017246 if (argvars[2].v_type != VAR_UNKNOWN)
17247 {
17248 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017249 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017250 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017251 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017252 }
17253 else
17254 end_idx = haystack_len;
17255
Bram Moolenaar0d660222005-01-07 21:51:51 +000017256 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017257 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017258 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017259 lastmatch = haystack + end_idx;
17260 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017261 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017262 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017263 for (rest = haystack; *rest != '\0'; ++rest)
17264 {
17265 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017266 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017267 break;
17268 lastmatch = rest;
17269 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017270 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017271
17272 if (lastmatch == NULL)
17273 rettv->vval.v_number = -1;
17274 else
17275 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17276}
17277
17278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 * "strtrans()" function
17280 */
17281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017282f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017283 typval_T *argvars;
17284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017286 rettv->v_type = VAR_STRING;
17287 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288}
17289
17290/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017291 * "submatch()" function
17292 */
17293 static void
17294f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017295 typval_T *argvars;
17296 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017297{
17298 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017299 rettv->vval.v_string =
17300 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017301}
17302
17303/*
17304 * "substitute()" function
17305 */
17306 static void
17307f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017308 typval_T *argvars;
17309 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017310{
17311 char_u patbuf[NUMBUFLEN];
17312 char_u subbuf[NUMBUFLEN];
17313 char_u flagsbuf[NUMBUFLEN];
17314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017315 char_u *str = get_tv_string_chk(&argvars[0]);
17316 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17317 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17318 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17319
Bram Moolenaar0d660222005-01-07 21:51:51 +000017320 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017321 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17322 rettv->vval.v_string = NULL;
17323 else
17324 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017325}
17326
17327/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017328 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017331f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017332 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334{
17335 int id = 0;
17336#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017337 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338 long col;
17339 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017340 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017342 lnum = get_tv_lnum(argvars); /* -1 on type error */
17343 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17344 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017346 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017347 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017348 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349#endif
17350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017351 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352}
17353
17354/*
17355 * "synIDattr(id, what [, mode])" function
17356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017358f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017359 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361{
17362 char_u *p = NULL;
17363#ifdef FEAT_SYN_HL
17364 int id;
17365 char_u *what;
17366 char_u *mode;
17367 char_u modebuf[NUMBUFLEN];
17368 int modec;
17369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017370 id = get_tv_number(&argvars[0]);
17371 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017372 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017374 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017376 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 modec = 0; /* replace invalid with current */
17378 }
17379 else
17380 {
17381#ifdef FEAT_GUI
17382 if (gui.in_use)
17383 modec = 'g';
17384 else
17385#endif
17386 if (t_colors > 1)
17387 modec = 'c';
17388 else
17389 modec = 't';
17390 }
17391
17392
17393 switch (TOLOWER_ASC(what[0]))
17394 {
17395 case 'b':
17396 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17397 p = highlight_color(id, what, modec);
17398 else /* bold */
17399 p = highlight_has_attr(id, HL_BOLD, modec);
17400 break;
17401
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017402 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 p = highlight_color(id, what, modec);
17404 break;
17405
17406 case 'i':
17407 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17408 p = highlight_has_attr(id, HL_INVERSE, modec);
17409 else /* italic */
17410 p = highlight_has_attr(id, HL_ITALIC, modec);
17411 break;
17412
17413 case 'n': /* name */
17414 p = get_highlight_name(NULL, id - 1);
17415 break;
17416
17417 case 'r': /* reverse */
17418 p = highlight_has_attr(id, HL_INVERSE, modec);
17419 break;
17420
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017421 case 's':
17422 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17423 p = highlight_color(id, what, modec);
17424 else /* standout */
17425 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426 break;
17427
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017428 case 'u':
17429 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17430 /* underline */
17431 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17432 else
17433 /* undercurl */
17434 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 break;
17436 }
17437
17438 if (p != NULL)
17439 p = vim_strsave(p);
17440#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017441 rettv->v_type = VAR_STRING;
17442 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443}
17444
17445/*
17446 * "synIDtrans(id)" function
17447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017449f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017450 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452{
17453 int id;
17454
17455#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017456 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457
17458 if (id > 0)
17459 id = syn_get_final_id(id);
17460 else
17461#endif
17462 id = 0;
17463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017464 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465}
17466
17467/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017468 * "synconcealed(lnum, col)" function
17469 */
17470 static void
17471f_synconcealed(argvars, rettv)
17472 typval_T *argvars UNUSED;
17473 typval_T *rettv;
17474{
17475#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17476 long lnum;
17477 long col;
17478 int syntax_flags = 0;
17479 int cchar;
17480 int matchid = 0;
17481 char_u str[NUMBUFLEN];
17482#endif
17483
17484 rettv->v_type = VAR_LIST;
17485 rettv->vval.v_list = NULL;
17486
17487#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17488 lnum = get_tv_lnum(argvars); /* -1 on type error */
17489 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17490
17491 vim_memset(str, NUL, sizeof(str));
17492
17493 if (rettv_list_alloc(rettv) != FAIL)
17494 {
17495 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17496 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17497 && curwin->w_p_cole > 0)
17498 {
17499 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17500 syntax_flags = get_syntax_info(&matchid);
17501
17502 /* get the conceal character */
17503 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17504 {
17505 cchar = syn_get_sub_char();
17506 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17507 cchar = lcs_conceal;
17508 if (cchar != NUL)
17509 {
17510# ifdef FEAT_MBYTE
17511 if (has_mbyte)
17512 (*mb_char2bytes)(cchar, str);
17513 else
17514# endif
17515 str[0] = cchar;
17516 }
17517 }
17518 }
17519
17520 list_append_number(rettv->vval.v_list,
17521 (syntax_flags & HL_CONCEAL) != 0);
17522 /* -1 to auto-determine strlen */
17523 list_append_string(rettv->vval.v_list, str, -1);
17524 list_append_number(rettv->vval.v_list, matchid);
17525 }
17526#endif
17527}
17528
17529/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017530 * "synstack(lnum, col)" function
17531 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017532 static void
17533f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017534 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017535 typval_T *rettv;
17536{
17537#ifdef FEAT_SYN_HL
17538 long lnum;
17539 long col;
17540 int i;
17541 int id;
17542#endif
17543
17544 rettv->v_type = VAR_LIST;
17545 rettv->vval.v_list = NULL;
17546
17547#ifdef FEAT_SYN_HL
17548 lnum = get_tv_lnum(argvars); /* -1 on type error */
17549 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17550
17551 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017552 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017553 && rettv_list_alloc(rettv) != FAIL)
17554 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017555 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017556 for (i = 0; ; ++i)
17557 {
17558 id = syn_get_stack_item(i);
17559 if (id < 0)
17560 break;
17561 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17562 break;
17563 }
17564 }
17565#endif
17566}
17567
17568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 * "system()" function
17570 */
17571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017572f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017573 typval_T *argvars;
17574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017576 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017578 char_u *infile = NULL;
17579 char_u buf[NUMBUFLEN];
17580 int err = FALSE;
17581 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017583 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017584 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017585
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017586 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017587 {
17588 /*
17589 * Write the string to a temp file, to be used for input of the shell
17590 * command.
17591 */
17592 if ((infile = vim_tempname('i')) == NULL)
17593 {
17594 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017595 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017596 }
17597
17598 fd = mch_fopen((char *)infile, WRITEBIN);
17599 if (fd == NULL)
17600 {
17601 EMSG2(_(e_notopen), infile);
17602 goto done;
17603 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017604 p = get_tv_string_buf_chk(&argvars[1], buf);
17605 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017606 {
17607 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017608 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017609 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017610 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17611 err = TRUE;
17612 if (fclose(fd) != 0)
17613 err = TRUE;
17614 if (err)
17615 {
17616 EMSG(_("E677: Error writing temp file"));
17617 goto done;
17618 }
17619 }
17620
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017621 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17622 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017623
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624#ifdef USE_CR
17625 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017626 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 {
17628 char_u *s;
17629
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017630 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 {
17632 if (*s == CAR)
17633 *s = NL;
17634 }
17635 }
17636#else
17637# ifdef USE_CRNL
17638 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017639 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 {
17641 char_u *s, *d;
17642
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017643 d = res;
17644 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 {
17646 if (s[0] == CAR && s[1] == NL)
17647 ++s;
17648 *d++ = *s;
17649 }
17650 *d = NUL;
17651 }
17652# endif
17653#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017654
17655done:
17656 if (infile != NULL)
17657 {
17658 mch_remove(infile);
17659 vim_free(infile);
17660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017661 rettv->v_type = VAR_STRING;
17662 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663}
17664
17665/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017666 * "tabpagebuflist()" function
17667 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017668 static void
17669f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017670 typval_T *argvars UNUSED;
17671 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017672{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017673#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017674 tabpage_T *tp;
17675 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017676
17677 if (argvars[0].v_type == VAR_UNKNOWN)
17678 wp = firstwin;
17679 else
17680 {
17681 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17682 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017683 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017684 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017685 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017686 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017687 for (; wp != NULL; wp = wp->w_next)
17688 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017689 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017690 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017691 }
17692#endif
17693}
17694
17695
17696/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017697 * "tabpagenr()" function
17698 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017699 static void
17700f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017701 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017702 typval_T *rettv;
17703{
17704 int nr = 1;
17705#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017706 char_u *arg;
17707
17708 if (argvars[0].v_type != VAR_UNKNOWN)
17709 {
17710 arg = get_tv_string_chk(&argvars[0]);
17711 nr = 0;
17712 if (arg != NULL)
17713 {
17714 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017715 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017716 else
17717 EMSG2(_(e_invexpr2), arg);
17718 }
17719 }
17720 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017721 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017722#endif
17723 rettv->vval.v_number = nr;
17724}
17725
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017726
17727#ifdef FEAT_WINDOWS
17728static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17729
17730/*
17731 * Common code for tabpagewinnr() and winnr().
17732 */
17733 static int
17734get_winnr(tp, argvar)
17735 tabpage_T *tp;
17736 typval_T *argvar;
17737{
17738 win_T *twin;
17739 int nr = 1;
17740 win_T *wp;
17741 char_u *arg;
17742
17743 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17744 if (argvar->v_type != VAR_UNKNOWN)
17745 {
17746 arg = get_tv_string_chk(argvar);
17747 if (arg == NULL)
17748 nr = 0; /* type error; errmsg already given */
17749 else if (STRCMP(arg, "$") == 0)
17750 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17751 else if (STRCMP(arg, "#") == 0)
17752 {
17753 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17754 if (twin == NULL)
17755 nr = 0;
17756 }
17757 else
17758 {
17759 EMSG2(_(e_invexpr2), arg);
17760 nr = 0;
17761 }
17762 }
17763
17764 if (nr > 0)
17765 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17766 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017767 {
17768 if (wp == NULL)
17769 {
17770 /* didn't find it in this tabpage */
17771 nr = 0;
17772 break;
17773 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017774 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017775 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017776 return nr;
17777}
17778#endif
17779
17780/*
17781 * "tabpagewinnr()" function
17782 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017783 static void
17784f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017785 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017786 typval_T *rettv;
17787{
17788 int nr = 1;
17789#ifdef FEAT_WINDOWS
17790 tabpage_T *tp;
17791
17792 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17793 if (tp == NULL)
17794 nr = 0;
17795 else
17796 nr = get_winnr(tp, &argvars[1]);
17797#endif
17798 rettv->vval.v_number = nr;
17799}
17800
17801
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017802/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017803 * "tagfiles()" function
17804 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017805 static void
17806f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017807 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017808 typval_T *rettv;
17809{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017810 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017811 tagname_T tn;
17812 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017813
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017814 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017815 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017816 fname = alloc(MAXPATHL);
17817 if (fname == NULL)
17818 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017819
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017820 for (first = TRUE; ; first = FALSE)
17821 if (get_tagfname(&tn, first, fname) == FAIL
17822 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017823 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017824 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017825 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017826}
17827
17828/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017829 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017830 */
17831 static void
17832f_taglist(argvars, rettv)
17833 typval_T *argvars;
17834 typval_T *rettv;
17835{
17836 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017837
17838 tag_pattern = get_tv_string(&argvars[0]);
17839
17840 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017841 if (*tag_pattern == NUL)
17842 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017843
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017844 if (rettv_list_alloc(rettv) == OK)
17845 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017846}
17847
17848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 * "tempname()" function
17850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017852f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017853 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855{
17856 static int x = 'A';
17857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017858 rettv->v_type = VAR_STRING;
17859 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860
17861 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17862 * names. Skip 'I' and 'O', they are used for shell redirection. */
17863 do
17864 {
17865 if (x == 'Z')
17866 x = '0';
17867 else if (x == '9')
17868 x = 'A';
17869 else
17870 {
17871#ifdef EBCDIC
17872 if (x == 'I')
17873 x = 'J';
17874 else if (x == 'R')
17875 x = 'S';
17876 else
17877#endif
17878 ++x;
17879 }
17880 } while (x == 'I' || x == 'O');
17881}
17882
17883/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017884 * "test(list)" function: Just checking the walls...
17885 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017886 static void
17887f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017888 typval_T *argvars UNUSED;
17889 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017890{
17891 /* Used for unit testing. Change the code below to your liking. */
17892#if 0
17893 listitem_T *li;
17894 list_T *l;
17895 char_u *bad, *good;
17896
17897 if (argvars[0].v_type != VAR_LIST)
17898 return;
17899 l = argvars[0].vval.v_list;
17900 if (l == NULL)
17901 return;
17902 li = l->lv_first;
17903 if (li == NULL)
17904 return;
17905 bad = get_tv_string(&li->li_tv);
17906 li = li->li_next;
17907 if (li == NULL)
17908 return;
17909 good = get_tv_string(&li->li_tv);
17910 rettv->vval.v_number = test_edit_score(bad, good);
17911#endif
17912}
17913
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017914#ifdef FEAT_FLOAT
17915/*
17916 * "tan()" function
17917 */
17918 static void
17919f_tan(argvars, rettv)
17920 typval_T *argvars;
17921 typval_T *rettv;
17922{
17923 float_T f;
17924
17925 rettv->v_type = VAR_FLOAT;
17926 if (get_float_arg(argvars, &f) == OK)
17927 rettv->vval.v_float = tan(f);
17928 else
17929 rettv->vval.v_float = 0.0;
17930}
17931
17932/*
17933 * "tanh()" function
17934 */
17935 static void
17936f_tanh(argvars, rettv)
17937 typval_T *argvars;
17938 typval_T *rettv;
17939{
17940 float_T f;
17941
17942 rettv->v_type = VAR_FLOAT;
17943 if (get_float_arg(argvars, &f) == OK)
17944 rettv->vval.v_float = tanh(f);
17945 else
17946 rettv->vval.v_float = 0.0;
17947}
17948#endif
17949
Bram Moolenaard52d9742005-08-21 22:20:28 +000017950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 * "tolower(string)" function
17952 */
17953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017954f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017955 typval_T *argvars;
17956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957{
17958 char_u *p;
17959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017960 p = vim_strsave(get_tv_string(&argvars[0]));
17961 rettv->v_type = VAR_STRING;
17962 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963
17964 if (p != NULL)
17965 while (*p != NUL)
17966 {
17967#ifdef FEAT_MBYTE
17968 int l;
17969
17970 if (enc_utf8)
17971 {
17972 int c, lc;
17973
17974 c = utf_ptr2char(p);
17975 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017976 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977 /* TODO: reallocate string when byte count changes. */
17978 if (utf_char2len(lc) == l)
17979 utf_char2bytes(lc, p);
17980 p += l;
17981 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017982 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 p += l; /* skip multi-byte character */
17984 else
17985#endif
17986 {
17987 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17988 ++p;
17989 }
17990 }
17991}
17992
17993/*
17994 * "toupper(string)" function
17995 */
17996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017997f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017998 typval_T *argvars;
17999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018001 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018002 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003}
18004
18005/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018006 * "tr(string, fromstr, tostr)" function
18007 */
18008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018009f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018010 typval_T *argvars;
18011 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018012{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018013 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018014 char_u *fromstr;
18015 char_u *tostr;
18016 char_u *p;
18017#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018018 int inlen;
18019 int fromlen;
18020 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018021 int idx;
18022 char_u *cpstr;
18023 int cplen;
18024 int first = TRUE;
18025#endif
18026 char_u buf[NUMBUFLEN];
18027 char_u buf2[NUMBUFLEN];
18028 garray_T ga;
18029
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018030 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018031 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18032 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018033
18034 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018035 rettv->v_type = VAR_STRING;
18036 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018037 if (fromstr == NULL || tostr == NULL)
18038 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018039 ga_init2(&ga, (int)sizeof(char), 80);
18040
18041#ifdef FEAT_MBYTE
18042 if (!has_mbyte)
18043#endif
18044 /* not multi-byte: fromstr and tostr must be the same length */
18045 if (STRLEN(fromstr) != STRLEN(tostr))
18046 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018047#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018048error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018049#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018050 EMSG2(_(e_invarg2), fromstr);
18051 ga_clear(&ga);
18052 return;
18053 }
18054
18055 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018056 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018057 {
18058#ifdef FEAT_MBYTE
18059 if (has_mbyte)
18060 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018061 inlen = (*mb_ptr2len)(in_str);
18062 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018063 cplen = inlen;
18064 idx = 0;
18065 for (p = fromstr; *p != NUL; p += fromlen)
18066 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018067 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018068 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018069 {
18070 for (p = tostr; *p != NUL; p += tolen)
18071 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018072 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018073 if (idx-- == 0)
18074 {
18075 cplen = tolen;
18076 cpstr = p;
18077 break;
18078 }
18079 }
18080 if (*p == NUL) /* tostr is shorter than fromstr */
18081 goto error;
18082 break;
18083 }
18084 ++idx;
18085 }
18086
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018087 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018088 {
18089 /* Check that fromstr and tostr have the same number of
18090 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018091 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018092 first = FALSE;
18093 for (p = tostr; *p != NUL; p += tolen)
18094 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018095 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018096 --idx;
18097 }
18098 if (idx != 0)
18099 goto error;
18100 }
18101
18102 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018103 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018104 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018105
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018106 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018107 }
18108 else
18109#endif
18110 {
18111 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018112 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018113 if (p != NULL)
18114 ga_append(&ga, tostr[p - fromstr]);
18115 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018116 ga_append(&ga, *in_str);
18117 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018118 }
18119 }
18120
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018121 /* add a terminating NUL */
18122 ga_grow(&ga, 1);
18123 ga_append(&ga, NUL);
18124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018125 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018126}
18127
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018128#ifdef FEAT_FLOAT
18129/*
18130 * "trunc({float})" function
18131 */
18132 static void
18133f_trunc(argvars, rettv)
18134 typval_T *argvars;
18135 typval_T *rettv;
18136{
18137 float_T f;
18138
18139 rettv->v_type = VAR_FLOAT;
18140 if (get_float_arg(argvars, &f) == OK)
18141 /* trunc() is not in C90, use floor() or ceil() instead. */
18142 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18143 else
18144 rettv->vval.v_float = 0.0;
18145}
18146#endif
18147
Bram Moolenaar8299df92004-07-10 09:47:34 +000018148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 * "type(expr)" function
18150 */
18151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018152f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018153 typval_T *argvars;
18154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018156 int n;
18157
18158 switch (argvars[0].v_type)
18159 {
18160 case VAR_NUMBER: n = 0; break;
18161 case VAR_STRING: n = 1; break;
18162 case VAR_FUNC: n = 2; break;
18163 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018164 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018165#ifdef FEAT_FLOAT
18166 case VAR_FLOAT: n = 5; break;
18167#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018168 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18169 }
18170 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171}
18172
18173/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018174 * "undofile(name)" function
18175 */
18176 static void
18177f_undofile(argvars, rettv)
18178 typval_T *argvars;
18179 typval_T *rettv;
18180{
18181 rettv->v_type = VAR_STRING;
18182#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018183 {
18184 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
18185
18186 if (ffname != NULL)
18187 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18188 vim_free(ffname);
18189 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018190#else
18191 rettv->vval.v_string = NULL;
18192#endif
18193}
18194
18195/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018196 * "undotree()" function
18197 */
18198 static void
18199f_undotree(argvars, rettv)
18200 typval_T *argvars UNUSED;
18201 typval_T *rettv;
18202{
18203 if (rettv_dict_alloc(rettv) == OK)
18204 {
18205 dict_T *dict = rettv->vval.v_dict;
18206 list_T *list;
18207
Bram Moolenaar730cde92010-06-27 05:18:54 +020018208 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018209 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018210 dict_add_nr_str(dict, "save_last",
18211 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018212 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18213 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018214 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018215
18216 list = list_alloc();
18217 if (list != NULL)
18218 {
18219 u_eval_tree(curbuf->b_u_oldhead, list);
18220 dict_add_list(dict, "entries", list);
18221 }
18222 }
18223}
18224
18225/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018226 * "values(dict)" function
18227 */
18228 static void
18229f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018230 typval_T *argvars;
18231 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018232{
18233 dict_list(argvars, rettv, 1);
18234}
18235
18236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 * "virtcol(string)" function
18238 */
18239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018240f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018241 typval_T *argvars;
18242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243{
18244 colnr_T vcol = 0;
18245 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018246 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018248 fp = var2fpos(&argvars[0], FALSE, &fnum);
18249 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18250 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251 {
18252 getvvcol(curwin, fp, NULL, NULL, &vcol);
18253 ++vcol;
18254 }
18255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018256 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257}
18258
18259/*
18260 * "visualmode()" function
18261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018263f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018264 typval_T *argvars UNUSED;
18265 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266{
18267#ifdef FEAT_VISUAL
18268 char_u str[2];
18269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018270 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 str[0] = curbuf->b_visual_mode_eval;
18272 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018273 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274
18275 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018276 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018277 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278#endif
18279}
18280
18281/*
18282 * "winbufnr(nr)" function
18283 */
18284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018285f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018286 typval_T *argvars;
18287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288{
18289 win_T *wp;
18290
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018291 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018293 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018295 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296}
18297
18298/*
18299 * "wincol()" function
18300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018302f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018303 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305{
18306 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018307 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308}
18309
18310/*
18311 * "winheight(nr)" function
18312 */
18313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018314f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018315 typval_T *argvars;
18316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317{
18318 win_T *wp;
18319
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018320 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018322 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018324 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325}
18326
18327/*
18328 * "winline()" function
18329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018331f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018332 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334{
18335 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018336 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337}
18338
18339/*
18340 * "winnr()" function
18341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018343f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018344 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346{
18347 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018348
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018350 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018352 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353}
18354
18355/*
18356 * "winrestcmd()" function
18357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018359f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018360 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362{
18363#ifdef FEAT_WINDOWS
18364 win_T *wp;
18365 int winnr = 1;
18366 garray_T ga;
18367 char_u buf[50];
18368
18369 ga_init2(&ga, (int)sizeof(char), 70);
18370 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18371 {
18372 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18373 ga_concat(&ga, buf);
18374# ifdef FEAT_VERTSPLIT
18375 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18376 ga_concat(&ga, buf);
18377# endif
18378 ++winnr;
18379 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018380 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018382 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018384 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018386 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387}
18388
18389/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018390 * "winrestview()" function
18391 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018392 static void
18393f_winrestview(argvars, rettv)
18394 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018395 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018396{
18397 dict_T *dict;
18398
18399 if (argvars[0].v_type != VAR_DICT
18400 || (dict = argvars[0].vval.v_dict) == NULL)
18401 EMSG(_(e_invarg));
18402 else
18403 {
18404 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18405 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18406#ifdef FEAT_VIRTUALEDIT
18407 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18408#endif
18409 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018410 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018411
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018412 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018413#ifdef FEAT_DIFF
18414 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18415#endif
18416 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18417 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18418
18419 check_cursor();
18420 changed_cline_bef_curs();
18421 invalidate_botline();
18422 redraw_later(VALID);
18423
18424 if (curwin->w_topline == 0)
18425 curwin->w_topline = 1;
18426 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18427 curwin->w_topline = curbuf->b_ml.ml_line_count;
18428#ifdef FEAT_DIFF
18429 check_topfill(curwin, TRUE);
18430#endif
18431 }
18432}
18433
18434/*
18435 * "winsaveview()" function
18436 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018437 static void
18438f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018439 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018440 typval_T *rettv;
18441{
18442 dict_T *dict;
18443
Bram Moolenaara800b422010-06-27 01:15:55 +020018444 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018445 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018446 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018447
18448 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18449 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18450#ifdef FEAT_VIRTUALEDIT
18451 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18452#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018453 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018454 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18455
18456 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18457#ifdef FEAT_DIFF
18458 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18459#endif
18460 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18461 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18462}
18463
18464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465 * "winwidth(nr)" function
18466 */
18467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018468f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018469 typval_T *argvars;
18470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471{
18472 win_T *wp;
18473
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018474 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018476 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 else
18478#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018479 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018481 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482#endif
18483}
18484
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018486 * "writefile()" function
18487 */
18488 static void
18489f_writefile(argvars, rettv)
18490 typval_T *argvars;
18491 typval_T *rettv;
18492{
18493 int binary = FALSE;
18494 char_u *fname;
18495 FILE *fd;
18496 listitem_T *li;
18497 char_u *s;
18498 int ret = 0;
18499 int c;
18500
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018501 if (check_restricted() || check_secure())
18502 return;
18503
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018504 if (argvars[0].v_type != VAR_LIST)
18505 {
18506 EMSG2(_(e_listarg), "writefile()");
18507 return;
18508 }
18509 if (argvars[0].vval.v_list == NULL)
18510 return;
18511
18512 if (argvars[2].v_type != VAR_UNKNOWN
18513 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18514 binary = TRUE;
18515
18516 /* Always open the file in binary mode, library functions have a mind of
18517 * their own about CR-LF conversion. */
18518 fname = get_tv_string(&argvars[1]);
18519 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18520 {
18521 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18522 ret = -1;
18523 }
18524 else
18525 {
18526 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18527 li = li->li_next)
18528 {
18529 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18530 {
18531 if (*s == '\n')
18532 c = putc(NUL, fd);
18533 else
18534 c = putc(*s, fd);
18535 if (c == EOF)
18536 {
18537 ret = -1;
18538 break;
18539 }
18540 }
18541 if (!binary || li->li_next != NULL)
18542 if (putc('\n', fd) == EOF)
18543 {
18544 ret = -1;
18545 break;
18546 }
18547 if (ret < 0)
18548 {
18549 EMSG(_(e_write));
18550 break;
18551 }
18552 }
18553 fclose(fd);
18554 }
18555
18556 rettv->vval.v_number = ret;
18557}
18558
18559/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018560 * "xor(expr, expr)" function
18561 */
18562 static void
18563f_xor(argvars, rettv)
18564 typval_T *argvars;
18565 typval_T *rettv;
18566{
18567 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18568 ^ get_tv_number_chk(&argvars[1], NULL);
18569}
18570
18571
18572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018574 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 */
18576 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018577var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018578 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018579 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018580 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018582 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018584 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585
Bram Moolenaara5525202006-03-02 22:52:09 +000018586 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018587 if (varp->v_type == VAR_LIST)
18588 {
18589 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018590 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018591 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018592 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018593
18594 l = varp->vval.v_list;
18595 if (l == NULL)
18596 return NULL;
18597
18598 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018599 pos.lnum = list_find_nr(l, 0L, &error);
18600 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018601 return NULL; /* invalid line number */
18602
18603 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018604 pos.col = list_find_nr(l, 1L, &error);
18605 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018606 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018607 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018608
18609 /* We accept "$" for the column number: last column. */
18610 li = list_find(l, 1L);
18611 if (li != NULL && li->li_tv.v_type == VAR_STRING
18612 && li->li_tv.vval.v_string != NULL
18613 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18614 pos.col = len + 1;
18615
Bram Moolenaara5525202006-03-02 22:52:09 +000018616 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018617 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018618 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018619 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018620
Bram Moolenaara5525202006-03-02 22:52:09 +000018621#ifdef FEAT_VIRTUALEDIT
18622 /* Get the virtual offset. Defaults to zero. */
18623 pos.coladd = list_find_nr(l, 2L, &error);
18624 if (error)
18625 pos.coladd = 0;
18626#endif
18627
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018628 return &pos;
18629 }
18630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018631 name = get_tv_string_chk(varp);
18632 if (name == NULL)
18633 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018634 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018636#ifdef FEAT_VISUAL
18637 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18638 {
18639 if (VIsual_active)
18640 return &VIsual;
18641 return &curwin->w_cursor;
18642 }
18643#endif
18644 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018646 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18648 return NULL;
18649 return pp;
18650 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018651
18652#ifdef FEAT_VIRTUALEDIT
18653 pos.coladd = 0;
18654#endif
18655
Bram Moolenaar477933c2007-07-17 14:32:23 +000018656 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018657 {
18658 pos.col = 0;
18659 if (name[1] == '0') /* "w0": first visible line */
18660 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018661 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018662 pos.lnum = curwin->w_topline;
18663 return &pos;
18664 }
18665 else if (name[1] == '$') /* "w$": last visible line */
18666 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018667 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018668 pos.lnum = curwin->w_botline - 1;
18669 return &pos;
18670 }
18671 }
18672 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018674 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 {
18676 pos.lnum = curbuf->b_ml.ml_line_count;
18677 pos.col = 0;
18678 }
18679 else
18680 {
18681 pos.lnum = curwin->w_cursor.lnum;
18682 pos.col = (colnr_T)STRLEN(ml_get_curline());
18683 }
18684 return &pos;
18685 }
18686 return NULL;
18687}
18688
18689/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018690 * Convert list in "arg" into a position and optional file number.
18691 * When "fnump" is NULL there is no file number, only 3 items.
18692 * Note that the column is passed on as-is, the caller may want to decrement
18693 * it to use 1 for the first column.
18694 * Return FAIL when conversion is not possible, doesn't check the position for
18695 * validity.
18696 */
18697 static int
18698list2fpos(arg, posp, fnump)
18699 typval_T *arg;
18700 pos_T *posp;
18701 int *fnump;
18702{
18703 list_T *l = arg->vval.v_list;
18704 long i = 0;
18705 long n;
18706
Bram Moolenaarbde35262006-07-23 20:12:24 +000018707 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18708 * when "fnump" isn't NULL and "coladd" is optional. */
18709 if (arg->v_type != VAR_LIST
18710 || l == NULL
18711 || l->lv_len < (fnump == NULL ? 2 : 3)
18712 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018713 return FAIL;
18714
18715 if (fnump != NULL)
18716 {
18717 n = list_find_nr(l, i++, NULL); /* fnum */
18718 if (n < 0)
18719 return FAIL;
18720 if (n == 0)
18721 n = curbuf->b_fnum; /* current buffer */
18722 *fnump = n;
18723 }
18724
18725 n = list_find_nr(l, i++, NULL); /* lnum */
18726 if (n < 0)
18727 return FAIL;
18728 posp->lnum = n;
18729
18730 n = list_find_nr(l, i++, NULL); /* col */
18731 if (n < 0)
18732 return FAIL;
18733 posp->col = n;
18734
18735#ifdef FEAT_VIRTUALEDIT
18736 n = list_find_nr(l, i, NULL);
18737 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018738 posp->coladd = 0;
18739 else
18740 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018741#endif
18742
18743 return OK;
18744}
18745
18746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 * Get the length of an environment variable name.
18748 * Advance "arg" to the first character after the name.
18749 * Return 0 for error.
18750 */
18751 static int
18752get_env_len(arg)
18753 char_u **arg;
18754{
18755 char_u *p;
18756 int len;
18757
18758 for (p = *arg; vim_isIDc(*p); ++p)
18759 ;
18760 if (p == *arg) /* no name found */
18761 return 0;
18762
18763 len = (int)(p - *arg);
18764 *arg = p;
18765 return len;
18766}
18767
18768/*
18769 * Get the length of the name of a function or internal variable.
18770 * "arg" is advanced to the first non-white character after the name.
18771 * Return 0 if something is wrong.
18772 */
18773 static int
18774get_id_len(arg)
18775 char_u **arg;
18776{
18777 char_u *p;
18778 int len;
18779
18780 /* Find the end of the name. */
18781 for (p = *arg; eval_isnamec(*p); ++p)
18782 ;
18783 if (p == *arg) /* no name found */
18784 return 0;
18785
18786 len = (int)(p - *arg);
18787 *arg = skipwhite(p);
18788
18789 return len;
18790}
18791
18792/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018793 * Get the length of the name of a variable or function.
18794 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018796 * Return -1 if curly braces expansion failed.
18797 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 * If the name contains 'magic' {}'s, expand them and return the
18799 * expanded name in an allocated string via 'alias' - caller must free.
18800 */
18801 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018802get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 char_u **arg;
18804 char_u **alias;
18805 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018806 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807{
18808 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 char_u *p;
18810 char_u *expr_start;
18811 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812
18813 *alias = NULL; /* default to no alias */
18814
18815 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18816 && (*arg)[2] == (int)KE_SNR)
18817 {
18818 /* hard coded <SNR>, already translated */
18819 *arg += 3;
18820 return get_id_len(arg) + 3;
18821 }
18822 len = eval_fname_script(*arg);
18823 if (len > 0)
18824 {
18825 /* literal "<SID>", "s:" or "<SNR>" */
18826 *arg += len;
18827 }
18828
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018830 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018832 p = find_name_end(*arg, &expr_start, &expr_end,
18833 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834 if (expr_start != NULL)
18835 {
18836 char_u *temp_string;
18837
18838 if (!evaluate)
18839 {
18840 len += (int)(p - *arg);
18841 *arg = skipwhite(p);
18842 return len;
18843 }
18844
18845 /*
18846 * Include any <SID> etc in the expanded string:
18847 * Thus the -len here.
18848 */
18849 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18850 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018851 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 *alias = temp_string;
18853 *arg = skipwhite(p);
18854 return (int)STRLEN(temp_string);
18855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856
18857 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018858 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 EMSG2(_(e_invexpr2), *arg);
18860
18861 return len;
18862}
18863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018864/*
18865 * Find the end of a variable or function name, taking care of magic braces.
18866 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18867 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018868 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018869 * Return a pointer to just after the name. Equal to "arg" if there is no
18870 * valid name.
18871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018873find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874 char_u *arg;
18875 char_u **expr_start;
18876 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018877 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018879 int mb_nest = 0;
18880 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 char_u *p;
18882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018883 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018885 *expr_start = NULL;
18886 *expr_end = NULL;
18887 }
18888
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018889 /* Quick check for valid starting character. */
18890 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18891 return arg;
18892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018893 for (p = arg; *p != NUL
18894 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018895 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018896 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018897 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018898 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018899 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018900 if (*p == '\'')
18901 {
18902 /* skip over 'string' to avoid counting [ and ] inside it. */
18903 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18904 ;
18905 if (*p == NUL)
18906 break;
18907 }
18908 else if (*p == '"')
18909 {
18910 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18911 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18912 if (*p == '\\' && p[1] != NUL)
18913 ++p;
18914 if (*p == NUL)
18915 break;
18916 }
18917
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018918 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018920 if (*p == '[')
18921 ++br_nest;
18922 else if (*p == ']')
18923 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018926 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018928 if (*p == '{')
18929 {
18930 mb_nest++;
18931 if (expr_start != NULL && *expr_start == NULL)
18932 *expr_start = p;
18933 }
18934 else if (*p == '}')
18935 {
18936 mb_nest--;
18937 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18938 *expr_end = p;
18939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 }
18942
18943 return p;
18944}
18945
18946/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018947 * Expands out the 'magic' {}'s in a variable/function name.
18948 * Note that this can call itself recursively, to deal with
18949 * constructs like foo{bar}{baz}{bam}
18950 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18951 * "in_start" ^
18952 * "expr_start" ^
18953 * "expr_end" ^
18954 * "in_end" ^
18955 *
18956 * Returns a new allocated string, which the caller must free.
18957 * Returns NULL for failure.
18958 */
18959 static char_u *
18960make_expanded_name(in_start, expr_start, expr_end, in_end)
18961 char_u *in_start;
18962 char_u *expr_start;
18963 char_u *expr_end;
18964 char_u *in_end;
18965{
18966 char_u c1;
18967 char_u *retval = NULL;
18968 char_u *temp_result;
18969 char_u *nextcmd = NULL;
18970
18971 if (expr_end == NULL || in_end == NULL)
18972 return NULL;
18973 *expr_start = NUL;
18974 *expr_end = NUL;
18975 c1 = *in_end;
18976 *in_end = NUL;
18977
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018978 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018979 if (temp_result != NULL && nextcmd == NULL)
18980 {
18981 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18982 + (in_end - expr_end) + 1));
18983 if (retval != NULL)
18984 {
18985 STRCPY(retval, in_start);
18986 STRCAT(retval, temp_result);
18987 STRCAT(retval, expr_end + 1);
18988 }
18989 }
18990 vim_free(temp_result);
18991
18992 *in_end = c1; /* put char back for error messages */
18993 *expr_start = '{';
18994 *expr_end = '}';
18995
18996 if (retval != NULL)
18997 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018998 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018999 if (expr_start != NULL)
19000 {
19001 /* Further expansion! */
19002 temp_result = make_expanded_name(retval, expr_start,
19003 expr_end, temp_result);
19004 vim_free(retval);
19005 retval = temp_result;
19006 }
19007 }
19008
19009 return retval;
19010}
19011
19012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019014 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 */
19016 static int
19017eval_isnamec(c)
19018 int c;
19019{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019020 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19021}
19022
19023/*
19024 * Return TRUE if character "c" can be used as the first character in a
19025 * variable or function name (excluding '{' and '}').
19026 */
19027 static int
19028eval_isnamec1(c)
19029 int c;
19030{
19031 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032}
19033
19034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 * Set number v: variable to "val".
19036 */
19037 void
19038set_vim_var_nr(idx, val)
19039 int idx;
19040 long val;
19041{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019042 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043}
19044
19045/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019046 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 */
19048 long
19049get_vim_var_nr(idx)
19050 int idx;
19051{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019052 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053}
19054
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019055/*
19056 * Get string v: variable value. Uses a static buffer, can only be used once.
19057 */
19058 char_u *
19059get_vim_var_str(idx)
19060 int idx;
19061{
19062 return get_tv_string(&vimvars[idx].vv_tv);
19063}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019064
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019066 * Get List v: variable value. Caller must take care of reference count when
19067 * needed.
19068 */
19069 list_T *
19070get_vim_var_list(idx)
19071 int idx;
19072{
19073 return vimvars[idx].vv_list;
19074}
19075
19076/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019077 * Set v:char to character "c".
19078 */
19079 void
19080set_vim_var_char(c)
19081 int c;
19082{
19083#ifdef FEAT_MBYTE
19084 char_u buf[MB_MAXBYTES];
19085#else
19086 char_u buf[2];
19087#endif
19088
19089#ifdef FEAT_MBYTE
19090 if (has_mbyte)
19091 buf[(*mb_char2bytes)(c, buf)] = NUL;
19092 else
19093#endif
19094 {
19095 buf[0] = c;
19096 buf[1] = NUL;
19097 }
19098 set_vim_var_string(VV_CHAR, buf, -1);
19099}
19100
19101/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019102 * Set v:count to "count" and v:count1 to "count1".
19103 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104 */
19105 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019106set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 long count;
19108 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019109 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019111 if (set_prevcount)
19112 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019113 vimvars[VV_COUNT].vv_nr = count;
19114 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115}
19116
19117/*
19118 * Set string v: variable to a copy of "val".
19119 */
19120 void
19121set_vim_var_string(idx, val, len)
19122 int idx;
19123 char_u *val;
19124 int len; /* length of "val" to use or -1 (whole string) */
19125{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019126 /* Need to do this (at least) once, since we can't initialize a union.
19127 * Will always be invoked when "v:progname" is set. */
19128 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19129
Bram Moolenaare9a41262005-01-15 22:18:47 +000019130 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019132 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019134 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019136 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137}
19138
19139/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019140 * Set List v: variable to "val".
19141 */
19142 void
19143set_vim_var_list(idx, val)
19144 int idx;
19145 list_T *val;
19146{
19147 list_unref(vimvars[idx].vv_list);
19148 vimvars[idx].vv_list = val;
19149 if (val != NULL)
19150 ++val->lv_refcount;
19151}
19152
19153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154 * Set v:register if needed.
19155 */
19156 void
19157set_reg_var(c)
19158 int c;
19159{
19160 char_u regname;
19161
19162 if (c == 0 || c == ' ')
19163 regname = '"';
19164 else
19165 regname = c;
19166 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019167 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 set_vim_var_string(VV_REG, &regname, 1);
19169}
19170
19171/*
19172 * Get or set v:exception. If "oldval" == NULL, return the current value.
19173 * Otherwise, restore the value to "oldval" and return NULL.
19174 * Must always be called in pairs to save and restore v:exception! Does not
19175 * take care of memory allocations.
19176 */
19177 char_u *
19178v_exception(oldval)
19179 char_u *oldval;
19180{
19181 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019182 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183
Bram Moolenaare9a41262005-01-15 22:18:47 +000019184 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185 return NULL;
19186}
19187
19188/*
19189 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19190 * Otherwise, restore the value to "oldval" and return NULL.
19191 * Must always be called in pairs to save and restore v:throwpoint! Does not
19192 * take care of memory allocations.
19193 */
19194 char_u *
19195v_throwpoint(oldval)
19196 char_u *oldval;
19197{
19198 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019199 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200
Bram Moolenaare9a41262005-01-15 22:18:47 +000019201 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 return NULL;
19203}
19204
19205#if defined(FEAT_AUTOCMD) || defined(PROTO)
19206/*
19207 * Set v:cmdarg.
19208 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19209 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19210 * Must always be called in pairs!
19211 */
19212 char_u *
19213set_cmdarg(eap, oldarg)
19214 exarg_T *eap;
19215 char_u *oldarg;
19216{
19217 char_u *oldval;
19218 char_u *newval;
19219 unsigned len;
19220
Bram Moolenaare9a41262005-01-15 22:18:47 +000019221 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019222 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019224 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019225 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019226 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 }
19228
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019229 if (eap->force_bin == FORCE_BIN)
19230 len = 6;
19231 else if (eap->force_bin == FORCE_NOBIN)
19232 len = 8;
19233 else
19234 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019235
19236 if (eap->read_edit)
19237 len += 7;
19238
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019239 if (eap->force_ff != 0)
19240 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19241# ifdef FEAT_MBYTE
19242 if (eap->force_enc != 0)
19243 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019244 if (eap->bad_char != 0)
19245 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019246# endif
19247
19248 newval = alloc(len + 1);
19249 if (newval == NULL)
19250 return NULL;
19251
19252 if (eap->force_bin == FORCE_BIN)
19253 sprintf((char *)newval, " ++bin");
19254 else if (eap->force_bin == FORCE_NOBIN)
19255 sprintf((char *)newval, " ++nobin");
19256 else
19257 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019258
19259 if (eap->read_edit)
19260 STRCAT(newval, " ++edit");
19261
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019262 if (eap->force_ff != 0)
19263 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19264 eap->cmd + eap->force_ff);
19265# ifdef FEAT_MBYTE
19266 if (eap->force_enc != 0)
19267 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19268 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019269 if (eap->bad_char == BAD_KEEP)
19270 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19271 else if (eap->bad_char == BAD_DROP)
19272 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19273 else if (eap->bad_char != 0)
19274 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019275# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019276 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019277 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278}
19279#endif
19280
19281/*
19282 * Get the value of internal variable "name".
19283 * Return OK or FAIL.
19284 */
19285 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019286get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 char_u *name;
19288 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019289 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019290 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291{
19292 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019293 typval_T *tv = NULL;
19294 typval_T atv;
19295 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019297
19298 /* truncate the name, so that we can use strcmp() */
19299 cc = name[len];
19300 name[len] = NUL;
19301
19302 /*
19303 * Check for "b:changedtick".
19304 */
19305 if (STRCMP(name, "b:changedtick") == 0)
19306 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019307 atv.v_type = VAR_NUMBER;
19308 atv.vval.v_number = curbuf->b_changedtick;
19309 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 }
19311
19312 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 * Check for user-defined variables.
19314 */
19315 else
19316 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019317 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019319 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320 }
19321
Bram Moolenaare9a41262005-01-15 22:18:47 +000019322 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019324 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019325 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 ret = FAIL;
19327 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019328 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019329 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330
19331 name[len] = cc;
19332
19333 return ret;
19334}
19335
19336/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019337 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19338 * Also handle function call with Funcref variable: func(expr)
19339 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19340 */
19341 static int
19342handle_subscript(arg, rettv, evaluate, verbose)
19343 char_u **arg;
19344 typval_T *rettv;
19345 int evaluate; /* do more than finding the end */
19346 int verbose; /* give error messages */
19347{
19348 int ret = OK;
19349 dict_T *selfdict = NULL;
19350 char_u *s;
19351 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019352 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019353
19354 while (ret == OK
19355 && (**arg == '['
19356 || (**arg == '.' && rettv->v_type == VAR_DICT)
19357 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19358 && !vim_iswhite(*(*arg - 1)))
19359 {
19360 if (**arg == '(')
19361 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019362 /* need to copy the funcref so that we can clear rettv */
19363 functv = *rettv;
19364 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019365
19366 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019367 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019368 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019369 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19370 &len, evaluate, selfdict);
19371
19372 /* Clear the funcref afterwards, so that deleting it while
19373 * evaluating the arguments is possible (see test55). */
19374 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019375
19376 /* Stop the expression evaluation when immediately aborting on
19377 * error, or when an interrupt occurred or an exception was thrown
19378 * but not caught. */
19379 if (aborting())
19380 {
19381 if (ret == OK)
19382 clear_tv(rettv);
19383 ret = FAIL;
19384 }
19385 dict_unref(selfdict);
19386 selfdict = NULL;
19387 }
19388 else /* **arg == '[' || **arg == '.' */
19389 {
19390 dict_unref(selfdict);
19391 if (rettv->v_type == VAR_DICT)
19392 {
19393 selfdict = rettv->vval.v_dict;
19394 if (selfdict != NULL)
19395 ++selfdict->dv_refcount;
19396 }
19397 else
19398 selfdict = NULL;
19399 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19400 {
19401 clear_tv(rettv);
19402 ret = FAIL;
19403 }
19404 }
19405 }
19406 dict_unref(selfdict);
19407 return ret;
19408}
19409
19410/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019411 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019412 * value).
19413 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019414 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019415alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019416{
Bram Moolenaar33570922005-01-25 22:26:29 +000019417 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019418}
19419
19420/*
19421 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 * The string "s" must have been allocated, it is consumed.
19423 * Return NULL for out of memory, the variable otherwise.
19424 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019425 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019426alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 char_u *s;
19428{
Bram Moolenaar33570922005-01-25 22:26:29 +000019429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019431 rettv = alloc_tv();
19432 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019434 rettv->v_type = VAR_STRING;
19435 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 }
19437 else
19438 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019439 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440}
19441
19442/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019443 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019445 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019446free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019447 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448{
19449 if (varp != NULL)
19450 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019451 switch (varp->v_type)
19452 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019453 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019454 func_unref(varp->vval.v_string);
19455 /*FALLTHROUGH*/
19456 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019457 vim_free(varp->vval.v_string);
19458 break;
19459 case VAR_LIST:
19460 list_unref(varp->vval.v_list);
19461 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019462 case VAR_DICT:
19463 dict_unref(varp->vval.v_dict);
19464 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019465 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019466#ifdef FEAT_FLOAT
19467 case VAR_FLOAT:
19468#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019469 case VAR_UNKNOWN:
19470 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019471 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019472 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019473 break;
19474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475 vim_free(varp);
19476 }
19477}
19478
19479/*
19480 * Free the memory for a variable value and set the value to NULL or 0.
19481 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019482 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019483clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019484 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485{
19486 if (varp != NULL)
19487 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019488 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019490 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019491 func_unref(varp->vval.v_string);
19492 /*FALLTHROUGH*/
19493 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019494 vim_free(varp->vval.v_string);
19495 varp->vval.v_string = NULL;
19496 break;
19497 case VAR_LIST:
19498 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019499 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019500 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019501 case VAR_DICT:
19502 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019503 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019504 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019505 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019506 varp->vval.v_number = 0;
19507 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019508#ifdef FEAT_FLOAT
19509 case VAR_FLOAT:
19510 varp->vval.v_float = 0.0;
19511 break;
19512#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019513 case VAR_UNKNOWN:
19514 break;
19515 default:
19516 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019518 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 }
19520}
19521
19522/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523 * Set the value of a variable to NULL without freeing items.
19524 */
19525 static void
19526init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019527 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019528{
19529 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019530 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019531}
19532
19533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 * Get the number value of a variable.
19535 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019536 * For incompatible types, return 0.
19537 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19538 * caller of incompatible types: it sets *denote to TRUE if "denote"
19539 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540 */
19541 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019542get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019543 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019545 int error = FALSE;
19546
19547 return get_tv_number_chk(varp, &error); /* return 0L on error */
19548}
19549
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019550 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019551get_tv_number_chk(varp, denote)
19552 typval_T *varp;
19553 int *denote;
19554{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019555 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019557 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019559 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019560 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019561#ifdef FEAT_FLOAT
19562 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019563 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019564 break;
19565#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019566 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019567 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019568 break;
19569 case VAR_STRING:
19570 if (varp->vval.v_string != NULL)
19571 vim_str2nr(varp->vval.v_string, NULL, NULL,
19572 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019573 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019574 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019575 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019576 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019577 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019578 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019579 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019580 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019581 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019582 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019584 if (denote == NULL) /* useful for values that must be unsigned */
19585 n = -1;
19586 else
19587 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019588 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589}
19590
19591/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019592 * Get the lnum from the first argument.
19593 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019594 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 */
19596 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019597get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019598 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599{
Bram Moolenaar33570922005-01-25 22:26:29 +000019600 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601 linenr_T lnum;
19602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019603 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 if (lnum == 0) /* no valid number, try using line() */
19605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019606 rettv.v_type = VAR_NUMBER;
19607 f_line(argvars, &rettv);
19608 lnum = rettv.vval.v_number;
19609 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610 }
19611 return lnum;
19612}
19613
19614/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019615 * Get the lnum from the first argument.
19616 * Also accepts "$", then "buf" is used.
19617 * Returns 0 on error.
19618 */
19619 static linenr_T
19620get_tv_lnum_buf(argvars, buf)
19621 typval_T *argvars;
19622 buf_T *buf;
19623{
19624 if (argvars[0].v_type == VAR_STRING
19625 && argvars[0].vval.v_string != NULL
19626 && argvars[0].vval.v_string[0] == '$'
19627 && buf != NULL)
19628 return buf->b_ml.ml_line_count;
19629 return get_tv_number_chk(&argvars[0], NULL);
19630}
19631
19632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 * Get the string value of a variable.
19634 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019635 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19636 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 * If the String variable has never been set, return an empty string.
19638 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019639 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19640 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641 */
19642 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019643get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019644 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645{
19646 static char_u mybuf[NUMBUFLEN];
19647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019648 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649}
19650
19651 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019652get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019653 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019654 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019656 char_u *res = get_tv_string_buf_chk(varp, buf);
19657
19658 return res != NULL ? res : (char_u *)"";
19659}
19660
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019661 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019662get_tv_string_chk(varp)
19663 typval_T *varp;
19664{
19665 static char_u mybuf[NUMBUFLEN];
19666
19667 return get_tv_string_buf_chk(varp, mybuf);
19668}
19669
19670 static char_u *
19671get_tv_string_buf_chk(varp, buf)
19672 typval_T *varp;
19673 char_u *buf;
19674{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019675 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019677 case VAR_NUMBER:
19678 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19679 return buf;
19680 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019681 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019682 break;
19683 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019684 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019685 break;
19686 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019687 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019688 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019689#ifdef FEAT_FLOAT
19690 case VAR_FLOAT:
19691 EMSG(_("E806: using Float as a String"));
19692 break;
19693#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019694 case VAR_STRING:
19695 if (varp->vval.v_string != NULL)
19696 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019697 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019698 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019699 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019700 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019702 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703}
19704
19705/*
19706 * Find variable "name" in the list of variables.
19707 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019708 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019709 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019710 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019712 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019713find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019715 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019718 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719
Bram Moolenaara7043832005-01-21 11:56:39 +000019720 ht = find_var_ht(name, &varname);
19721 if (htp != NULL)
19722 *htp = ht;
19723 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019725 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726}
19727
19728/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019729 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019730 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019732 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019733find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019734 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019735 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019736 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019737{
Bram Moolenaar33570922005-01-25 22:26:29 +000019738 hashitem_T *hi;
19739
19740 if (*varname == NUL)
19741 {
19742 /* Must be something like "s:", otherwise "ht" would be NULL. */
19743 switch (varname[-2])
19744 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019745 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019746 case 'g': return &globvars_var;
19747 case 'v': return &vimvars_var;
19748 case 'b': return &curbuf->b_bufvar;
19749 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019750#ifdef FEAT_WINDOWS
19751 case 't': return &curtab->tp_winvar;
19752#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019753 case 'l': return current_funccal == NULL
19754 ? NULL : &current_funccal->l_vars_var;
19755 case 'a': return current_funccal == NULL
19756 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019757 }
19758 return NULL;
19759 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019760
19761 hi = hash_find(ht, varname);
19762 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019763 {
19764 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019765 * worked find the variable again. Don't auto-load a script if it was
19766 * loaded already, otherwise it would be loaded every time when
19767 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019768 if (ht == &globvarht && !writing)
19769 {
19770 /* Note: script_autoload() may make "hi" invalid. It must either
19771 * be obtained again or not used. */
19772 if (!script_autoload(varname, FALSE) || aborting())
19773 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019774 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019775 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019776 if (HASHITEM_EMPTY(hi))
19777 return NULL;
19778 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019779 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019780}
19781
19782/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019783 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019784 * Set "varname" to the start of name without ':'.
19785 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019786 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019787find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 char_u *name;
19789 char_u **varname;
19790{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019791 hashitem_T *hi;
19792
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 if (name[1] != ':')
19794 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019795 /* The name must not start with a colon or #. */
19796 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 return NULL;
19798 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019799
19800 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019801 hi = hash_find(&compat_hashtab, name);
19802 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019803 return &compat_hashtab;
19804
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 return &globvarht; /* global variable */
19807 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 }
19809 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019810 if (*name == 'g') /* global variable */
19811 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019812 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19813 */
19814 if (vim_strchr(name + 2, ':') != NULL
19815 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019816 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019818 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019820 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019821#ifdef FEAT_WINDOWS
19822 if (*name == 't') /* tab page variable */
19823 return &curtab->tp_vars.dv_hashtab;
19824#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019825 if (*name == 'v') /* v: variable */
19826 return &vimvarht;
19827 if (*name == 'a' && current_funccal != NULL) /* function argument */
19828 return &current_funccal->l_avars.dv_hashtab;
19829 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19830 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 if (*name == 's' /* script variable */
19832 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19833 return &SCRIPT_VARS(current_SID);
19834 return NULL;
19835}
19836
19837/*
19838 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019839 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 * Returns NULL when it doesn't exist.
19841 */
19842 char_u *
19843get_var_value(name)
19844 char_u *name;
19845{
Bram Moolenaar33570922005-01-25 22:26:29 +000019846 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847
Bram Moolenaara7043832005-01-21 11:56:39 +000019848 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 if (v == NULL)
19850 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019851 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852}
19853
19854/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019855 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856 * sourcing this script and when executing functions defined in the script.
19857 */
19858 void
19859new_script_vars(id)
19860 scid_T id;
19861{
Bram Moolenaara7043832005-01-21 11:56:39 +000019862 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019863 hashtab_T *ht;
19864 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019865
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19867 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019868 /* Re-allocating ga_data means that an ht_array pointing to
19869 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019870 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019871 for (i = 1; i <= ga_scripts.ga_len; ++i)
19872 {
19873 ht = &SCRIPT_VARS(i);
19874 if (ht->ht_mask == HT_INIT_SIZE - 1)
19875 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019876 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019877 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019878 }
19879
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 while (ga_scripts.ga_len < id)
19881 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019882 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019883 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019884 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 }
19887 }
19888}
19889
19890/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019891 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19892 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893 */
19894 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019895init_var_dict(dict, dict_var)
19896 dict_T *dict;
19897 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898{
Bram Moolenaar33570922005-01-25 22:26:29 +000019899 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019900 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019901 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019902 dict_var->di_tv.vval.v_dict = dict;
19903 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019904 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019905 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19906 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907}
19908
19909/*
19910 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019911 * Frees all allocated variables and the value they contain.
19912 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 */
19914 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019915vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019916 hashtab_T *ht;
19917{
19918 vars_clear_ext(ht, TRUE);
19919}
19920
19921/*
19922 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19923 */
19924 static void
19925vars_clear_ext(ht, free_val)
19926 hashtab_T *ht;
19927 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928{
Bram Moolenaara7043832005-01-21 11:56:39 +000019929 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019930 hashitem_T *hi;
19931 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932
Bram Moolenaar33570922005-01-25 22:26:29 +000019933 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019934 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019935 for (hi = ht->ht_array; todo > 0; ++hi)
19936 {
19937 if (!HASHITEM_EMPTY(hi))
19938 {
19939 --todo;
19940
Bram Moolenaar33570922005-01-25 22:26:29 +000019941 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019942 * ht_array might change then. hash_clear() takes care of it
19943 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019944 v = HI2DI(hi);
19945 if (free_val)
19946 clear_tv(&v->di_tv);
19947 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19948 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019949 }
19950 }
19951 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019952 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953}
19954
Bram Moolenaara7043832005-01-21 11:56:39 +000019955/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019956 * Delete a variable from hashtab "ht" at item "hi".
19957 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019960delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019961 hashtab_T *ht;
19962 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963{
Bram Moolenaar33570922005-01-25 22:26:29 +000019964 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019965
19966 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019967 clear_tv(&di->di_tv);
19968 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969}
19970
19971/*
19972 * List the value of one internal variable.
19973 */
19974 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019975list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019976 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019978 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019980 char_u *tofree;
19981 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019982 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019983
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019984 current_copyID += COPYID_INC;
19985 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019986 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019987 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019988 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989}
19990
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019992list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 char_u *prefix;
19994 char_u *name;
19995 int type;
19996 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019997 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998{
Bram Moolenaar31859182007-08-14 20:41:13 +000019999 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20000 msg_start();
20001 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 if (name != NULL) /* "a:" vars don't have a name stored */
20003 msg_puts(name);
20004 msg_putchar(' ');
20005 msg_advance(22);
20006 if (type == VAR_NUMBER)
20007 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020008 else if (type == VAR_FUNC)
20009 msg_putchar('*');
20010 else if (type == VAR_LIST)
20011 {
20012 msg_putchar('[');
20013 if (*string == '[')
20014 ++string;
20015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020016 else if (type == VAR_DICT)
20017 {
20018 msg_putchar('{');
20019 if (*string == '{')
20020 ++string;
20021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 else
20023 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020024
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020026
20027 if (type == VAR_FUNC)
20028 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020029 if (*first)
20030 {
20031 msg_clr_eos();
20032 *first = FALSE;
20033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034}
20035
20036/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020037 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 * If the variable already exists, the value is updated.
20039 * Otherwise the variable is created.
20040 */
20041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020042set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020044 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020045 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046{
Bram Moolenaar33570922005-01-25 22:26:29 +000020047 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020049 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020051 ht = find_var_ht(name, &varname);
20052 if (ht == NULL || *varname == NUL)
20053 {
20054 EMSG2(_(e_illvar), name);
20055 return;
20056 }
20057 v = find_var_in_ht(ht, varname, TRUE);
20058
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020059 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20060 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020061
Bram Moolenaar33570922005-01-25 22:26:29 +000020062 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020064 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020065 if (var_check_ro(v->di_flags, name)
20066 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020067 return;
20068 if (v->di_tv.v_type != tv->v_type
20069 && !((v->di_tv.v_type == VAR_STRING
20070 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020071 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020072 || tv->v_type == VAR_NUMBER))
20073#ifdef FEAT_FLOAT
20074 && !((v->di_tv.v_type == VAR_NUMBER
20075 || v->di_tv.v_type == VAR_FLOAT)
20076 && (tv->v_type == VAR_NUMBER
20077 || tv->v_type == VAR_FLOAT))
20078#endif
20079 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020080 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020081 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020082 return;
20083 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020084
20085 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020086 * Handle setting internal v: variables separately: we don't change
20087 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020088 */
20089 if (ht == &vimvarht)
20090 {
20091 if (v->di_tv.v_type == VAR_STRING)
20092 {
20093 vim_free(v->di_tv.vval.v_string);
20094 if (copy || tv->v_type != VAR_STRING)
20095 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20096 else
20097 {
20098 /* Take over the string to avoid an extra alloc/free. */
20099 v->di_tv.vval.v_string = tv->vval.v_string;
20100 tv->vval.v_string = NULL;
20101 }
20102 }
20103 else if (v->di_tv.v_type != VAR_NUMBER)
20104 EMSG2(_(e_intern2), "set_var()");
20105 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020106 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020107 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020108 if (STRCMP(varname, "searchforward") == 0)
20109 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20110 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020111 return;
20112 }
20113
20114 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 }
20116 else /* add a new variable */
20117 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020118 /* Can't add "v:" variable. */
20119 if (ht == &vimvarht)
20120 {
20121 EMSG2(_(e_illvar), name);
20122 return;
20123 }
20124
Bram Moolenaar92124a32005-06-17 22:03:40 +000020125 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020126 if (!valid_varname(varname))
20127 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020128
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020129 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20130 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020131 if (v == NULL)
20132 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020133 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020134 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020136 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 return;
20138 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020139 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020141
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020142 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020143 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020144 else
20145 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020146 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020147 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020148 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150}
20151
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020152/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020153 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020154 * Also give an error message.
20155 */
20156 static int
20157var_check_ro(flags, name)
20158 int flags;
20159 char_u *name;
20160{
20161 if (flags & DI_FLAGS_RO)
20162 {
20163 EMSG2(_(e_readonlyvar), name);
20164 return TRUE;
20165 }
20166 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20167 {
20168 EMSG2(_(e_readonlysbx), name);
20169 return TRUE;
20170 }
20171 return FALSE;
20172}
20173
20174/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020175 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20176 * Also give an error message.
20177 */
20178 static int
20179var_check_fixed(flags, name)
20180 int flags;
20181 char_u *name;
20182{
20183 if (flags & DI_FLAGS_FIX)
20184 {
20185 EMSG2(_("E795: Cannot delete variable %s"), name);
20186 return TRUE;
20187 }
20188 return FALSE;
20189}
20190
20191/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020192 * Check if a funcref is assigned to a valid variable name.
20193 * Return TRUE and give an error if not.
20194 */
20195 static int
20196var_check_func_name(name, new_var)
20197 char_u *name; /* points to start of variable name */
20198 int new_var; /* TRUE when creating the variable */
20199{
20200 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20201 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20202 ? name[2] : name[0]))
20203 {
20204 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20205 name);
20206 return TRUE;
20207 }
20208 /* Don't allow hiding a function. When "v" is not NULL we might be
20209 * assigning another function to the same var, the type is checked
20210 * below. */
20211 if (new_var && function_exists(name))
20212 {
20213 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20214 name);
20215 return TRUE;
20216 }
20217 return FALSE;
20218}
20219
20220/*
20221 * Check if a variable name is valid.
20222 * Return FALSE and give an error if not.
20223 */
20224 static int
20225valid_varname(varname)
20226 char_u *varname;
20227{
20228 char_u *p;
20229
20230 for (p = varname; *p != NUL; ++p)
20231 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20232 && *p != AUTOLOAD_CHAR)
20233 {
20234 EMSG2(_(e_illvar), varname);
20235 return FALSE;
20236 }
20237 return TRUE;
20238}
20239
20240/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020241 * Return TRUE if typeval "tv" is set to be locked (immutable).
20242 * Also give an error message, using "name".
20243 */
20244 static int
20245tv_check_lock(lock, name)
20246 int lock;
20247 char_u *name;
20248{
20249 if (lock & VAR_LOCKED)
20250 {
20251 EMSG2(_("E741: Value is locked: %s"),
20252 name == NULL ? (char_u *)_("Unknown") : name);
20253 return TRUE;
20254 }
20255 if (lock & VAR_FIXED)
20256 {
20257 EMSG2(_("E742: Cannot change value of %s"),
20258 name == NULL ? (char_u *)_("Unknown") : name);
20259 return TRUE;
20260 }
20261 return FALSE;
20262}
20263
20264/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020266 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020267 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020268 * It is OK for "from" and "to" to point to the same item. This is used to
20269 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020270 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020271 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020272copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020273 typval_T *from;
20274 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020276 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020277 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020278 switch (from->v_type)
20279 {
20280 case VAR_NUMBER:
20281 to->vval.v_number = from->vval.v_number;
20282 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020283#ifdef FEAT_FLOAT
20284 case VAR_FLOAT:
20285 to->vval.v_float = from->vval.v_float;
20286 break;
20287#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020288 case VAR_STRING:
20289 case VAR_FUNC:
20290 if (from->vval.v_string == NULL)
20291 to->vval.v_string = NULL;
20292 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020293 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020294 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020295 if (from->v_type == VAR_FUNC)
20296 func_ref(to->vval.v_string);
20297 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020298 break;
20299 case VAR_LIST:
20300 if (from->vval.v_list == NULL)
20301 to->vval.v_list = NULL;
20302 else
20303 {
20304 to->vval.v_list = from->vval.v_list;
20305 ++to->vval.v_list->lv_refcount;
20306 }
20307 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020308 case VAR_DICT:
20309 if (from->vval.v_dict == NULL)
20310 to->vval.v_dict = NULL;
20311 else
20312 {
20313 to->vval.v_dict = from->vval.v_dict;
20314 ++to->vval.v_dict->dv_refcount;
20315 }
20316 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020317 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020318 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020319 break;
20320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321}
20322
20323/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020324 * Make a copy of an item.
20325 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020326 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20327 * reference to an already copied list/dict can be used.
20328 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020329 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020330 static int
20331item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020332 typval_T *from;
20333 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020334 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020335 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020336{
20337 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020338 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020339
Bram Moolenaar33570922005-01-25 22:26:29 +000020340 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020341 {
20342 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020343 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020344 }
20345 ++recurse;
20346
20347 switch (from->v_type)
20348 {
20349 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020350#ifdef FEAT_FLOAT
20351 case VAR_FLOAT:
20352#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020353 case VAR_STRING:
20354 case VAR_FUNC:
20355 copy_tv(from, to);
20356 break;
20357 case VAR_LIST:
20358 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020359 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020360 if (from->vval.v_list == NULL)
20361 to->vval.v_list = NULL;
20362 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20363 {
20364 /* use the copy made earlier */
20365 to->vval.v_list = from->vval.v_list->lv_copylist;
20366 ++to->vval.v_list->lv_refcount;
20367 }
20368 else
20369 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20370 if (to->vval.v_list == NULL)
20371 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020372 break;
20373 case VAR_DICT:
20374 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020375 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020376 if (from->vval.v_dict == NULL)
20377 to->vval.v_dict = NULL;
20378 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20379 {
20380 /* use the copy made earlier */
20381 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20382 ++to->vval.v_dict->dv_refcount;
20383 }
20384 else
20385 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20386 if (to->vval.v_dict == NULL)
20387 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020388 break;
20389 default:
20390 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020391 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020392 }
20393 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020394 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020395}
20396
20397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 * ":echo expr1 ..." print each argument separated with a space, add a
20399 * newline at the end.
20400 * ":echon expr1 ..." print each argument plain.
20401 */
20402 void
20403ex_echo(eap)
20404 exarg_T *eap;
20405{
20406 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020407 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020408 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 char_u *p;
20410 int needclr = TRUE;
20411 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020412 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413
20414 if (eap->skip)
20415 ++emsg_skip;
20416 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20417 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020418 /* If eval1() causes an error message the text from the command may
20419 * still need to be cleared. E.g., "echo 22,44". */
20420 need_clr_eos = needclr;
20421
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 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);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020432 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433 break;
20434 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020435 need_clr_eos = FALSE;
20436
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 if (!eap->skip)
20438 {
20439 if (atstart)
20440 {
20441 atstart = FALSE;
20442 /* Call msg_start() after eval1(), evaluating the expression
20443 * may cause a message to appear. */
20444 if (eap->cmdidx == CMD_echo)
20445 msg_start();
20446 }
20447 else if (eap->cmdidx == CMD_echo)
20448 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020449 current_copyID += COPYID_INC;
20450 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020451 if (p != NULL)
20452 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020454 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020456 if (*p != TAB && needclr)
20457 {
20458 /* remove any text still there from the command */
20459 msg_clr_eos();
20460 needclr = FALSE;
20461 }
20462 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 }
20464 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020465 {
20466#ifdef FEAT_MBYTE
20467 if (has_mbyte)
20468 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020469 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020470
20471 (void)msg_outtrans_len_attr(p, i, echo_attr);
20472 p += i - 1;
20473 }
20474 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020476 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020479 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020481 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 arg = skipwhite(arg);
20483 }
20484 eap->nextcmd = check_nextcmd(arg);
20485
20486 if (eap->skip)
20487 --emsg_skip;
20488 else
20489 {
20490 /* remove text that may still be there from the command */
20491 if (needclr)
20492 msg_clr_eos();
20493 if (eap->cmdidx == CMD_echo)
20494 msg_end();
20495 }
20496}
20497
20498/*
20499 * ":echohl {name}".
20500 */
20501 void
20502ex_echohl(eap)
20503 exarg_T *eap;
20504{
20505 int id;
20506
20507 id = syn_name2id(eap->arg);
20508 if (id == 0)
20509 echo_attr = 0;
20510 else
20511 echo_attr = syn_id2attr(id);
20512}
20513
20514/*
20515 * ":execute expr1 ..." execute the result of an expression.
20516 * ":echomsg expr1 ..." Print a message
20517 * ":echoerr expr1 ..." Print an error
20518 * Each gets spaces around each argument and a newline at the end for
20519 * echo commands
20520 */
20521 void
20522ex_execute(eap)
20523 exarg_T *eap;
20524{
20525 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020526 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 int ret = OK;
20528 char_u *p;
20529 garray_T ga;
20530 int len;
20531 int save_did_emsg;
20532
20533 ga_init2(&ga, 1, 80);
20534
20535 if (eap->skip)
20536 ++emsg_skip;
20537 while (*arg != NUL && *arg != '|' && *arg != '\n')
20538 {
20539 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020540 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541 {
20542 /*
20543 * Report the invalid expression unless the expression evaluation
20544 * has been cancelled due to an aborting error, an interrupt, or an
20545 * exception.
20546 */
20547 if (!aborting())
20548 EMSG2(_(e_invexpr2), p);
20549 ret = FAIL;
20550 break;
20551 }
20552
20553 if (!eap->skip)
20554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020555 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556 len = (int)STRLEN(p);
20557 if (ga_grow(&ga, len + 2) == FAIL)
20558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020559 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560 ret = FAIL;
20561 break;
20562 }
20563 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 ga.ga_len += len;
20567 }
20568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020569 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570 arg = skipwhite(arg);
20571 }
20572
20573 if (ret != FAIL && ga.ga_data != NULL)
20574 {
20575 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020578 out_flush();
20579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580 else if (eap->cmdidx == CMD_echoerr)
20581 {
20582 /* We don't want to abort following commands, restore did_emsg. */
20583 save_did_emsg = did_emsg;
20584 EMSG((char_u *)ga.ga_data);
20585 if (!force_abort)
20586 did_emsg = save_did_emsg;
20587 }
20588 else if (eap->cmdidx == CMD_execute)
20589 do_cmdline((char_u *)ga.ga_data,
20590 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20591 }
20592
20593 ga_clear(&ga);
20594
20595 if (eap->skip)
20596 --emsg_skip;
20597
20598 eap->nextcmd = check_nextcmd(arg);
20599}
20600
20601/*
20602 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20603 * "arg" points to the "&" or '+' when called, to "option" when returning.
20604 * Returns NULL when no option name found. Otherwise pointer to the char
20605 * after the option name.
20606 */
20607 static char_u *
20608find_option_end(arg, opt_flags)
20609 char_u **arg;
20610 int *opt_flags;
20611{
20612 char_u *p = *arg;
20613
20614 ++p;
20615 if (*p == 'g' && p[1] == ':')
20616 {
20617 *opt_flags = OPT_GLOBAL;
20618 p += 2;
20619 }
20620 else if (*p == 'l' && p[1] == ':')
20621 {
20622 *opt_flags = OPT_LOCAL;
20623 p += 2;
20624 }
20625 else
20626 *opt_flags = 0;
20627
20628 if (!ASCII_ISALPHA(*p))
20629 return NULL;
20630 *arg = p;
20631
20632 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20633 p += 4; /* termcap option */
20634 else
20635 while (ASCII_ISALPHA(*p))
20636 ++p;
20637 return p;
20638}
20639
20640/*
20641 * ":function"
20642 */
20643 void
20644ex_function(eap)
20645 exarg_T *eap;
20646{
20647 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020648 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 int j;
20650 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652 char_u *name = NULL;
20653 char_u *p;
20654 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020655 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656 garray_T newargs;
20657 garray_T newlines;
20658 int varargs = FALSE;
20659 int mustend = FALSE;
20660 int flags = 0;
20661 ufunc_T *fp;
20662 int indent;
20663 int nesting;
20664 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020665 dictitem_T *v;
20666 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020667 static int func_nr = 0; /* number for nameless function */
20668 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020669 hashtab_T *ht;
20670 int todo;
20671 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020672 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673
20674 /*
20675 * ":function" without argument: list functions.
20676 */
20677 if (ends_excmd(*eap->arg))
20678 {
20679 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020680 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020681 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020682 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020683 {
20684 if (!HASHITEM_EMPTY(hi))
20685 {
20686 --todo;
20687 fp = HI2UF(hi);
20688 if (!isdigit(*fp->uf_name))
20689 list_func_head(fp, FALSE);
20690 }
20691 }
20692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693 eap->nextcmd = check_nextcmd(eap->arg);
20694 return;
20695 }
20696
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020697 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020698 * ":function /pat": list functions matching pattern.
20699 */
20700 if (*eap->arg == '/')
20701 {
20702 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20703 if (!eap->skip)
20704 {
20705 regmatch_T regmatch;
20706
20707 c = *p;
20708 *p = NUL;
20709 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20710 *p = c;
20711 if (regmatch.regprog != NULL)
20712 {
20713 regmatch.rm_ic = p_ic;
20714
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020715 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020716 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20717 {
20718 if (!HASHITEM_EMPTY(hi))
20719 {
20720 --todo;
20721 fp = HI2UF(hi);
20722 if (!isdigit(*fp->uf_name)
20723 && vim_regexec(&regmatch, fp->uf_name, 0))
20724 list_func_head(fp, FALSE);
20725 }
20726 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020727 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020728 }
20729 }
20730 if (*p == '/')
20731 ++p;
20732 eap->nextcmd = check_nextcmd(p);
20733 return;
20734 }
20735
20736 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020737 * Get the function name. There are these situations:
20738 * func normal function name
20739 * "name" == func, "fudi.fd_dict" == NULL
20740 * dict.func new dictionary entry
20741 * "name" == NULL, "fudi.fd_dict" set,
20742 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20743 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020744 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020745 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20746 * dict.func existing dict entry that's not a Funcref
20747 * "name" == NULL, "fudi.fd_dict" set,
20748 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020751 name = trans_function_name(&p, eap->skip, 0, &fudi);
20752 paren = (vim_strchr(p, '(') != NULL);
20753 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 {
20755 /*
20756 * Return on an invalid expression in braces, unless the expression
20757 * evaluation has been cancelled due to an aborting error, an
20758 * interrupt, or an exception.
20759 */
20760 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020761 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020762 if (!eap->skip && fudi.fd_newkey != NULL)
20763 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020764 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767 else
20768 eap->skip = TRUE;
20769 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020770
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 /* An error in a function call during evaluation of an expression in magic
20772 * braces should not cause the function not to be defined. */
20773 saved_did_emsg = did_emsg;
20774 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775
20776 /*
20777 * ":function func" with only function name: list function.
20778 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020779 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 {
20781 if (!ends_excmd(*skipwhite(p)))
20782 {
20783 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020784 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 }
20786 eap->nextcmd = check_nextcmd(p);
20787 if (eap->nextcmd != NULL)
20788 *p = NUL;
20789 if (!eap->skip && !got_int)
20790 {
20791 fp = find_func(name);
20792 if (fp != NULL)
20793 {
20794 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020795 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020797 if (FUNCLINE(fp, j) == NULL)
20798 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799 msg_putchar('\n');
20800 msg_outnum((long)(j + 1));
20801 if (j < 9)
20802 msg_putchar(' ');
20803 if (j < 99)
20804 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020805 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 out_flush(); /* show a line at a time */
20807 ui_breakcheck();
20808 }
20809 if (!got_int)
20810 {
20811 msg_putchar('\n');
20812 msg_puts((char_u *)" endfunction");
20813 }
20814 }
20815 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020816 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020818 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819 }
20820
20821 /*
20822 * ":function name(arg1, arg2)" Define function.
20823 */
20824 p = skipwhite(p);
20825 if (*p != '(')
20826 {
20827 if (!eap->skip)
20828 {
20829 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020830 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 }
20832 /* attempt to continue by skipping some text */
20833 if (vim_strchr(p, '(') != NULL)
20834 p = vim_strchr(p, '(');
20835 }
20836 p = skipwhite(p + 1);
20837
20838 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20839 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20840
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020841 if (!eap->skip)
20842 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020843 /* Check the name of the function. Unless it's a dictionary function
20844 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020845 if (name != NULL)
20846 arg = name;
20847 else
20848 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020849 if (arg != NULL && (fudi.fd_di == NULL
20850 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020851 {
20852 if (*arg == K_SPECIAL)
20853 j = 3;
20854 else
20855 j = 0;
20856 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20857 : eval_isnamec(arg[j])))
20858 ++j;
20859 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020860 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020861 }
20862 }
20863
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 /*
20865 * Isolate the arguments: "arg1, arg2, ...)"
20866 */
20867 while (*p != ')')
20868 {
20869 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20870 {
20871 varargs = TRUE;
20872 p += 3;
20873 mustend = TRUE;
20874 }
20875 else
20876 {
20877 arg = p;
20878 while (ASCII_ISALNUM(*p) || *p == '_')
20879 ++p;
20880 if (arg == p || isdigit(*arg)
20881 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20882 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20883 {
20884 if (!eap->skip)
20885 EMSG2(_("E125: Illegal argument: %s"), arg);
20886 break;
20887 }
20888 if (ga_grow(&newargs, 1) == FAIL)
20889 goto erret;
20890 c = *p;
20891 *p = NUL;
20892 arg = vim_strsave(arg);
20893 if (arg == NULL)
20894 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020895
20896 /* Check for duplicate argument name. */
20897 for (i = 0; i < newargs.ga_len; ++i)
20898 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
20899 {
20900 EMSG2(_("E853: Duplicate argument name: %s"), arg);
20901 goto erret;
20902 }
20903
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20905 *p = c;
20906 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 if (*p == ',')
20908 ++p;
20909 else
20910 mustend = TRUE;
20911 }
20912 p = skipwhite(p);
20913 if (mustend && *p != ')')
20914 {
20915 if (!eap->skip)
20916 EMSG2(_(e_invarg2), eap->arg);
20917 break;
20918 }
20919 }
20920 ++p; /* skip the ')' */
20921
Bram Moolenaare9a41262005-01-15 22:18:47 +000020922 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923 for (;;)
20924 {
20925 p = skipwhite(p);
20926 if (STRNCMP(p, "range", 5) == 0)
20927 {
20928 flags |= FC_RANGE;
20929 p += 5;
20930 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020931 else if (STRNCMP(p, "dict", 4) == 0)
20932 {
20933 flags |= FC_DICT;
20934 p += 4;
20935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936 else if (STRNCMP(p, "abort", 5) == 0)
20937 {
20938 flags |= FC_ABORT;
20939 p += 5;
20940 }
20941 else
20942 break;
20943 }
20944
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020945 /* When there is a line break use what follows for the function body.
20946 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20947 if (*p == '\n')
20948 line_arg = p + 1;
20949 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 EMSG(_(e_trailing));
20951
20952 /*
20953 * Read the body of the function, until ":endfunction" is found.
20954 */
20955 if (KeyTyped)
20956 {
20957 /* Check if the function already exists, don't let the user type the
20958 * whole function before telling him it doesn't work! For a script we
20959 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020960 if (!eap->skip && !eap->forceit)
20961 {
20962 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20963 EMSG(_(e_funcdict));
20964 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020965 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020968 if (!eap->skip && did_emsg)
20969 goto erret;
20970
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 msg_putchar('\n'); /* don't overwrite the function name */
20972 cmdline_row = msg_row;
20973 }
20974
20975 indent = 2;
20976 nesting = 0;
20977 for (;;)
20978 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020020979 if (KeyTyped)
20980 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020982 sourcing_lnum_off = sourcing_lnum;
20983
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020984 if (line_arg != NULL)
20985 {
20986 /* Use eap->arg, split up in parts by line breaks. */
20987 theline = line_arg;
20988 p = vim_strchr(theline, '\n');
20989 if (p == NULL)
20990 line_arg += STRLEN(line_arg);
20991 else
20992 {
20993 *p = NUL;
20994 line_arg = p + 1;
20995 }
20996 }
20997 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998 theline = getcmdline(':', 0L, indent);
20999 else
21000 theline = eap->getline(':', eap->cookie, indent);
21001 if (KeyTyped)
21002 lines_left = Rows - 1;
21003 if (theline == NULL)
21004 {
21005 EMSG(_("E126: Missing :endfunction"));
21006 goto erret;
21007 }
21008
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021009 /* Detect line continuation: sourcing_lnum increased more than one. */
21010 if (sourcing_lnum > sourcing_lnum_off + 1)
21011 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21012 else
21013 sourcing_lnum_off = 0;
21014
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 if (skip_until != NULL)
21016 {
21017 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21018 * don't check for ":endfunc". */
21019 if (STRCMP(theline, skip_until) == 0)
21020 {
21021 vim_free(skip_until);
21022 skip_until = NULL;
21023 }
21024 }
21025 else
21026 {
21027 /* skip ':' and blanks*/
21028 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21029 ;
21030
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021031 /* Check for "endfunction". */
21032 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021034 if (line_arg == NULL)
21035 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 break;
21037 }
21038
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021039 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040 * at "end". */
21041 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21042 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021043 else if (STRNCMP(p, "if", 2) == 0
21044 || STRNCMP(p, "wh", 2) == 0
21045 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046 || STRNCMP(p, "try", 3) == 0)
21047 indent += 2;
21048
21049 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021050 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021052 if (*p == '!')
21053 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054 p += eval_fname_script(p);
21055 if (ASCII_ISALPHA(*p))
21056 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021057 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058 if (*skipwhite(p) == '(')
21059 {
21060 ++nesting;
21061 indent += 2;
21062 }
21063 }
21064 }
21065
21066 /* Check for ":append" or ":insert". */
21067 p = skip_range(p, NULL);
21068 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21069 || (p[0] == 'i'
21070 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21071 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21072 skip_until = vim_strsave((char_u *)".");
21073
21074 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21075 arg = skipwhite(skiptowhite(p));
21076 if (arg[0] == '<' && arg[1] =='<'
21077 && ((p[0] == 'p' && p[1] == 'y'
21078 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21079 || (p[0] == 'p' && p[1] == 'e'
21080 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21081 || (p[0] == 't' && p[1] == 'c'
21082 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021083 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21084 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21086 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021087 || (p[0] == 'm' && p[1] == 'z'
21088 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 ))
21090 {
21091 /* ":python <<" continues until a dot, like ":append" */
21092 p = skipwhite(arg + 2);
21093 if (*p == NUL)
21094 skip_until = vim_strsave((char_u *)".");
21095 else
21096 skip_until = vim_strsave(p);
21097 }
21098 }
21099
21100 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021101 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021102 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021103 if (line_arg == NULL)
21104 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021106 }
21107
21108 /* Copy the line to newly allocated memory. get_one_sourceline()
21109 * allocates 250 bytes per line, this saves 80% on average. The cost
21110 * is an extra alloc/free. */
21111 p = vim_strsave(theline);
21112 if (p != NULL)
21113 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021114 if (line_arg == NULL)
21115 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021116 theline = p;
21117 }
21118
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021119 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21120
21121 /* Add NULL lines for continuation lines, so that the line count is
21122 * equal to the index in the growarray. */
21123 while (sourcing_lnum_off-- > 0)
21124 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021125
21126 /* Check for end of eap->arg. */
21127 if (line_arg != NULL && *line_arg == NUL)
21128 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 }
21130
21131 /* Don't define the function when skipping commands or when an error was
21132 * detected. */
21133 if (eap->skip || did_emsg)
21134 goto erret;
21135
21136 /*
21137 * If there are no errors, add the function
21138 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021139 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021140 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021141 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021142 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021143 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021144 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021145 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021146 goto erret;
21147 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021148
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021149 fp = find_func(name);
21150 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021152 if (!eap->forceit)
21153 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021154 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021155 goto erret;
21156 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021157 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021158 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021159 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021160 name);
21161 goto erret;
21162 }
21163 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021164 ga_clear_strings(&(fp->uf_args));
21165 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021166 vim_free(name);
21167 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169 }
21170 else
21171 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021172 char numbuf[20];
21173
21174 fp = NULL;
21175 if (fudi.fd_newkey == NULL && !eap->forceit)
21176 {
21177 EMSG(_(e_funcdict));
21178 goto erret;
21179 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021180 if (fudi.fd_di == NULL)
21181 {
21182 /* Can't add a function to a locked dictionary */
21183 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21184 goto erret;
21185 }
21186 /* Can't change an existing function if it is locked */
21187 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21188 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021189
21190 /* Give the function a sequential number. Can only be used with a
21191 * Funcref! */
21192 vim_free(name);
21193 sprintf(numbuf, "%d", ++func_nr);
21194 name = vim_strsave((char_u *)numbuf);
21195 if (name == NULL)
21196 goto erret;
21197 }
21198
21199 if (fp == NULL)
21200 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021201 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021202 {
21203 int slen, plen;
21204 char_u *scriptname;
21205
21206 /* Check that the autoload name matches the script name. */
21207 j = FAIL;
21208 if (sourcing_name != NULL)
21209 {
21210 scriptname = autoload_name(name);
21211 if (scriptname != NULL)
21212 {
21213 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021214 plen = (int)STRLEN(p);
21215 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021216 if (slen > plen && fnamecmp(p,
21217 sourcing_name + slen - plen) == 0)
21218 j = OK;
21219 vim_free(scriptname);
21220 }
21221 }
21222 if (j == FAIL)
21223 {
21224 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21225 goto erret;
21226 }
21227 }
21228
21229 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 if (fp == NULL)
21231 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021232
21233 if (fudi.fd_dict != NULL)
21234 {
21235 if (fudi.fd_di == NULL)
21236 {
21237 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021238 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021239 if (fudi.fd_di == NULL)
21240 {
21241 vim_free(fp);
21242 goto erret;
21243 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021244 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21245 {
21246 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021247 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021248 goto erret;
21249 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021250 }
21251 else
21252 /* overwrite existing dict entry */
21253 clear_tv(&fudi.fd_di->di_tv);
21254 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021255 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021256 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021257 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021258
21259 /* behave like "dict" was used */
21260 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021261 }
21262
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021264 STRCPY(fp->uf_name, name);
21265 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021267 fp->uf_args = newargs;
21268 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021269#ifdef FEAT_PROFILE
21270 fp->uf_tml_count = NULL;
21271 fp->uf_tml_total = NULL;
21272 fp->uf_tml_self = NULL;
21273 fp->uf_profiling = FALSE;
21274 if (prof_def_func())
21275 func_do_profile(fp);
21276#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021277 fp->uf_varargs = varargs;
21278 fp->uf_flags = flags;
21279 fp->uf_calls = 0;
21280 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021281 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282
21283erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 ga_clear_strings(&newargs);
21285 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021286ret_free:
21287 vim_free(skip_until);
21288 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291}
21292
21293/*
21294 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021295 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021297 * flags:
21298 * TFN_INT: internal function name OK
21299 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300 * Advances "pp" to just after the function name (if no error).
21301 */
21302 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021303trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 char_u **pp;
21305 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021306 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021307 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021309 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 char_u *start;
21311 char_u *end;
21312 int lead;
21313 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021315 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021316
21317 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021318 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021320
21321 /* Check for hard coded <SNR>: already translated function ID (from a user
21322 * command). */
21323 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21324 && (*pp)[2] == (int)KE_SNR)
21325 {
21326 *pp += 3;
21327 len = get_id_len(pp) + 3;
21328 return vim_strnsave(start, len);
21329 }
21330
21331 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21332 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021334 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021335 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021336
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021337 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21338 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021339 if (end == start)
21340 {
21341 if (!skip)
21342 EMSG(_("E129: Function name required"));
21343 goto theend;
21344 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021345 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021346 {
21347 /*
21348 * Report an invalid expression in braces, unless the expression
21349 * evaluation has been cancelled due to an aborting error, an
21350 * interrupt, or an exception.
21351 */
21352 if (!aborting())
21353 {
21354 if (end != NULL)
21355 EMSG2(_(e_invarg2), start);
21356 }
21357 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021358 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021359 goto theend;
21360 }
21361
21362 if (lv.ll_tv != NULL)
21363 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021364 if (fdp != NULL)
21365 {
21366 fdp->fd_dict = lv.ll_dict;
21367 fdp->fd_newkey = lv.ll_newkey;
21368 lv.ll_newkey = NULL;
21369 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021370 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021371 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21372 {
21373 name = vim_strsave(lv.ll_tv->vval.v_string);
21374 *pp = end;
21375 }
21376 else
21377 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021378 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21379 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021380 EMSG(_(e_funcref));
21381 else
21382 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021383 name = NULL;
21384 }
21385 goto theend;
21386 }
21387
21388 if (lv.ll_name == NULL)
21389 {
21390 /* Error found, but continue after the function name. */
21391 *pp = end;
21392 goto theend;
21393 }
21394
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021395 /* Check if the name is a Funcref. If so, use the value. */
21396 if (lv.ll_exp_name != NULL)
21397 {
21398 len = (int)STRLEN(lv.ll_exp_name);
21399 name = deref_func_name(lv.ll_exp_name, &len);
21400 if (name == lv.ll_exp_name)
21401 name = NULL;
21402 }
21403 else
21404 {
21405 len = (int)(end - *pp);
21406 name = deref_func_name(*pp, &len);
21407 if (name == *pp)
21408 name = NULL;
21409 }
21410 if (name != NULL)
21411 {
21412 name = vim_strsave(name);
21413 *pp = end;
21414 goto theend;
21415 }
21416
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021417 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021418 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021419 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021420 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21421 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21422 {
21423 /* When there was "s:" already or the name expanded to get a
21424 * leading "s:" then remove it. */
21425 lv.ll_name += 2;
21426 len -= 2;
21427 lead = 2;
21428 }
21429 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021430 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021431 {
21432 if (lead == 2) /* skip over "s:" */
21433 lv.ll_name += 2;
21434 len = (int)(end - lv.ll_name);
21435 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021436
21437 /*
21438 * Copy the function name to allocated memory.
21439 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21440 * Accept <SNR>123_name() outside a script.
21441 */
21442 if (skip)
21443 lead = 0; /* do nothing */
21444 else if (lead > 0)
21445 {
21446 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021447 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21448 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021449 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021450 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021451 if (current_SID <= 0)
21452 {
21453 EMSG(_(e_usingsid));
21454 goto theend;
21455 }
21456 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21457 lead += (int)STRLEN(sid_buf);
21458 }
21459 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021460 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021461 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021462 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021463 goto theend;
21464 }
21465 name = alloc((unsigned)(len + lead + 1));
21466 if (name != NULL)
21467 {
21468 if (lead > 0)
21469 {
21470 name[0] = K_SPECIAL;
21471 name[1] = KS_EXTRA;
21472 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021473 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021474 STRCPY(name + 3, sid_buf);
21475 }
21476 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21477 name[len + lead] = NUL;
21478 }
21479 *pp = end;
21480
21481theend:
21482 clear_lval(&lv);
21483 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021484}
21485
21486/*
21487 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21488 * Return 2 if "p" starts with "s:".
21489 * Return 0 otherwise.
21490 */
21491 static int
21492eval_fname_script(p)
21493 char_u *p;
21494{
21495 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21496 || STRNICMP(p + 1, "SNR>", 4) == 0))
21497 return 5;
21498 if (p[0] == 's' && p[1] == ':')
21499 return 2;
21500 return 0;
21501}
21502
21503/*
21504 * Return TRUE if "p" starts with "<SID>" or "s:".
21505 * Only works if eval_fname_script() returned non-zero for "p"!
21506 */
21507 static int
21508eval_fname_sid(p)
21509 char_u *p;
21510{
21511 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21512}
21513
21514/*
21515 * List the head of the function: "name(arg1, arg2)".
21516 */
21517 static void
21518list_func_head(fp, indent)
21519 ufunc_T *fp;
21520 int indent;
21521{
21522 int j;
21523
21524 msg_start();
21525 if (indent)
21526 MSG_PUTS(" ");
21527 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021528 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 {
21530 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021531 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 }
21533 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021534 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021536 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 {
21538 if (j)
21539 MSG_PUTS(", ");
21540 msg_puts(FUNCARG(fp, j));
21541 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021542 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 {
21544 if (j)
21545 MSG_PUTS(", ");
21546 MSG_PUTS("...");
21547 }
21548 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021549 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021550 if (p_verbose > 0)
21551 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552}
21553
21554/*
21555 * Find a function by name, return pointer to it in ufuncs.
21556 * Return NULL for unknown function.
21557 */
21558 static ufunc_T *
21559find_func(name)
21560 char_u *name;
21561{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021562 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021564 hi = hash_find(&func_hashtab, name);
21565 if (!HASHITEM_EMPTY(hi))
21566 return HI2UF(hi);
21567 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568}
21569
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021570#if defined(EXITFREE) || defined(PROTO)
21571 void
21572free_all_functions()
21573{
21574 hashitem_T *hi;
21575
21576 /* Need to start all over every time, because func_free() may change the
21577 * hash table. */
21578 while (func_hashtab.ht_used > 0)
21579 for (hi = func_hashtab.ht_array; ; ++hi)
21580 if (!HASHITEM_EMPTY(hi))
21581 {
21582 func_free(HI2UF(hi));
21583 break;
21584 }
21585}
21586#endif
21587
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021588/*
21589 * Return TRUE if a function "name" exists.
21590 */
21591 static int
21592function_exists(name)
21593 char_u *name;
21594{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021595 char_u *nm = name;
21596 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021597 int n = FALSE;
21598
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021599 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021600 nm = skipwhite(nm);
21601
21602 /* Only accept "funcname", "funcname ", "funcname (..." and
21603 * "funcname(...", not "funcname!...". */
21604 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021605 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021606 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021607 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021608 else
21609 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021610 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021611 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021612 return n;
21613}
21614
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021615/*
21616 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021617 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021618 */
21619 static int
21620builtin_function(name)
21621 char_u *name;
21622{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021623 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21624 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021625}
21626
Bram Moolenaar05159a02005-02-26 23:04:13 +000021627#if defined(FEAT_PROFILE) || defined(PROTO)
21628/*
21629 * Start profiling function "fp".
21630 */
21631 static void
21632func_do_profile(fp)
21633 ufunc_T *fp;
21634{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021635 int len = fp->uf_lines.ga_len;
21636
21637 if (len == 0)
21638 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021639 fp->uf_tm_count = 0;
21640 profile_zero(&fp->uf_tm_self);
21641 profile_zero(&fp->uf_tm_total);
21642 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021643 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021644 if (fp->uf_tml_total == NULL)
21645 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021646 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021647 if (fp->uf_tml_self == NULL)
21648 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021649 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021650 fp->uf_tml_idx = -1;
21651 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21652 || fp->uf_tml_self == NULL)
21653 return; /* out of memory */
21654
21655 fp->uf_profiling = TRUE;
21656}
21657
21658/*
21659 * Dump the profiling results for all functions in file "fd".
21660 */
21661 void
21662func_dump_profile(fd)
21663 FILE *fd;
21664{
21665 hashitem_T *hi;
21666 int todo;
21667 ufunc_T *fp;
21668 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021669 ufunc_T **sorttab;
21670 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021671
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021672 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021673 if (todo == 0)
21674 return; /* nothing to dump */
21675
Bram Moolenaar73830342005-02-28 22:48:19 +000021676 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21677
Bram Moolenaar05159a02005-02-26 23:04:13 +000021678 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21679 {
21680 if (!HASHITEM_EMPTY(hi))
21681 {
21682 --todo;
21683 fp = HI2UF(hi);
21684 if (fp->uf_profiling)
21685 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021686 if (sorttab != NULL)
21687 sorttab[st_len++] = fp;
21688
Bram Moolenaar05159a02005-02-26 23:04:13 +000021689 if (fp->uf_name[0] == K_SPECIAL)
21690 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21691 else
21692 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21693 if (fp->uf_tm_count == 1)
21694 fprintf(fd, "Called 1 time\n");
21695 else
21696 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21697 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21698 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21699 fprintf(fd, "\n");
21700 fprintf(fd, "count total (s) self (s)\n");
21701
21702 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21703 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021704 if (FUNCLINE(fp, i) == NULL)
21705 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021706 prof_func_line(fd, fp->uf_tml_count[i],
21707 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021708 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21709 }
21710 fprintf(fd, "\n");
21711 }
21712 }
21713 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021714
21715 if (sorttab != NULL && st_len > 0)
21716 {
21717 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21718 prof_total_cmp);
21719 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21720 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21721 prof_self_cmp);
21722 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21723 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021724
21725 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021726}
Bram Moolenaar73830342005-02-28 22:48:19 +000021727
21728 static void
21729prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21730 FILE *fd;
21731 ufunc_T **sorttab;
21732 int st_len;
21733 char *title;
21734 int prefer_self; /* when equal print only self time */
21735{
21736 int i;
21737 ufunc_T *fp;
21738
21739 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21740 fprintf(fd, "count total (s) self (s) function\n");
21741 for (i = 0; i < 20 && i < st_len; ++i)
21742 {
21743 fp = sorttab[i];
21744 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21745 prefer_self);
21746 if (fp->uf_name[0] == K_SPECIAL)
21747 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21748 else
21749 fprintf(fd, " %s()\n", fp->uf_name);
21750 }
21751 fprintf(fd, "\n");
21752}
21753
21754/*
21755 * Print the count and times for one function or function line.
21756 */
21757 static void
21758prof_func_line(fd, count, total, self, prefer_self)
21759 FILE *fd;
21760 int count;
21761 proftime_T *total;
21762 proftime_T *self;
21763 int prefer_self; /* when equal print only self time */
21764{
21765 if (count > 0)
21766 {
21767 fprintf(fd, "%5d ", count);
21768 if (prefer_self && profile_equal(total, self))
21769 fprintf(fd, " ");
21770 else
21771 fprintf(fd, "%s ", profile_msg(total));
21772 if (!prefer_self && profile_equal(total, self))
21773 fprintf(fd, " ");
21774 else
21775 fprintf(fd, "%s ", profile_msg(self));
21776 }
21777 else
21778 fprintf(fd, " ");
21779}
21780
21781/*
21782 * Compare function for total time sorting.
21783 */
21784 static int
21785#ifdef __BORLANDC__
21786_RTLENTRYF
21787#endif
21788prof_total_cmp(s1, s2)
21789 const void *s1;
21790 const void *s2;
21791{
21792 ufunc_T *p1, *p2;
21793
21794 p1 = *(ufunc_T **)s1;
21795 p2 = *(ufunc_T **)s2;
21796 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21797}
21798
21799/*
21800 * Compare function for self time sorting.
21801 */
21802 static int
21803#ifdef __BORLANDC__
21804_RTLENTRYF
21805#endif
21806prof_self_cmp(s1, s2)
21807 const void *s1;
21808 const void *s2;
21809{
21810 ufunc_T *p1, *p2;
21811
21812 p1 = *(ufunc_T **)s1;
21813 p2 = *(ufunc_T **)s2;
21814 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21815}
21816
Bram Moolenaar05159a02005-02-26 23:04:13 +000021817#endif
21818
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021819/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021820 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021821 * Return TRUE if a package was loaded.
21822 */
21823 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021824script_autoload(name, reload)
21825 char_u *name;
21826 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021827{
21828 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021829 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021830 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021831 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021832
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021833 /* Return quickly when autoload disabled. */
21834 if (no_autoload)
21835 return FALSE;
21836
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021837 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021838 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021839 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021840 return FALSE;
21841
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021842 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021843
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021844 /* Find the name in the list of previously loaded package names. Skip
21845 * "autoload/", it's always the same. */
21846 for (i = 0; i < ga_loaded.ga_len; ++i)
21847 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21848 break;
21849 if (!reload && i < ga_loaded.ga_len)
21850 ret = FALSE; /* was loaded already */
21851 else
21852 {
21853 /* Remember the name if it wasn't loaded already. */
21854 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21855 {
21856 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21857 tofree = NULL;
21858 }
21859
21860 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021861 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021862 ret = TRUE;
21863 }
21864
21865 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021866 return ret;
21867}
21868
21869/*
21870 * Return the autoload script name for a function or variable name.
21871 * Returns NULL when out of memory.
21872 */
21873 static char_u *
21874autoload_name(name)
21875 char_u *name;
21876{
21877 char_u *p;
21878 char_u *scriptname;
21879
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021880 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021881 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21882 if (scriptname == NULL)
21883 return FALSE;
21884 STRCPY(scriptname, "autoload/");
21885 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021886 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021887 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021888 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021889 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021890 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021891}
21892
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21894
21895/*
21896 * Function given to ExpandGeneric() to obtain the list of user defined
21897 * function names.
21898 */
21899 char_u *
21900get_user_func_name(xp, idx)
21901 expand_T *xp;
21902 int idx;
21903{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021904 static long_u done;
21905 static hashitem_T *hi;
21906 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907
21908 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021910 done = 0;
21911 hi = func_hashtab.ht_array;
21912 }
21913 if (done < func_hashtab.ht_used)
21914 {
21915 if (done++ > 0)
21916 ++hi;
21917 while (HASHITEM_EMPTY(hi))
21918 ++hi;
21919 fp = HI2UF(hi);
21920
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010021921 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010021922 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010021923
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021924 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21925 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926
21927 cat_func_name(IObuff, fp);
21928 if (xp->xp_context != EXPAND_USER_FUNC)
21929 {
21930 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021931 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932 STRCAT(IObuff, ")");
21933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934 return IObuff;
21935 }
21936 return NULL;
21937}
21938
21939#endif /* FEAT_CMDL_COMPL */
21940
21941/*
21942 * Copy the function name of "fp" to buffer "buf".
21943 * "buf" must be able to hold the function name plus three bytes.
21944 * Takes care of script-local function names.
21945 */
21946 static void
21947cat_func_name(buf, fp)
21948 char_u *buf;
21949 ufunc_T *fp;
21950{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021951 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952 {
21953 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021954 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 }
21956 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021957 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958}
21959
21960/*
21961 * ":delfunction {name}"
21962 */
21963 void
21964ex_delfunction(eap)
21965 exarg_T *eap;
21966{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021967 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 char_u *p;
21969 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021970 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971
21972 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021973 name = trans_function_name(&p, eap->skip, 0, &fudi);
21974 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021976 {
21977 if (fudi.fd_dict != NULL && !eap->skip)
21978 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 if (!ends_excmd(*skipwhite(p)))
21982 {
21983 vim_free(name);
21984 EMSG(_(e_trailing));
21985 return;
21986 }
21987 eap->nextcmd = check_nextcmd(p);
21988 if (eap->nextcmd != NULL)
21989 *p = NUL;
21990
21991 if (!eap->skip)
21992 fp = find_func(name);
21993 vim_free(name);
21994
21995 if (!eap->skip)
21996 {
21997 if (fp == NULL)
21998 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021999 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 return;
22001 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022002 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 {
22004 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22005 return;
22006 }
22007
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022008 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022010 /* Delete the dict item that refers to the function, it will
22011 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022012 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022014 else
22015 func_free(fp);
22016 }
22017}
22018
22019/*
22020 * Free a function and remove it from the list of functions.
22021 */
22022 static void
22023func_free(fp)
22024 ufunc_T *fp;
22025{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022026 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022027
22028 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022029 ga_clear_strings(&(fp->uf_args));
22030 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022031#ifdef FEAT_PROFILE
22032 vim_free(fp->uf_tml_count);
22033 vim_free(fp->uf_tml_total);
22034 vim_free(fp->uf_tml_self);
22035#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022036
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022037 /* remove the function from the function hashtable */
22038 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22039 if (HASHITEM_EMPTY(hi))
22040 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022041 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022042 hash_remove(&func_hashtab, hi);
22043
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022044 vim_free(fp);
22045}
22046
22047/*
22048 * Unreference a Function: decrement the reference count and free it when it
22049 * becomes zero. Only for numbered functions.
22050 */
22051 static void
22052func_unref(name)
22053 char_u *name;
22054{
22055 ufunc_T *fp;
22056
22057 if (name != NULL && isdigit(*name))
22058 {
22059 fp = find_func(name);
22060 if (fp == NULL)
22061 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022062 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022063 {
22064 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022065 * when "uf_calls" becomes zero. */
22066 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022067 func_free(fp);
22068 }
22069 }
22070}
22071
22072/*
22073 * Count a reference to a Function.
22074 */
22075 static void
22076func_ref(name)
22077 char_u *name;
22078{
22079 ufunc_T *fp;
22080
22081 if (name != NULL && isdigit(*name))
22082 {
22083 fp = find_func(name);
22084 if (fp == NULL)
22085 EMSG2(_(e_intern2), "func_ref()");
22086 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022087 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 }
22089}
22090
22091/*
22092 * Call a user function.
22093 */
22094 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022095call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 ufunc_T *fp; /* pointer to function */
22097 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022098 typval_T *argvars; /* arguments */
22099 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 linenr_T firstline; /* first line of range */
22101 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022102 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103{
Bram Moolenaar33570922005-01-25 22:26:29 +000022104 char_u *save_sourcing_name;
22105 linenr_T save_sourcing_lnum;
22106 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022107 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022108 int save_did_emsg;
22109 static int depth = 0;
22110 dictitem_T *v;
22111 int fixvar_idx = 0; /* index in fixvar[] */
22112 int i;
22113 int ai;
22114 char_u numbuf[NUMBUFLEN];
22115 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022116#ifdef FEAT_PROFILE
22117 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022118 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120
22121 /* If depth of calling is getting too high, don't execute the function */
22122 if (depth >= p_mfd)
22123 {
22124 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022125 rettv->v_type = VAR_NUMBER;
22126 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 return;
22128 }
22129 ++depth;
22130
22131 line_breakcheck(); /* check for CTRL-C hit */
22132
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022133 fc = (funccall_T *)alloc(sizeof(funccall_T));
22134 fc->caller = current_funccal;
22135 current_funccal = fc;
22136 fc->func = fp;
22137 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022138 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022139 fc->linenr = 0;
22140 fc->returned = FALSE;
22141 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022143 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22144 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145
Bram Moolenaar33570922005-01-25 22:26:29 +000022146 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022147 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022148 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22149 * each argument variable and saves a lot of time.
22150 */
22151 /*
22152 * Init l: variables.
22153 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022154 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000022155 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022156 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022157 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22158 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022159 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022160 name = v->di_key;
22161 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022162 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022163 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022164 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022165 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022166 v->di_tv.vval.v_dict = selfdict;
22167 ++selfdict->dv_refcount;
22168 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022169
Bram Moolenaar33570922005-01-25 22:26:29 +000022170 /*
22171 * Init a: variables.
22172 * Set a:0 to "argcount".
22173 * Set a:000 to a list with room for the "..." arguments.
22174 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022175 init_var_dict(&fc->l_avars, &fc->l_avars_var);
22176 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022177 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022178 /* Use "name" to avoid a warning from some compiler that checks the
22179 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022180 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022181 name = v->di_key;
22182 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022183 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022184 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022185 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022186 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022187 v->di_tv.vval.v_list = &fc->l_varlist;
22188 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22189 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22190 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022191
22192 /*
22193 * Set a:firstline to "firstline" and a:lastline to "lastline".
22194 * Set a:name to named arguments.
22195 * Set a:N to the "..." arguments.
22196 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022197 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022198 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022199 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022200 (varnumber_T)lastline);
22201 for (i = 0; i < argcount; ++i)
22202 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022203 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022204 if (ai < 0)
22205 /* named argument a:name */
22206 name = FUNCARG(fp, i);
22207 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022208 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022209 /* "..." argument a:1, a:2, etc. */
22210 sprintf((char *)numbuf, "%d", ai + 1);
22211 name = numbuf;
22212 }
22213 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22214 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022215 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022216 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22217 }
22218 else
22219 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022220 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22221 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022222 if (v == NULL)
22223 break;
22224 v->di_flags = DI_FLAGS_RO;
22225 }
22226 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022227 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022228
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022229 /* Note: the values are copied directly to avoid alloc/free.
22230 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022231 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022232 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022233
22234 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22235 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022236 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22237 fc->l_listitems[ai].li_tv = argvars[i];
22238 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022239 }
22240 }
22241
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 /* Don't redraw while executing the function. */
22243 ++RedrawingDisabled;
22244 save_sourcing_name = sourcing_name;
22245 save_sourcing_lnum = sourcing_lnum;
22246 sourcing_lnum = 1;
22247 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022248 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249 if (sourcing_name != NULL)
22250 {
22251 if (save_sourcing_name != NULL
22252 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22253 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22254 else
22255 STRCPY(sourcing_name, "function ");
22256 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22257
22258 if (p_verbose >= 12)
22259 {
22260 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022261 verbose_enter_scroll();
22262
Bram Moolenaar555b2802005-05-19 21:08:39 +000022263 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 if (p_verbose >= 14)
22265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022267 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022268 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022269 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270
22271 msg_puts((char_u *)"(");
22272 for (i = 0; i < argcount; ++i)
22273 {
22274 if (i > 0)
22275 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022276 if (argvars[i].v_type == VAR_NUMBER)
22277 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 else
22279 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022280 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22281 if (s != NULL)
22282 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022283 if (vim_strsize(s) > MSG_BUF_CLEN)
22284 {
22285 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22286 s = buf;
22287 }
22288 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022289 vim_free(tofree);
22290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291 }
22292 }
22293 msg_puts((char_u *)")");
22294 }
22295 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022296
22297 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 --no_wait_return;
22299 }
22300 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022301#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022302 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022303 {
22304 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22305 func_do_profile(fp);
22306 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022307 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022308 {
22309 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022310 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022311 profile_zero(&fp->uf_tm_children);
22312 }
22313 script_prof_save(&wait_start);
22314 }
22315#endif
22316
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022318 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 save_did_emsg = did_emsg;
22320 did_emsg = FALSE;
22321
22322 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022323 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22325
22326 --RedrawingDisabled;
22327
22328 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022329 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022331 clear_tv(rettv);
22332 rettv->v_type = VAR_NUMBER;
22333 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 }
22335
Bram Moolenaar05159a02005-02-26 23:04:13 +000022336#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022337 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022338 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022339 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022340 profile_end(&call_start);
22341 profile_sub_wait(&wait_start, &call_start);
22342 profile_add(&fp->uf_tm_total, &call_start);
22343 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022344 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022345 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022346 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22347 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022348 }
22349 }
22350#endif
22351
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352 /* when being verbose, mention the return value */
22353 if (p_verbose >= 12)
22354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022356 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022359 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022360 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022361 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022362 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022363 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022365 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022366 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022367 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022368 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022369
Bram Moolenaar555b2802005-05-19 21:08:39 +000022370 /* The value may be very long. Skip the middle part, so that we
22371 * have some idea how it starts and ends. smsg() would always
22372 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022373 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022374 if (s != NULL)
22375 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022376 if (vim_strsize(s) > MSG_BUF_CLEN)
22377 {
22378 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22379 s = buf;
22380 }
22381 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022382 vim_free(tofree);
22383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 }
22385 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022386
22387 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 --no_wait_return;
22389 }
22390
22391 vim_free(sourcing_name);
22392 sourcing_name = save_sourcing_name;
22393 sourcing_lnum = save_sourcing_lnum;
22394 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022395#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022396 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022397 script_prof_restore(&wait_start);
22398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399
22400 if (p_verbose >= 12 && sourcing_name != NULL)
22401 {
22402 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022403 verbose_enter_scroll();
22404
Bram Moolenaar555b2802005-05-19 21:08:39 +000022405 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022407
22408 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 --no_wait_return;
22410 }
22411
22412 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022413 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022415
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022416 /* If the a:000 list and the l: and a: dicts are not referenced we can
22417 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022418 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22419 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22420 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22421 {
22422 free_funccal(fc, FALSE);
22423 }
22424 else
22425 {
22426 hashitem_T *hi;
22427 listitem_T *li;
22428 int todo;
22429
22430 /* "fc" is still in use. This can happen when returning "a:000" or
22431 * assigning "l:" to a global variable.
22432 * Link "fc" in the list for garbage collection later. */
22433 fc->caller = previous_funccal;
22434 previous_funccal = fc;
22435
22436 /* Make a copy of the a: variables, since we didn't do that above. */
22437 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22438 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22439 {
22440 if (!HASHITEM_EMPTY(hi))
22441 {
22442 --todo;
22443 v = HI2DI(hi);
22444 copy_tv(&v->di_tv, &v->di_tv);
22445 }
22446 }
22447
22448 /* Make a copy of the a:000 items, since we didn't do that above. */
22449 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22450 copy_tv(&li->li_tv, &li->li_tv);
22451 }
22452}
22453
22454/*
22455 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022456 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022457 */
22458 static int
22459can_free_funccal(fc, copyID)
22460 funccall_T *fc;
22461 int copyID;
22462{
22463 return (fc->l_varlist.lv_copyID != copyID
22464 && fc->l_vars.dv_copyID != copyID
22465 && fc->l_avars.dv_copyID != copyID);
22466}
22467
22468/*
22469 * Free "fc" and what it contains.
22470 */
22471 static void
22472free_funccal(fc, free_val)
22473 funccall_T *fc;
22474 int free_val; /* a: vars were allocated */
22475{
22476 listitem_T *li;
22477
22478 /* The a: variables typevals may not have been allocated, only free the
22479 * allocated variables. */
22480 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22481
22482 /* free all l: variables */
22483 vars_clear(&fc->l_vars.dv_hashtab);
22484
22485 /* Free the a:000 variables if they were allocated. */
22486 if (free_val)
22487 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22488 clear_tv(&li->li_tv);
22489
22490 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491}
22492
22493/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022494 * Add a number variable "name" to dict "dp" with value "nr".
22495 */
22496 static void
22497add_nr_var(dp, v, name, nr)
22498 dict_T *dp;
22499 dictitem_T *v;
22500 char *name;
22501 varnumber_T nr;
22502{
22503 STRCPY(v->di_key, name);
22504 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22505 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22506 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022507 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022508 v->di_tv.vval.v_number = nr;
22509}
22510
22511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 * ":return [expr]"
22513 */
22514 void
22515ex_return(eap)
22516 exarg_T *eap;
22517{
22518 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022519 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 int returning = FALSE;
22521
22522 if (current_funccal == NULL)
22523 {
22524 EMSG(_("E133: :return not inside a function"));
22525 return;
22526 }
22527
22528 if (eap->skip)
22529 ++emsg_skip;
22530
22531 eap->nextcmd = NULL;
22532 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022533 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 {
22535 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022536 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022538 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539 }
22540 /* It's safer to return also on error. */
22541 else if (!eap->skip)
22542 {
22543 /*
22544 * Return unless the expression evaluation has been cancelled due to an
22545 * aborting error, an interrupt, or an exception.
22546 */
22547 if (!aborting())
22548 returning = do_return(eap, FALSE, TRUE, NULL);
22549 }
22550
22551 /* When skipping or the return gets pending, advance to the next command
22552 * in this line (!returning). Otherwise, ignore the rest of the line.
22553 * Following lines will be ignored by get_func_line(). */
22554 if (returning)
22555 eap->nextcmd = NULL;
22556 else if (eap->nextcmd == NULL) /* no argument */
22557 eap->nextcmd = check_nextcmd(arg);
22558
22559 if (eap->skip)
22560 --emsg_skip;
22561}
22562
22563/*
22564 * Return from a function. Possibly makes the return pending. Also called
22565 * for a pending return at the ":endtry" or after returning from an extra
22566 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022567 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022568 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569 * FALSE when the return gets pending.
22570 */
22571 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022572do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 exarg_T *eap;
22574 int reanimate;
22575 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022576 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577{
22578 int idx;
22579 struct condstack *cstack = eap->cstack;
22580
22581 if (reanimate)
22582 /* Undo the return. */
22583 current_funccal->returned = FALSE;
22584
22585 /*
22586 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22587 * not in its finally clause (which then is to be executed next) is found.
22588 * In this case, make the ":return" pending for execution at the ":endtry".
22589 * Otherwise, return normally.
22590 */
22591 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22592 if (idx >= 0)
22593 {
22594 cstack->cs_pending[idx] = CSTP_RETURN;
22595
22596 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022597 /* A pending return again gets pending. "rettv" points to an
22598 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022600 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 else
22602 {
22603 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022604 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022606 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022608 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 {
22610 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022611 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022612 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 else
22614 EMSG(_(e_outofmem));
22615 }
22616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022617 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618
22619 if (reanimate)
22620 {
22621 /* The pending return value could be overwritten by a ":return"
22622 * without argument in a finally clause; reset the default
22623 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022624 current_funccal->rettv->v_type = VAR_NUMBER;
22625 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 }
22627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022628 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 }
22630 else
22631 {
22632 current_funccal->returned = TRUE;
22633
22634 /* If the return is carried out now, store the return value. For
22635 * a return immediately after reanimation, the value is already
22636 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022637 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022639 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022640 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022642 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 }
22644 }
22645
22646 return idx < 0;
22647}
22648
22649/*
22650 * Free the variable with a pending return value.
22651 */
22652 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022653discard_pending_return(rettv)
22654 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655{
Bram Moolenaar33570922005-01-25 22:26:29 +000022656 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657}
22658
22659/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022660 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022661 * is an allocated string. Used by report_pending() for verbose messages.
22662 */
22663 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022664get_return_cmd(rettv)
22665 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022667 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022668 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022669 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022671 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022672 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022673 if (s == NULL)
22674 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022675
22676 STRCPY(IObuff, ":return ");
22677 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22678 if (STRLEN(s) + 8 >= IOSIZE)
22679 STRCPY(IObuff + IOSIZE - 4, "...");
22680 vim_free(tofree);
22681 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682}
22683
22684/*
22685 * Get next function line.
22686 * Called by do_cmdline() to get the next line.
22687 * Returns allocated string, or NULL for end of function.
22688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 char_u *
22690get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022691 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022693 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694{
Bram Moolenaar33570922005-01-25 22:26:29 +000022695 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022696 ufunc_T *fp = fcp->func;
22697 char_u *retval;
22698 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699
22700 /* If breakpoints have been added/deleted need to check for it. */
22701 if (fcp->dbg_tick != debug_tick)
22702 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022703 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 sourcing_lnum);
22705 fcp->dbg_tick = debug_tick;
22706 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022707#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022708 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022709 func_line_end(cookie);
22710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711
Bram Moolenaar05159a02005-02-26 23:04:13 +000022712 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022713 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22714 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 retval = NULL;
22716 else
22717 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022718 /* Skip NULL lines (continuation lines). */
22719 while (fcp->linenr < gap->ga_len
22720 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22721 ++fcp->linenr;
22722 if (fcp->linenr >= gap->ga_len)
22723 retval = NULL;
22724 else
22725 {
22726 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22727 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022728#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022729 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022730 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022731#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 }
22734
22735 /* Did we encounter a breakpoint? */
22736 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22737 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022738 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022740 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741 sourcing_lnum);
22742 fcp->dbg_tick = debug_tick;
22743 }
22744
22745 return retval;
22746}
22747
Bram Moolenaar05159a02005-02-26 23:04:13 +000022748#if defined(FEAT_PROFILE) || defined(PROTO)
22749/*
22750 * Called when starting to read a function line.
22751 * "sourcing_lnum" must be correct!
22752 * When skipping lines it may not actually be executed, but we won't find out
22753 * until later and we need to store the time now.
22754 */
22755 void
22756func_line_start(cookie)
22757 void *cookie;
22758{
22759 funccall_T *fcp = (funccall_T *)cookie;
22760 ufunc_T *fp = fcp->func;
22761
22762 if (fp->uf_profiling && sourcing_lnum >= 1
22763 && sourcing_lnum <= fp->uf_lines.ga_len)
22764 {
22765 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022766 /* Skip continuation lines. */
22767 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22768 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022769 fp->uf_tml_execed = FALSE;
22770 profile_start(&fp->uf_tml_start);
22771 profile_zero(&fp->uf_tml_children);
22772 profile_get_wait(&fp->uf_tml_wait);
22773 }
22774}
22775
22776/*
22777 * Called when actually executing a function line.
22778 */
22779 void
22780func_line_exec(cookie)
22781 void *cookie;
22782{
22783 funccall_T *fcp = (funccall_T *)cookie;
22784 ufunc_T *fp = fcp->func;
22785
22786 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22787 fp->uf_tml_execed = TRUE;
22788}
22789
22790/*
22791 * Called when done with a function line.
22792 */
22793 void
22794func_line_end(cookie)
22795 void *cookie;
22796{
22797 funccall_T *fcp = (funccall_T *)cookie;
22798 ufunc_T *fp = fcp->func;
22799
22800 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22801 {
22802 if (fp->uf_tml_execed)
22803 {
22804 ++fp->uf_tml_count[fp->uf_tml_idx];
22805 profile_end(&fp->uf_tml_start);
22806 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022807 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022808 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22809 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022810 }
22811 fp->uf_tml_idx = -1;
22812 }
22813}
22814#endif
22815
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816/*
22817 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022818 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 */
22820 int
22821func_has_ended(cookie)
22822 void *cookie;
22823{
Bram Moolenaar33570922005-01-25 22:26:29 +000022824 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825
22826 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22827 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022828 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829 || fcp->returned);
22830}
22831
22832/*
22833 * return TRUE if cookie indicates a function which "abort"s on errors.
22834 */
22835 int
22836func_has_abort(cookie)
22837 void *cookie;
22838{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022839 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840}
22841
22842#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22843typedef enum
22844{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022845 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22846 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22847 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848} var_flavour_T;
22849
22850static var_flavour_T var_flavour __ARGS((char_u *varname));
22851
22852 static var_flavour_T
22853var_flavour(varname)
22854 char_u *varname;
22855{
22856 char_u *p = varname;
22857
22858 if (ASCII_ISUPPER(*p))
22859 {
22860 while (*(++p))
22861 if (ASCII_ISLOWER(*p))
22862 return VAR_FLAVOUR_SESSION;
22863 return VAR_FLAVOUR_VIMINFO;
22864 }
22865 else
22866 return VAR_FLAVOUR_DEFAULT;
22867}
22868#endif
22869
22870#if defined(FEAT_VIMINFO) || defined(PROTO)
22871/*
22872 * Restore global vars that start with a capital from the viminfo file
22873 */
22874 int
22875read_viminfo_varlist(virp, writing)
22876 vir_T *virp;
22877 int writing;
22878{
22879 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022880 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022881 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882
22883 if (!writing && (find_viminfo_parameter('!') != NULL))
22884 {
22885 tab = vim_strchr(virp->vir_line + 1, '\t');
22886 if (tab != NULL)
22887 {
22888 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022889 switch (*tab)
22890 {
22891 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022892#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022893 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022894#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022895 case 'D': type = VAR_DICT; break;
22896 case 'L': type = VAR_LIST; break;
22897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898
22899 tab = vim_strchr(tab, '\t');
22900 if (tab != NULL)
22901 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022902 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022903 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022904 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022906#ifdef FEAT_FLOAT
22907 else if (type == VAR_FLOAT)
22908 (void)string2float(tab + 1, &tv.vval.v_float);
22909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022911 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022912 if (type == VAR_DICT || type == VAR_LIST)
22913 {
22914 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22915
22916 if (etv == NULL)
22917 /* Failed to parse back the dict or list, use it as a
22918 * string. */
22919 tv.v_type = VAR_STRING;
22920 else
22921 {
22922 vim_free(tv.vval.v_string);
22923 tv = *etv;
22924 }
22925 }
22926
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022927 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022928
22929 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022930 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022931 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22932 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 }
22934 }
22935 }
22936
22937 return viminfo_readline(virp);
22938}
22939
22940/*
22941 * Write global vars that start with a capital to the viminfo file
22942 */
22943 void
22944write_viminfo_varlist(fp)
22945 FILE *fp;
22946{
Bram Moolenaar33570922005-01-25 22:26:29 +000022947 hashitem_T *hi;
22948 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022949 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022950 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022951 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022952 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022953 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954
22955 if (find_viminfo_parameter('!') == NULL)
22956 return;
22957
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022958 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022959
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022960 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022961 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022963 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022965 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022966 this_var = HI2DI(hi);
22967 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022968 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022969 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022970 {
22971 case VAR_STRING: s = "STR"; break;
22972 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022973#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022974 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022975#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022976 case VAR_DICT: s = "DIC"; break;
22977 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022978 default: continue;
22979 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022980 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022981 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022982 if (p != NULL)
22983 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022984 vim_free(tofree);
22985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 }
22987 }
22988}
22989#endif
22990
22991#if defined(FEAT_SESSION) || defined(PROTO)
22992 int
22993store_session_globals(fd)
22994 FILE *fd;
22995{
Bram Moolenaar33570922005-01-25 22:26:29 +000022996 hashitem_T *hi;
22997 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022998 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999 char_u *p, *t;
23000
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023001 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023002 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023004 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023006 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023007 this_var = HI2DI(hi);
23008 if ((this_var->di_tv.v_type == VAR_NUMBER
23009 || this_var->di_tv.v_type == VAR_STRING)
23010 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023011 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023012 /* Escape special characters with a backslash. Turn a LF and
23013 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023014 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023015 (char_u *)"\\\"\n\r");
23016 if (p == NULL) /* out of memory */
23017 break;
23018 for (t = p; *t != NUL; ++t)
23019 if (*t == '\n')
23020 *t = 'n';
23021 else if (*t == '\r')
23022 *t = 'r';
23023 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023024 this_var->di_key,
23025 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23026 : ' ',
23027 p,
23028 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23029 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023030 || put_eol(fd) == FAIL)
23031 {
23032 vim_free(p);
23033 return FAIL;
23034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035 vim_free(p);
23036 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023037#ifdef FEAT_FLOAT
23038 else if (this_var->di_tv.v_type == VAR_FLOAT
23039 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23040 {
23041 float_T f = this_var->di_tv.vval.v_float;
23042 int sign = ' ';
23043
23044 if (f < 0)
23045 {
23046 f = -f;
23047 sign = '-';
23048 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023049 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023050 this_var->di_key, sign, f) < 0)
23051 || put_eol(fd) == FAIL)
23052 return FAIL;
23053 }
23054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 }
23056 }
23057 return OK;
23058}
23059#endif
23060
Bram Moolenaar661b1822005-07-28 22:36:45 +000023061/*
23062 * Display script name where an item was last set.
23063 * Should only be invoked when 'verbose' is non-zero.
23064 */
23065 void
23066last_set_msg(scriptID)
23067 scid_T scriptID;
23068{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023069 char_u *p;
23070
Bram Moolenaar661b1822005-07-28 22:36:45 +000023071 if (scriptID != 0)
23072 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023073 p = home_replace_save(NULL, get_scriptname(scriptID));
23074 if (p != NULL)
23075 {
23076 verbose_enter();
23077 MSG_PUTS(_("\n\tLast set from "));
23078 MSG_PUTS(p);
23079 vim_free(p);
23080 verbose_leave();
23081 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023082 }
23083}
23084
Bram Moolenaard812df62008-11-09 12:46:09 +000023085/*
23086 * List v:oldfiles in a nice way.
23087 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023088 void
23089ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023090 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023091{
23092 list_T *l = vimvars[VV_OLDFILES].vv_list;
23093 listitem_T *li;
23094 int nr = 0;
23095
23096 if (l == NULL)
23097 msg((char_u *)_("No old files"));
23098 else
23099 {
23100 msg_start();
23101 msg_scroll = TRUE;
23102 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23103 {
23104 msg_outnum((long)++nr);
23105 MSG_PUTS(": ");
23106 msg_outtrans(get_tv_string(&li->li_tv));
23107 msg_putchar('\n');
23108 out_flush(); /* output one line at a time */
23109 ui_breakcheck();
23110 }
23111 /* Assume "got_int" was set to truncate the listing. */
23112 got_int = FALSE;
23113
23114#ifdef FEAT_BROWSE_CMD
23115 if (cmdmod.browse)
23116 {
23117 quit_more = FALSE;
23118 nr = prompt_for_number(FALSE);
23119 msg_starthere();
23120 if (nr > 0)
23121 {
23122 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23123 (long)nr);
23124
23125 if (p != NULL)
23126 {
23127 p = expand_env_save(p);
23128 eap->arg = p;
23129 eap->cmdidx = CMD_edit;
23130 cmdmod.browse = FALSE;
23131 do_exedit(eap, NULL);
23132 vim_free(p);
23133 }
23134 }
23135 }
23136#endif
23137 }
23138}
23139
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140#endif /* FEAT_EVAL */
23141
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023143#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144
23145#ifdef WIN3264
23146/*
23147 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23148 */
23149static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23150static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23151static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23152
23153/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023154 * Get the short path (8.3) for the filename in "fnamep".
23155 * Only works for a valid file name.
23156 * When the path gets longer "fnamep" is changed and the allocated buffer
23157 * is put in "bufp".
23158 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23159 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160 */
23161 static int
23162get_short_pathname(fnamep, bufp, fnamelen)
23163 char_u **fnamep;
23164 char_u **bufp;
23165 int *fnamelen;
23166{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023167 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168 char_u *newbuf;
23169
23170 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 l = GetShortPathName(*fnamep, *fnamep, len);
23172 if (l > len - 1)
23173 {
23174 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023175 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 newbuf = vim_strnsave(*fnamep, l);
23177 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023178 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179
23180 vim_free(*bufp);
23181 *fnamep = *bufp = newbuf;
23182
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023183 /* Really should always succeed, as the buffer is big enough. */
23184 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 }
23186
23187 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023188 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189}
23190
23191/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023192 * Get the short path (8.3) for the filename in "fname". The converted
23193 * path is returned in "bufp".
23194 *
23195 * Some of the directories specified in "fname" may not exist. This function
23196 * will shorten the existing directories at the beginning of the path and then
23197 * append the remaining non-existing path.
23198 *
23199 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023200 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023201 * bufp - Pointer to an allocated buffer for the filename.
23202 * fnamelen - Length of the filename pointed to by fname
23203 *
23204 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 */
23206 static int
23207shortpath_for_invalid_fname(fname, bufp, fnamelen)
23208 char_u **fname;
23209 char_u **bufp;
23210 int *fnamelen;
23211{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023212 char_u *short_fname, *save_fname, *pbuf_unused;
23213 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023215 int old_len, len;
23216 int new_len, sfx_len;
23217 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218
23219 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023220 old_len = *fnamelen;
23221 save_fname = vim_strnsave(*fname, old_len);
23222 pbuf_unused = NULL;
23223 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023225 endp = save_fname + old_len - 1; /* Find the end of the copy */
23226 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023228 /*
23229 * Try shortening the supplied path till it succeeds by removing one
23230 * directory at a time from the tail of the path.
23231 */
23232 len = 0;
23233 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023235 /* go back one path-separator */
23236 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23237 --endp;
23238 if (endp <= save_fname)
23239 break; /* processed the complete path */
23240
23241 /*
23242 * Replace the path separator with a NUL and try to shorten the
23243 * resulting path.
23244 */
23245 ch = *endp;
23246 *endp = 0;
23247 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023248 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023249 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23250 {
23251 retval = FAIL;
23252 goto theend;
23253 }
23254 *endp = ch; /* preserve the string */
23255
23256 if (len > 0)
23257 break; /* successfully shortened the path */
23258
23259 /* failed to shorten the path. Skip the path separator */
23260 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 }
23262
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023263 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023265 /*
23266 * Succeeded in shortening the path. Now concatenate the shortened
23267 * path with the remaining path at the tail.
23268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023270 /* Compute the length of the new path. */
23271 sfx_len = (int)(save_endp - endp) + 1;
23272 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023274 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023276 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023278 /* There is not enough space in the currently allocated string,
23279 * copy it to a buffer big enough. */
23280 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023282 {
23283 retval = FAIL;
23284 goto theend;
23285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023286 }
23287 else
23288 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023289 /* Transfer short_fname to the main buffer (it's big enough),
23290 * unless get_short_pathname() did its work in-place. */
23291 *fname = *bufp = save_fname;
23292 if (short_fname != save_fname)
23293 vim_strncpy(save_fname, short_fname, len);
23294 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023296
23297 /* concat the not-shortened part of the path */
23298 vim_strncpy(*fname + len, endp, sfx_len);
23299 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023301
23302theend:
23303 vim_free(pbuf_unused);
23304 vim_free(save_fname);
23305
23306 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307}
23308
23309/*
23310 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023311 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312 */
23313 static int
23314shortpath_for_partial(fnamep, bufp, fnamelen)
23315 char_u **fnamep;
23316 char_u **bufp;
23317 int *fnamelen;
23318{
23319 int sepcount, len, tflen;
23320 char_u *p;
23321 char_u *pbuf, *tfname;
23322 int hasTilde;
23323
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023324 /* Count up the path separators from the RHS.. so we know which part
23325 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023327 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328 if (vim_ispathsep(*p))
23329 ++sepcount;
23330
23331 /* Need full path first (use expand_env() to remove a "~/") */
23332 hasTilde = (**fnamep == '~');
23333 if (hasTilde)
23334 pbuf = tfname = expand_env_save(*fnamep);
23335 else
23336 pbuf = tfname = FullName_save(*fnamep, FALSE);
23337
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023338 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023340 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023342
23343 if (len == 0)
23344 {
23345 /* Don't have a valid filename, so shorten the rest of the
23346 * path if we can. This CAN give us invalid 8.3 filenames, but
23347 * there's not a lot of point in guessing what it might be.
23348 */
23349 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023350 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23351 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 }
23353
23354 /* Count the paths backward to find the beginning of the desired string. */
23355 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023356 {
23357#ifdef FEAT_MBYTE
23358 if (has_mbyte)
23359 p -= mb_head_off(tfname, p);
23360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 if (vim_ispathsep(*p))
23362 {
23363 if (sepcount == 0 || (hasTilde && sepcount == 1))
23364 break;
23365 else
23366 sepcount --;
23367 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369 if (hasTilde)
23370 {
23371 --p;
23372 if (p >= tfname)
23373 *p = '~';
23374 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023375 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 }
23377 else
23378 ++p;
23379
23380 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23381 vim_free(*bufp);
23382 *fnamelen = (int)STRLEN(p);
23383 *bufp = pbuf;
23384 *fnamep = p;
23385
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023386 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387}
23388#endif /* WIN3264 */
23389
23390/*
23391 * Adjust a filename, according to a string of modifiers.
23392 * *fnamep must be NUL terminated when called. When returning, the length is
23393 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023394 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395 * When there is an error, *fnamep is set to NULL.
23396 */
23397 int
23398modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23399 char_u *src; /* string with modifiers */
23400 int *usedlen; /* characters after src that are used */
23401 char_u **fnamep; /* file name so far */
23402 char_u **bufp; /* buffer for allocated file name or NULL */
23403 int *fnamelen; /* length of fnamep */
23404{
23405 int valid = 0;
23406 char_u *tail;
23407 char_u *s, *p, *pbuf;
23408 char_u dirname[MAXPATHL];
23409 int c;
23410 int has_fullname = 0;
23411#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023412 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413 int has_shortname = 0;
23414#endif
23415
23416repeat:
23417 /* ":p" - full path/file_name */
23418 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23419 {
23420 has_fullname = 1;
23421
23422 valid |= VALID_PATH;
23423 *usedlen += 2;
23424
23425 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23426 if ((*fnamep)[0] == '~'
23427#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23428 && ((*fnamep)[1] == '/'
23429# ifdef BACKSLASH_IN_FILENAME
23430 || (*fnamep)[1] == '\\'
23431# endif
23432 || (*fnamep)[1] == NUL)
23433
23434#endif
23435 )
23436 {
23437 *fnamep = expand_env_save(*fnamep);
23438 vim_free(*bufp); /* free any allocated file name */
23439 *bufp = *fnamep;
23440 if (*fnamep == NULL)
23441 return -1;
23442 }
23443
23444 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023445 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446 {
23447 if (vim_ispathsep(*p)
23448 && p[1] == '.'
23449 && (p[2] == NUL
23450 || vim_ispathsep(p[2])
23451 || (p[2] == '.'
23452 && (p[3] == NUL || vim_ispathsep(p[3])))))
23453 break;
23454 }
23455
23456 /* FullName_save() is slow, don't use it when not needed. */
23457 if (*p != NUL || !vim_isAbsName(*fnamep))
23458 {
23459 *fnamep = FullName_save(*fnamep, *p != NUL);
23460 vim_free(*bufp); /* free any allocated file name */
23461 *bufp = *fnamep;
23462 if (*fnamep == NULL)
23463 return -1;
23464 }
23465
23466 /* Append a path separator to a directory. */
23467 if (mch_isdir(*fnamep))
23468 {
23469 /* Make room for one or two extra characters. */
23470 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23471 vim_free(*bufp); /* free any allocated file name */
23472 *bufp = *fnamep;
23473 if (*fnamep == NULL)
23474 return -1;
23475 add_pathsep(*fnamep);
23476 }
23477 }
23478
23479 /* ":." - path relative to the current directory */
23480 /* ":~" - path relative to the home directory */
23481 /* ":8" - shortname path - postponed till after */
23482 while (src[*usedlen] == ':'
23483 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23484 {
23485 *usedlen += 2;
23486 if (c == '8')
23487 {
23488#ifdef WIN3264
23489 has_shortname = 1; /* Postpone this. */
23490#endif
23491 continue;
23492 }
23493 pbuf = NULL;
23494 /* Need full path first (use expand_env() to remove a "~/") */
23495 if (!has_fullname)
23496 {
23497 if (c == '.' && **fnamep == '~')
23498 p = pbuf = expand_env_save(*fnamep);
23499 else
23500 p = pbuf = FullName_save(*fnamep, FALSE);
23501 }
23502 else
23503 p = *fnamep;
23504
23505 has_fullname = 0;
23506
23507 if (p != NULL)
23508 {
23509 if (c == '.')
23510 {
23511 mch_dirname(dirname, MAXPATHL);
23512 s = shorten_fname(p, dirname);
23513 if (s != NULL)
23514 {
23515 *fnamep = s;
23516 if (pbuf != NULL)
23517 {
23518 vim_free(*bufp); /* free any allocated file name */
23519 *bufp = pbuf;
23520 pbuf = NULL;
23521 }
23522 }
23523 }
23524 else
23525 {
23526 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23527 /* Only replace it when it starts with '~' */
23528 if (*dirname == '~')
23529 {
23530 s = vim_strsave(dirname);
23531 if (s != NULL)
23532 {
23533 *fnamep = s;
23534 vim_free(*bufp);
23535 *bufp = s;
23536 }
23537 }
23538 }
23539 vim_free(pbuf);
23540 }
23541 }
23542
23543 tail = gettail(*fnamep);
23544 *fnamelen = (int)STRLEN(*fnamep);
23545
23546 /* ":h" - head, remove "/file_name", can be repeated */
23547 /* Don't remove the first "/" or "c:\" */
23548 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23549 {
23550 valid |= VALID_HEAD;
23551 *usedlen += 2;
23552 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023553 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023554 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 *fnamelen = (int)(tail - *fnamep);
23556#ifdef VMS
23557 if (*fnamelen > 0)
23558 *fnamelen += 1; /* the path separator is part of the path */
23559#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023560 if (*fnamelen == 0)
23561 {
23562 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23563 p = vim_strsave((char_u *)".");
23564 if (p == NULL)
23565 return -1;
23566 vim_free(*bufp);
23567 *bufp = *fnamep = tail = p;
23568 *fnamelen = 1;
23569 }
23570 else
23571 {
23572 while (tail > s && !after_pathsep(s, tail))
23573 mb_ptr_back(*fnamep, tail);
23574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 }
23576
23577 /* ":8" - shortname */
23578 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23579 {
23580 *usedlen += 2;
23581#ifdef WIN3264
23582 has_shortname = 1;
23583#endif
23584 }
23585
23586#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023587 /*
23588 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 */
23590 if (has_shortname)
23591 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023592 /* Copy the string if it is shortened by :h and when it wasn't copied
23593 * yet, because we are going to change it in place. Avoids changing
23594 * the buffer name for "%:8". */
23595 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596 {
23597 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023598 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 return -1;
23600 vim_free(*bufp);
23601 *bufp = *fnamep = p;
23602 }
23603
23604 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023605 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606 if (!has_fullname && !vim_isAbsName(*fnamep))
23607 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023608 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023609 return -1;
23610 }
23611 else
23612 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023613 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614
Bram Moolenaardc935552011-08-17 15:23:23 +020023615 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023617 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 return -1;
23619
23620 if (l == 0)
23621 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023622 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023624 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 return -1;
23626 }
23627 *fnamelen = l;
23628 }
23629 }
23630#endif /* WIN3264 */
23631
23632 /* ":t" - tail, just the basename */
23633 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23634 {
23635 *usedlen += 2;
23636 *fnamelen -= (int)(tail - *fnamep);
23637 *fnamep = tail;
23638 }
23639
23640 /* ":e" - extension, can be repeated */
23641 /* ":r" - root, without extension, can be repeated */
23642 while (src[*usedlen] == ':'
23643 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23644 {
23645 /* find a '.' in the tail:
23646 * - for second :e: before the current fname
23647 * - otherwise: The last '.'
23648 */
23649 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23650 s = *fnamep - 2;
23651 else
23652 s = *fnamep + *fnamelen - 1;
23653 for ( ; s > tail; --s)
23654 if (s[0] == '.')
23655 break;
23656 if (src[*usedlen + 1] == 'e') /* :e */
23657 {
23658 if (s > tail)
23659 {
23660 *fnamelen += (int)(*fnamep - (s + 1));
23661 *fnamep = s + 1;
23662#ifdef VMS
23663 /* cut version from the extension */
23664 s = *fnamep + *fnamelen - 1;
23665 for ( ; s > *fnamep; --s)
23666 if (s[0] == ';')
23667 break;
23668 if (s > *fnamep)
23669 *fnamelen = s - *fnamep;
23670#endif
23671 }
23672 else if (*fnamep <= tail)
23673 *fnamelen = 0;
23674 }
23675 else /* :r */
23676 {
23677 if (s > tail) /* remove one extension */
23678 *fnamelen = (int)(s - *fnamep);
23679 }
23680 *usedlen += 2;
23681 }
23682
23683 /* ":s?pat?foo?" - substitute */
23684 /* ":gs?pat?foo?" - global substitute */
23685 if (src[*usedlen] == ':'
23686 && (src[*usedlen + 1] == 's'
23687 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23688 {
23689 char_u *str;
23690 char_u *pat;
23691 char_u *sub;
23692 int sep;
23693 char_u *flags;
23694 int didit = FALSE;
23695
23696 flags = (char_u *)"";
23697 s = src + *usedlen + 2;
23698 if (src[*usedlen + 1] == 'g')
23699 {
23700 flags = (char_u *)"g";
23701 ++s;
23702 }
23703
23704 sep = *s++;
23705 if (sep)
23706 {
23707 /* find end of pattern */
23708 p = vim_strchr(s, sep);
23709 if (p != NULL)
23710 {
23711 pat = vim_strnsave(s, (int)(p - s));
23712 if (pat != NULL)
23713 {
23714 s = p + 1;
23715 /* find end of substitution */
23716 p = vim_strchr(s, sep);
23717 if (p != NULL)
23718 {
23719 sub = vim_strnsave(s, (int)(p - s));
23720 str = vim_strnsave(*fnamep, *fnamelen);
23721 if (sub != NULL && str != NULL)
23722 {
23723 *usedlen = (int)(p + 1 - src);
23724 s = do_string_sub(str, pat, sub, flags);
23725 if (s != NULL)
23726 {
23727 *fnamep = s;
23728 *fnamelen = (int)STRLEN(s);
23729 vim_free(*bufp);
23730 *bufp = s;
23731 didit = TRUE;
23732 }
23733 }
23734 vim_free(sub);
23735 vim_free(str);
23736 }
23737 vim_free(pat);
23738 }
23739 }
23740 /* after using ":s", repeat all the modifiers */
23741 if (didit)
23742 goto repeat;
23743 }
23744 }
23745
23746 return valid;
23747}
23748
23749/*
23750 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23751 * "flags" can be "g" to do a global substitute.
23752 * Returns an allocated string, NULL for error.
23753 */
23754 char_u *
23755do_string_sub(str, pat, sub, flags)
23756 char_u *str;
23757 char_u *pat;
23758 char_u *sub;
23759 char_u *flags;
23760{
23761 int sublen;
23762 regmatch_T regmatch;
23763 int i;
23764 int do_all;
23765 char_u *tail;
23766 garray_T ga;
23767 char_u *ret;
23768 char_u *save_cpo;
23769
23770 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23771 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023772 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773
23774 ga_init2(&ga, 1, 200);
23775
23776 do_all = (flags[0] == 'g');
23777
23778 regmatch.rm_ic = p_ic;
23779 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23780 if (regmatch.regprog != NULL)
23781 {
23782 tail = str;
23783 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23784 {
23785 /*
23786 * Get some space for a temporary buffer to do the substitution
23787 * into. It will contain:
23788 * - The text up to where the match is.
23789 * - The substituted text.
23790 * - The text after the match.
23791 */
23792 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23793 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23794 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23795 {
23796 ga_clear(&ga);
23797 break;
23798 }
23799
23800 /* copy the text up to where the match is */
23801 i = (int)(regmatch.startp[0] - tail);
23802 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23803 /* add the substituted text */
23804 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23805 + ga.ga_len + i, TRUE, TRUE, FALSE);
23806 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023807 /* avoid getting stuck on a match with an empty string */
23808 if (tail == regmatch.endp[0])
23809 {
23810 if (*tail == NUL)
23811 break;
23812 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23813 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814 }
23815 else
23816 {
23817 tail = regmatch.endp[0];
23818 if (*tail == NUL)
23819 break;
23820 }
23821 if (!do_all)
23822 break;
23823 }
23824
23825 if (ga.ga_data != NULL)
23826 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23827
23828 vim_free(regmatch.regprog);
23829 }
23830
23831 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23832 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023833 if (p_cpo == empty_option)
23834 p_cpo = save_cpo;
23835 else
23836 /* Darn, evaluating {sub} expression changed the value. */
23837 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838
23839 return ret;
23840}
23841
23842#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */