blob: 33b1947570873939cf3896530e7322138582180a [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 Moolenaar1dced572012-04-05 16:54:08 +0200625#ifdef FEAT_LUA
626static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000632static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000633static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000634static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000636static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000640#ifdef vim_mkdir
641static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100644#ifdef FEAT_MZSCHEME
645static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100649static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000650static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000651#ifdef FEAT_FLOAT
652static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000655static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000656static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000659static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
672static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
673#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000675static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000677static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000684static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000685static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000686static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000687static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200689static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000690static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000692static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
695static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200696static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000699static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000700static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#ifdef FEAT_FLOAT
704static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
706#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000707static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200708static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709#ifdef HAVE_STRFTIME
710static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
711#endif
712static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200718static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200719static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000725static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200726static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000728static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000729static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000730static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000731static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000732static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000734static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200735#ifdef FEAT_FLOAT
736static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000742#ifdef FEAT_FLOAT
743static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
744#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200746static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200747static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000757static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100761static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000763static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000764static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static int get_env_len __ARGS((char_u **arg));
766static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
769#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
770#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
771 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000772static 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 +0000773static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000774static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000775static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
776static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static typval_T *alloc_tv __ARGS((void));
778static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static void init_tv __ARGS((typval_T *varp));
780static long get_tv_number __ARGS((typval_T *varp));
781static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000782static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static char_u *get_tv_string __ARGS((typval_T *varp));
784static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000785static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000787static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
789static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
790static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000791static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
792static 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 +0000793static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
794static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000795static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200796static int var_check_func_name __ARGS((char_u *name, int new_var));
797static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000798static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000799static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
801static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
802static int eval_fname_script __ARGS((char_u *p));
803static int eval_fname_sid __ARGS((char_u *p));
804static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static ufunc_T *find_func __ARGS((char_u *name));
806static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000807static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
809static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000810static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
811static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_total_cmp __ARGS((const void *s1, const void *s2));
817static int
818# ifdef __BORLANDC__
819 _RTLENTRYF
820# endif
821 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000823static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000824static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000825static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static void func_free __ARGS((ufunc_T *fp));
827static void func_unref __ARGS((char_u *name));
828static void func_ref __ARGS((char_u *name));
829static 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 +0000830static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
831static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000832static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
834static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000835static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000836static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000837static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000838
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200839
840#ifdef EBCDIC
841static int compare_func_name __ARGS((const void *s1, const void *s2));
842static void sortFunctions __ARGS(());
843#endif
844
845
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000846/* Character used as separated in autoload function/variable names. */
847#define AUTOLOAD_CHAR '#'
848
Bram Moolenaar33570922005-01-25 22:26:29 +0000849/*
850 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000851 */
852 void
853eval_init()
854{
Bram Moolenaar33570922005-01-25 22:26:29 +0000855 int i;
856 struct vimvar *p;
857
858 init_var_dict(&globvardict, &globvars_var);
859 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200860 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000861 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000862 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000863
864 for (i = 0; i < VV_LEN; ++i)
865 {
866 p = &vimvars[i];
867 STRCPY(p->vv_di.di_key, p->vv_name);
868 if (p->vv_flags & VV_RO)
869 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
870 else if (p->vv_flags & VV_RO_SBX)
871 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
872 else
873 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000874
875 /* add to v: scope dict, unless the value is not always available */
876 if (p->vv_type != VAR_UNKNOWN)
877 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000879 /* add to compat scope dict */
880 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000881 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000882 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883
884#ifdef EBCDIC
885 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100886 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200887 */
888 sortFunctions();
889#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000890}
891
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000892#if defined(EXITFREE) || defined(PROTO)
893 void
894eval_clear()
895{
896 int i;
897 struct vimvar *p;
898
899 for (i = 0; i < VV_LEN; ++i)
900 {
901 p = &vimvars[i];
902 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000903 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000904 vim_free(p->vv_str);
905 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000906 }
907 else if (p->vv_di.di_tv.v_type == VAR_LIST)
908 {
909 list_unref(p->vv_list);
910 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000911 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000912 }
913 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000914 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915 hash_clear(&compat_hashtab);
916
Bram Moolenaard9fba312005-06-26 22:34:35 +0000917 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200918 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000919
920 /* global variables */
921 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000922
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000923 /* autoloaded script names */
924 ga_clear_strings(&ga_loaded);
925
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200926 /* script-local variables */
927 for (i = 1; i <= ga_scripts.ga_len; ++i)
928 {
929 vars_clear(&SCRIPT_VARS(i));
930 vim_free(SCRIPT_SV(i));
931 }
932 ga_clear(&ga_scripts);
933
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000934 /* unreferenced lists and dicts */
935 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000936
937 /* functions */
938 free_all_functions();
939 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000940}
941#endif
942
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 * Return the name of the executed function.
945 */
946 char_u *
947func_name(cookie)
948 void *cookie;
949{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000950 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951}
952
953/*
954 * Return the address holding the next breakpoint line for a funccall cookie.
955 */
956 linenr_T *
957func_breakpoint(cookie)
958 void *cookie;
959{
Bram Moolenaar33570922005-01-25 22:26:29 +0000960 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961}
962
963/*
964 * Return the address holding the debug tick for a funccall cookie.
965 */
966 int *
967func_dbg_tick(cookie)
968 void *cookie;
969{
Bram Moolenaar33570922005-01-25 22:26:29 +0000970 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/*
974 * Return the nesting level for a funccall cookie.
975 */
976 int
977func_level(cookie)
978 void *cookie;
979{
Bram Moolenaar33570922005-01-25 22:26:29 +0000980 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981}
982
983/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000984funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000986/* pointer to list of previously used funccal, still around because some
987 * item in it is still being used. */
988funccall_T *previous_funccal = NULL;
989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990/*
991 * Return TRUE when a function was ended by a ":return" command.
992 */
993 int
994current_func_returned()
995{
996 return current_funccal->returned;
997}
998
999
1000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 * Set an internal variable to a string value. Creates the variable if it does
1002 * not already exist.
1003 */
1004 void
1005set_internal_string_var(name, value)
1006 char_u *name;
1007 char_u *value;
1008{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001009 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001010 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
1012 val = vim_strsave(value);
1013 if (val != NULL)
1014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001015 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001016 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001018 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001019 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 }
1021 }
1022}
1023
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001024static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001025static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026static char_u *redir_endp = NULL;
1027static char_u *redir_varname = NULL;
1028
1029/*
1030 * Start recording command output to a variable
1031 * Returns OK if successfully completed the setup. FAIL otherwise.
1032 */
1033 int
1034var_redir_start(name, append)
1035 char_u *name;
1036 int append; /* append to an existing variable */
1037{
1038 int save_emsg;
1039 int err;
1040 typval_T tv;
1041
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001042 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001043 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044 {
1045 EMSG(_(e_invarg));
1046 return FAIL;
1047 }
1048
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001049 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050 redir_varname = vim_strsave(name);
1051 if (redir_varname == NULL)
1052 return FAIL;
1053
1054 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1055 if (redir_lval == NULL)
1056 {
1057 var_redir_stop();
1058 return FAIL;
1059 }
1060
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001061 /* The output is stored in growarray "redir_ga" until redirection ends. */
1062 ga_init2(&redir_ga, (int)sizeof(char), 500);
1063
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001065 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1066 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1068 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001069 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 if (redir_endp != NULL && *redir_endp != NUL)
1071 /* Trailing characters are present after the variable name */
1072 EMSG(_(e_trailing));
1073 else
1074 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001075 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 var_redir_stop();
1077 return FAIL;
1078 }
1079
1080 /* check if we can write to the variable: set it to or append an empty
1081 * string */
1082 save_emsg = did_emsg;
1083 did_emsg = FALSE;
1084 tv.v_type = VAR_STRING;
1085 tv.vval.v_string = (char_u *)"";
1086 if (append)
1087 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1088 else
1089 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001090 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001092 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 if (err)
1094 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001095 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 var_redir_stop();
1097 return FAIL;
1098 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001099
1100 return OK;
1101}
1102
1103/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001104 * Append "value[value_len]" to the variable set by var_redir_start().
1105 * The actual appending is postponed until redirection ends, because the value
1106 * appended may in fact be the string we write to, changing it may cause freed
1107 * memory to be used:
1108 * :redir => foo
1109 * :let foo
1110 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 */
1112 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001113var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118
1119 if (redir_lval == NULL)
1120 return;
1121
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001123 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001125 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001127 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001128 {
1129 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001130 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131 }
1132 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134}
1135
1136/*
1137 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001138 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 */
1140 void
1141var_redir_stop()
1142{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 typval_T tv;
1144
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 if (redir_lval != NULL)
1146 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001147 /* If there was no error: assign the text to the variable. */
1148 if (redir_endp != NULL)
1149 {
1150 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1151 tv.v_type = VAR_STRING;
1152 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001153 /* Call get_lval() again, if it's inside a Dict or List it may
1154 * have changed. */
1155 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1156 FALSE, FALSE, FALSE, FNE_CHECK_START);
1157 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1158 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1159 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001160 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001161
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001162 /* free the collected output */
1163 vim_free(redir_ga.ga_data);
1164 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001165
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 vim_free(redir_lval);
1167 redir_lval = NULL;
1168 }
1169 vim_free(redir_varname);
1170 redir_varname = NULL;
1171}
1172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173# if defined(FEAT_MBYTE) || defined(PROTO)
1174 int
1175eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1176 char_u *enc_from;
1177 char_u *enc_to;
1178 char_u *fname_from;
1179 char_u *fname_to;
1180{
1181 int err = FALSE;
1182
1183 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1184 set_vim_var_string(VV_CC_TO, enc_to, -1);
1185 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1186 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1187 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1188 err = TRUE;
1189 set_vim_var_string(VV_CC_FROM, NULL, -1);
1190 set_vim_var_string(VV_CC_TO, NULL, -1);
1191 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1192 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1193
1194 if (err)
1195 return FAIL;
1196 return OK;
1197}
1198# endif
1199
1200# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1201 int
1202eval_printexpr(fname, args)
1203 char_u *fname;
1204 char_u *args;
1205{
1206 int err = FALSE;
1207
1208 set_vim_var_string(VV_FNAME_IN, fname, -1);
1209 set_vim_var_string(VV_CMDARG, args, -1);
1210 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1211 err = TRUE;
1212 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1213 set_vim_var_string(VV_CMDARG, NULL, -1);
1214
1215 if (err)
1216 {
1217 mch_remove(fname);
1218 return FAIL;
1219 }
1220 return OK;
1221}
1222# endif
1223
1224# if defined(FEAT_DIFF) || defined(PROTO)
1225 void
1226eval_diff(origfile, newfile, outfile)
1227 char_u *origfile;
1228 char_u *newfile;
1229 char_u *outfile;
1230{
1231 int err = FALSE;
1232
1233 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1234 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1235 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1236 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1237 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1238 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1239 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1240}
1241
1242 void
1243eval_patch(origfile, difffile, outfile)
1244 char_u *origfile;
1245 char_u *difffile;
1246 char_u *outfile;
1247{
1248 int err;
1249
1250 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1251 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1252 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1253 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1254 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1255 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1256 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1257}
1258# endif
1259
1260/*
1261 * Top level evaluation function, returning a boolean.
1262 * Sets "error" to TRUE if there was an error.
1263 * Return TRUE or FALSE.
1264 */
1265 int
1266eval_to_bool(arg, error, nextcmd, skip)
1267 char_u *arg;
1268 int *error;
1269 char_u **nextcmd;
1270 int skip; /* only parse, don't execute */
1271{
Bram Moolenaar33570922005-01-25 22:26:29 +00001272 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 int retval = FALSE;
1274
1275 if (skip)
1276 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 else
1280 {
1281 *error = FALSE;
1282 if (!skip)
1283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001284 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001285 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287 }
1288 if (skip)
1289 --emsg_skip;
1290
1291 return retval;
1292}
1293
1294/*
1295 * Top level evaluation function, returning a string. If "skip" is TRUE,
1296 * only parsing to "nextcmd" is done, without reporting errors. Return
1297 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1298 */
1299 char_u *
1300eval_to_string_skip(arg, nextcmd, skip)
1301 char_u *arg;
1302 char_u **nextcmd;
1303 int skip; /* only parse, don't execute */
1304{
Bram Moolenaar33570922005-01-25 22:26:29 +00001305 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 char_u *retval;
1307
1308 if (skip)
1309 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001310 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 retval = NULL;
1312 else
1313 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001314 retval = vim_strsave(get_tv_string(&tv));
1315 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 }
1317 if (skip)
1318 --emsg_skip;
1319
1320 return retval;
1321}
1322
1323/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324 * Skip over an expression at "*pp".
1325 * Return FAIL for an error, OK otherwise.
1326 */
1327 int
1328skip_expr(pp)
1329 char_u **pp;
1330{
Bram Moolenaar33570922005-01-25 22:26:29 +00001331 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001332
1333 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001335}
1336
1337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339 * When "convert" is TRUE convert a List into a sequence of lines and convert
1340 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 * Return pointer to allocated memory, or NULL for failure.
1342 */
1343 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *arg;
1346 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001347 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
Bram Moolenaar33570922005-01-25 22:26:29 +00001349 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001351 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001352#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001353 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001356 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 retval = NULL;
1358 else
1359 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001360 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001361 {
1362 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001363 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001364 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001365 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001366 if (tv.vval.v_list->lv_len > 0)
1367 ga_append(&ga, NL);
1368 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001369 ga_append(&ga, NUL);
1370 retval = (char_u *)ga.ga_data;
1371 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372#ifdef FEAT_FLOAT
1373 else if (convert && tv.v_type == VAR_FLOAT)
1374 {
1375 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1376 retval = vim_strsave(numbuf);
1377 }
1378#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001379 else
1380 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383
1384 return retval;
1385}
1386
1387/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388 * Call eval_to_string() without using current local variables and using
1389 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 */
1391 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 char_u *arg;
1394 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001395 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396{
1397 char_u *retval;
1398 void *save_funccalp;
1399
1400 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001401 if (use_sandbox)
1402 ++sandbox;
1403 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001404 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001405 if (use_sandbox)
1406 --sandbox;
1407 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 restore_funccal(save_funccalp);
1409 return retval;
1410}
1411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412/*
1413 * Top level evaluation function, returning a number.
1414 * Evaluates "expr" silently.
1415 * Returns -1 for an error.
1416 */
1417 int
1418eval_to_number(expr)
1419 char_u *expr;
1420{
Bram Moolenaar33570922005-01-25 22:26:29 +00001421 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001423 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424
1425 ++emsg_off;
1426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001427 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 retval = -1;
1429 else
1430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001431 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 }
1434 --emsg_off;
1435
1436 return retval;
1437}
1438
Bram Moolenaara40058a2005-07-11 22:42:07 +00001439/*
1440 * Prepare v: variable "idx" to be used.
1441 * Save the current typeval in "save_tv".
1442 * When not used yet add the variable to the v: hashtable.
1443 */
1444 static void
1445prepare_vimvar(idx, save_tv)
1446 int idx;
1447 typval_T *save_tv;
1448{
1449 *save_tv = vimvars[idx].vv_tv;
1450 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1451 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1452}
1453
1454/*
1455 * Restore v: variable "idx" to typeval "save_tv".
1456 * When no longer defined, remove the variable from the v: hashtable.
1457 */
1458 static void
1459restore_vimvar(idx, save_tv)
1460 int idx;
1461 typval_T *save_tv;
1462{
1463 hashitem_T *hi;
1464
Bram Moolenaara40058a2005-07-11 22:42:07 +00001465 vimvars[idx].vv_tv = *save_tv;
1466 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1467 {
1468 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1469 if (HASHITEM_EMPTY(hi))
1470 EMSG2(_(e_intern2), "restore_vimvar()");
1471 else
1472 hash_remove(&vimvarht, hi);
1473 }
1474}
1475
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001476#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001477/*
1478 * Evaluate an expression to a list with suggestions.
1479 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001480 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001481 */
1482 list_T *
1483eval_spell_expr(badword, expr)
1484 char_u *badword;
1485 char_u *expr;
1486{
1487 typval_T save_val;
1488 typval_T rettv;
1489 list_T *list = NULL;
1490 char_u *p = skipwhite(expr);
1491
1492 /* Set "v:val" to the bad word. */
1493 prepare_vimvar(VV_VAL, &save_val);
1494 vimvars[VV_VAL].vv_type = VAR_STRING;
1495 vimvars[VV_VAL].vv_str = badword;
1496 if (p_verbose == 0)
1497 ++emsg_off;
1498
1499 if (eval1(&p, &rettv, TRUE) == OK)
1500 {
1501 if (rettv.v_type != VAR_LIST)
1502 clear_tv(&rettv);
1503 else
1504 list = rettv.vval.v_list;
1505 }
1506
1507 if (p_verbose == 0)
1508 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001509 restore_vimvar(VV_VAL, &save_val);
1510
1511 return list;
1512}
1513
1514/*
1515 * "list" is supposed to contain two items: a word and a number. Return the
1516 * word in "pp" and the number as the return value.
1517 * Return -1 if anything isn't right.
1518 * Used to get the good word and score from the eval_spell_expr() result.
1519 */
1520 int
1521get_spellword(list, pp)
1522 list_T *list;
1523 char_u **pp;
1524{
1525 listitem_T *li;
1526
1527 li = list->lv_first;
1528 if (li == NULL)
1529 return -1;
1530 *pp = get_tv_string(&li->li_tv);
1531
1532 li = li->li_next;
1533 if (li == NULL)
1534 return -1;
1535 return get_tv_number(&li->li_tv);
1536}
1537#endif
1538
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001539/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001540 * Top level evaluation function.
1541 * Returns an allocated typval_T with the result.
1542 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001543 */
1544 typval_T *
1545eval_expr(arg, nextcmd)
1546 char_u *arg;
1547 char_u **nextcmd;
1548{
1549 typval_T *tv;
1550
1551 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001552 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001553 {
1554 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001555 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001556 }
1557
1558 return tv;
1559}
1560
1561
Bram Moolenaar4f688582007-07-24 12:34:30 +00001562#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1563 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001566 * Uses argv[argc] for the function arguments. Only Number and String
1567 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001570 int
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001571call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 char_u *func;
1573 int argc;
1574 char_u **argv;
1575 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577{
Bram Moolenaar33570922005-01-25 22:26:29 +00001578 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 long n;
1580 int len;
1581 int i;
1582 int doesrange;
1583 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001586 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
1590 for (i = 0; i < argc; i++)
1591 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001592 /* Pass a NULL or empty argument as an empty string */
1593 if (argv[i] == NULL || *argv[i] == NUL)
1594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001595 argvars[i].v_type = VAR_STRING;
1596 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001597 continue;
1598 }
1599
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 /* Recognize a number argument, the others must be strings. */
1601 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1602 if (len != 0 && len == (int)STRLEN(argv[i]))
1603 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001604 argvars[i].v_type = VAR_NUMBER;
1605 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 else
1608 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001609 argvars[i].v_type = VAR_STRING;
1610 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612 }
1613
1614 if (safe)
1615 {
1616 save_funccalp = save_funccal();
1617 ++sandbox;
1618 }
1619
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1621 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 if (safe)
1625 {
1626 --sandbox;
1627 restore_funccal(save_funccalp);
1628 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629 vim_free(argvars);
1630
1631 if (ret == FAIL)
1632 clear_tv(rettv);
1633
1634 return ret;
1635}
1636
Bram Moolenaar4f688582007-07-24 12:34:30 +00001637# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001639 * Call vimL function "func" and return the result as a string.
1640 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641 * Uses argv[argc] for the function arguments.
1642 */
1643 void *
1644call_func_retstr(func, argc, argv, safe)
1645 char_u *func;
1646 int argc;
1647 char_u **argv;
1648 int safe; /* use the sandbox */
1649{
1650 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001651 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652
1653 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1654 return NULL;
1655
1656 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 return retval;
1659}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001660# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001661
Bram Moolenaar4f688582007-07-24 12:34:30 +00001662# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001663/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001664 * Call vimL function "func" and return the result as a number.
1665 * Returns -1 when calling the function fails.
1666 * Uses argv[argc] for the function arguments.
1667 */
1668 long
1669call_func_retnr(func, argc, argv, safe)
1670 char_u *func;
1671 int argc;
1672 char_u **argv;
1673 int safe; /* use the sandbox */
1674{
1675 typval_T rettv;
1676 long retval;
1677
1678 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1679 return -1;
1680
1681 retval = get_tv_number_chk(&rettv, NULL);
1682 clear_tv(&rettv);
1683 return retval;
1684}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001685# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001686
1687/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001688 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001690 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691 */
1692 void *
1693call_func_retlist(func, argc, argv, safe)
1694 char_u *func;
1695 int argc;
1696 char_u **argv;
1697 int safe; /* use the sandbox */
1698{
1699 typval_T rettv;
1700
1701 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1702 return NULL;
1703
1704 if (rettv.v_type != VAR_LIST)
1705 {
1706 clear_tv(&rettv);
1707 return NULL;
1708 }
1709
1710 return rettv.vval.v_list;
1711}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712#endif
1713
Bram Moolenaar4f688582007-07-24 12:34:30 +00001714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715/*
1716 * Save the current function call pointer, and set it to NULL.
1717 * Used when executing autocommands and for ":source".
1718 */
1719 void *
1720save_funccal()
1721{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001722 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 current_funccal = NULL;
1725 return (void *)fc;
1726}
1727
1728 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729restore_funccal(vfc)
1730 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732 funccall_T *fc = (funccall_T *)vfc;
1733
1734 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735}
1736
Bram Moolenaar05159a02005-02-26 23:04:13 +00001737#if defined(FEAT_PROFILE) || defined(PROTO)
1738/*
1739 * Prepare profiling for entering a child or something else that is not
1740 * counted for the script/function itself.
1741 * Should always be called in pair with prof_child_exit().
1742 */
1743 void
1744prof_child_enter(tm)
1745 proftime_T *tm; /* place to store waittime */
1746{
1747 funccall_T *fc = current_funccal;
1748
1749 if (fc != NULL && fc->func->uf_profiling)
1750 profile_start(&fc->prof_child);
1751 script_prof_save(tm);
1752}
1753
1754/*
1755 * Take care of time spent in a child.
1756 * Should always be called after prof_child_enter().
1757 */
1758 void
1759prof_child_exit(tm)
1760 proftime_T *tm; /* where waittime was stored */
1761{
1762 funccall_T *fc = current_funccal;
1763
1764 if (fc != NULL && fc->func->uf_profiling)
1765 {
1766 profile_end(&fc->prof_child);
1767 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1768 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1769 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1770 }
1771 script_prof_restore(tm);
1772}
1773#endif
1774
1775
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#ifdef FEAT_FOLDING
1777/*
1778 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1779 * it in "*cp". Doesn't give error messages.
1780 */
1781 int
1782eval_foldexpr(arg, cp)
1783 char_u *arg;
1784 int *cp;
1785{
Bram Moolenaar33570922005-01-25 22:26:29 +00001786 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 int retval;
1788 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001789 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1790 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
1792 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001793 if (use_sandbox)
1794 ++sandbox;
1795 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 retval = 0;
1799 else
1800 {
1801 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 if (tv.v_type == VAR_NUMBER)
1803 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001804 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 retval = 0;
1806 else
1807 {
1808 /* If the result is a string, check if there is a non-digit before
1809 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (!VIM_ISDIGIT(*s) && *s != '-')
1812 *cp = *s++;
1813 retval = atol((char *)s);
1814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001815 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 }
1817 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001818 if (use_sandbox)
1819 --sandbox;
1820 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 return retval;
1823}
1824#endif
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 * ":let" list all variable values
1828 * ":let var1 var2" list variable values
1829 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001830 * ":let var += expr" assignment command.
1831 * ":let var -= expr" assignment command.
1832 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 */
1835 void
1836ex_let(eap)
1837 exarg_T *eap;
1838{
1839 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001841 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 int var_count = 0;
1844 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001847 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Bram Moolenaardb552d602006-03-23 22:59:57 +00001849 argend = skip_var_list(arg, &var_count, &semicolon);
1850 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001852 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1853 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001854 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001857 /*
1858 * ":let" without "=": list variables
1859 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 if (*arg == '[')
1861 EMSG(_(e_invarg));
1862 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 list_glob_vars(&first);
1869 list_buf_vars(&first);
1870 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001871#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001873#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001874 list_script_vars(&first);
1875 list_func_vars(&first);
1876 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 eap->nextcmd = check_nextcmd(arg);
1879 }
1880 else
1881 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 op[0] = '=';
1883 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001884 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001885 {
1886 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1887 op[0] = expr[-1]; /* +=, -= or .= */
1888 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (eap->skip)
1892 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (eap->skip)
1895 {
1896 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 --emsg_skip;
1899 }
1900 else if (i != FAIL)
1901 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 }
1907}
1908
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909/*
1910 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1911 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 * When "nextchars" is not NULL it points to a string with characters that
1913 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1914 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 * Returns OK or FAIL;
1916 */
1917 static int
1918ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1919 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 int copy; /* copy values from "tv", don't move */
1922 int semicolon; /* from skip_var_list() */
1923 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925{
1926 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001927 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001929 listitem_T *item;
1930 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931
1932 if (*arg != '[')
1933 {
1934 /*
1935 * ":let var = expr" or ":for var in list"
1936 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 return FAIL;
1939 return OK;
1940 }
1941
1942 /*
1943 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1944 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001945 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 {
1947 EMSG(_(e_listreq));
1948 return FAIL;
1949 }
1950
1951 i = list_len(l);
1952 if (semicolon == 0 && var_count < i)
1953 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001954 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 return FAIL;
1956 }
1957 if (var_count - semicolon > i)
1958 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001959 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 return FAIL;
1961 }
1962
1963 item = l->lv_first;
1964 while (*arg != ']')
1965 {
1966 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 item = item->li_next;
1969 if (arg == NULL)
1970 return FAIL;
1971
1972 arg = skipwhite(arg);
1973 if (*arg == ';')
1974 {
1975 /* Put the rest of the list (may be empty) in the var after ';'.
1976 * Create a new list for this. */
1977 l = list_alloc();
1978 if (l == NULL)
1979 return FAIL;
1980 while (item != NULL)
1981 {
1982 list_append_tv(l, &item->li_tv);
1983 item = item->li_next;
1984 }
1985
1986 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001987 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 ltv.vval.v_list = l;
1989 l->lv_refcount = 1;
1990
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001991 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1992 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993 clear_tv(&ltv);
1994 if (arg == NULL)
1995 return FAIL;
1996 break;
1997 }
1998 else if (*arg != ',' && *arg != ']')
1999 {
2000 EMSG2(_(e_intern2), "ex_let_vars()");
2001 return FAIL;
2002 }
2003 }
2004
2005 return OK;
2006}
2007
2008/*
2009 * Skip over assignable variable "var" or list of variables "[var, var]".
2010 * Used for ":let varvar = expr" and ":for varvar in expr".
2011 * For "[var, var]" increment "*var_count" for each variable.
2012 * for "[var, var; var]" set "semicolon".
2013 * Return NULL for an error.
2014 */
2015 static char_u *
2016skip_var_list(arg, var_count, semicolon)
2017 char_u *arg;
2018 int *var_count;
2019 int *semicolon;
2020{
2021 char_u *p, *s;
2022
2023 if (*arg == '[')
2024 {
2025 /* "[var, var]": find the matching ']'. */
2026 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002027 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 {
2029 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2030 s = skip_var_one(p);
2031 if (s == p)
2032 {
2033 EMSG2(_(e_invarg2), p);
2034 return NULL;
2035 }
2036 ++*var_count;
2037
2038 p = skipwhite(s);
2039 if (*p == ']')
2040 break;
2041 else if (*p == ';')
2042 {
2043 if (*semicolon == 1)
2044 {
2045 EMSG(_("Double ; in list of variables"));
2046 return NULL;
2047 }
2048 *semicolon = 1;
2049 }
2050 else if (*p != ',')
2051 {
2052 EMSG2(_(e_invarg2), p);
2053 return NULL;
2054 }
2055 }
2056 return p + 1;
2057 }
2058 else
2059 return skip_var_one(arg);
2060}
2061
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002063 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002064 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002065 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066 static char_u *
2067skip_var_one(arg)
2068 char_u *arg;
2069{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002070 if (*arg == '@' && arg[1] != NUL)
2071 return arg + 2;
2072 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2073 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002074}
2075
Bram Moolenaara7043832005-01-21 11:56:39 +00002076/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002077 * List variables for hashtab "ht" with prefix "prefix".
2078 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002082 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002084 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002085 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002086{
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 hashitem_T *hi;
2088 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 int todo;
2090
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002091 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2093 {
2094 if (!HASHITEM_EMPTY(hi))
2095 {
2096 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 di = HI2DI(hi);
2098 if (empty || di->di_tv.v_type != VAR_STRING
2099 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002101 }
2102 }
2103}
2104
2105/*
2106 * List global variables.
2107 */
2108 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109list_glob_vars(first)
2110 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002111{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002112 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002113}
2114
2115/*
2116 * List buffer variables.
2117 */
2118 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119list_buf_vars(first)
2120 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122 char_u numbuf[NUMBUFLEN];
2123
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2125 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126
2127 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2129 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002130}
2131
2132/*
2133 * List window variables.
2134 */
2135 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136list_win_vars(first)
2137 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002138{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2140 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002141}
2142
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002143#ifdef FEAT_WINDOWS
2144/*
2145 * List tab page variables.
2146 */
2147 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148list_tab_vars(first)
2149 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002150{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2152 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002153}
2154#endif
2155
Bram Moolenaara7043832005-01-21 11:56:39 +00002156/*
2157 * List Vim variables.
2158 */
2159 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160list_vim_vars(first)
2161 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164}
2165
2166/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002167 * List script-local variables, if there is a script.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_script_vars(first)
2171 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002172{
2173 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2175 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176}
2177
2178/*
2179 * List function variables, if there is a function.
2180 */
2181 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182list_func_vars(first)
2183 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002184{
2185 if (current_funccal != NULL)
2186 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002188}
2189
2190/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 * List variables in "arg".
2192 */
2193 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 exarg_T *eap;
2196 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198{
2199 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002202 char_u *name_start;
2203 char_u *arg_subsc;
2204 char_u *tofree;
2205 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206
2207 while (!ends_excmd(*arg) && !got_int)
2208 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002211 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2213 {
2214 emsg_severe = TRUE;
2215 EMSG(_(e_trailing));
2216 break;
2217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 /* get_name_len() takes care of expanding curly braces */
2222 name_start = name = arg;
2223 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2224 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 /* This is mainly to keep test 49 working: when expanding
2227 * curly braces fails overrule the exception error message. */
2228 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 emsg_severe = TRUE;
2231 EMSG2(_(e_invarg2), arg);
2232 break;
2233 }
2234 error = TRUE;
2235 }
2236 else
2237 {
2238 if (tofree != NULL)
2239 name = tofree;
2240 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 else
2243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 /* handle d.key, l[idx], f(expr) */
2245 arg_subsc = arg;
2246 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002254 case 'g': list_glob_vars(first); break;
2255 case 'b': list_buf_vars(first); break;
2256 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002257#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002258 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002259#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 case 'v': list_vim_vars(first); break;
2261 case 's': list_script_vars(first); break;
2262 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 default:
2264 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002265 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002266 }
2267 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 {
2269 char_u numbuf[NUMBUFLEN];
2270 char_u *tf;
2271 int c;
2272 char_u *s;
2273
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002274 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 c = *arg;
2276 *arg = NUL;
2277 list_one_var_a((char_u *)"",
2278 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002279 tv.v_type,
2280 s == NULL ? (char_u *)"" : s,
2281 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 *arg = c;
2283 vim_free(tf);
2284 }
2285 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 }
2288 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289
2290 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292
2293 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 }
2295
2296 return arg;
2297}
2298
2299/*
2300 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2301 * Returns a pointer to the char just after the var name.
2302 * Returns NULL if there is an error.
2303 */
2304 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002307 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002309 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311{
2312 int c1;
2313 char_u *name;
2314 char_u *p;
2315 char_u *arg_end = NULL;
2316 int len;
2317 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002318 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319
2320 /*
2321 * ":let $VAR = expr": Set environment variable.
2322 */
2323 if (*arg == '$')
2324 {
2325 /* Find the end of the name. */
2326 ++arg;
2327 name = arg;
2328 len = get_env_len(&arg);
2329 if (len == 0)
2330 EMSG2(_(e_invarg2), name - 1);
2331 else
2332 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333 if (op != NULL && (*op == '+' || *op == '-'))
2334 EMSG2(_(e_letwrong), op);
2335 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002336 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002338 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 {
2340 c1 = name[len];
2341 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002342 p = get_tv_string_chk(tv);
2343 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002344 {
2345 int mustfree = FALSE;
2346 char_u *s = vim_getenv(name, &mustfree);
2347
2348 if (s != NULL)
2349 {
2350 p = tofree = concat_str(s, p);
2351 if (mustfree)
2352 vim_free(s);
2353 }
2354 }
2355 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002358 if (STRICMP(name, "HOME") == 0)
2359 init_homedir();
2360 else if (didset_vim && STRICMP(name, "VIM") == 0)
2361 didset_vim = FALSE;
2362 else if (didset_vimruntime
2363 && STRICMP(name, "VIMRUNTIME") == 0)
2364 didset_vimruntime = FALSE;
2365 arg_end = arg;
2366 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 }
2370 }
2371 }
2372
2373 /*
2374 * ":let &option = expr": Set option value.
2375 * ":let &l:option = expr": Set local option value.
2376 * ":let &g:option = expr": Set global option value.
2377 */
2378 else if (*arg == '&')
2379 {
2380 /* Find the end of the name. */
2381 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002382 if (p == NULL || (endchars != NULL
2383 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 EMSG(_(e_letunexp));
2385 else
2386 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 long n;
2388 int opt_type;
2389 long numval;
2390 char_u *stringval = NULL;
2391 char_u *s;
2392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 c1 = *p;
2394 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395
2396 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002397 s = get_tv_string_chk(tv); /* != NULL if number or string */
2398 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399 {
2400 opt_type = get_option_value(arg, &numval,
2401 &stringval, opt_flags);
2402 if ((opt_type == 1 && *op == '.')
2403 || (opt_type == 0 && *op != '.'))
2404 EMSG2(_(e_letwrong), op);
2405 else
2406 {
2407 if (opt_type == 1) /* number */
2408 {
2409 if (*op == '+')
2410 n = numval + n;
2411 else
2412 n = numval - n;
2413 }
2414 else if (opt_type == 0 && stringval != NULL) /* string */
2415 {
2416 s = concat_str(stringval, s);
2417 vim_free(stringval);
2418 stringval = s;
2419 }
2420 }
2421 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002422 if (s != NULL)
2423 {
2424 set_option_value(arg, n, s, opt_flags);
2425 arg_end = p;
2426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 }
2430 }
2431
2432 /*
2433 * ":let @r = expr": Set register contents.
2434 */
2435 else if (*arg == '@')
2436 {
2437 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 if (op != NULL && (*op == '+' || *op == '-'))
2439 EMSG2(_(e_letwrong), op);
2440 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002441 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 EMSG(_(e_letunexp));
2443 else
2444 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002445 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 char_u *s;
2447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002448 p = get_tv_string_chk(tv);
2449 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002451 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 if (s != NULL)
2453 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 vim_free(s);
2456 }
2457 }
2458 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 arg_end = arg + 1;
2462 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002463 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 }
2465 }
2466
2467 /*
2468 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002471 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002473 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002475 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2479 EMSG(_(e_letunexp));
2480 else
2481 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 arg_end = p;
2484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 }
2488
2489 else
2490 EMSG2(_(e_invarg2), arg);
2491
2492 return arg_end;
2493}
2494
2495/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2497 */
2498 static int
2499check_changedtick(arg)
2500 char_u *arg;
2501{
2502 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2503 {
2504 EMSG2(_(e_readonlyvar), arg);
2505 return TRUE;
2506 }
2507 return FALSE;
2508}
2509
2510/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 * Get an lval: variable, Dict item or List item that can be assigned a value
2512 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2513 * "name.key", "name.key[expr]" etc.
2514 * Indexing only works if "name" is an existing List or Dictionary.
2515 * "name" points to the start of the name.
2516 * If "rettv" is not NULL it points to the value to be assigned.
2517 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2518 * wrong; must end in space or cmd separator.
2519 *
2520 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002521 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 */
2524 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002525get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 typval_T *rettv;
2528 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 int unlet;
2530 int skip;
2531 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002532 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 char_u *expr_start, *expr_end;
2536 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 dictitem_T *v;
2538 typval_T var1;
2539 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002545
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548
2549 if (skip)
2550 {
2551 /* When skipping just find the end of the name. */
2552 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 }
2555
2556 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002557 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (expr_start != NULL)
2559 {
2560 /* Don't expand the name when we already know there is an error. */
2561 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2562 && *p != '[' && *p != '.')
2563 {
2564 EMSG(_(e_trailing));
2565 return NULL;
2566 }
2567
2568 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2569 if (lp->ll_exp_name == NULL)
2570 {
2571 /* Report an invalid expression in braces, unless the
2572 * expression evaluation has been cancelled due to an
2573 * aborting error, an interrupt, or an exception. */
2574 if (!aborting() && !quiet)
2575 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002576 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 EMSG2(_(e_invarg2), name);
2578 return NULL;
2579 }
2580 }
2581 lp->ll_name = lp->ll_exp_name;
2582 }
2583 else
2584 lp->ll_name = name;
2585
2586 /* Without [idx] or .key we are done. */
2587 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2588 return p;
2589
2590 cc = *p;
2591 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002592 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 if (v == NULL && !quiet)
2594 EMSG2(_(e_undefvar), lp->ll_name);
2595 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 if (v == NULL)
2597 return NULL;
2598
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 /*
2600 * Loop until no more [idx] or .key is following.
2601 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002602 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2606 && !(lp->ll_tv->v_type == VAR_DICT
2607 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (!quiet)
2610 EMSG(_("E689: Can only index a List or Dictionary"));
2611 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (!quiet)
2616 EMSG(_("E708: [:] must come last"));
2617 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002619
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 len = -1;
2621 if (*p == '.')
2622 {
2623 key = p + 1;
2624 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2625 ;
2626 if (len == 0)
2627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 if (!quiet)
2629 EMSG(_(e_emptykey));
2630 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 }
2632 p = key + len;
2633 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 else
2635 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 if (*p == ':')
2639 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002640 else
2641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 empty1 = FALSE;
2643 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002645 if (get_tv_string_chk(&var1) == NULL)
2646 {
2647 /* not a number or string */
2648 clear_tv(&var1);
2649 return NULL;
2650 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 }
2652
2653 /* Optionally get the second index [ :expr]. */
2654 if (*p == ':')
2655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002659 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 if (!empty1)
2661 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (rettv != NULL && (rettv->v_type != VAR_LIST
2665 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
2668 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 if (!empty1)
2670 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 }
2673 p = skipwhite(p + 1);
2674 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 else
2677 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 if (!empty1)
2682 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002685 if (get_tv_string_chk(&var2) == NULL)
2686 {
2687 /* not a number or string */
2688 if (!empty1)
2689 clear_tv(&var1);
2690 clear_tv(&var2);
2691 return NULL;
2692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!quiet)
2702 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (!empty1)
2704 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
2709
2710 /* Skip to past ']'. */
2711 ++p;
2712 }
2713
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 {
2716 if (len == -1)
2717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002719 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (*key == NUL)
2721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (!quiet)
2723 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002728 lp->ll_list = NULL;
2729 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002730 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002731
2732 /* When assigning to g: check that a function and variable name is
2733 * valid. */
2734 if (rettv != NULL && lp->ll_dict == &globvardict)
2735 {
2736 if (rettv->v_type == VAR_FUNC
2737 && var_check_func_name(key, lp->ll_di == NULL))
2738 return NULL;
2739 if (!valid_varname(key))
2740 return NULL;
2741 }
2742
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002745 /* Can't add "v:" variable. */
2746 if (lp->ll_dict == &vimvardict)
2747 {
2748 EMSG2(_(e_illvar), name);
2749 return NULL;
2750 }
2751
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002752 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002756 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (len == -1)
2758 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 }
2761 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (len == -1)
2766 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 p = NULL;
2769 break;
2770 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002771 /* existing variable, need to check if it can be changed */
2772 else if (var_check_ro(lp->ll_di->di_flags, name))
2773 return NULL;
2774
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 if (len == -1)
2776 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 }
2779 else
2780 {
2781 /*
2782 * Get the number and item for the only or first index of the List.
2783 */
2784 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 else
2787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002788 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 clear_tv(&var1);
2790 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002791 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 lp->ll_list = lp->ll_tv->vval.v_list;
2793 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2794 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002796 if (lp->ll_n1 < 0)
2797 {
2798 lp->ll_n1 = 0;
2799 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2800 }
2801 }
2802 if (lp->ll_li == NULL)
2803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002806 if (!quiet)
2807 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 }
2810
2811 /*
2812 * May need to find the item or absolute index for the second
2813 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 * When no index given: "lp->ll_empty2" is TRUE.
2815 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002819 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002825 {
2826 if (!quiet)
2827 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002829 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 }
2832
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2834 if (lp->ll_n1 < 0)
2835 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2836 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002837 {
2838 if (!quiet)
2839 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002841 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002842 }
2843
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002846 }
2847
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 return p;
2849}
2850
2851/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002852 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 */
2854 static void
2855clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002856 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857{
2858 vim_free(lp->ll_exp_name);
2859 vim_free(lp->ll_newkey);
2860}
2861
2862/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002863 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002865 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 */
2867 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002868set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002869 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002871 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002873 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874{
2875 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002876 listitem_T *ri;
2877 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878
2879 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002880 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 cc = *endp;
2884 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002885 if (op != NULL && *op != '=')
2886 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002887 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002888
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002889 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002890 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002891 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002892 {
2893 if (tv_op(&tv, rettv, op) == OK)
2894 set_var(lp->ll_name, &tv, FALSE);
2895 clear_tv(&tv);
2896 }
2897 }
2898 else
2899 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002901 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002903 else if (tv_check_lock(lp->ll_newkey == NULL
2904 ? lp->ll_tv->v_lock
2905 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2906 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 else if (lp->ll_range)
2908 {
2909 /*
2910 * Assign the List values to the list items.
2911 */
2912 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002913 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002914 if (op != NULL && *op != '=')
2915 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2916 else
2917 {
2918 clear_tv(&lp->ll_li->li_tv);
2919 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2920 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 ri = ri->li_next;
2922 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2923 break;
2924 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002925 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002927 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002928 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 ri = NULL;
2930 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002931 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002932 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 lp->ll_li = lp->ll_li->li_next;
2934 ++lp->ll_n1;
2935 }
2936 if (ri != NULL)
2937 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938 else if (lp->ll_empty2
2939 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 : lp->ll_n1 != lp->ll_n2)
2941 EMSG(_("E711: List value has not enough items"));
2942 }
2943 else
2944 {
2945 /*
2946 * Assign to a List or Dictionary item.
2947 */
2948 if (lp->ll_newkey != NULL)
2949 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 if (op != NULL && *op != '=')
2951 {
2952 EMSG2(_(e_letwrong), op);
2953 return;
2954 }
2955
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002957 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 if (di == NULL)
2959 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002960 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2961 {
2962 vim_free(di);
2963 return;
2964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002966 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002967 else if (op != NULL && *op != '=')
2968 {
2969 tv_op(lp->ll_tv, rettv, op);
2970 return;
2971 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002972 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002974
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 /*
2976 * Assign the value to the variable or list item.
2977 */
2978 if (copy)
2979 copy_tv(rettv, lp->ll_tv);
2980 else
2981 {
2982 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002983 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002985 }
2986 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002987}
2988
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002989/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2991 * Returns OK or FAIL.
2992 */
2993 static int
2994tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002995 typval_T *tv1;
2996 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 char_u *op;
2998{
2999 long n;
3000 char_u numbuf[NUMBUFLEN];
3001 char_u *s;
3002
3003 /* Can't do anything with a Funcref or a Dict on the right. */
3004 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3005 {
3006 switch (tv1->v_type)
3007 {
3008 case VAR_DICT:
3009 case VAR_FUNC:
3010 break;
3011
3012 case VAR_LIST:
3013 if (*op != '+' || tv2->v_type != VAR_LIST)
3014 break;
3015 /* List += List */
3016 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3017 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3018 return OK;
3019
3020 case VAR_NUMBER:
3021 case VAR_STRING:
3022 if (tv2->v_type == VAR_LIST)
3023 break;
3024 if (*op == '+' || *op == '-')
3025 {
3026 /* nr += nr or nr -= nr*/
3027 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003028#ifdef FEAT_FLOAT
3029 if (tv2->v_type == VAR_FLOAT)
3030 {
3031 float_T f = n;
3032
3033 if (*op == '+')
3034 f += tv2->vval.v_float;
3035 else
3036 f -= tv2->vval.v_float;
3037 clear_tv(tv1);
3038 tv1->v_type = VAR_FLOAT;
3039 tv1->vval.v_float = f;
3040 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003041 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003042#endif
3043 {
3044 if (*op == '+')
3045 n += get_tv_number(tv2);
3046 else
3047 n -= get_tv_number(tv2);
3048 clear_tv(tv1);
3049 tv1->v_type = VAR_NUMBER;
3050 tv1->vval.v_number = n;
3051 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 }
3053 else
3054 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003055 if (tv2->v_type == VAR_FLOAT)
3056 break;
3057
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003058 /* str .= str */
3059 s = get_tv_string(tv1);
3060 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3061 clear_tv(tv1);
3062 tv1->v_type = VAR_STRING;
3063 tv1->vval.v_string = s;
3064 }
3065 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003066
3067#ifdef FEAT_FLOAT
3068 case VAR_FLOAT:
3069 {
3070 float_T f;
3071
3072 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3073 && tv2->v_type != VAR_NUMBER
3074 && tv2->v_type != VAR_STRING))
3075 break;
3076 if (tv2->v_type == VAR_FLOAT)
3077 f = tv2->vval.v_float;
3078 else
3079 f = get_tv_number(tv2);
3080 if (*op == '+')
3081 tv1->vval.v_float += f;
3082 else
3083 tv1->vval.v_float -= f;
3084 }
3085 return OK;
3086#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 }
3088 }
3089
3090 EMSG2(_(e_letwrong), op);
3091 return FAIL;
3092}
3093
3094/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003095 * Add a watcher to a list.
3096 */
3097 static void
3098list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003099 list_T *l;
3100 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003101{
3102 lw->lw_next = l->lv_watch;
3103 l->lv_watch = lw;
3104}
3105
3106/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003107 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108 * No warning when it isn't found...
3109 */
3110 static void
3111list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003112 list_T *l;
3113 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114{
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003116
3117 lwp = &l->lv_watch;
3118 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3119 {
3120 if (lw == lwrem)
3121 {
3122 *lwp = lw->lw_next;
3123 break;
3124 }
3125 lwp = &lw->lw_next;
3126 }
3127}
3128
3129/*
3130 * Just before removing an item from a list: advance watchers to the next
3131 * item.
3132 */
3133 static void
3134list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003135 list_T *l;
3136 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137{
Bram Moolenaar33570922005-01-25 22:26:29 +00003138 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003139
3140 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3141 if (lw->lw_item == item)
3142 lw->lw_item = item->li_next;
3143}
3144
3145/*
3146 * Evaluate the expression used in a ":for var in expr" command.
3147 * "arg" points to "var".
3148 * Set "*errp" to TRUE for an error, FALSE otherwise;
3149 * Return a pointer that holds the info. Null when there is an error.
3150 */
3151 void *
3152eval_for_line(arg, errp, nextcmdp, skip)
3153 char_u *arg;
3154 int *errp;
3155 char_u **nextcmdp;
3156 int skip;
3157{
Bram Moolenaar33570922005-01-25 22:26:29 +00003158 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003159 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003160 typval_T tv;
3161 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162
3163 *errp = TRUE; /* default: there is an error */
3164
Bram Moolenaar33570922005-01-25 22:26:29 +00003165 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003166 if (fi == NULL)
3167 return NULL;
3168
3169 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3170 if (expr == NULL)
3171 return fi;
3172
3173 expr = skipwhite(expr);
3174 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3175 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003176 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 return fi;
3178 }
3179
3180 if (skip)
3181 ++emsg_skip;
3182 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3183 {
3184 *errp = FALSE;
3185 if (!skip)
3186 {
3187 l = tv.vval.v_list;
3188 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003189 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003190 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003191 clear_tv(&tv);
3192 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193 else
3194 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003195 /* No need to increment the refcount, it's already set for the
3196 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197 fi->fi_list = l;
3198 list_add_watch(l, &fi->fi_lw);
3199 fi->fi_lw.lw_item = l->lv_first;
3200 }
3201 }
3202 }
3203 if (skip)
3204 --emsg_skip;
3205
3206 return fi;
3207}
3208
3209/*
3210 * Use the first item in a ":for" list. Advance to the next.
3211 * Assign the values to the variable (list). "arg" points to the first one.
3212 * Return TRUE when a valid item was found, FALSE when at end of list or
3213 * something wrong.
3214 */
3215 int
3216next_for_item(fi_void, arg)
3217 void *fi_void;
3218 char_u *arg;
3219{
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223
3224 item = fi->fi_lw.lw_item;
3225 if (item == NULL)
3226 result = FALSE;
3227 else
3228 {
3229 fi->fi_lw.lw_item = item->li_next;
3230 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3231 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3232 }
3233 return result;
3234}
3235
3236/*
3237 * Free the structure used to store info used by ":for".
3238 */
3239 void
3240free_for_info(fi_void)
3241 void *fi_void;
3242{
Bram Moolenaar33570922005-01-25 22:26:29 +00003243 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003245 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003246 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003248 list_unref(fi->fi_list);
3249 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250 vim_free(fi);
3251}
3252
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3254
3255 void
3256set_context_for_expression(xp, arg, cmdidx)
3257 expand_T *xp;
3258 char_u *arg;
3259 cmdidx_T cmdidx;
3260{
3261 int got_eq = FALSE;
3262 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 if (cmdidx == CMD_let)
3266 {
3267 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003268 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 {
3270 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003271 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 {
3273 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 if (vim_iswhite(*p))
3276 break;
3277 }
3278 return;
3279 }
3280 }
3281 else
3282 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3283 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 while ((xp->xp_pattern = vim_strpbrk(arg,
3285 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3286 {
3287 c = *xp->xp_pattern;
3288 if (c == '&')
3289 {
3290 c = xp->xp_pattern[1];
3291 if (c == '&')
3292 {
3293 ++xp->xp_pattern;
3294 xp->xp_context = cmdidx != CMD_let || got_eq
3295 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3296 }
3297 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003300 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3301 xp->xp_pattern += 2;
3302
3303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
3305 else if (c == '$')
3306 {
3307 /* environment variable */
3308 xp->xp_context = EXPAND_ENV_VARS;
3309 }
3310 else if (c == '=')
3311 {
3312 got_eq = TRUE;
3313 xp->xp_context = EXPAND_EXPRESSION;
3314 }
3315 else if (c == '<'
3316 && xp->xp_context == EXPAND_FUNCTIONS
3317 && vim_strchr(xp->xp_pattern, '(') == NULL)
3318 {
3319 /* Function name can start with "<SNR>" */
3320 break;
3321 }
3322 else if (cmdidx != CMD_let || got_eq)
3323 {
3324 if (c == '"') /* string */
3325 {
3326 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3327 if (c == '\\' && xp->xp_pattern[1] != NUL)
3328 ++xp->xp_pattern;
3329 xp->xp_context = EXPAND_NOTHING;
3330 }
3331 else if (c == '\'') /* literal string */
3332 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003333 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3335 /* skip */ ;
3336 xp->xp_context = EXPAND_NOTHING;
3337 }
3338 else if (c == '|')
3339 {
3340 if (xp->xp_pattern[1] == '|')
3341 {
3342 ++xp->xp_pattern;
3343 xp->xp_context = EXPAND_EXPRESSION;
3344 }
3345 else
3346 xp->xp_context = EXPAND_COMMANDS;
3347 }
3348 else
3349 xp->xp_context = EXPAND_EXPRESSION;
3350 }
3351 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 /* Doesn't look like something valid, expand as an expression
3353 * anyway. */
3354 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 arg = xp->xp_pattern;
3356 if (*arg != NUL)
3357 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3358 /* skip */ ;
3359 }
3360 xp->xp_pattern = arg;
3361}
3362
3363#endif /* FEAT_CMDL_COMPL */
3364
3365/*
3366 * ":1,25call func(arg1, arg2)" function call.
3367 */
3368 void
3369ex_call(eap)
3370 exarg_T *eap;
3371{
3372 char_u *arg = eap->arg;
3373 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003375 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003377 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 linenr_T lnum;
3379 int doesrange;
3380 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003381 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003383 if (eap->skip)
3384 {
3385 /* trans_function_name() doesn't work well when skipping, use eval0()
3386 * instead to skip to any following command, e.g. for:
3387 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003388 ++emsg_skip;
3389 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3390 clear_tv(&rettv);
3391 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003392 return;
3393 }
3394
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003395 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003396 if (fudi.fd_newkey != NULL)
3397 {
3398 /* Still need to give an error message for missing key. */
3399 EMSG2(_(e_dictkey), fudi.fd_newkey);
3400 vim_free(fudi.fd_newkey);
3401 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003402 if (tofree == NULL)
3403 return;
3404
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003405 /* Increase refcount on dictionary, it could get deleted when evaluating
3406 * the arguments. */
3407 if (fudi.fd_dict != NULL)
3408 ++fudi.fd_dict->dv_refcount;
3409
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003410 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003411 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003412 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaar532c7802005-01-27 14:44:31 +00003414 /* Skip white space to allow ":call func ()". Not good, but required for
3415 * backward compatibility. */
3416 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003417 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
3419 if (*startarg != '(')
3420 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003421 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 goto end;
3423 }
3424
3425 /*
3426 * When skipping, evaluate the function once, to find the end of the
3427 * arguments.
3428 * When the function takes a range, this is discovered after the first
3429 * call, and the loop is broken.
3430 */
3431 if (eap->skip)
3432 {
3433 ++emsg_skip;
3434 lnum = eap->line2; /* do it once, also with an invalid range */
3435 }
3436 else
3437 lnum = eap->line1;
3438 for ( ; lnum <= eap->line2; ++lnum)
3439 {
3440 if (!eap->skip && eap->addr_count > 0)
3441 {
3442 curwin->w_cursor.lnum = lnum;
3443 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003444#ifdef FEAT_VIRTUALEDIT
3445 curwin->w_cursor.coladd = 0;
3446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003449 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003450 eap->line1, eap->line2, &doesrange,
3451 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 failed = TRUE;
3454 break;
3455 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003456
3457 /* Handle a function returning a Funcref, Dictionary or List. */
3458 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3459 {
3460 failed = TRUE;
3461 break;
3462 }
3463
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003464 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 if (doesrange || eap->skip)
3466 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003467
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003469 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003470 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003471 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 if (aborting())
3473 break;
3474 }
3475 if (eap->skip)
3476 --emsg_skip;
3477
3478 if (!failed)
3479 {
3480 /* Check for trailing illegal characters and a following command. */
3481 if (!ends_excmd(*arg))
3482 {
3483 emsg_severe = TRUE;
3484 EMSG(_(e_trailing));
3485 }
3486 else
3487 eap->nextcmd = check_nextcmd(arg);
3488 }
3489
3490end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003491 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003492 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493}
3494
3495/*
3496 * ":unlet[!] var1 ... " command.
3497 */
3498 void
3499ex_unlet(eap)
3500 exarg_T *eap;
3501{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003502 ex_unletlock(eap, eap->arg, 0);
3503}
3504
3505/*
3506 * ":lockvar" and ":unlockvar" commands
3507 */
3508 void
3509ex_lockvar(eap)
3510 exarg_T *eap;
3511{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003513 int deep = 2;
3514
3515 if (eap->forceit)
3516 deep = -1;
3517 else if (vim_isdigit(*arg))
3518 {
3519 deep = getdigits(&arg);
3520 arg = skipwhite(arg);
3521 }
3522
3523 ex_unletlock(eap, arg, deep);
3524}
3525
3526/*
3527 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3528 */
3529 static void
3530ex_unletlock(eap, argstart, deep)
3531 exarg_T *eap;
3532 char_u *argstart;
3533 int deep;
3534{
3535 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003538 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
3540 do
3541 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003542 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003543 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3544 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003545 if (lv.ll_name == NULL)
3546 error = TRUE; /* error but continue parsing */
3547 if (name_end == NULL || (!vim_iswhite(*name_end)
3548 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003550 if (name_end != NULL)
3551 {
3552 emsg_severe = TRUE;
3553 EMSG(_(e_trailing));
3554 }
3555 if (!(eap->skip || error))
3556 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 break;
3558 }
3559
3560 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003561 {
3562 if (eap->cmdidx == CMD_unlet)
3563 {
3564 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3565 error = TRUE;
3566 }
3567 else
3568 {
3569 if (do_lock_var(&lv, name_end, deep,
3570 eap->cmdidx == CMD_lockvar) == FAIL)
3571 error = TRUE;
3572 }
3573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003575 if (!eap->skip)
3576 clear_lval(&lv);
3577
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 arg = skipwhite(name_end);
3579 } while (!ends_excmd(*arg));
3580
3581 eap->nextcmd = check_nextcmd(arg);
3582}
3583
Bram Moolenaar8c711452005-01-14 21:53:12 +00003584 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003585do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003586 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003587 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003588 int forceit;
3589{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003590 int ret = OK;
3591 int cc;
3592
3593 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003594 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003595 cc = *name_end;
3596 *name_end = NUL;
3597
3598 /* Normal name or expanded name. */
3599 if (check_changedtick(lp->ll_name))
3600 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003601 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003602 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3606 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 else if (lp->ll_range)
3608 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003609 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610
3611 /* Delete a range of List items. */
3612 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3613 {
3614 li = lp->ll_li->li_next;
3615 listitem_remove(lp->ll_list, lp->ll_li);
3616 lp->ll_li = li;
3617 ++lp->ll_n1;
3618 }
3619 }
3620 else
3621 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 /* unlet a List item. */
3624 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003627 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003628 }
3629
3630 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003631}
3632
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633/*
3634 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003635 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 */
3637 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003640 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
Bram Moolenaar33570922005-01-25 22:26:29 +00003642 hashtab_T *ht;
3643 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003644 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003645 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
Bram Moolenaar33570922005-01-25 22:26:29 +00003647 ht = find_var_ht(name, &varname);
3648 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003650 hi = hash_find(ht, varname);
3651 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003652 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003653 di = HI2DI(hi);
3654 if (var_check_fixed(di->di_flags, name)
3655 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003656 return FAIL;
3657 delete_var(ht, hi);
3658 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661 if (forceit)
3662 return OK;
3663 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 return FAIL;
3665}
3666
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667/*
3668 * Lock or unlock variable indicated by "lp".
3669 * "deep" is the levels to go (-1 for unlimited);
3670 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3671 */
3672 static int
3673do_lock_var(lp, name_end, deep, lock)
3674 lval_T *lp;
3675 char_u *name_end;
3676 int deep;
3677 int lock;
3678{
3679 int ret = OK;
3680 int cc;
3681 dictitem_T *di;
3682
3683 if (deep == 0) /* nothing to do */
3684 return OK;
3685
3686 if (lp->ll_tv == NULL)
3687 {
3688 cc = *name_end;
3689 *name_end = NUL;
3690
3691 /* Normal name or expanded name. */
3692 if (check_changedtick(lp->ll_name))
3693 ret = FAIL;
3694 else
3695 {
3696 di = find_var(lp->ll_name, NULL);
3697 if (di == NULL)
3698 ret = FAIL;
3699 else
3700 {
3701 if (lock)
3702 di->di_flags |= DI_FLAGS_LOCK;
3703 else
3704 di->di_flags &= ~DI_FLAGS_LOCK;
3705 item_lock(&di->di_tv, deep, lock);
3706 }
3707 }
3708 *name_end = cc;
3709 }
3710 else if (lp->ll_range)
3711 {
3712 listitem_T *li = lp->ll_li;
3713
3714 /* (un)lock a range of List items. */
3715 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3716 {
3717 item_lock(&li->li_tv, deep, lock);
3718 li = li->li_next;
3719 ++lp->ll_n1;
3720 }
3721 }
3722 else if (lp->ll_list != NULL)
3723 /* (un)lock a List item. */
3724 item_lock(&lp->ll_li->li_tv, deep, lock);
3725 else
3726 /* un(lock) a Dictionary item. */
3727 item_lock(&lp->ll_di->di_tv, deep, lock);
3728
3729 return ret;
3730}
3731
3732/*
3733 * Lock or unlock an item. "deep" is nr of levels to go.
3734 */
3735 static void
3736item_lock(tv, deep, lock)
3737 typval_T *tv;
3738 int deep;
3739 int lock;
3740{
3741 static int recurse = 0;
3742 list_T *l;
3743 listitem_T *li;
3744 dict_T *d;
3745 hashitem_T *hi;
3746 int todo;
3747
3748 if (recurse >= DICT_MAXNEST)
3749 {
3750 EMSG(_("E743: variable nested too deep for (un)lock"));
3751 return;
3752 }
3753 if (deep == 0)
3754 return;
3755 ++recurse;
3756
3757 /* lock/unlock the item itself */
3758 if (lock)
3759 tv->v_lock |= VAR_LOCKED;
3760 else
3761 tv->v_lock &= ~VAR_LOCKED;
3762
3763 switch (tv->v_type)
3764 {
3765 case VAR_LIST:
3766 if ((l = tv->vval.v_list) != NULL)
3767 {
3768 if (lock)
3769 l->lv_lock |= VAR_LOCKED;
3770 else
3771 l->lv_lock &= ~VAR_LOCKED;
3772 if (deep < 0 || deep > 1)
3773 /* recursive: lock/unlock the items the List contains */
3774 for (li = l->lv_first; li != NULL; li = li->li_next)
3775 item_lock(&li->li_tv, deep - 1, lock);
3776 }
3777 break;
3778 case VAR_DICT:
3779 if ((d = tv->vval.v_dict) != NULL)
3780 {
3781 if (lock)
3782 d->dv_lock |= VAR_LOCKED;
3783 else
3784 d->dv_lock &= ~VAR_LOCKED;
3785 if (deep < 0 || deep > 1)
3786 {
3787 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003788 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003789 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3790 {
3791 if (!HASHITEM_EMPTY(hi))
3792 {
3793 --todo;
3794 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3795 }
3796 }
3797 }
3798 }
3799 }
3800 --recurse;
3801}
3802
Bram Moolenaara40058a2005-07-11 22:42:07 +00003803/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003804 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3805 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003806 */
3807 static int
3808tv_islocked(tv)
3809 typval_T *tv;
3810{
3811 return (tv->v_lock & VAR_LOCKED)
3812 || (tv->v_type == VAR_LIST
3813 && tv->vval.v_list != NULL
3814 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3815 || (tv->v_type == VAR_DICT
3816 && tv->vval.v_dict != NULL
3817 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3818}
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3821/*
3822 * Delete all "menutrans_" variables.
3823 */
3824 void
3825del_menutrans_vars()
3826{
Bram Moolenaar33570922005-01-25 22:26:29 +00003827 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003828 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829
Bram Moolenaar33570922005-01-25 22:26:29 +00003830 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003831 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003832 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003833 {
3834 if (!HASHITEM_EMPTY(hi))
3835 {
3836 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003837 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3838 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003839 }
3840 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003841 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842}
3843#endif
3844
3845#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3846
3847/*
3848 * Local string buffer for the next two functions to store a variable name
3849 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3850 * get_user_var_name().
3851 */
3852
3853static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3854
3855static char_u *varnamebuf = NULL;
3856static int varnamebuflen = 0;
3857
3858/*
3859 * Function to concatenate a prefix and a variable name.
3860 */
3861 static char_u *
3862cat_prefix_varname(prefix, name)
3863 int prefix;
3864 char_u *name;
3865{
3866 int len;
3867
3868 len = (int)STRLEN(name) + 3;
3869 if (len > varnamebuflen)
3870 {
3871 vim_free(varnamebuf);
3872 len += 10; /* some additional space */
3873 varnamebuf = alloc(len);
3874 if (varnamebuf == NULL)
3875 {
3876 varnamebuflen = 0;
3877 return NULL;
3878 }
3879 varnamebuflen = len;
3880 }
3881 *varnamebuf = prefix;
3882 varnamebuf[1] = ':';
3883 STRCPY(varnamebuf + 2, name);
3884 return varnamebuf;
3885}
3886
3887/*
3888 * Function given to ExpandGeneric() to obtain the list of user defined
3889 * (global/buffer/window/built-in) variable names.
3890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 char_u *
3892get_user_var_name(xp, idx)
3893 expand_T *xp;
3894 int idx;
3895{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003896 static long_u gdone;
3897 static long_u bdone;
3898 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003899#ifdef FEAT_WINDOWS
3900 static long_u tdone;
3901#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003902 static int vidx;
3903 static hashitem_T *hi;
3904 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
3906 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003907 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003908 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003909#ifdef FEAT_WINDOWS
3910 tdone = 0;
3911#endif
3912 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003913
3914 /* Global variables */
3915 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003917 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003918 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003919 else
3920 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003921 while (HASHITEM_EMPTY(hi))
3922 ++hi;
3923 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3924 return cat_prefix_varname('g', hi->hi_key);
3925 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003927
3928 /* b: variables */
3929 ht = &curbuf->b_vars.dv_hashtab;
3930 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003933 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003934 else
3935 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003936 while (HASHITEM_EMPTY(hi))
3937 ++hi;
3938 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 return (char_u *)"b:changedtick";
3944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003945
3946 /* w: variables */
3947 ht = &curwin->w_vars.dv_hashtab;
3948 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003950 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003952 else
3953 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 while (HASHITEM_EMPTY(hi))
3955 ++hi;
3956 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003958
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003959#ifdef FEAT_WINDOWS
3960 /* t: variables */
3961 ht = &curtab->tp_vars.dv_hashtab;
3962 if (tdone < ht->ht_used)
3963 {
3964 if (tdone++ == 0)
3965 hi = ht->ht_array;
3966 else
3967 ++hi;
3968 while (HASHITEM_EMPTY(hi))
3969 ++hi;
3970 return cat_prefix_varname('t', hi->hi_key);
3971 }
3972#endif
3973
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 /* v: variables */
3975 if (vidx < VV_LEN)
3976 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978 vim_free(varnamebuf);
3979 varnamebuf = NULL;
3980 varnamebuflen = 0;
3981 return NULL;
3982}
3983
3984#endif /* FEAT_CMDL_COMPL */
3985
3986/*
3987 * types for expressions.
3988 */
3989typedef enum
3990{
3991 TYPE_UNKNOWN = 0
3992 , TYPE_EQUAL /* == */
3993 , TYPE_NEQUAL /* != */
3994 , TYPE_GREATER /* > */
3995 , TYPE_GEQUAL /* >= */
3996 , TYPE_SMALLER /* < */
3997 , TYPE_SEQUAL /* <= */
3998 , TYPE_MATCH /* =~ */
3999 , TYPE_NOMATCH /* !~ */
4000} exptype_T;
4001
4002/*
4003 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004004 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4006 */
4007
4008/*
4009 * Handle zero level expression.
4010 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004011 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004012 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 * Return OK or FAIL.
4014 */
4015 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004016eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 char_u **nextcmd;
4020 int evaluate;
4021{
4022 int ret;
4023 char_u *p;
4024
4025 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 if (ret == FAIL || !ends_excmd(*p))
4028 {
4029 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004030 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 /*
4032 * Report the invalid expression unless the expression evaluation has
4033 * been cancelled due to an aborting error, an interrupt, or an
4034 * exception.
4035 */
4036 if (!aborting())
4037 EMSG2(_(e_invexpr2), arg);
4038 ret = FAIL;
4039 }
4040 if (nextcmd != NULL)
4041 *nextcmd = check_nextcmd(p);
4042
4043 return ret;
4044}
4045
4046/*
4047 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004048 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 *
4050 * "arg" must point to the first non-white of the expression.
4051 * "arg" is advanced to the next non-white after the recognized expression.
4052 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004053 * Note: "rettv.v_lock" is not set.
4054 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 * Return OK or FAIL.
4056 */
4057 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 int evaluate;
4062{
4063 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004064 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 /*
4067 * Get the first variable.
4068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 return FAIL;
4071
4072 if ((*arg)[0] == '?')
4073 {
4074 result = FALSE;
4075 if (evaluate)
4076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004077 int error = FALSE;
4078
4079 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004082 if (error)
4083 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085
4086 /*
4087 * Get the second variable.
4088 */
4089 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 return FAIL;
4092
4093 /*
4094 * Check for the ":".
4095 */
4096 if ((*arg)[0] != ':')
4097 {
4098 EMSG(_("E109: Missing ':' after '?'"));
4099 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 return FAIL;
4102 }
4103
4104 /*
4105 * Get the third variable.
4106 */
4107 *arg = skipwhite(*arg + 1);
4108 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4109 {
4110 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 return FAIL;
4113 }
4114 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117
4118 return OK;
4119}
4120
4121/*
4122 * Handle first level expression:
4123 * expr2 || expr2 || expr2 logical OR
4124 *
4125 * "arg" must point to the first non-white of the expression.
4126 * "arg" is advanced to the next non-white after the recognized expression.
4127 *
4128 * Return OK or FAIL.
4129 */
4130 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 int evaluate;
4135{
Bram Moolenaar33570922005-01-25 22:26:29 +00004136 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 long result;
4138 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004139 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140
4141 /*
4142 * Get the first variable.
4143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 return FAIL;
4146
4147 /*
4148 * Repeat until there is no following "||".
4149 */
4150 first = TRUE;
4151 result = FALSE;
4152 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4153 {
4154 if (evaluate && first)
4155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004156 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004158 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004159 if (error)
4160 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 first = FALSE;
4162 }
4163
4164 /*
4165 * Get the second variable.
4166 */
4167 *arg = skipwhite(*arg + 2);
4168 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4169 return FAIL;
4170
4171 /*
4172 * Compute the result.
4173 */
4174 if (evaluate && !result)
4175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004176 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 if (error)
4180 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182 if (evaluate)
4183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004184 rettv->v_type = VAR_NUMBER;
4185 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 }
4188
4189 return OK;
4190}
4191
4192/*
4193 * Handle second level expression:
4194 * expr3 && expr3 && expr3 logical AND
4195 *
4196 * "arg" must point to the first non-white of the expression.
4197 * "arg" is advanced to the next non-white after the recognized expression.
4198 *
4199 * Return OK or FAIL.
4200 */
4201 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 int evaluate;
4206{
Bram Moolenaar33570922005-01-25 22:26:29 +00004207 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 long result;
4209 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004210 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
4212 /*
4213 * Get the first variable.
4214 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return FAIL;
4217
4218 /*
4219 * Repeat until there is no following "&&".
4220 */
4221 first = TRUE;
4222 result = TRUE;
4223 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4224 {
4225 if (evaluate && first)
4226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004227 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004230 if (error)
4231 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 first = FALSE;
4233 }
4234
4235 /*
4236 * Get the second variable.
4237 */
4238 *arg = skipwhite(*arg + 2);
4239 if (eval4(arg, &var2, evaluate && result) == FAIL)
4240 return FAIL;
4241
4242 /*
4243 * Compute the result.
4244 */
4245 if (evaluate && result)
4246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 if (error)
4251 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
4253 if (evaluate)
4254 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 rettv->v_type = VAR_NUMBER;
4256 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 }
4259
4260 return OK;
4261}
4262
4263/*
4264 * Handle third level expression:
4265 * var1 == var2
4266 * var1 =~ var2
4267 * var1 != var2
4268 * var1 !~ var2
4269 * var1 > var2
4270 * var1 >= var2
4271 * var1 < var2
4272 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004273 * var1 is var2
4274 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 *
4276 * "arg" must point to the first non-white of the expression.
4277 * "arg" is advanced to the next non-white after the recognized expression.
4278 *
4279 * Return OK or FAIL.
4280 */
4281 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 int evaluate;
4286{
Bram Moolenaar33570922005-01-25 22:26:29 +00004287 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 char_u *p;
4289 int i;
4290 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004291 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 int len = 2;
4293 long n1, n2;
4294 char_u *s1, *s2;
4295 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4296 regmatch_T regmatch;
4297 int ic;
4298 char_u *save_cpo;
4299
4300 /*
4301 * Get the first variable.
4302 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004303 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 return FAIL;
4305
4306 p = *arg;
4307 switch (p[0])
4308 {
4309 case '=': if (p[1] == '=')
4310 type = TYPE_EQUAL;
4311 else if (p[1] == '~')
4312 type = TYPE_MATCH;
4313 break;
4314 case '!': if (p[1] == '=')
4315 type = TYPE_NEQUAL;
4316 else if (p[1] == '~')
4317 type = TYPE_NOMATCH;
4318 break;
4319 case '>': if (p[1] != '=')
4320 {
4321 type = TYPE_GREATER;
4322 len = 1;
4323 }
4324 else
4325 type = TYPE_GEQUAL;
4326 break;
4327 case '<': if (p[1] != '=')
4328 {
4329 type = TYPE_SMALLER;
4330 len = 1;
4331 }
4332 else
4333 type = TYPE_SEQUAL;
4334 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004335 case 'i': if (p[1] == 's')
4336 {
4337 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4338 len = 5;
4339 if (!vim_isIDc(p[len]))
4340 {
4341 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4342 type_is = TRUE;
4343 }
4344 }
4345 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347
4348 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004349 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 */
4351 if (type != TYPE_UNKNOWN)
4352 {
4353 /* extra question mark appended: ignore case */
4354 if (p[len] == '?')
4355 {
4356 ic = TRUE;
4357 ++len;
4358 }
4359 /* extra '#' appended: match case */
4360 else if (p[len] == '#')
4361 {
4362 ic = FALSE;
4363 ++len;
4364 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004365 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 else
4367 ic = p_ic;
4368
4369 /*
4370 * Get the second variable.
4371 */
4372 *arg = skipwhite(p + len);
4373 if (eval5(arg, &var2, evaluate) == FAIL)
4374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004375 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 return FAIL;
4377 }
4378
4379 if (evaluate)
4380 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004381 if (type_is && rettv->v_type != var2.v_type)
4382 {
4383 /* For "is" a different type always means FALSE, for "notis"
4384 * it means TRUE. */
4385 n1 = (type == TYPE_NEQUAL);
4386 }
4387 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4388 {
4389 if (type_is)
4390 {
4391 n1 = (rettv->v_type == var2.v_type
4392 && rettv->vval.v_list == var2.vval.v_list);
4393 if (type == TYPE_NEQUAL)
4394 n1 = !n1;
4395 }
4396 else if (rettv->v_type != var2.v_type
4397 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4398 {
4399 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004400 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004401 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004402 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004403 clear_tv(rettv);
4404 clear_tv(&var2);
4405 return FAIL;
4406 }
4407 else
4408 {
4409 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004410 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4411 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004412 if (type == TYPE_NEQUAL)
4413 n1 = !n1;
4414 }
4415 }
4416
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004417 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4418 {
4419 if (type_is)
4420 {
4421 n1 = (rettv->v_type == var2.v_type
4422 && rettv->vval.v_dict == var2.vval.v_dict);
4423 if (type == TYPE_NEQUAL)
4424 n1 = !n1;
4425 }
4426 else if (rettv->v_type != var2.v_type
4427 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4428 {
4429 if (rettv->v_type != var2.v_type)
4430 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4431 else
4432 EMSG(_("E736: Invalid operation for Dictionary"));
4433 clear_tv(rettv);
4434 clear_tv(&var2);
4435 return FAIL;
4436 }
4437 else
4438 {
4439 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004440 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4441 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004442 if (type == TYPE_NEQUAL)
4443 n1 = !n1;
4444 }
4445 }
4446
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004447 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4448 {
4449 if (rettv->v_type != var2.v_type
4450 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4451 {
4452 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004453 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004454 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004455 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004456 clear_tv(rettv);
4457 clear_tv(&var2);
4458 return FAIL;
4459 }
4460 else
4461 {
4462 /* Compare two Funcrefs for being equal or unequal. */
4463 if (rettv->vval.v_string == NULL
4464 || var2.vval.v_string == NULL)
4465 n1 = FALSE;
4466 else
4467 n1 = STRCMP(rettv->vval.v_string,
4468 var2.vval.v_string) == 0;
4469 if (type == TYPE_NEQUAL)
4470 n1 = !n1;
4471 }
4472 }
4473
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004474#ifdef FEAT_FLOAT
4475 /*
4476 * If one of the two variables is a float, compare as a float.
4477 * When using "=~" or "!~", always compare as string.
4478 */
4479 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4480 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4481 {
4482 float_T f1, f2;
4483
4484 if (rettv->v_type == VAR_FLOAT)
4485 f1 = rettv->vval.v_float;
4486 else
4487 f1 = get_tv_number(rettv);
4488 if (var2.v_type == VAR_FLOAT)
4489 f2 = var2.vval.v_float;
4490 else
4491 f2 = get_tv_number(&var2);
4492 n1 = FALSE;
4493 switch (type)
4494 {
4495 case TYPE_EQUAL: n1 = (f1 == f2); break;
4496 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4497 case TYPE_GREATER: n1 = (f1 > f2); break;
4498 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4499 case TYPE_SMALLER: n1 = (f1 < f2); break;
4500 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4501 case TYPE_UNKNOWN:
4502 case TYPE_MATCH:
4503 case TYPE_NOMATCH: break; /* avoid gcc warning */
4504 }
4505 }
4506#endif
4507
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 /*
4509 * If one of the two variables is a number, compare as a number.
4510 * When using "=~" or "!~", always compare as string.
4511 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4514 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004515 n1 = get_tv_number(rettv);
4516 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 switch (type)
4518 {
4519 case TYPE_EQUAL: n1 = (n1 == n2); break;
4520 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4521 case TYPE_GREATER: n1 = (n1 > n2); break;
4522 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4523 case TYPE_SMALLER: n1 = (n1 < n2); break;
4524 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4525 case TYPE_UNKNOWN:
4526 case TYPE_MATCH:
4527 case TYPE_NOMATCH: break; /* avoid gcc warning */
4528 }
4529 }
4530 else
4531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004532 s1 = get_tv_string_buf(rettv, buf1);
4533 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4535 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4536 else
4537 i = 0;
4538 n1 = FALSE;
4539 switch (type)
4540 {
4541 case TYPE_EQUAL: n1 = (i == 0); break;
4542 case TYPE_NEQUAL: n1 = (i != 0); break;
4543 case TYPE_GREATER: n1 = (i > 0); break;
4544 case TYPE_GEQUAL: n1 = (i >= 0); break;
4545 case TYPE_SMALLER: n1 = (i < 0); break;
4546 case TYPE_SEQUAL: n1 = (i <= 0); break;
4547
4548 case TYPE_MATCH:
4549 case TYPE_NOMATCH:
4550 /* avoid 'l' flag in 'cpoptions' */
4551 save_cpo = p_cpo;
4552 p_cpo = (char_u *)"";
4553 regmatch.regprog = vim_regcomp(s2,
4554 RE_MAGIC + RE_STRING);
4555 regmatch.rm_ic = ic;
4556 if (regmatch.regprog != NULL)
4557 {
4558 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4559 vim_free(regmatch.regprog);
4560 if (type == TYPE_NOMATCH)
4561 n1 = !n1;
4562 }
4563 p_cpo = save_cpo;
4564 break;
4565
4566 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4567 }
4568 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004569 clear_tv(rettv);
4570 clear_tv(&var2);
4571 rettv->v_type = VAR_NUMBER;
4572 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 }
4574 }
4575
4576 return OK;
4577}
4578
4579/*
4580 * Handle fourth level expression:
4581 * + number addition
4582 * - number subtraction
4583 * . string concatenation
4584 *
4585 * "arg" must point to the first non-white of the expression.
4586 * "arg" is advanced to the next non-white after the recognized expression.
4587 *
4588 * Return OK or FAIL.
4589 */
4590 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 int evaluate;
4595{
Bram Moolenaar33570922005-01-25 22:26:29 +00004596 typval_T var2;
4597 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 int op;
4599 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004600#ifdef FEAT_FLOAT
4601 float_T f1 = 0, f2 = 0;
4602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 char_u *s1, *s2;
4604 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4605 char_u *p;
4606
4607 /*
4608 * Get the first variable.
4609 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004610 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 return FAIL;
4612
4613 /*
4614 * Repeat computing, until no '+', '-' or '.' is following.
4615 */
4616 for (;;)
4617 {
4618 op = **arg;
4619 if (op != '+' && op != '-' && op != '.')
4620 break;
4621
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004622 if ((op != '+' || rettv->v_type != VAR_LIST)
4623#ifdef FEAT_FLOAT
4624 && (op == '.' || rettv->v_type != VAR_FLOAT)
4625#endif
4626 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004627 {
4628 /* For "list + ...", an illegal use of the first operand as
4629 * a number cannot be determined before evaluating the 2nd
4630 * operand: if this is also a list, all is ok.
4631 * For "something . ...", "something - ..." or "non-list + ...",
4632 * we know that the first operand needs to be a string or number
4633 * without evaluating the 2nd operand. So check before to avoid
4634 * side effects after an error. */
4635 if (evaluate && get_tv_string_chk(rettv) == NULL)
4636 {
4637 clear_tv(rettv);
4638 return FAIL;
4639 }
4640 }
4641
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 /*
4643 * Get the second variable.
4644 */
4645 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004646 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004648 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 return FAIL;
4650 }
4651
4652 if (evaluate)
4653 {
4654 /*
4655 * Compute the result.
4656 */
4657 if (op == '.')
4658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004659 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4660 s2 = get_tv_string_buf_chk(&var2, buf2);
4661 if (s2 == NULL) /* type error ? */
4662 {
4663 clear_tv(rettv);
4664 clear_tv(&var2);
4665 return FAIL;
4666 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004667 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
4669 rettv->v_type = VAR_STRING;
4670 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004672 else if (op == '+' && rettv->v_type == VAR_LIST
4673 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004674 {
4675 /* concatenate Lists */
4676 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4677 &var3) == FAIL)
4678 {
4679 clear_tv(rettv);
4680 clear_tv(&var2);
4681 return FAIL;
4682 }
4683 clear_tv(rettv);
4684 *rettv = var3;
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 else
4687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 int error = FALSE;
4689
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004690#ifdef FEAT_FLOAT
4691 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004692 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004693 f1 = rettv->vval.v_float;
4694 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004695 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696 else
4697#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004698 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004699 n1 = get_tv_number_chk(rettv, &error);
4700 if (error)
4701 {
4702 /* This can only happen for "list + non-list". For
4703 * "non-list + ..." or "something - ...", we returned
4704 * before evaluating the 2nd operand. */
4705 clear_tv(rettv);
4706 return FAIL;
4707 }
4708#ifdef FEAT_FLOAT
4709 if (var2.v_type == VAR_FLOAT)
4710 f1 = n1;
4711#endif
4712 }
4713#ifdef FEAT_FLOAT
4714 if (var2.v_type == VAR_FLOAT)
4715 {
4716 f2 = var2.vval.v_float;
4717 n2 = 0;
4718 }
4719 else
4720#endif
4721 {
4722 n2 = get_tv_number_chk(&var2, &error);
4723 if (error)
4724 {
4725 clear_tv(rettv);
4726 clear_tv(&var2);
4727 return FAIL;
4728 }
4729#ifdef FEAT_FLOAT
4730 if (rettv->v_type == VAR_FLOAT)
4731 f2 = n2;
4732#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004733 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004734 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004735
4736#ifdef FEAT_FLOAT
4737 /* If there is a float on either side the result is a float. */
4738 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4739 {
4740 if (op == '+')
4741 f1 = f1 + f2;
4742 else
4743 f1 = f1 - f2;
4744 rettv->v_type = VAR_FLOAT;
4745 rettv->vval.v_float = f1;
4746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004748#endif
4749 {
4750 if (op == '+')
4751 n1 = n1 + n2;
4752 else
4753 n1 = n1 - n2;
4754 rettv->v_type = VAR_NUMBER;
4755 rettv->vval.v_number = n1;
4756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760 }
4761 return OK;
4762}
4763
4764/*
4765 * Handle fifth level expression:
4766 * * number multiplication
4767 * / number division
4768 * % number modulo
4769 *
4770 * "arg" must point to the first non-white of the expression.
4771 * "arg" is advanced to the next non-white after the recognized expression.
4772 *
4773 * Return OK or FAIL.
4774 */
4775 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004776eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004780 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781{
Bram Moolenaar33570922005-01-25 22:26:29 +00004782 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 int op;
4784 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004785#ifdef FEAT_FLOAT
4786 int use_float = FALSE;
4787 float_T f1 = 0, f2;
4788#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
4791 /*
4792 * Get the first variable.
4793 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004794 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FAIL;
4796
4797 /*
4798 * Repeat computing, until no '*', '/' or '%' is following.
4799 */
4800 for (;;)
4801 {
4802 op = **arg;
4803 if (op != '*' && op != '/' && op != '%')
4804 break;
4805
4806 if (evaluate)
4807 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808#ifdef FEAT_FLOAT
4809 if (rettv->v_type == VAR_FLOAT)
4810 {
4811 f1 = rettv->vval.v_float;
4812 use_float = TRUE;
4813 n1 = 0;
4814 }
4815 else
4816#endif
4817 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004818 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004819 if (error)
4820 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
4822 else
4823 n1 = 0;
4824
4825 /*
4826 * Get the second variable.
4827 */
4828 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004829 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 return FAIL;
4831
4832 if (evaluate)
4833 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004834#ifdef FEAT_FLOAT
4835 if (var2.v_type == VAR_FLOAT)
4836 {
4837 if (!use_float)
4838 {
4839 f1 = n1;
4840 use_float = TRUE;
4841 }
4842 f2 = var2.vval.v_float;
4843 n2 = 0;
4844 }
4845 else
4846#endif
4847 {
4848 n2 = get_tv_number_chk(&var2, &error);
4849 clear_tv(&var2);
4850 if (error)
4851 return FAIL;
4852#ifdef FEAT_FLOAT
4853 if (use_float)
4854 f2 = n2;
4855#endif
4856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857
4858 /*
4859 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004860 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004862#ifdef FEAT_FLOAT
4863 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004865 if (op == '*')
4866 f1 = f1 * f2;
4867 else if (op == '/')
4868 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004869# ifdef VMS
4870 /* VMS crashes on divide by zero, work around it */
4871 if (f2 == 0.0)
4872 {
4873 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004874 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004875 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004876 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004877 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004878 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004879 }
4880 else
4881 f1 = f1 / f2;
4882# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 /* We rely on the floating point library to handle divide
4884 * by zero to result in "inf" and not a crash. */
4885 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004886# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004889 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004890 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891 return FAIL;
4892 }
4893 rettv->v_type = VAR_FLOAT;
4894 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
4896 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899 if (op == '*')
4900 n1 = n1 * n2;
4901 else if (op == '/')
4902 {
4903 if (n2 == 0) /* give an error message? */
4904 {
4905 if (n1 == 0)
4906 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4907 else if (n1 < 0)
4908 n1 = -0x7fffffffL;
4909 else
4910 n1 = 0x7fffffffL;
4911 }
4912 else
4913 n1 = n1 / n2;
4914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916 {
4917 if (n2 == 0) /* give an error message? */
4918 n1 = 0;
4919 else
4920 n1 = n1 % n2;
4921 }
4922 rettv->v_type = VAR_NUMBER;
4923 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926 }
4927
4928 return OK;
4929}
4930
4931/*
4932 * Handle sixth level expression:
4933 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004934 * "string" string constant
4935 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 * &option-name option value
4937 * @r register contents
4938 * identifier variable value
4939 * function() function call
4940 * $VAR environment variable
4941 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004942 * [expr, expr] List
4943 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 *
4945 * Also handle:
4946 * ! in front logical NOT
4947 * - in front unary minus
4948 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004949 * trailing [] subscript in String or List
4950 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 *
4952 * "arg" must point to the first non-white of the expression.
4953 * "arg" is advanced to the next non-white after the recognized expression.
4954 *
4955 * Return OK or FAIL.
4956 */
4957 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004958eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004962 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 long n;
4965 int len;
4966 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 char_u *start_leader, *end_leader;
4968 int ret = OK;
4969 char_u *alias;
4970
4971 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004972 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004973 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004975 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
4977 /*
4978 * Skip '!' and '-' characters. They are handled later.
4979 */
4980 start_leader = *arg;
4981 while (**arg == '!' || **arg == '-' || **arg == '+')
4982 *arg = skipwhite(*arg + 1);
4983 end_leader = *arg;
4984
4985 switch (**arg)
4986 {
4987 /*
4988 * Number constant.
4989 */
4990 case '0':
4991 case '1':
4992 case '2':
4993 case '3':
4994 case '4':
4995 case '5':
4996 case '6':
4997 case '7':
4998 case '8':
4999 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005000 {
5001#ifdef FEAT_FLOAT
5002 char_u *p = skipdigits(*arg + 1);
5003 int get_float = FALSE;
5004
5005 /* We accept a float when the format matches
5006 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005007 * strict to avoid backwards compatibility problems.
5008 * Don't look for a float after the "." operator, so that
5009 * ":let vers = 1.2.3" doesn't fail. */
5010 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 get_float = TRUE;
5013 p = skipdigits(p + 2);
5014 if (*p == 'e' || *p == 'E')
5015 {
5016 ++p;
5017 if (*p == '-' || *p == '+')
5018 ++p;
5019 if (!vim_isdigit(*p))
5020 get_float = FALSE;
5021 else
5022 p = skipdigits(p + 1);
5023 }
5024 if (ASCII_ISALPHA(*p) || *p == '.')
5025 get_float = FALSE;
5026 }
5027 if (get_float)
5028 {
5029 float_T f;
5030
5031 *arg += string2float(*arg, &f);
5032 if (evaluate)
5033 {
5034 rettv->v_type = VAR_FLOAT;
5035 rettv->vval.v_float = f;
5036 }
5037 }
5038 else
5039#endif
5040 {
5041 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5042 *arg += len;
5043 if (evaluate)
5044 {
5045 rettv->v_type = VAR_NUMBER;
5046 rettv->vval.v_number = n;
5047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051
5052 /*
5053 * String constant: "string".
5054 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005055 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 break;
5057
5058 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005061 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005062 break;
5063
5064 /*
5065 * List: [expr, expr]
5066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 break;
5069
5070 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 * Dictionary: {key: val, key: val}
5072 */
5073 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5074 break;
5075
5076 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005077 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005079 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 break;
5081
5082 /*
5083 * Environment variable: $VAR.
5084 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005085 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 break;
5087
5088 /*
5089 * Register contents: @r.
5090 */
5091 case '@': ++*arg;
5092 if (evaluate)
5093 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005095 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097 if (**arg != NUL)
5098 ++*arg;
5099 break;
5100
5101 /*
5102 * nested expression: (expression).
5103 */
5104 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005105 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (**arg == ')')
5107 ++*arg;
5108 else if (ret == OK)
5109 {
5110 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005111 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 ret = FAIL;
5113 }
5114 break;
5115
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 break;
5118 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119
5120 if (ret == NOTDONE)
5121 {
5122 /*
5123 * Must be a variable or function name.
5124 * Can also be a curly-braces kind of name: {expr}.
5125 */
5126 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005127 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128 if (alias != NULL)
5129 s = alias;
5130
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005131 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 ret = FAIL;
5133 else
5134 {
5135 if (**arg == '(') /* recursive! */
5136 {
5137 /* If "s" is the name of a variable of type VAR_FUNC
5138 * use its contents. */
5139 s = deref_func_name(s, &len);
5140
5141 /* Invoke the function. */
5142 ret = get_func_tv(s, len, rettv, arg,
5143 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005144 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 /* Stop the expression evaluation when immediately
5146 * aborting on error, or when an interrupt occurred or
5147 * an exception was thrown but not caught. */
5148 if (aborting())
5149 {
5150 if (ret == OK)
5151 clear_tv(rettv);
5152 ret = FAIL;
5153 }
5154 }
5155 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005156 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005157 else
5158 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005160 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 }
5162
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 *arg = skipwhite(*arg);
5164
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005165 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5166 * expr(expr). */
5167 if (ret == OK)
5168 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
5170 /*
5171 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5172 */
5173 if (ret == OK && evaluate && end_leader > start_leader)
5174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005175 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005176 int val = 0;
5177#ifdef FEAT_FLOAT
5178 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005179
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005180 if (rettv->v_type == VAR_FLOAT)
5181 f = rettv->vval.v_float;
5182 else
5183#endif
5184 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005185 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005187 clear_tv(rettv);
5188 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005190 else
5191 {
5192 while (end_leader > start_leader)
5193 {
5194 --end_leader;
5195 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005196 {
5197#ifdef FEAT_FLOAT
5198 if (rettv->v_type == VAR_FLOAT)
5199 f = !f;
5200 else
5201#endif
5202 val = !val;
5203 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005204 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005205 {
5206#ifdef FEAT_FLOAT
5207 if (rettv->v_type == VAR_FLOAT)
5208 f = -f;
5209 else
5210#endif
5211 val = -val;
5212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005213 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005214#ifdef FEAT_FLOAT
5215 if (rettv->v_type == VAR_FLOAT)
5216 {
5217 clear_tv(rettv);
5218 rettv->vval.v_float = f;
5219 }
5220 else
5221#endif
5222 {
5223 clear_tv(rettv);
5224 rettv->v_type = VAR_NUMBER;
5225 rettv->vval.v_number = val;
5226 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229
5230 return ret;
5231}
5232
5233/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005234 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5235 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5237 */
5238 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005239eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005241 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005243 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244{
5245 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005246 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 long len = -1;
5249 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005253 if (rettv->v_type == VAR_FUNC
5254#ifdef FEAT_FLOAT
5255 || rettv->v_type == VAR_FLOAT
5256#endif
5257 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005259 if (verbose)
5260 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 return FAIL;
5262 }
5263
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 /*
5267 * dict.name
5268 */
5269 key = *arg + 1;
5270 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5271 ;
5272 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 }
5276 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 /*
5279 * something[idx]
5280 *
5281 * Get the (first) variable from inside the [].
5282 */
5283 *arg = skipwhite(*arg + 1);
5284 if (**arg == ':')
5285 empty1 = TRUE;
5286 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5287 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005288 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5289 {
5290 /* not a number or string */
5291 clear_tv(&var1);
5292 return FAIL;
5293 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294
5295 /*
5296 * Get the second variable from inside the [:].
5297 */
5298 if (**arg == ':')
5299 {
5300 range = TRUE;
5301 *arg = skipwhite(*arg + 1);
5302 if (**arg == ']')
5303 empty2 = TRUE;
5304 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 if (!empty1)
5307 clear_tv(&var1);
5308 return FAIL;
5309 }
5310 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5311 {
5312 /* not a number or string */
5313 if (!empty1)
5314 clear_tv(&var1);
5315 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005316 return FAIL;
5317 }
5318 }
5319
5320 /* Check for the ']'. */
5321 if (**arg != ']')
5322 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005323 if (verbose)
5324 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325 clear_tv(&var1);
5326 if (range)
5327 clear_tv(&var2);
5328 return FAIL;
5329 }
5330 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 }
5332
5333 if (evaluate)
5334 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005335 n1 = 0;
5336 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005338 n1 = get_tv_number(&var1);
5339 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 }
5341 if (range)
5342 {
5343 if (empty2)
5344 n2 = -1;
5345 else
5346 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005347 n2 = get_tv_number(&var2);
5348 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 }
5350 }
5351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005352 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 {
5354 case VAR_NUMBER:
5355 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005356 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 len = (long)STRLEN(s);
5358 if (range)
5359 {
5360 /* The resulting variable is a substring. If the indexes
5361 * are out of range the result is empty. */
5362 if (n1 < 0)
5363 {
5364 n1 = len + n1;
5365 if (n1 < 0)
5366 n1 = 0;
5367 }
5368 if (n2 < 0)
5369 n2 = len + n2;
5370 else if (n2 >= len)
5371 n2 = len;
5372 if (n1 >= len || n2 < 0 || n1 > n2)
5373 s = NULL;
5374 else
5375 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5376 }
5377 else
5378 {
5379 /* The resulting variable is a string of a single
5380 * character. If the index is too big or negative the
5381 * result is empty. */
5382 if (n1 >= len || n1 < 0)
5383 s = NULL;
5384 else
5385 s = vim_strnsave(s + n1, 1);
5386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005387 clear_tv(rettv);
5388 rettv->v_type = VAR_STRING;
5389 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 break;
5391
5392 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005393 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 if (n1 < 0)
5395 n1 = len + n1;
5396 if (!empty1 && (n1 < 0 || n1 >= len))
5397 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005398 /* For a range we allow invalid values and return an empty
5399 * list. A list index out of range is an error. */
5400 if (!range)
5401 {
5402 if (verbose)
5403 EMSGN(_(e_listidx), n1);
5404 return FAIL;
5405 }
5406 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 }
5408 if (range)
5409 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005410 list_T *l;
5411 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412
5413 if (n2 < 0)
5414 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005415 else if (n2 >= len)
5416 n2 = len - 1;
5417 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005418 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 l = list_alloc();
5420 if (l == NULL)
5421 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005422 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 n1 <= n2; ++n1)
5424 {
5425 if (list_append_tv(l, &item->li_tv) == FAIL)
5426 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005427 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 return FAIL;
5429 }
5430 item = item->li_next;
5431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005432 clear_tv(rettv);
5433 rettv->v_type = VAR_LIST;
5434 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005435 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 }
5437 else
5438 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005439 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 clear_tv(rettv);
5441 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 }
5443 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444
5445 case VAR_DICT:
5446 if (range)
5447 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005448 if (verbose)
5449 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 if (len == -1)
5451 clear_tv(&var1);
5452 return FAIL;
5453 }
5454 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005455 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456
5457 if (len == -1)
5458 {
5459 key = get_tv_string(&var1);
5460 if (*key == NUL)
5461 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005462 if (verbose)
5463 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 clear_tv(&var1);
5465 return FAIL;
5466 }
5467 }
5468
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005469 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005470
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005471 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005472 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005473 if (len == -1)
5474 clear_tv(&var1);
5475 if (item == NULL)
5476 return FAIL;
5477
5478 copy_tv(&item->di_tv, &var1);
5479 clear_tv(rettv);
5480 *rettv = var1;
5481 }
5482 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 }
5484 }
5485
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 return OK;
5487}
5488
5489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 * Get an option value.
5491 * "arg" points to the '&' or '+' before the option name.
5492 * "arg" is advanced to character after the option name.
5493 * Return OK or FAIL.
5494 */
5495 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005496get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 int evaluate;
5500{
5501 char_u *option_end;
5502 long numval;
5503 char_u *stringval;
5504 int opt_type;
5505 int c;
5506 int working = (**arg == '+'); /* has("+option") */
5507 int ret = OK;
5508 int opt_flags;
5509
5510 /*
5511 * Isolate the option name and find its value.
5512 */
5513 option_end = find_option_end(arg, &opt_flags);
5514 if (option_end == NULL)
5515 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 EMSG2(_("E112: Option name missing: %s"), *arg);
5518 return FAIL;
5519 }
5520
5521 if (!evaluate)
5522 {
5523 *arg = option_end;
5524 return OK;
5525 }
5526
5527 c = *option_end;
5528 *option_end = NUL;
5529 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
5532 if (opt_type == -3) /* invalid name */
5533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005534 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 EMSG2(_("E113: Unknown option: %s"), *arg);
5536 ret = FAIL;
5537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005538 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
5540 if (opt_type == -2) /* hidden string option */
5541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 rettv->v_type = VAR_STRING;
5543 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 }
5545 else if (opt_type == -1) /* hidden number option */
5546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005547 rettv->v_type = VAR_NUMBER;
5548 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
5550 else if (opt_type == 1) /* number option */
5551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 rettv->v_type = VAR_NUMBER;
5553 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 }
5555 else /* string option */
5556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 rettv->v_type = VAR_STRING;
5558 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 }
5560 }
5561 else if (working && (opt_type == -2 || opt_type == -1))
5562 ret = FAIL;
5563
5564 *option_end = c; /* put back for error messages */
5565 *arg = option_end;
5566
5567 return ret;
5568}
5569
5570/*
5571 * Allocate a variable for a string constant.
5572 * Return OK or FAIL.
5573 */
5574 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 int evaluate;
5579{
5580 char_u *p;
5581 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 int extra = 0;
5583
5584 /*
5585 * Find the end of the string, skipping backslashed characters.
5586 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005587 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 {
5589 if (*p == '\\' && p[1] != NUL)
5590 {
5591 ++p;
5592 /* A "\<x>" form occupies at least 4 characters, and produces up
5593 * to 6 characters: reserve space for 2 extra */
5594 if (*p == '<')
5595 extra += 2;
5596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
5598
5599 if (*p != '"')
5600 {
5601 EMSG2(_("E114: Missing quote: %s"), *arg);
5602 return FAIL;
5603 }
5604
5605 /* If only parsing, set *arg and return here */
5606 if (!evaluate)
5607 {
5608 *arg = p + 1;
5609 return OK;
5610 }
5611
5612 /*
5613 * Copy the string into allocated memory, handling backslashed
5614 * characters.
5615 */
5616 name = alloc((unsigned)(p - *arg + extra));
5617 if (name == NULL)
5618 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 rettv->v_type = VAR_STRING;
5620 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 {
5624 if (*p == '\\')
5625 {
5626 switch (*++p)
5627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005628 case 'b': *name++ = BS; ++p; break;
5629 case 'e': *name++ = ESC; ++p; break;
5630 case 'f': *name++ = FF; ++p; break;
5631 case 'n': *name++ = NL; ++p; break;
5632 case 'r': *name++ = CAR; ++p; break;
5633 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
5635 case 'X': /* hex: "\x1", "\x12" */
5636 case 'x':
5637 case 'u': /* Unicode: "\u0023" */
5638 case 'U':
5639 if (vim_isxdigit(p[1]))
5640 {
5641 int n, nr;
5642 int c = toupper(*p);
5643
5644 if (c == 'X')
5645 n = 2;
5646 else
5647 n = 4;
5648 nr = 0;
5649 while (--n >= 0 && vim_isxdigit(p[1]))
5650 {
5651 ++p;
5652 nr = (nr << 4) + hex2nr(*p);
5653 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655#ifdef FEAT_MBYTE
5656 /* For "\u" store the number according to
5657 * 'encoding'. */
5658 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 else
5661#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 break;
5665
5666 /* octal: "\1", "\12", "\123" */
5667 case '0':
5668 case '1':
5669 case '2':
5670 case '3':
5671 case '4':
5672 case '5':
5673 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 case '7': *name = *p++ - '0';
5675 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 *name = (*name << 3) + *p++ - '0';
5678 if (*p >= '0' && *p <= '7')
5679 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 break;
5683
5684 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (extra != 0)
5687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005688 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 break;
5690 }
5691 /* FALLTHROUGH */
5692
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 break;
5695 }
5696 }
5697 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 *arg = p + 1;
5703
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 return OK;
5705}
5706
5707/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 * Return OK or FAIL.
5710 */
5711 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005712get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 int evaluate;
5716{
5717 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005718 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719 int reduce = 0;
5720
5721 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005723 */
5724 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5725 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005729 break;
5730 ++reduce;
5731 ++p;
5732 }
5733 }
5734
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005736 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005738 return FAIL;
5739 }
5740
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005742 if (!evaluate)
5743 {
5744 *arg = p + 1;
5745 return OK;
5746 }
5747
5748 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 */
5751 str = alloc((unsigned)((p - *arg) - reduce));
5752 if (str == NULL)
5753 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 rettv->v_type = VAR_STRING;
5755 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005762 break;
5763 ++p;
5764 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 *arg = p + 1;
5769
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 return OK;
5771}
5772
5773/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005774 * Allocate a variable for a List and fill it from "*arg".
5775 * Return OK or FAIL.
5776 */
5777 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005778get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005780 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781 int evaluate;
5782{
Bram Moolenaar33570922005-01-25 22:26:29 +00005783 list_T *l = NULL;
5784 typval_T tv;
5785 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005786
5787 if (evaluate)
5788 {
5789 l = list_alloc();
5790 if (l == NULL)
5791 return FAIL;
5792 }
5793
5794 *arg = skipwhite(*arg + 1);
5795 while (**arg != ']' && **arg != NUL)
5796 {
5797 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5798 goto failret;
5799 if (evaluate)
5800 {
5801 item = listitem_alloc();
5802 if (item != NULL)
5803 {
5804 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005805 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 list_append(l, item);
5807 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005808 else
5809 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810 }
5811
5812 if (**arg == ']')
5813 break;
5814 if (**arg != ',')
5815 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817 goto failret;
5818 }
5819 *arg = skipwhite(*arg + 1);
5820 }
5821
5822 if (**arg != ']')
5823 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825failret:
5826 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005827 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 return FAIL;
5829 }
5830
5831 *arg = skipwhite(*arg + 1);
5832 if (evaluate)
5833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005834 rettv->v_type = VAR_LIST;
5835 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 ++l->lv_refcount;
5837 }
5838
5839 return OK;
5840}
5841
5842/*
5843 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005844 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005845 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005846 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847list_alloc()
5848{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005849 list_T *l;
5850
5851 l = (list_T *)alloc_clear(sizeof(list_T));
5852 if (l != NULL)
5853 {
5854 /* Prepend the list to the list of lists for garbage collection. */
5855 if (first_list != NULL)
5856 first_list->lv_used_prev = l;
5857 l->lv_used_prev = NULL;
5858 l->lv_used_next = first_list;
5859 first_list = l;
5860 }
5861 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862}
5863
5864/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005865 * Allocate an empty list for a return value.
5866 * Returns OK or FAIL.
5867 */
5868 static int
5869rettv_list_alloc(rettv)
5870 typval_T *rettv;
5871{
5872 list_T *l = list_alloc();
5873
5874 if (l == NULL)
5875 return FAIL;
5876
5877 rettv->vval.v_list = l;
5878 rettv->v_type = VAR_LIST;
5879 ++l->lv_refcount;
5880 return OK;
5881}
5882
5883/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884 * Unreference a list: decrement the reference count and free it when it
5885 * becomes zero.
5886 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005887 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005889 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005891 if (l != NULL && --l->lv_refcount <= 0)
5892 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893}
5894
5895/*
5896 * Free a list, including all items it points to.
5897 * Ignores the reference count.
5898 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005899 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005900list_free(l, recurse)
5901 list_T *l;
5902 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903{
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005906 /* Remove the list from the list of lists for garbage collection. */
5907 if (l->lv_used_prev == NULL)
5908 first_list = l->lv_used_next;
5909 else
5910 l->lv_used_prev->lv_used_next = l->lv_used_next;
5911 if (l->lv_used_next != NULL)
5912 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5913
Bram Moolenaard9fba312005-06-26 22:34:35 +00005914 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005916 /* Remove the item before deleting it. */
5917 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005918 if (recurse || (item->li_tv.v_type != VAR_LIST
5919 && item->li_tv.v_type != VAR_DICT))
5920 clear_tv(&item->li_tv);
5921 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 }
5923 vim_free(l);
5924}
5925
5926/*
5927 * Allocate a list item.
5928 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930listitem_alloc()
5931{
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933}
5934
5935/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005936 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 */
5938 static void
5939listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005940 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005942 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 vim_free(item);
5944}
5945
5946/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005947 * Remove a list item from a List and free it. Also clears the value.
5948 */
5949 static void
5950listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 list_T *l;
5952 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005953{
5954 list_remove(l, item, item);
5955 listitem_free(item);
5956}
5957
5958/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 * Get the number of items in a list.
5960 */
5961 static long
5962list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005963 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 if (l == NULL)
5966 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005967 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968}
5969
5970/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005971 * Return TRUE when two lists have exactly the same values.
5972 */
5973 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005974list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005975 list_T *l1;
5976 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005977 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005978 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005979{
Bram Moolenaar33570922005-01-25 22:26:29 +00005980 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005981
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005982 if (l1 == NULL || l2 == NULL)
5983 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005984 if (l1 == l2)
5985 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005986 if (list_len(l1) != list_len(l2))
5987 return FALSE;
5988
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005989 for (item1 = l1->lv_first, item2 = l2->lv_first;
5990 item1 != NULL && item2 != NULL;
5991 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005992 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005993 return FALSE;
5994 return item1 == NULL && item2 == NULL;
5995}
5996
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005997#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5998 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005999/*
6000 * Return the dictitem that an entry in a hashtable points to.
6001 */
6002 dictitem_T *
6003dict_lookup(hi)
6004 hashitem_T *hi;
6005{
6006 return HI2DI(hi);
6007}
6008#endif
6009
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006010/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006011 * Return TRUE when two dictionaries have exactly the same key/values.
6012 */
6013 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006014dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006015 dict_T *d1;
6016 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006017 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006018 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006019{
Bram Moolenaar33570922005-01-25 22:26:29 +00006020 hashitem_T *hi;
6021 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006022 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006023
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006024 if (d1 == NULL || d2 == NULL)
6025 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006026 if (d1 == d2)
6027 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006028 if (dict_len(d1) != dict_len(d2))
6029 return FALSE;
6030
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006031 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006034 if (!HASHITEM_EMPTY(hi))
6035 {
6036 item2 = dict_find(d2, hi->hi_key, -1);
6037 if (item2 == NULL)
6038 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006039 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006040 return FALSE;
6041 --todo;
6042 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006043 }
6044 return TRUE;
6045}
6046
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006047static int tv_equal_recurse_limit;
6048
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006049/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006050 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006051 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006052 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006053 */
6054 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006056 typval_T *tv1;
6057 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006058 int ic; /* ignore case */
6059 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006060{
6061 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006062 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006063 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006064 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006065
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006066 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006067 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006068
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006069 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006070 * recursiveness to a limit. We guess they are equal then.
6071 * A fixed limit has the problem of still taking an awful long time.
6072 * Reduce the limit every time running into it. That should work fine for
6073 * deeply linked structures that are not recursively linked and catch
6074 * recursiveness quickly. */
6075 if (!recursive)
6076 tv_equal_recurse_limit = 1000;
6077 if (recursive_cnt >= tv_equal_recurse_limit)
6078 {
6079 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006080 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006081 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006082
6083 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006084 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006085 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086 ++recursive_cnt;
6087 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6088 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006089 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006090
6091 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006092 ++recursive_cnt;
6093 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6094 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006095 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006096
6097 case VAR_FUNC:
6098 return (tv1->vval.v_string != NULL
6099 && tv2->vval.v_string != NULL
6100 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6101
6102 case VAR_NUMBER:
6103 return tv1->vval.v_number == tv2->vval.v_number;
6104
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006105#ifdef FEAT_FLOAT
6106 case VAR_FLOAT:
6107 return tv1->vval.v_float == tv2->vval.v_float;
6108#endif
6109
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006110 case VAR_STRING:
6111 s1 = get_tv_string_buf(tv1, buf1);
6112 s2 = get_tv_string_buf(tv2, buf2);
6113 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006115
6116 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117 return TRUE;
6118}
6119
6120/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006121 * Locate item with index "n" in list "l" and return it.
6122 * A negative index is counted from the end; -1 is the last item.
6123 * Returns NULL when "n" is out of range.
6124 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006125 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006126list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006127 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006128 long n;
6129{
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006131 long idx;
6132
6133 if (l == NULL)
6134 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006135
6136 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006137 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006138 n = l->lv_len + n;
6139
6140 /* Check for index out of range. */
6141 if (n < 0 || n >= l->lv_len)
6142 return NULL;
6143
6144 /* When there is a cached index may start search from there. */
6145 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006147 if (n < l->lv_idx / 2)
6148 {
6149 /* closest to the start of the list */
6150 item = l->lv_first;
6151 idx = 0;
6152 }
6153 else if (n > (l->lv_idx + l->lv_len) / 2)
6154 {
6155 /* closest to the end of the list */
6156 item = l->lv_last;
6157 idx = l->lv_len - 1;
6158 }
6159 else
6160 {
6161 /* closest to the cached index */
6162 item = l->lv_idx_item;
6163 idx = l->lv_idx;
6164 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165 }
6166 else
6167 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006168 if (n < l->lv_len / 2)
6169 {
6170 /* closest to the start of the list */
6171 item = l->lv_first;
6172 idx = 0;
6173 }
6174 else
6175 {
6176 /* closest to the end of the list */
6177 item = l->lv_last;
6178 idx = l->lv_len - 1;
6179 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006180 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006181
6182 while (n > idx)
6183 {
6184 /* search forward */
6185 item = item->li_next;
6186 ++idx;
6187 }
6188 while (n < idx)
6189 {
6190 /* search backward */
6191 item = item->li_prev;
6192 --idx;
6193 }
6194
6195 /* cache the used index */
6196 l->lv_idx = idx;
6197 l->lv_idx_item = item;
6198
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199 return item;
6200}
6201
6202/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006203 * Get list item "l[idx]" as a number.
6204 */
6205 static long
6206list_find_nr(l, idx, errorp)
6207 list_T *l;
6208 long idx;
6209 int *errorp; /* set to TRUE when something wrong */
6210{
6211 listitem_T *li;
6212
6213 li = list_find(l, idx);
6214 if (li == NULL)
6215 {
6216 if (errorp != NULL)
6217 *errorp = TRUE;
6218 return -1L;
6219 }
6220 return get_tv_number_chk(&li->li_tv, errorp);
6221}
6222
6223/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006224 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6225 */
6226 char_u *
6227list_find_str(l, idx)
6228 list_T *l;
6229 long idx;
6230{
6231 listitem_T *li;
6232
6233 li = list_find(l, idx - 1);
6234 if (li == NULL)
6235 {
6236 EMSGN(_(e_listidx), idx);
6237 return NULL;
6238 }
6239 return get_tv_string(&li->li_tv);
6240}
6241
6242/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006243 * Locate "item" list "l" and return its index.
6244 * Returns -1 when "item" is not in the list.
6245 */
6246 static long
6247list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006248 list_T *l;
6249 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006250{
6251 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006253
6254 if (l == NULL)
6255 return -1;
6256 idx = 0;
6257 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6258 ++idx;
6259 if (li == NULL)
6260 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006261 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006262}
6263
6264/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006265 * Append item "item" to the end of list "l".
6266 */
6267 static void
6268list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006269 list_T *l;
6270 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271{
6272 if (l->lv_last == NULL)
6273 {
6274 /* empty list */
6275 l->lv_first = item;
6276 l->lv_last = item;
6277 item->li_prev = NULL;
6278 }
6279 else
6280 {
6281 l->lv_last->li_next = item;
6282 item->li_prev = l->lv_last;
6283 l->lv_last = item;
6284 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006285 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 item->li_next = NULL;
6287}
6288
6289/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006290 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006291 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006293 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 list_T *l;
6296 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006298 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006299
Bram Moolenaar05159a02005-02-26 23:04:13 +00006300 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006301 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006302 copy_tv(tv, &li->li_tv);
6303 list_append(l, li);
6304 return OK;
6305}
6306
6307/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006308 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006309 * Return FAIL when out of memory.
6310 */
6311 int
6312list_append_dict(list, dict)
6313 list_T *list;
6314 dict_T *dict;
6315{
6316 listitem_T *li = listitem_alloc();
6317
6318 if (li == NULL)
6319 return FAIL;
6320 li->li_tv.v_type = VAR_DICT;
6321 li->li_tv.v_lock = 0;
6322 li->li_tv.vval.v_dict = dict;
6323 list_append(list, li);
6324 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325 return OK;
6326}
6327
6328/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006329 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006330 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006331 * Returns FAIL when out of memory.
6332 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006333 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006334list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006335 list_T *l;
6336 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006337 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006338{
6339 listitem_T *li = listitem_alloc();
6340
6341 if (li == NULL)
6342 return FAIL;
6343 list_append(l, li);
6344 li->li_tv.v_type = VAR_STRING;
6345 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006346 if (str == NULL)
6347 li->li_tv.vval.v_string = NULL;
6348 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006349 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006350 return FAIL;
6351 return OK;
6352}
6353
6354/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006355 * Append "n" to list "l".
6356 * Returns FAIL when out of memory.
6357 */
6358 static int
6359list_append_number(l, n)
6360 list_T *l;
6361 varnumber_T n;
6362{
6363 listitem_T *li;
6364
6365 li = listitem_alloc();
6366 if (li == NULL)
6367 return FAIL;
6368 li->li_tv.v_type = VAR_NUMBER;
6369 li->li_tv.v_lock = 0;
6370 li->li_tv.vval.v_number = n;
6371 list_append(l, li);
6372 return OK;
6373}
6374
6375/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006376 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006377 * If "item" is NULL append at the end.
6378 * Return FAIL when out of memory.
6379 */
6380 static int
6381list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 list_T *l;
6383 typval_T *tv;
6384 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006385{
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006387
6388 if (ni == NULL)
6389 return FAIL;
6390 copy_tv(tv, &ni->li_tv);
6391 if (item == NULL)
6392 /* Append new item at end of list. */
6393 list_append(l, ni);
6394 else
6395 {
6396 /* Insert new item before existing item. */
6397 ni->li_prev = item->li_prev;
6398 ni->li_next = item;
6399 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006400 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006401 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006402 ++l->lv_idx;
6403 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006404 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006405 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006407 l->lv_idx_item = NULL;
6408 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006409 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006410 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006411 }
6412 return OK;
6413}
6414
6415/*
6416 * Extend "l1" with "l2".
6417 * If "bef" is NULL append at the end, otherwise insert before this item.
6418 * Returns FAIL when out of memory.
6419 */
6420 static int
6421list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 list_T *l1;
6423 list_T *l2;
6424 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425{
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006427 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006429 /* We also quit the loop when we have inserted the original item count of
6430 * the list, avoid a hang when we extend a list with itself. */
6431 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6433 return FAIL;
6434 return OK;
6435}
6436
6437/*
6438 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6439 * Return FAIL when out of memory.
6440 */
6441 static int
6442list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 list_T *l1;
6444 list_T *l2;
6445 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446{
Bram Moolenaar33570922005-01-25 22:26:29 +00006447 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006449 if (l1 == NULL || l2 == NULL)
6450 return FAIL;
6451
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006453 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006454 if (l == NULL)
6455 return FAIL;
6456 tv->v_type = VAR_LIST;
6457 tv->vval.v_list = l;
6458
6459 /* append all items from the second list */
6460 return list_extend(l, l2, NULL);
6461}
6462
6463/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006464 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006465 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006466 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006467 * Returns NULL when out of memory.
6468 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006469 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006470list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006471 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006472 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006473 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006474{
Bram Moolenaar33570922005-01-25 22:26:29 +00006475 list_T *copy;
6476 listitem_T *item;
6477 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478
6479 if (orig == NULL)
6480 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006481
6482 copy = list_alloc();
6483 if (copy != NULL)
6484 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006485 if (copyID != 0)
6486 {
6487 /* Do this before adding the items, because one of the items may
6488 * refer back to this list. */
6489 orig->lv_copyID = copyID;
6490 orig->lv_copylist = copy;
6491 }
6492 for (item = orig->lv_first; item != NULL && !got_int;
6493 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006494 {
6495 ni = listitem_alloc();
6496 if (ni == NULL)
6497 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006498 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006499 {
6500 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6501 {
6502 vim_free(ni);
6503 break;
6504 }
6505 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006507 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508 list_append(copy, ni);
6509 }
6510 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006511 if (item != NULL)
6512 {
6513 list_unref(copy);
6514 copy = NULL;
6515 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006516 }
6517
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518 return copy;
6519}
6520
6521/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006523 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006526list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 list_T *l;
6528 listitem_T *item;
6529 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530{
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 /* notify watchers */
6534 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006535 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006536 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537 list_fix_watch(l, ip);
6538 if (ip == item2)
6539 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541
6542 if (item2->li_next == NULL)
6543 l->lv_last = item->li_prev;
6544 else
6545 item2->li_next->li_prev = item->li_prev;
6546 if (item->li_prev == NULL)
6547 l->lv_first = item2->li_next;
6548 else
6549 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006550 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006551}
6552
6553/*
6554 * Return an allocated string with the string representation of a list.
6555 * May return NULL.
6556 */
6557 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006558list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006559 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006560 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006561{
6562 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563
6564 if (tv->vval.v_list == NULL)
6565 return NULL;
6566 ga_init2(&ga, (int)sizeof(char), 80);
6567 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006568 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006569 {
6570 vim_free(ga.ga_data);
6571 return NULL;
6572 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006573 ga_append(&ga, ']');
6574 ga_append(&ga, NUL);
6575 return (char_u *)ga.ga_data;
6576}
6577
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006578typedef struct join_S {
6579 char_u *s;
6580 char_u *tofree;
6581} join_T;
6582
6583 static int
6584list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6585 garray_T *gap; /* to store the result in */
6586 list_T *l;
6587 char_u *sep;
6588 int echo_style;
6589 int copyID;
6590 garray_T *join_gap; /* to keep each list item string */
6591{
6592 int i;
6593 join_T *p;
6594 int len;
6595 int sumlen = 0;
6596 int first = TRUE;
6597 char_u *tofree;
6598 char_u numbuf[NUMBUFLEN];
6599 listitem_T *item;
6600 char_u *s;
6601
6602 /* Stringify each item in the list. */
6603 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6604 {
6605 if (echo_style)
6606 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6607 else
6608 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6609 if (s == NULL)
6610 return FAIL;
6611
6612 len = (int)STRLEN(s);
6613 sumlen += len;
6614
6615 ga_grow(join_gap, 1);
6616 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6617 if (tofree != NULL || s != numbuf)
6618 {
6619 p->s = s;
6620 p->tofree = tofree;
6621 }
6622 else
6623 {
6624 p->s = vim_strnsave(s, len);
6625 p->tofree = p->s;
6626 }
6627
6628 line_breakcheck();
6629 }
6630
6631 /* Allocate result buffer with its total size, avoid re-allocation and
6632 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6633 if (join_gap->ga_len >= 2)
6634 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6635 if (ga_grow(gap, sumlen + 2) == FAIL)
6636 return FAIL;
6637
6638 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6639 {
6640 if (first)
6641 first = FALSE;
6642 else
6643 ga_concat(gap, sep);
6644 p = ((join_T *)join_gap->ga_data) + i;
6645
6646 if (p->s != NULL)
6647 ga_concat(gap, p->s);
6648 line_breakcheck();
6649 }
6650
6651 return OK;
6652}
6653
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006655 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006656 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006657 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006658 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006659 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006660list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006661 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006662 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006663 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006664 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006665 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006666{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006667 garray_T join_ga;
6668 int retval;
6669 join_T *p;
6670 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006671
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006672 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6673 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6674
6675 /* Dispose each item in join_ga. */
6676 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006677 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006678 p = (join_T *)join_ga.ga_data;
6679 for (i = 0; i < join_ga.ga_len; ++i)
6680 {
6681 vim_free(p->tofree);
6682 ++p;
6683 }
6684 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006685 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006686
6687 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006688}
6689
6690/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006691 * Garbage collection for lists and dictionaries.
6692 *
6693 * We use reference counts to be able to free most items right away when they
6694 * are no longer used. But for composite items it's possible that it becomes
6695 * unused while the reference count is > 0: When there is a recursive
6696 * reference. Example:
6697 * :let l = [1, 2, 3]
6698 * :let d = {9: l}
6699 * :let l[1] = d
6700 *
6701 * Since this is quite unusual we handle this with garbage collection: every
6702 * once in a while find out which lists and dicts are not referenced from any
6703 * variable.
6704 *
6705 * Here is a good reference text about garbage collection (refers to Python
6706 * but it applies to all reference-counting mechanisms):
6707 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006708 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006709
6710/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006711 * Do garbage collection for lists and dicts.
6712 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006713 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006714 int
6715garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006716{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006717 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006718 buf_T *buf;
6719 win_T *wp;
6720 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006721 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006722 int did_free;
6723 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006724#ifdef FEAT_WINDOWS
6725 tabpage_T *tp;
6726#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006727
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006728 /* Only do this once. */
6729 want_garbage_collect = FALSE;
6730 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006731 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006732
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006733 /* We advance by two because we add one for items referenced through
6734 * previous_funccal. */
6735 current_copyID += COPYID_INC;
6736 copyID = current_copyID;
6737
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006738 /*
6739 * 1. Go through all accessible variables and mark all lists and dicts
6740 * with copyID.
6741 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006742
6743 /* Don't free variables in the previous_funccal list unless they are only
6744 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006745 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006746 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6747 {
6748 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6749 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6750 }
6751
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006752 /* script-local variables */
6753 for (i = 1; i <= ga_scripts.ga_len; ++i)
6754 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6755
6756 /* buffer-local variables */
6757 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6758 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6759
6760 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006761 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006762 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6763
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006764#ifdef FEAT_WINDOWS
6765 /* tabpage-local variables */
6766 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6767 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6768#endif
6769
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 /* global variables */
6771 set_ref_in_ht(&globvarht, copyID);
6772
6773 /* function-local variables */
6774 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6775 {
6776 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6777 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6778 }
6779
Bram Moolenaard812df62008-11-09 12:46:09 +00006780 /* v: vars */
6781 set_ref_in_ht(&vimvarht, copyID);
6782
Bram Moolenaar1dced572012-04-05 16:54:08 +02006783#ifdef FEAT_LUA
6784 set_ref_in_lua(copyID);
6785#endif
6786
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006787 /*
6788 * 2. Free lists and dictionaries that are not referenced.
6789 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006790 did_free = free_unref_items(copyID);
6791
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006792 /*
6793 * 3. Check if any funccal can be freed now.
6794 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006795 for (pfc = &previous_funccal; *pfc != NULL; )
6796 {
6797 if (can_free_funccal(*pfc, copyID))
6798 {
6799 fc = *pfc;
6800 *pfc = fc->caller;
6801 free_funccal(fc, TRUE);
6802 did_free = TRUE;
6803 did_free_funccal = TRUE;
6804 }
6805 else
6806 pfc = &(*pfc)->caller;
6807 }
6808 if (did_free_funccal)
6809 /* When a funccal was freed some more items might be garbage
6810 * collected, so run again. */
6811 (void)garbage_collect();
6812
6813 return did_free;
6814}
6815
6816/*
6817 * Free lists and dictionaries that are no longer referenced.
6818 */
6819 static int
6820free_unref_items(copyID)
6821 int copyID;
6822{
6823 dict_T *dd;
6824 list_T *ll;
6825 int did_free = FALSE;
6826
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006827 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006828 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006829 */
6830 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006831 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006833 /* Free the Dictionary and ordinary items it contains, but don't
6834 * recurse into Lists and Dictionaries, they will be in the list
6835 * of dicts or list of lists. */
6836 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 did_free = TRUE;
6838
6839 /* restart, next dict may also have been freed */
6840 dd = first_dict;
6841 }
6842 else
6843 dd = dd->dv_used_next;
6844
6845 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006846 * Go through the list of lists and free items without the copyID.
6847 * But don't free a list that has a watcher (used in a for loop), these
6848 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006849 */
6850 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006851 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6852 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006854 /* Free the List and ordinary items it contains, but don't recurse
6855 * into Lists and Dictionaries, they will be in the list of dicts
6856 * or list of lists. */
6857 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 did_free = TRUE;
6859
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006860 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 ll = first_list;
6862 }
6863 else
6864 ll = ll->lv_used_next;
6865
6866 return did_free;
6867}
6868
6869/*
6870 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6871 */
6872 static void
6873set_ref_in_ht(ht, copyID)
6874 hashtab_T *ht;
6875 int copyID;
6876{
6877 int todo;
6878 hashitem_T *hi;
6879
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006880 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881 for (hi = ht->ht_array; todo > 0; ++hi)
6882 if (!HASHITEM_EMPTY(hi))
6883 {
6884 --todo;
6885 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6886 }
6887}
6888
6889/*
6890 * Mark all lists and dicts referenced through list "l" with "copyID".
6891 */
6892 static void
6893set_ref_in_list(l, copyID)
6894 list_T *l;
6895 int copyID;
6896{
6897 listitem_T *li;
6898
6899 for (li = l->lv_first; li != NULL; li = li->li_next)
6900 set_ref_in_item(&li->li_tv, copyID);
6901}
6902
6903/*
6904 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6905 */
6906 static void
6907set_ref_in_item(tv, copyID)
6908 typval_T *tv;
6909 int copyID;
6910{
6911 dict_T *dd;
6912 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006913
6914 switch (tv->v_type)
6915 {
6916 case VAR_DICT:
6917 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006918 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006919 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 /* Didn't see this dict yet. */
6921 dd->dv_copyID = copyID;
6922 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006923 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006925
6926 case VAR_LIST:
6927 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006928 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006929 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930 /* Didn't see this list yet. */
6931 ll->lv_copyID = copyID;
6932 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006933 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006934 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006935 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006937}
6938
6939/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006940 * Allocate an empty header for a dictionary.
6941 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006942 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006943dict_alloc()
6944{
Bram Moolenaar33570922005-01-25 22:26:29 +00006945 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006946
Bram Moolenaar33570922005-01-25 22:26:29 +00006947 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006948 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006949 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006950 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951 if (first_dict != NULL)
6952 first_dict->dv_used_prev = d;
6953 d->dv_used_next = first_dict;
6954 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006955 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956
Bram Moolenaar33570922005-01-25 22:26:29 +00006957 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006958 d->dv_lock = 0;
6959 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006960 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006961 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006962 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006963}
6964
6965/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006966 * Allocate an empty dict for a return value.
6967 * Returns OK or FAIL.
6968 */
6969 static int
6970rettv_dict_alloc(rettv)
6971 typval_T *rettv;
6972{
6973 dict_T *d = dict_alloc();
6974
6975 if (d == NULL)
6976 return FAIL;
6977
6978 rettv->vval.v_dict = d;
6979 rettv->v_type = VAR_DICT;
6980 ++d->dv_refcount;
6981 return OK;
6982}
6983
6984
6985/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006986 * Unreference a Dictionary: decrement the reference count and free it when it
6987 * becomes zero.
6988 */
Bram Moolenaar82139082011-09-14 16:52:09 +02006989 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006990dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006992{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006993 if (d != NULL && --d->dv_refcount <= 0)
6994 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006995}
6996
6997/*
6998 * Free a Dictionary, including all items it contains.
6999 * Ignores the reference count.
7000 */
7001 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007002dict_free(d, recurse)
7003 dict_T *d;
7004 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007005{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007006 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007007 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007008 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007009
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 /* Remove the dict from the list of dicts for garbage collection. */
7011 if (d->dv_used_prev == NULL)
7012 first_dict = d->dv_used_next;
7013 else
7014 d->dv_used_prev->dv_used_next = d->dv_used_next;
7015 if (d->dv_used_next != NULL)
7016 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7017
7018 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007019 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007020 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007021 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007023 if (!HASHITEM_EMPTY(hi))
7024 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007025 /* Remove the item before deleting it, just in case there is
7026 * something recursive causing trouble. */
7027 di = HI2DI(hi);
7028 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007029 if (recurse || (di->di_tv.v_type != VAR_LIST
7030 && di->di_tv.v_type != VAR_DICT))
7031 clear_tv(&di->di_tv);
7032 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007033 --todo;
7034 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007035 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007037 vim_free(d);
7038}
7039
7040/*
7041 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007042 * The "key" is copied to the new item.
7043 * Note that the value of the item "di_tv" still needs to be initialized!
7044 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007045 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007046 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007047dictitem_alloc(key)
7048 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049{
Bram Moolenaar33570922005-01-25 22:26:29 +00007050 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007052 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007053 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007054 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007055 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007056 di->di_flags = 0;
7057 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007058 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007059}
7060
7061/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007062 * Make a copy of a Dictionary item.
7063 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007064 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007065dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007066 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007067{
Bram Moolenaar33570922005-01-25 22:26:29 +00007068 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007069
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007070 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7071 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007072 if (di != NULL)
7073 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007074 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007075 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007076 copy_tv(&org->di_tv, &di->di_tv);
7077 }
7078 return di;
7079}
7080
7081/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082 * Remove item "item" from Dictionary "dict" and free it.
7083 */
7084 static void
7085dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007086 dict_T *dict;
7087 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007088{
Bram Moolenaar33570922005-01-25 22:26:29 +00007089 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007090
Bram Moolenaar33570922005-01-25 22:26:29 +00007091 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092 if (HASHITEM_EMPTY(hi))
7093 EMSG2(_(e_intern2), "dictitem_remove()");
7094 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007096 dictitem_free(item);
7097}
7098
7099/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100 * Free a dict item. Also clears the value.
7101 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007102 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007105{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106 clear_tv(&item->di_tv);
7107 vim_free(item);
7108}
7109
7110/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007111 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7112 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007113 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114 * Returns NULL when out of memory.
7115 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007117dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007119 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007120 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007121{
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 dict_T *copy;
7123 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007126
7127 if (orig == NULL)
7128 return NULL;
7129
7130 copy = dict_alloc();
7131 if (copy != NULL)
7132 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007133 if (copyID != 0)
7134 {
7135 orig->dv_copyID = copyID;
7136 orig->dv_copydict = copy;
7137 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007138 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007139 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007140 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007141 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007142 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007143 --todo;
7144
7145 di = dictitem_alloc(hi->hi_key);
7146 if (di == NULL)
7147 break;
7148 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007149 {
7150 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7151 copyID) == FAIL)
7152 {
7153 vim_free(di);
7154 break;
7155 }
7156 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157 else
7158 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7159 if (dict_add(copy, di) == FAIL)
7160 {
7161 dictitem_free(di);
7162 break;
7163 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007164 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007165 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007166
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007168 if (todo > 0)
7169 {
7170 dict_unref(copy);
7171 copy = NULL;
7172 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 }
7174
7175 return copy;
7176}
7177
7178/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007180 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007182 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 dict_T *d;
7185 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007186{
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188}
7189
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007191 * Add a number or string entry to dictionary "d".
7192 * When "str" is NULL use number "nr", otherwise use "str".
7193 * Returns FAIL when out of memory and when key already exists.
7194 */
7195 int
7196dict_add_nr_str(d, key, nr, str)
7197 dict_T *d;
7198 char *key;
7199 long nr;
7200 char_u *str;
7201{
7202 dictitem_T *item;
7203
7204 item = dictitem_alloc((char_u *)key);
7205 if (item == NULL)
7206 return FAIL;
7207 item->di_tv.v_lock = 0;
7208 if (str == NULL)
7209 {
7210 item->di_tv.v_type = VAR_NUMBER;
7211 item->di_tv.vval.v_number = nr;
7212 }
7213 else
7214 {
7215 item->di_tv.v_type = VAR_STRING;
7216 item->di_tv.vval.v_string = vim_strsave(str);
7217 }
7218 if (dict_add(d, item) == FAIL)
7219 {
7220 dictitem_free(item);
7221 return FAIL;
7222 }
7223 return OK;
7224}
7225
7226/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007227 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007228 * Returns FAIL when out of memory and when key already exists.
7229 */
7230 int
7231dict_add_list(d, key, list)
7232 dict_T *d;
7233 char *key;
7234 list_T *list;
7235{
7236 dictitem_T *item;
7237
7238 item = dictitem_alloc((char_u *)key);
7239 if (item == NULL)
7240 return FAIL;
7241 item->di_tv.v_lock = 0;
7242 item->di_tv.v_type = VAR_LIST;
7243 item->di_tv.vval.v_list = list;
7244 if (dict_add(d, item) == FAIL)
7245 {
7246 dictitem_free(item);
7247 return FAIL;
7248 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007249 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007250 return OK;
7251}
7252
7253/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007254 * Get the number of items in a Dictionary.
7255 */
7256 static long
7257dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007259{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007260 if (d == NULL)
7261 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007262 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007263}
7264
7265/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 * Find item "key[len]" in Dictionary "d".
7267 * If "len" is negative use strlen(key).
7268 * Returns NULL when not found.
7269 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007270 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007272 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 char_u *key;
7274 int len;
7275{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276#define AKEYLEN 200
7277 char_u buf[AKEYLEN];
7278 char_u *akey;
7279 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007280 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282 if (len < 0)
7283 akey = key;
7284 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007285 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 tofree = akey = vim_strnsave(key, len);
7287 if (akey == NULL)
7288 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007289 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 else
7291 {
7292 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007293 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007294 akey = buf;
7295 }
7296
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 vim_free(tofree);
7299 if (HASHITEM_EMPTY(hi))
7300 return NULL;
7301 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302}
7303
7304/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007305 * Get a string item from a dictionary.
7306 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007307 * Returns NULL if the entry doesn't exist or out of memory.
7308 */
7309 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007310get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007311 dict_T *d;
7312 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007313 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007314{
7315 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007316 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007317
7318 di = dict_find(d, key, -1);
7319 if (di == NULL)
7320 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007321 s = get_tv_string(&di->di_tv);
7322 if (save && s != NULL)
7323 s = vim_strsave(s);
7324 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007325}
7326
7327/*
7328 * Get a number item from a dictionary.
7329 * Returns 0 if the entry doesn't exist or out of memory.
7330 */
7331 long
7332get_dict_number(d, key)
7333 dict_T *d;
7334 char_u *key;
7335{
7336 dictitem_T *di;
7337
7338 di = dict_find(d, key, -1);
7339 if (di == NULL)
7340 return 0;
7341 return get_tv_number(&di->di_tv);
7342}
7343
7344/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345 * Return an allocated string with the string representation of a Dictionary.
7346 * May return NULL.
7347 */
7348 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007349dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007351 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007352{
7353 garray_T ga;
7354 int first = TRUE;
7355 char_u *tofree;
7356 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007358 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007360 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007363 return NULL;
7364 ga_init2(&ga, (int)sizeof(char), 80);
7365 ga_append(&ga, '{');
7366
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007367 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007368 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007372 --todo;
7373
7374 if (first)
7375 first = FALSE;
7376 else
7377 ga_concat(&ga, (char_u *)", ");
7378
7379 tofree = string_quote(hi->hi_key, FALSE);
7380 if (tofree != NULL)
7381 {
7382 ga_concat(&ga, tofree);
7383 vim_free(tofree);
7384 }
7385 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007386 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387 if (s != NULL)
7388 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007389 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007390 if (s == NULL)
7391 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007394 if (todo > 0)
7395 {
7396 vim_free(ga.ga_data);
7397 return NULL;
7398 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399
7400 ga_append(&ga, '}');
7401 ga_append(&ga, NUL);
7402 return (char_u *)ga.ga_data;
7403}
7404
7405/*
7406 * Allocate a variable for a Dictionary and fill it from "*arg".
7407 * Return OK or FAIL. Returns NOTDONE for {expr}.
7408 */
7409 static int
7410get_dict_tv(arg, rettv, evaluate)
7411 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007412 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 int evaluate;
7414{
Bram Moolenaar33570922005-01-25 22:26:29 +00007415 dict_T *d = NULL;
7416 typval_T tvkey;
7417 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007418 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007421 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422
7423 /*
7424 * First check if it's not a curly-braces thing: {expr}.
7425 * Must do this without evaluating, otherwise a function may be called
7426 * twice. Unfortunately this means we need to call eval1() twice for the
7427 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007430 if (*start != '}')
7431 {
7432 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7433 return FAIL;
7434 if (*start == '}')
7435 return NOTDONE;
7436 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437
7438 if (evaluate)
7439 {
7440 d = dict_alloc();
7441 if (d == NULL)
7442 return FAIL;
7443 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007444 tvkey.v_type = VAR_UNKNOWN;
7445 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446
7447 *arg = skipwhite(*arg + 1);
7448 while (**arg != '}' && **arg != NUL)
7449 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007450 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 goto failret;
7452 if (**arg != ':')
7453 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007454 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007455 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456 goto failret;
7457 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007458 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007459 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007460 key = get_tv_string_buf_chk(&tvkey, buf);
7461 if (key == NULL || *key == NUL)
7462 {
7463 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7464 if (key != NULL)
7465 EMSG(_(e_emptykey));
7466 clear_tv(&tvkey);
7467 goto failret;
7468 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007469 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470
7471 *arg = skipwhite(*arg + 1);
7472 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7473 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007474 if (evaluate)
7475 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476 goto failret;
7477 }
7478 if (evaluate)
7479 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007480 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481 if (item != NULL)
7482 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007483 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007484 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 clear_tv(&tv);
7486 goto failret;
7487 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488 item = dictitem_alloc(key);
7489 clear_tv(&tvkey);
7490 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007493 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494 if (dict_add(d, item) == FAIL)
7495 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496 }
7497 }
7498
7499 if (**arg == '}')
7500 break;
7501 if (**arg != ',')
7502 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007503 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504 goto failret;
7505 }
7506 *arg = skipwhite(*arg + 1);
7507 }
7508
7509 if (**arg != '}')
7510 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007511 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512failret:
7513 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007514 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 return FAIL;
7516 }
7517
7518 *arg = skipwhite(*arg + 1);
7519 if (evaluate)
7520 {
7521 rettv->v_type = VAR_DICT;
7522 rettv->vval.v_dict = d;
7523 ++d->dv_refcount;
7524 }
7525
7526 return OK;
7527}
7528
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007530 * Return a string with the string representation of a variable.
7531 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007532 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007533 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007534 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007535 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007536 */
7537 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007538echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007539 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007540 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007541 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007542 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007543{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007544 static int recurse = 0;
7545 char_u *r = NULL;
7546
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007548 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007549 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007550 *tofree = NULL;
7551 return NULL;
7552 }
7553 ++recurse;
7554
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007555 switch (tv->v_type)
7556 {
7557 case VAR_FUNC:
7558 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007559 r = tv->vval.v_string;
7560 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007561
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007562 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007563 if (tv->vval.v_list == NULL)
7564 {
7565 *tofree = NULL;
7566 r = NULL;
7567 }
7568 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7569 {
7570 *tofree = NULL;
7571 r = (char_u *)"[...]";
7572 }
7573 else
7574 {
7575 tv->vval.v_list->lv_copyID = copyID;
7576 *tofree = list2string(tv, copyID);
7577 r = *tofree;
7578 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007579 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007580
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007582 if (tv->vval.v_dict == NULL)
7583 {
7584 *tofree = NULL;
7585 r = NULL;
7586 }
7587 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7588 {
7589 *tofree = NULL;
7590 r = (char_u *)"{...}";
7591 }
7592 else
7593 {
7594 tv->vval.v_dict->dv_copyID = copyID;
7595 *tofree = dict2string(tv, copyID);
7596 r = *tofree;
7597 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007598 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007599
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 case VAR_STRING:
7601 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007602 *tofree = NULL;
7603 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007604 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007605
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007606#ifdef FEAT_FLOAT
7607 case VAR_FLOAT:
7608 *tofree = NULL;
7609 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7610 r = numbuf;
7611 break;
7612#endif
7613
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007614 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007615 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007616 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007617 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007618
7619 --recurse;
7620 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007621}
7622
7623/*
7624 * Return a string with the string representation of a variable.
7625 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7626 * "numbuf" is used for a number.
7627 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007628 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007629 */
7630 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007631tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007632 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007633 char_u **tofree;
7634 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007635 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007636{
7637 switch (tv->v_type)
7638 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007639 case VAR_FUNC:
7640 *tofree = string_quote(tv->vval.v_string, TRUE);
7641 return *tofree;
7642 case VAR_STRING:
7643 *tofree = string_quote(tv->vval.v_string, FALSE);
7644 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007645#ifdef FEAT_FLOAT
7646 case VAR_FLOAT:
7647 *tofree = NULL;
7648 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7649 return numbuf;
7650#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007651 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007652 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007654 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007655 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007656 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007657 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007658 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007659}
7660
7661/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 * Return string "str" in ' quotes, doubling ' characters.
7663 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007665 */
7666 static char_u *
7667string_quote(str, function)
7668 char_u *str;
7669 int function;
7670{
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007672 char_u *p, *r, *s;
7673
Bram Moolenaar33570922005-01-25 22:26:29 +00007674 len = (function ? 13 : 3);
7675 if (str != NULL)
7676 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007677 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 for (p = str; *p != NUL; mb_ptr_adv(p))
7679 if (*p == '\'')
7680 ++len;
7681 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007682 s = r = alloc(len);
7683 if (r != NULL)
7684 {
7685 if (function)
7686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007688 r += 10;
7689 }
7690 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 if (str != NULL)
7693 for (p = str; *p != NUL; )
7694 {
7695 if (*p == '\'')
7696 *r++ = '\'';
7697 MB_COPY_CHAR(p, r);
7698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007700 if (function)
7701 *r++ = ')';
7702 *r++ = NUL;
7703 }
7704 return s;
7705}
7706
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007707#ifdef FEAT_FLOAT
7708/*
7709 * Convert the string "text" to a floating point number.
7710 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7711 * this always uses a decimal point.
7712 * Returns the length of the text that was consumed.
7713 */
7714 static int
7715string2float(text, value)
7716 char_u *text;
7717 float_T *value; /* result stored here */
7718{
7719 char *s = (char *)text;
7720 float_T f;
7721
7722 f = strtod(s, &s);
7723 *value = f;
7724 return (int)((char_u *)s - text);
7725}
7726#endif
7727
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 * Get the value of an environment variable.
7730 * "arg" is pointing to the '$'. It is advanced to after the name.
7731 * If the environment variable was not set, silently assume it is empty.
7732 * Always return OK.
7733 */
7734 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007735get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 int evaluate;
7739{
7740 char_u *string = NULL;
7741 int len;
7742 int cc;
7743 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007744 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745
7746 ++*arg;
7747 name = *arg;
7748 len = get_env_len(arg);
7749 if (evaluate)
7750 {
7751 if (len != 0)
7752 {
7753 cc = name[len];
7754 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007755 /* first try vim_getenv(), fast for normal environment vars */
7756 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007758 {
7759 if (!mustfree)
7760 string = vim_strsave(string);
7761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 else
7763 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007764 if (mustfree)
7765 vim_free(string);
7766
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 /* next try expanding things like $VIM and ${HOME} */
7768 string = expand_env_save(name - 1);
7769 if (string != NULL && *string == '$')
7770 {
7771 vim_free(string);
7772 string = NULL;
7773 }
7774 }
7775 name[len] = cc;
7776 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777 rettv->v_type = VAR_STRING;
7778 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 }
7780
7781 return OK;
7782}
7783
7784/*
7785 * Array with names and number of arguments of all internal functions
7786 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7787 */
7788static struct fst
7789{
7790 char *f_name; /* function name */
7791 char f_min_argc; /* minimal number of arguments */
7792 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007794 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795} functions[] =
7796{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007797#ifdef FEAT_FLOAT
7798 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007799 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007800#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007801 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007802 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {"append", 2, 2, f_append},
7804 {"argc", 0, 0, f_argc},
7805 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007806 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007807#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007808 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007809 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007810 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007813 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 {"bufexists", 1, 1, f_bufexists},
7815 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7816 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7817 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7818 {"buflisted", 1, 1, f_buflisted},
7819 {"bufloaded", 1, 1, f_bufloaded},
7820 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007821 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {"bufwinnr", 1, 1, f_bufwinnr},
7823 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007824 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007826#ifdef FEAT_FLOAT
7827 {"ceil", 1, 1, f_ceil},
7828#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007829 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {"char2nr", 1, 1, f_char2nr},
7831 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007832 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007834#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007835 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007836 {"complete_add", 1, 1, f_complete_add},
7837 {"complete_check", 0, 0, f_complete_check},
7838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007840 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007841#ifdef FEAT_FLOAT
7842 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007843 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007844#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007845 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007847 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007848 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {"delete", 1, 1, f_delete},
7850 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007851 {"diff_filler", 1, 1, f_diff_filler},
7852 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007853 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007855 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 {"eventhandler", 0, 0, f_eventhandler},
7857 {"executable", 1, 1, f_executable},
7858 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007859#ifdef FEAT_FLOAT
7860 {"exp", 1, 1, f_exp},
7861#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007862 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007863 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007864 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7866 {"filereadable", 1, 1, f_filereadable},
7867 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007868 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007869 {"finddir", 1, 3, f_finddir},
7870 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#ifdef FEAT_FLOAT
7872 {"float2nr", 1, 1, f_float2nr},
7873 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007874 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007875#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007876 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"fnamemodify", 2, 2, f_fnamemodify},
7878 {"foldclosed", 1, 1, f_foldclosed},
7879 {"foldclosedend", 1, 1, f_foldclosedend},
7880 {"foldlevel", 1, 1, f_foldlevel},
7881 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007882 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007884 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007885 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007886 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007887 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {"getbufvar", 2, 2, f_getbufvar},
7889 {"getchar", 0, 1, f_getchar},
7890 {"getcharmod", 0, 0, f_getcharmod},
7891 {"getcmdline", 0, 0, f_getcmdline},
7892 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007893 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007895 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007896 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {"getfsize", 1, 1, f_getfsize},
7898 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007899 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007900 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007901 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007902 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007903 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007904 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007905 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007906 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007908 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007909 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {"getwinposx", 0, 0, f_getwinposx},
7911 {"getwinposy", 0, 0, f_getwinposy},
7912 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007913 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007914 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007917 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007918 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7920 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7921 {"histadd", 2, 2, f_histadd},
7922 {"histdel", 1, 2, f_histdel},
7923 {"histget", 1, 2, f_histget},
7924 {"histnr", 1, 1, f_histnr},
7925 {"hlID", 1, 1, f_hlID},
7926 {"hlexists", 1, 1, f_hlexists},
7927 {"hostname", 0, 0, f_hostname},
7928 {"iconv", 3, 3, f_iconv},
7929 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007930 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007931 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007933 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {"inputrestore", 0, 0, f_inputrestore},
7935 {"inputsave", 0, 0, f_inputsave},
7936 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007937 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007938 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007940 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007945 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {"libcall", 3, 3, f_libcall},
7947 {"libcallnr", 3, 3, f_libcallnr},
7948 {"line", 1, 1, f_line},
7949 {"line2byte", 1, 1, f_line2byte},
7950 {"lispindent", 1, 1, f_lispindent},
7951 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007952#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007953 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007954 {"log10", 1, 1, f_log10},
7955#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007956#ifdef FEAT_LUA
7957 {"luaeval", 1, 2, f_luaeval},
7958#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007959 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007960 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007961 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007962 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007963 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007964 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007965 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007966 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007967 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007968 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007969 {"max", 1, 1, f_max},
7970 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007971#ifdef vim_mkdir
7972 {"mkdir", 1, 3, f_mkdir},
7973#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007974 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007975#ifdef FEAT_MZSCHEME
7976 {"mzeval", 1, 1, f_mzeval},
7977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {"nextnonblank", 1, 1, f_nextnonblank},
7979 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007980 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007981 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007982#ifdef FEAT_FLOAT
7983 {"pow", 2, 2, f_pow},
7984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007986 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007987 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007988 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007989 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007990 {"reltime", 0, 2, f_reltime},
7991 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 {"remote_expr", 2, 3, f_remote_expr},
7993 {"remote_foreground", 1, 1, f_remote_foreground},
7994 {"remote_peek", 1, 2, f_remote_peek},
7995 {"remote_read", 1, 1, f_remote_read},
7996 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007997 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007999 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008001 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008002#ifdef FEAT_FLOAT
8003 {"round", 1, 1, f_round},
8004#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00008005 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008006 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008007 {"searchpair", 3, 7, f_searchpair},
8008 {"searchpairpos", 3, 7, f_searchpairpos},
8009 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {"server2client", 2, 2, f_server2client},
8011 {"serverlist", 0, 0, f_serverlist},
8012 {"setbufvar", 3, 3, f_setbufvar},
8013 {"setcmdpos", 1, 1, f_setcmdpos},
8014 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008015 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008016 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008017 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008018 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008020 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008021 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008023 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008025#ifdef FEAT_FLOAT
8026 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008027 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008028#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008029 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008030 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008031 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008032 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008033 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008034#ifdef FEAT_FLOAT
8035 {"sqrt", 1, 1, f_sqrt},
8036 {"str2float", 1, 1, f_str2float},
8037#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008038 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008039 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008040 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041#ifdef HAVE_STRFTIME
8042 {"strftime", 1, 2, f_strftime},
8043#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008045 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 {"strlen", 1, 1, f_strlen},
8047 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008048 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008050 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 {"submatch", 1, 1, f_submatch},
8052 {"substitute", 4, 4, f_substitute},
8053 {"synID", 3, 3, f_synID},
8054 {"synIDattr", 2, 3, f_synIDattr},
8055 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008056 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008057 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008058 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008059 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008060 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008061 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008062 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008063 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008064#ifdef FEAT_FLOAT
8065 {"tan", 1, 1, f_tan},
8066 {"tanh", 1, 1, f_tanh},
8067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008069 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"tolower", 1, 1, f_tolower},
8071 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008072 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073#ifdef FEAT_FLOAT
8074 {"trunc", 1, 1, f_trunc},
8075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008077 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008078 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008079 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"virtcol", 1, 1, f_virtcol},
8081 {"visualmode", 0, 1, f_visualmode},
8082 {"winbufnr", 1, 1, f_winbufnr},
8083 {"wincol", 0, 0, f_wincol},
8084 {"winheight", 1, 1, f_winheight},
8085 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008086 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008088 {"winrestview", 1, 1, f_winrestview},
8089 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008091 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008092 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093};
8094
8095#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8096
8097/*
8098 * Function given to ExpandGeneric() to obtain the list of internal
8099 * or user defined function names.
8100 */
8101 char_u *
8102get_function_name(xp, idx)
8103 expand_T *xp;
8104 int idx;
8105{
8106 static int intidx = -1;
8107 char_u *name;
8108
8109 if (idx == 0)
8110 intidx = -1;
8111 if (intidx < 0)
8112 {
8113 name = get_user_func_name(xp, idx);
8114 if (name != NULL)
8115 return name;
8116 }
8117 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8118 {
8119 STRCPY(IObuff, functions[intidx].f_name);
8120 STRCAT(IObuff, "(");
8121 if (functions[intidx].f_max_argc == 0)
8122 STRCAT(IObuff, ")");
8123 return IObuff;
8124 }
8125
8126 return NULL;
8127}
8128
8129/*
8130 * Function given to ExpandGeneric() to obtain the list of internal or
8131 * user defined variable or function names.
8132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 char_u *
8134get_expr_name(xp, idx)
8135 expand_T *xp;
8136 int idx;
8137{
8138 static int intidx = -1;
8139 char_u *name;
8140
8141 if (idx == 0)
8142 intidx = -1;
8143 if (intidx < 0)
8144 {
8145 name = get_function_name(xp, idx);
8146 if (name != NULL)
8147 return name;
8148 }
8149 return get_user_var_name(xp, ++intidx);
8150}
8151
8152#endif /* FEAT_CMDL_COMPL */
8153
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008154#if defined(EBCDIC) || defined(PROTO)
8155/*
8156 * Compare struct fst by function name.
8157 */
8158 static int
8159compare_func_name(s1, s2)
8160 const void *s1;
8161 const void *s2;
8162{
8163 struct fst *p1 = (struct fst *)s1;
8164 struct fst *p2 = (struct fst *)s2;
8165
8166 return STRCMP(p1->f_name, p2->f_name);
8167}
8168
8169/*
8170 * Sort the function table by function name.
8171 * The sorting of the table above is ASCII dependant.
8172 * On machines using EBCDIC we have to sort it.
8173 */
8174 static void
8175sortFunctions()
8176{
8177 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8178
8179 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8180}
8181#endif
8182
8183
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184/*
8185 * Find internal function in table above.
8186 * Return index, or -1 if not found
8187 */
8188 static int
8189find_internal_func(name)
8190 char_u *name; /* name of the function */
8191{
8192 int first = 0;
8193 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8194 int cmp;
8195 int x;
8196
8197 /*
8198 * Find the function name in the table. Binary search.
8199 */
8200 while (first <= last)
8201 {
8202 x = first + ((unsigned)(last - first) >> 1);
8203 cmp = STRCMP(name, functions[x].f_name);
8204 if (cmp < 0)
8205 last = x - 1;
8206 else if (cmp > 0)
8207 first = x + 1;
8208 else
8209 return x;
8210 }
8211 return -1;
8212}
8213
8214/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008215 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8216 * name it contains, otherwise return "name".
8217 */
8218 static char_u *
8219deref_func_name(name, lenp)
8220 char_u *name;
8221 int *lenp;
8222{
Bram Moolenaar33570922005-01-25 22:26:29 +00008223 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008224 int cc;
8225
8226 cc = name[*lenp];
8227 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008228 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008229 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008230 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008231 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008232 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008233 {
8234 *lenp = 0;
8235 return (char_u *)""; /* just in case */
8236 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008237 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008238 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008239 }
8240
8241 return name;
8242}
8243
8244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 * Allocate a variable for the result of a function.
8246 * Return OK or FAIL.
8247 */
8248 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008249get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8250 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 char_u *name; /* name of the function */
8252 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 char_u **arg; /* argument, pointing to the '(' */
8255 linenr_T firstline; /* first line of range */
8256 linenr_T lastline; /* last line of range */
8257 int *doesrange; /* return: function handled range */
8258 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008259 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
8261 char_u *argp;
8262 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008263 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 int argcount = 0; /* number of arguments found */
8265
8266 /*
8267 * Get the arguments.
8268 */
8269 argp = *arg;
8270 while (argcount < MAX_FUNC_ARGS)
8271 {
8272 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8273 if (*argp == ')' || *argp == ',' || *argp == NUL)
8274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8276 {
8277 ret = FAIL;
8278 break;
8279 }
8280 ++argcount;
8281 if (*argp != ',')
8282 break;
8283 }
8284 if (*argp == ')')
8285 ++argp;
8286 else
8287 ret = FAIL;
8288
8289 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008290 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008291 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008293 {
8294 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008295 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008297 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299
8300 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008301 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302
8303 *arg = skipwhite(argp);
8304 return ret;
8305}
8306
8307
8308/*
8309 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008310 * Return OK when the function can't be called, FAIL otherwise.
8311 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 */
8313 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008314call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008315 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008316 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008318 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008320 typval_T *argvars; /* vars for arguments, must have "argcount"
8321 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 linenr_T firstline; /* first line of range */
8323 linenr_T lastline; /* last line of range */
8324 int *doesrange; /* return: function handled range */
8325 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008326 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327{
8328 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329#define ERROR_UNKNOWN 0
8330#define ERROR_TOOMANY 1
8331#define ERROR_TOOFEW 2
8332#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008333#define ERROR_DICT 4
8334#define ERROR_NONE 5
8335#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 int error = ERROR_NONE;
8337 int i;
8338 int llen;
8339 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340#define FLEN_FIXED 40
8341 char_u fname_buf[FLEN_FIXED + 1];
8342 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008343 char_u *name;
8344
8345 /* Make a copy of the name, if it comes from a funcref variable it could
8346 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008347 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008348 if (name == NULL)
8349 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350
8351 /*
8352 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8353 * Change <SNR>123_name() to K_SNR 123_name().
8354 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 llen = eval_fname_script(name);
8357 if (llen > 0)
8358 {
8359 fname_buf[0] = K_SPECIAL;
8360 fname_buf[1] = KS_EXTRA;
8361 fname_buf[2] = (int)KE_SNR;
8362 i = 3;
8363 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8364 {
8365 if (current_SID <= 0)
8366 error = ERROR_SCRIPT;
8367 else
8368 {
8369 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8370 i = (int)STRLEN(fname_buf);
8371 }
8372 }
8373 if (i + STRLEN(name + llen) < FLEN_FIXED)
8374 {
8375 STRCPY(fname_buf + i, name + llen);
8376 fname = fname_buf;
8377 }
8378 else
8379 {
8380 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8381 if (fname == NULL)
8382 error = ERROR_OTHER;
8383 else
8384 {
8385 mch_memmove(fname, fname_buf, (size_t)i);
8386 STRCPY(fname + i, name + llen);
8387 }
8388 }
8389 }
8390 else
8391 fname = name;
8392
8393 *doesrange = FALSE;
8394
8395
8396 /* execute the function if no errors detected and executing */
8397 if (evaluate && error == ERROR_NONE)
8398 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008399 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8400 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 error = ERROR_UNKNOWN;
8402
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008403 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 {
8405 /*
8406 * User defined function.
8407 */
8408 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008409
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008411 /* Trigger FuncUndefined event, may load the function. */
8412 if (fp == NULL
8413 && apply_autocmds(EVENT_FUNCUNDEFINED,
8414 fname, fname, TRUE, NULL)
8415 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008417 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 fp = find_func(fname);
8419 }
8420#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008421 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008422 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008423 {
8424 /* loaded a package, search for the function again */
8425 fp = find_func(fname);
8426 }
8427
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 if (fp != NULL)
8429 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008430 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008432 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008434 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008436 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008437 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 else
8439 {
8440 /*
8441 * Call the user function.
8442 * Save and restore search patterns, script variables and
8443 * redo buffer.
8444 */
8445 save_search_patterns();
8446 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008447 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008448 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008449 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008450 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8451 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8452 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008453 /* Function was unreferenced while being used, free it
8454 * now. */
8455 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 restoreRedobuff();
8457 restore_search_patterns();
8458 error = ERROR_NONE;
8459 }
8460 }
8461 }
8462 else
8463 {
8464 /*
8465 * Find the function name in the table, call its implementation.
8466 */
8467 i = find_internal_func(fname);
8468 if (i >= 0)
8469 {
8470 if (argcount < functions[i].f_min_argc)
8471 error = ERROR_TOOFEW;
8472 else if (argcount > functions[i].f_max_argc)
8473 error = ERROR_TOOMANY;
8474 else
8475 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008476 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008477 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 error = ERROR_NONE;
8479 }
8480 }
8481 }
8482 /*
8483 * The function call (or "FuncUndefined" autocommand sequence) might
8484 * have been aborted by an error, an interrupt, or an explicitly thrown
8485 * exception that has not been caught so far. This situation can be
8486 * tested for by calling aborting(). For an error in an internal
8487 * function or for the "E132" error in call_user_func(), however, the
8488 * throw point at which the "force_abort" flag (temporarily reset by
8489 * emsg()) is normally updated has not been reached yet. We need to
8490 * update that flag first to make aborting() reliable.
8491 */
8492 update_force_abort();
8493 }
8494 if (error == ERROR_NONE)
8495 ret = OK;
8496
8497 /*
8498 * Report an error unless the argument evaluation or function call has been
8499 * cancelled due to an aborting error, an interrupt, or an exception.
8500 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008501 if (!aborting())
8502 {
8503 switch (error)
8504 {
8505 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008506 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008507 break;
8508 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008509 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008510 break;
8511 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008512 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008513 name);
8514 break;
8515 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008516 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008517 name);
8518 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008520 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008521 name);
8522 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008523 }
8524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 if (fname != name && fname != fname_buf)
8527 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008528 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529
8530 return ret;
8531}
8532
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008533/*
8534 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008535 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008536 */
8537 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008538emsg_funcname(ermsg, name)
8539 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008540 char_u *name;
8541{
8542 char_u *p;
8543
8544 if (*name == K_SPECIAL)
8545 p = concat_str((char_u *)"<SNR>", name + 3);
8546 else
8547 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008548 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008549 if (p != name)
8550 vim_free(p);
8551}
8552
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008553/*
8554 * Return TRUE for a non-zero Number and a non-empty String.
8555 */
8556 static int
8557non_zero_arg(argvars)
8558 typval_T *argvars;
8559{
8560 return ((argvars[0].v_type == VAR_NUMBER
8561 && argvars[0].vval.v_number != 0)
8562 || (argvars[0].v_type == VAR_STRING
8563 && argvars[0].vval.v_string != NULL
8564 && *argvars[0].vval.v_string != NUL));
8565}
8566
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567/*********************************************
8568 * Implementation of the built-in functions
8569 */
8570
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008571#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008572static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8573
8574/*
8575 * Get the float value of "argvars[0]" into "f".
8576 * Returns FAIL when the argument is not a Number or Float.
8577 */
8578 static int
8579get_float_arg(argvars, f)
8580 typval_T *argvars;
8581 float_T *f;
8582{
8583 if (argvars[0].v_type == VAR_FLOAT)
8584 {
8585 *f = argvars[0].vval.v_float;
8586 return OK;
8587 }
8588 if (argvars[0].v_type == VAR_NUMBER)
8589 {
8590 *f = (float_T)argvars[0].vval.v_number;
8591 return OK;
8592 }
8593 EMSG(_("E808: Number or Float required"));
8594 return FAIL;
8595}
8596
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008597/*
8598 * "abs(expr)" function
8599 */
8600 static void
8601f_abs(argvars, rettv)
8602 typval_T *argvars;
8603 typval_T *rettv;
8604{
8605 if (argvars[0].v_type == VAR_FLOAT)
8606 {
8607 rettv->v_type = VAR_FLOAT;
8608 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8609 }
8610 else
8611 {
8612 varnumber_T n;
8613 int error = FALSE;
8614
8615 n = get_tv_number_chk(&argvars[0], &error);
8616 if (error)
8617 rettv->vval.v_number = -1;
8618 else if (n > 0)
8619 rettv->vval.v_number = n;
8620 else
8621 rettv->vval.v_number = -n;
8622 }
8623}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008624
8625/*
8626 * "acos()" function
8627 */
8628 static void
8629f_acos(argvars, rettv)
8630 typval_T *argvars;
8631 typval_T *rettv;
8632{
8633 float_T f;
8634
8635 rettv->v_type = VAR_FLOAT;
8636 if (get_float_arg(argvars, &f) == OK)
8637 rettv->vval.v_float = acos(f);
8638 else
8639 rettv->vval.v_float = 0.0;
8640}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008641#endif
8642
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008644 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 */
8646 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008647f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008648 typval_T *argvars;
8649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650{
Bram Moolenaar33570922005-01-25 22:26:29 +00008651 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008653 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008656 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008657 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008658 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008659 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008660 }
8661 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008662 EMSG(_(e_listreq));
8663}
8664
8665/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008666 * "and(expr, expr)" function
8667 */
8668 static void
8669f_and(argvars, rettv)
8670 typval_T *argvars;
8671 typval_T *rettv;
8672{
8673 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8674 & get_tv_number_chk(&argvars[1], NULL);
8675}
8676
8677/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008678 * "append(lnum, string/list)" function
8679 */
8680 static void
8681f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008682 typval_T *argvars;
8683 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008684{
8685 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008686 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008687 list_T *l = NULL;
8688 listitem_T *li = NULL;
8689 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008690 long added = 0;
8691
Bram Moolenaar0d660222005-01-07 21:51:51 +00008692 lnum = get_tv_lnum(argvars);
8693 if (lnum >= 0
8694 && lnum <= curbuf->b_ml.ml_line_count
8695 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008696 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008697 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008698 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008699 l = argvars[1].vval.v_list;
8700 if (l == NULL)
8701 return;
8702 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008703 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008704 for (;;)
8705 {
8706 if (l == NULL)
8707 tv = &argvars[1]; /* append a string */
8708 else if (li == NULL)
8709 break; /* end of list */
8710 else
8711 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008712 line = get_tv_string_chk(tv);
8713 if (line == NULL) /* type error */
8714 {
8715 rettv->vval.v_number = 1; /* Failed */
8716 break;
8717 }
8718 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008719 ++added;
8720 if (l == NULL)
8721 break;
8722 li = li->li_next;
8723 }
8724
8725 appended_lines_mark(lnum, added);
8726 if (curwin->w_cursor.lnum > lnum)
8727 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008729 else
8730 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731}
8732
8733/*
8734 * "argc()" function
8735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008738 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008741 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742}
8743
8744/*
8745 * "argidx()" function
8746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008748f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008749 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008752 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753}
8754
8755/*
8756 * "argv(nr)" function
8757 */
8758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008759f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008760 typval_T *argvars;
8761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762{
8763 int idx;
8764
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008765 if (argvars[0].v_type != VAR_UNKNOWN)
8766 {
8767 idx = get_tv_number_chk(&argvars[0], NULL);
8768 if (idx >= 0 && idx < ARGCOUNT)
8769 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8770 else
8771 rettv->vval.v_string = NULL;
8772 rettv->v_type = VAR_STRING;
8773 }
8774 else if (rettv_list_alloc(rettv) == OK)
8775 for (idx = 0; idx < ARGCOUNT; ++idx)
8776 list_append_string(rettv->vval.v_list,
8777 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778}
8779
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008780#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008781/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008782 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008783 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008784 static void
8785f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008786 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008787 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008788{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008789 float_T f;
8790
8791 rettv->v_type = VAR_FLOAT;
8792 if (get_float_arg(argvars, &f) == OK)
8793 rettv->vval.v_float = asin(f);
8794 else
8795 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008796}
8797
8798/*
8799 * "atan()" function
8800 */
8801 static void
8802f_atan(argvars, rettv)
8803 typval_T *argvars;
8804 typval_T *rettv;
8805{
8806 float_T f;
8807
8808 rettv->v_type = VAR_FLOAT;
8809 if (get_float_arg(argvars, &f) == OK)
8810 rettv->vval.v_float = atan(f);
8811 else
8812 rettv->vval.v_float = 0.0;
8813}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008814
8815/*
8816 * "atan2()" function
8817 */
8818 static void
8819f_atan2(argvars, rettv)
8820 typval_T *argvars;
8821 typval_T *rettv;
8822{
8823 float_T fx, fy;
8824
8825 rettv->v_type = VAR_FLOAT;
8826 if (get_float_arg(argvars, &fx) == OK
8827 && get_float_arg(&argvars[1], &fy) == OK)
8828 rettv->vval.v_float = atan2(fx, fy);
8829 else
8830 rettv->vval.v_float = 0.0;
8831}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008832#endif
8833
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834/*
8835 * "browse(save, title, initdir, default)" function
8836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008839 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
8842#ifdef FEAT_BROWSE
8843 int save;
8844 char_u *title;
8845 char_u *initdir;
8846 char_u *defname;
8847 char_u buf[NUMBUFLEN];
8848 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008849 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008851 save = get_tv_number_chk(&argvars[0], &error);
8852 title = get_tv_string_chk(&argvars[1]);
8853 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8854 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008856 if (error || title == NULL || initdir == NULL || defname == NULL)
8857 rettv->vval.v_string = NULL;
8858 else
8859 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008860 do_browse(save ? BROWSE_SAVE : 0,
8861 title, defname, NULL, initdir, NULL, curbuf);
8862#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008864#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008866}
8867
8868/*
8869 * "browsedir(title, initdir)" function
8870 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008873 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008874 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008875{
8876#ifdef FEAT_BROWSE
8877 char_u *title;
8878 char_u *initdir;
8879 char_u buf[NUMBUFLEN];
8880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008881 title = get_tv_string_chk(&argvars[0]);
8882 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008884 if (title == NULL || initdir == NULL)
8885 rettv->vval.v_string = NULL;
8886 else
8887 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008888 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008890 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008892 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893}
8894
Bram Moolenaar33570922005-01-25 22:26:29 +00008895static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008896
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897/*
8898 * Find a buffer by number or exact name.
8899 */
8900 static buf_T *
8901find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008902 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903{
8904 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008906 if (avar->v_type == VAR_NUMBER)
8907 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008908 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008910 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008911 if (buf == NULL)
8912 {
8913 /* No full path name match, try a match with a URL or a "nofile"
8914 * buffer, these don't use the full path. */
8915 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8916 if (buf->b_fname != NULL
8917 && (path_with_url(buf->b_fname)
8918#ifdef FEAT_QUICKFIX
8919 || bt_nofile(buf)
8920#endif
8921 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008922 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008923 break;
8924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 }
8926 return buf;
8927}
8928
8929/*
8930 * "bufexists(expr)" function
8931 */
8932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008934 typval_T *argvars;
8935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008937 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938}
8939
8940/*
8941 * "buflisted(expr)" function
8942 */
8943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008945 typval_T *argvars;
8946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
8948 buf_T *buf;
8949
8950 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008951 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952}
8953
8954/*
8955 * "bufloaded(expr)" function
8956 */
8957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008959 typval_T *argvars;
8960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961{
8962 buf_T *buf;
8963
8964 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008965 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966}
8967
Bram Moolenaar33570922005-01-25 22:26:29 +00008968static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008969
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970/*
8971 * Get buffer by number or pattern.
8972 */
8973 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008975 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 int save_magic;
8979 char_u *save_cpo;
8980 buf_T *buf;
8981
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 if (tv->v_type == VAR_NUMBER)
8983 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008984 if (tv->v_type != VAR_STRING)
8985 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 if (name == NULL || *name == NUL)
8987 return curbuf;
8988 if (name[0] == '$' && name[1] == NUL)
8989 return lastbuf;
8990
8991 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8992 save_magic = p_magic;
8993 p_magic = TRUE;
8994 save_cpo = p_cpo;
8995 p_cpo = (char_u *)"";
8996
8997 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8998 TRUE, FALSE));
8999
9000 p_magic = save_magic;
9001 p_cpo = save_cpo;
9002
9003 /* If not found, try expanding the name, like done for bufexists(). */
9004 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006
9007 return buf;
9008}
9009
9010/*
9011 * "bufname(expr)" function
9012 */
9013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009014f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009015 typval_T *argvars;
9016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017{
9018 buf_T *buf;
9019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009020 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009022 buf = get_buf_tv(&argvars[0]);
9023 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 --emsg_off;
9029}
9030
9031/*
9032 * "bufnr(expr)" function
9033 */
9034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009036 typval_T *argvars;
9037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
9039 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009040 int error = FALSE;
9041 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009046 --emsg_off;
9047
9048 /* If the buffer isn't found and the second argument is not zero create a
9049 * new buffer. */
9050 if (buf == NULL
9051 && argvars[1].v_type != VAR_UNKNOWN
9052 && get_tv_number_chk(&argvars[1], &error) != 0
9053 && !error
9054 && (name = get_tv_string_chk(&argvars[0])) != NULL
9055 && !error)
9056 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9057
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009059 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062}
9063
9064/*
9065 * "bufwinnr(nr)" function
9066 */
9067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009069 typval_T *argvars;
9070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071{
9072#ifdef FEAT_WINDOWS
9073 win_T *wp;
9074 int winnr = 0;
9075#endif
9076 buf_T *buf;
9077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009078 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081#ifdef FEAT_WINDOWS
9082 for (wp = firstwin; wp; wp = wp->w_next)
9083 {
9084 ++winnr;
9085 if (wp->w_buffer == buf)
9086 break;
9087 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091#endif
9092 --emsg_off;
9093}
9094
9095/*
9096 * "byte2line(byte)" function
9097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009100 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102{
9103#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105#else
9106 long boff = 0;
9107
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009108 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 (linenr_T)0, &boff);
9114#endif
9115}
9116
9117/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009118 * "byteidx()" function
9119 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009122 typval_T *argvars;
9123 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009124{
9125#ifdef FEAT_MBYTE
9126 char_u *t;
9127#endif
9128 char_u *str;
9129 long idx;
9130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009131 str = get_tv_string_chk(&argvars[0]);
9132 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009134 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009135 return;
9136
9137#ifdef FEAT_MBYTE
9138 t = str;
9139 for ( ; idx > 0; idx--)
9140 {
9141 if (*t == NUL) /* EOL reached */
9142 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009143 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009144 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009145 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009146#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009147 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009148 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009149#endif
9150}
9151
9152/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009153 * "call(func, arglist)" function
9154 */
9155 static void
9156f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009157 typval_T *argvars;
9158 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009159{
9160 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009161 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009162 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009163 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009164 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009165 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009166
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009167 if (argvars[1].v_type != VAR_LIST)
9168 {
9169 EMSG(_(e_listreq));
9170 return;
9171 }
9172 if (argvars[1].vval.v_list == NULL)
9173 return;
9174
9175 if (argvars[0].v_type == VAR_FUNC)
9176 func = argvars[0].vval.v_string;
9177 else
9178 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 if (*func == NUL)
9180 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009181
Bram Moolenaare9a41262005-01-15 22:18:47 +00009182 if (argvars[2].v_type != VAR_UNKNOWN)
9183 {
9184 if (argvars[2].v_type != VAR_DICT)
9185 {
9186 EMSG(_(e_dictreq));
9187 return;
9188 }
9189 selfdict = argvars[2].vval.v_dict;
9190 }
9191
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009192 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9193 item = item->li_next)
9194 {
9195 if (argc == MAX_FUNC_ARGS)
9196 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009197 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009198 break;
9199 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009200 /* Make a copy of each argument. This is needed to be able to set
9201 * v_lock to VAR_FIXED in the copy without changing the original list.
9202 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009203 copy_tv(&item->li_tv, &argv[argc++]);
9204 }
9205
9206 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009207 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009208 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9209 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009210
9211 /* Free the arguments. */
9212 while (argc > 0)
9213 clear_tv(&argv[--argc]);
9214}
9215
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009216#ifdef FEAT_FLOAT
9217/*
9218 * "ceil({float})" function
9219 */
9220 static void
9221f_ceil(argvars, rettv)
9222 typval_T *argvars;
9223 typval_T *rettv;
9224{
9225 float_T f;
9226
9227 rettv->v_type = VAR_FLOAT;
9228 if (get_float_arg(argvars, &f) == OK)
9229 rettv->vval.v_float = ceil(f);
9230 else
9231 rettv->vval.v_float = 0.0;
9232}
9233#endif
9234
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009235/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009236 * "changenr()" function
9237 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009238 static void
9239f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009240 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009241 typval_T *rettv;
9242{
9243 rettv->vval.v_number = curbuf->b_u_seq_cur;
9244}
9245
9246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 * "char2nr(string)" function
9248 */
9249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009250f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009251 typval_T *argvars;
9252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253{
9254#ifdef FEAT_MBYTE
9255 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 else
9258#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009259 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260}
9261
9262/*
9263 * "cindent(lnum)" function
9264 */
9265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009266f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009267 typval_T *argvars;
9268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269{
9270#ifdef FEAT_CINDENT
9271 pos_T pos;
9272 linenr_T lnum;
9273
9274 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009275 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9277 {
9278 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009279 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 curwin->w_cursor = pos;
9281 }
9282 else
9283#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009284 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285}
9286
9287/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009288 * "clearmatches()" function
9289 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009290 static void
9291f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009292 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009293 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009294{
9295#ifdef FEAT_SEARCH_EXTRA
9296 clear_matches(curwin);
9297#endif
9298}
9299
9300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 * "col(string)" function
9302 */
9303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009304f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009305 typval_T *argvars;
9306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307{
9308 colnr_T col = 0;
9309 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009310 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009312 fp = var2fpos(&argvars[0], FALSE, &fnum);
9313 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 {
9315 if (fp->col == MAXCOL)
9316 {
9317 /* '> can be MAXCOL, get the length of the line then */
9318 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009319 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 else
9321 col = MAXCOL;
9322 }
9323 else
9324 {
9325 col = fp->col + 1;
9326#ifdef FEAT_VIRTUALEDIT
9327 /* col(".") when the cursor is on the NUL at the end of the line
9328 * because of "coladd" can be seen as an extra column. */
9329 if (virtual_active() && fp == &curwin->w_cursor)
9330 {
9331 char_u *p = ml_get_cursor();
9332
9333 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9334 curwin->w_virtcol - curwin->w_cursor.coladd))
9335 {
9336# ifdef FEAT_MBYTE
9337 int l;
9338
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009339 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 col += l;
9341# else
9342 if (*p != NUL && p[1] == NUL)
9343 ++col;
9344# endif
9345 }
9346 }
9347#endif
9348 }
9349 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351}
9352
Bram Moolenaar572cb562005-08-05 21:35:02 +00009353#if defined(FEAT_INS_EXPAND)
9354/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009355 * "complete()" function
9356 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009357 static void
9358f_complete(argvars, rettv)
9359 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009360 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009361{
9362 int startcol;
9363
9364 if ((State & INSERT) == 0)
9365 {
9366 EMSG(_("E785: complete() can only be used in Insert mode"));
9367 return;
9368 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009369
9370 /* Check for undo allowed here, because if something was already inserted
9371 * the line was already saved for undo and this check isn't done. */
9372 if (!undo_allowed())
9373 return;
9374
Bram Moolenaarade00832006-03-10 21:46:58 +00009375 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9376 {
9377 EMSG(_(e_invarg));
9378 return;
9379 }
9380
9381 startcol = get_tv_number_chk(&argvars[0], NULL);
9382 if (startcol <= 0)
9383 return;
9384
9385 set_completion(startcol - 1, argvars[1].vval.v_list);
9386}
9387
9388/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009389 * "complete_add()" function
9390 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009391 static void
9392f_complete_add(argvars, rettv)
9393 typval_T *argvars;
9394 typval_T *rettv;
9395{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009396 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009397}
9398
9399/*
9400 * "complete_check()" function
9401 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009402 static void
9403f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009404 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009405 typval_T *rettv;
9406{
9407 int saved = RedrawingDisabled;
9408
9409 RedrawingDisabled = 0;
9410 ins_compl_check_keys(0);
9411 rettv->vval.v_number = compl_interrupted;
9412 RedrawingDisabled = saved;
9413}
9414#endif
9415
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416/*
9417 * "confirm(message, buttons[, default [, type]])" function
9418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009421 typval_T *argvars UNUSED;
9422 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423{
9424#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9425 char_u *message;
9426 char_u *buttons = NULL;
9427 char_u buf[NUMBUFLEN];
9428 char_u buf2[NUMBUFLEN];
9429 int def = 1;
9430 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009431 char_u *typestr;
9432 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009434 message = get_tv_string_chk(&argvars[0]);
9435 if (message == NULL)
9436 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009437 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009439 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9440 if (buttons == NULL)
9441 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009442 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009444 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009445 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009447 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9448 if (typestr == NULL)
9449 error = TRUE;
9450 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009452 switch (TOUPPER_ASC(*typestr))
9453 {
9454 case 'E': type = VIM_ERROR; break;
9455 case 'Q': type = VIM_QUESTION; break;
9456 case 'I': type = VIM_INFO; break;
9457 case 'W': type = VIM_WARNING; break;
9458 case 'G': type = VIM_GENERIC; break;
9459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 }
9461 }
9462 }
9463 }
9464
9465 if (buttons == NULL || *buttons == NUL)
9466 buttons = (char_u *)_("&Ok");
9467
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009468 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009469 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009470 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471#endif
9472}
9473
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009474/*
9475 * "copy()" function
9476 */
9477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009479 typval_T *argvars;
9480 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009481{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009482 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009485#ifdef FEAT_FLOAT
9486/*
9487 * "cos()" function
9488 */
9489 static void
9490f_cos(argvars, rettv)
9491 typval_T *argvars;
9492 typval_T *rettv;
9493{
9494 float_T f;
9495
9496 rettv->v_type = VAR_FLOAT;
9497 if (get_float_arg(argvars, &f) == OK)
9498 rettv->vval.v_float = cos(f);
9499 else
9500 rettv->vval.v_float = 0.0;
9501}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009502
9503/*
9504 * "cosh()" function
9505 */
9506 static void
9507f_cosh(argvars, rettv)
9508 typval_T *argvars;
9509 typval_T *rettv;
9510{
9511 float_T f;
9512
9513 rettv->v_type = VAR_FLOAT;
9514 if (get_float_arg(argvars, &f) == OK)
9515 rettv->vval.v_float = cosh(f);
9516 else
9517 rettv->vval.v_float = 0.0;
9518}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009519#endif
9520
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009522 * "count()" function
9523 */
9524 static void
9525f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009526 typval_T *argvars;
9527 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009528{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009529 long n = 0;
9530 int ic = FALSE;
9531
Bram Moolenaare9a41262005-01-15 22:18:47 +00009532 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009533 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009534 listitem_T *li;
9535 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009536 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009537
Bram Moolenaare9a41262005-01-15 22:18:47 +00009538 if ((l = argvars[0].vval.v_list) != NULL)
9539 {
9540 li = l->lv_first;
9541 if (argvars[2].v_type != VAR_UNKNOWN)
9542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009543 int error = FALSE;
9544
9545 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009546 if (argvars[3].v_type != VAR_UNKNOWN)
9547 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009548 idx = get_tv_number_chk(&argvars[3], &error);
9549 if (!error)
9550 {
9551 li = list_find(l, idx);
9552 if (li == NULL)
9553 EMSGN(_(e_listidx), idx);
9554 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009555 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009556 if (error)
9557 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009558 }
9559
9560 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009561 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009562 ++n;
9563 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009564 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009565 else if (argvars[0].v_type == VAR_DICT)
9566 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009567 int todo;
9568 dict_T *d;
9569 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009570
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009571 if ((d = argvars[0].vval.v_dict) != NULL)
9572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009573 int error = FALSE;
9574
Bram Moolenaare9a41262005-01-15 22:18:47 +00009575 if (argvars[2].v_type != VAR_UNKNOWN)
9576 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009577 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009578 if (argvars[3].v_type != VAR_UNKNOWN)
9579 EMSG(_(e_invarg));
9580 }
9581
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009582 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009583 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009584 {
9585 if (!HASHITEM_EMPTY(hi))
9586 {
9587 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009588 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009589 ++n;
9590 }
9591 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009592 }
9593 }
9594 else
9595 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009596 rettv->vval.v_number = n;
9597}
9598
9599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9601 *
9602 * Checks the existence of a cscope connection.
9603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009606 typval_T *argvars UNUSED;
9607 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
9609#ifdef FEAT_CSCOPE
9610 int num = 0;
9611 char_u *dbpath = NULL;
9612 char_u *prepend = NULL;
9613 char_u buf[NUMBUFLEN];
9614
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009615 if (argvars[0].v_type != VAR_UNKNOWN
9616 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 num = (int)get_tv_number(&argvars[0]);
9619 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009620 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 }
9623
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625#endif
9626}
9627
9628/*
9629 * "cursor(lnum, col)" function
9630 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009631 * Moves the cursor to the specified line and column.
9632 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009636 typval_T *argvars;
9637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638{
9639 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009640#ifdef FEAT_VIRTUALEDIT
9641 long coladd = 0;
9642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009644 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009645 if (argvars[1].v_type == VAR_UNKNOWN)
9646 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009647 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009648
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009649 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009650 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009651 line = pos.lnum;
9652 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009653#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009654 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009655#endif
9656 }
9657 else
9658 {
9659 line = get_tv_lnum(argvars);
9660 col = get_tv_number_chk(&argvars[1], NULL);
9661#ifdef FEAT_VIRTUALEDIT
9662 if (argvars[2].v_type != VAR_UNKNOWN)
9663 coladd = get_tv_number_chk(&argvars[2], NULL);
9664#endif
9665 }
9666 if (line < 0 || col < 0
9667#ifdef FEAT_VIRTUALEDIT
9668 || coladd < 0
9669#endif
9670 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009671 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 if (line > 0)
9673 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 if (col > 0)
9675 curwin->w_cursor.col = col - 1;
9676#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009677 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678#endif
9679
9680 /* Make sure the cursor is in a valid position. */
9681 check_cursor();
9682#ifdef FEAT_MBYTE
9683 /* Correct cursor for multi-byte character. */
9684 if (has_mbyte)
9685 mb_adjust_cursor();
9686#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009687
9688 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009689 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690}
9691
9692/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009693 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 */
9695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009697 typval_T *argvars;
9698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009700 int noref = 0;
9701
9702 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009703 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009704 if (noref < 0 || noref > 1)
9705 EMSG(_(e_invarg));
9706 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009707 {
9708 current_copyID += COPYID_INC;
9709 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711}
9712
9713/*
9714 * "delete()" function
9715 */
9716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009718 typval_T *argvars;
9719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
9721 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725}
9726
9727/*
9728 * "did_filetype()" function
9729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009732 typval_T *argvars UNUSED;
9733 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734{
9735#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737#endif
9738}
9739
9740/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009741 * "diff_filler()" function
9742 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009744f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009745 typval_T *argvars UNUSED;
9746 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009747{
9748#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009750#endif
9751}
9752
9753/*
9754 * "diff_hlID()" function
9755 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009757f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009758 typval_T *argvars UNUSED;
9759 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009760{
9761#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009763 static linenr_T prev_lnum = 0;
9764 static int changedtick = 0;
9765 static int fnum = 0;
9766 static int change_start = 0;
9767 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009768 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009769 int filler_lines;
9770 int col;
9771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009772 if (lnum < 0) /* ignore type error in {lnum} arg */
9773 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009774 if (lnum != prev_lnum
9775 || changedtick != curbuf->b_changedtick
9776 || fnum != curbuf->b_fnum)
9777 {
9778 /* New line, buffer, change: need to get the values. */
9779 filler_lines = diff_check(curwin, lnum);
9780 if (filler_lines < 0)
9781 {
9782 if (filler_lines == -1)
9783 {
9784 change_start = MAXCOL;
9785 change_end = -1;
9786 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9787 hlID = HLF_ADD; /* added line */
9788 else
9789 hlID = HLF_CHD; /* changed line */
9790 }
9791 else
9792 hlID = HLF_ADD; /* added line */
9793 }
9794 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009795 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009796 prev_lnum = lnum;
9797 changedtick = curbuf->b_changedtick;
9798 fnum = curbuf->b_fnum;
9799 }
9800
9801 if (hlID == HLF_CHD || hlID == HLF_TXD)
9802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009804 if (col >= change_start && col <= change_end)
9805 hlID = HLF_TXD; /* changed text */
9806 else
9807 hlID = HLF_CHD; /* changed line */
9808 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009809 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009810#endif
9811}
9812
9813/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009814 * "empty({expr})" function
9815 */
9816 static void
9817f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009818 typval_T *argvars;
9819 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009820{
9821 int n;
9822
9823 switch (argvars[0].v_type)
9824 {
9825 case VAR_STRING:
9826 case VAR_FUNC:
9827 n = argvars[0].vval.v_string == NULL
9828 || *argvars[0].vval.v_string == NUL;
9829 break;
9830 case VAR_NUMBER:
9831 n = argvars[0].vval.v_number == 0;
9832 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009833#ifdef FEAT_FLOAT
9834 case VAR_FLOAT:
9835 n = argvars[0].vval.v_float == 0.0;
9836 break;
9837#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009838 case VAR_LIST:
9839 n = argvars[0].vval.v_list == NULL
9840 || argvars[0].vval.v_list->lv_first == NULL;
9841 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009842 case VAR_DICT:
9843 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009844 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009845 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009846 default:
9847 EMSG2(_(e_intern2), "f_empty()");
9848 n = 0;
9849 }
9850
9851 rettv->vval.v_number = n;
9852}
9853
9854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 * "escape({string}, {chars})" function
9856 */
9857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009859 typval_T *argvars;
9860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861{
9862 char_u buf[NUMBUFLEN];
9863
Bram Moolenaar758711c2005-02-02 23:11:38 +00009864 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9865 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867}
9868
9869/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009870 * "eval()" function
9871 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009872 static void
9873f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009874 typval_T *argvars;
9875 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009876{
9877 char_u *s;
9878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009879 s = get_tv_string_chk(&argvars[0]);
9880 if (s != NULL)
9881 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009883 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9884 {
9885 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009886 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009887 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009888 else if (*s != NUL)
9889 EMSG(_(e_trailing));
9890}
9891
9892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 * "eventhandler()" function
9894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009896f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009897 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009900 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901}
9902
9903/*
9904 * "executable()" function
9905 */
9906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009908 typval_T *argvars;
9909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009911 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912}
9913
9914/*
9915 * "exists()" function
9916 */
9917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009919 typval_T *argvars;
9920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921{
9922 char_u *p;
9923 char_u *name;
9924 int n = FALSE;
9925 int len = 0;
9926
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009927 no_autoload = TRUE;
9928
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009929 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 if (*p == '$') /* environment variable */
9931 {
9932 /* first try "normal" environment variables (fast) */
9933 if (mch_getenv(p + 1) != NULL)
9934 n = TRUE;
9935 else
9936 {
9937 /* try expanding things like $VIM and ${HOME} */
9938 p = expand_env_save(p);
9939 if (p != NULL && *p != '$')
9940 n = TRUE;
9941 vim_free(p);
9942 }
9943 }
9944 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009947 if (*skipwhite(p) != NUL)
9948 n = FALSE; /* trailing garbage */
9949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 else if (*p == '*') /* internal or user defined function */
9951 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009952 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 }
9954 else if (*p == ':')
9955 {
9956 n = cmd_exists(p + 1);
9957 }
9958 else if (*p == '#')
9959 {
9960#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009961 if (p[1] == '#')
9962 n = autocmd_supported(p + 2);
9963 else
9964 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965#endif
9966 }
9967 else /* internal variable */
9968 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009969 char_u *tofree;
9970 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009972 /* get_name_len() takes care of expanding curly braces */
9973 name = p;
9974 len = get_name_len(&p, &tofree, TRUE, FALSE);
9975 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009977 if (tofree != NULL)
9978 name = tofree;
9979 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9980 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009982 /* handle d.key, l[idx], f(expr) */
9983 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9984 if (n)
9985 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 }
9987 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009988 if (*p != NUL)
9989 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009991 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 }
9993
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009995
9996 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997}
9998
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009999#ifdef FEAT_FLOAT
10000/*
10001 * "exp()" function
10002 */
10003 static void
10004f_exp(argvars, rettv)
10005 typval_T *argvars;
10006 typval_T *rettv;
10007{
10008 float_T f;
10009
10010 rettv->v_type = VAR_FLOAT;
10011 if (get_float_arg(argvars, &f) == OK)
10012 rettv->vval.v_float = exp(f);
10013 else
10014 rettv->vval.v_float = 0.0;
10015}
10016#endif
10017
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018/*
10019 * "expand()" function
10020 */
10021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010022f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010023 typval_T *argvars;
10024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025{
10026 char_u *s;
10027 int len;
10028 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010029 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010031 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010032 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010034 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010035 if (argvars[1].v_type != VAR_UNKNOWN
10036 && argvars[2].v_type != VAR_UNKNOWN
10037 && get_tv_number_chk(&argvars[2], &error)
10038 && !error)
10039 {
10040 rettv->v_type = VAR_LIST;
10041 rettv->vval.v_list = NULL;
10042 }
10043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 if (*s == '%' || *s == '#' || *s == '<')
10046 {
10047 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010048 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010050 if (rettv->v_type == VAR_LIST)
10051 {
10052 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10053 list_append_string(rettv->vval.v_list, result, -1);
10054 else
10055 vim_free(result);
10056 }
10057 else
10058 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 }
10060 else
10061 {
10062 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010063 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010064 if (argvars[1].v_type != VAR_UNKNOWN
10065 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010066 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010067 if (!error)
10068 {
10069 ExpandInit(&xpc);
10070 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010071 if (p_wic)
10072 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010073 if (rettv->v_type == VAR_STRING)
10074 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10075 options, WILD_ALL);
10076 else if (rettv_list_alloc(rettv) != FAIL)
10077 {
10078 int i;
10079
10080 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10081 for (i = 0; i < xpc.xp_numfiles; i++)
10082 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10083 ExpandCleanup(&xpc);
10084 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010085 }
10086 else
10087 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 }
10089}
10090
10091/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010092 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010093 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010094 */
10095 static void
10096f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010097 typval_T *argvars;
10098 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010099{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010100 char *arg_errmsg = N_("extend() argument");
10101
Bram Moolenaare9a41262005-01-15 22:18:47 +000010102 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010103 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010104 list_T *l1, *l2;
10105 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010106 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010107 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010108
Bram Moolenaare9a41262005-01-15 22:18:47 +000010109 l1 = argvars[0].vval.v_list;
10110 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010111 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010112 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010113 {
10114 if (argvars[2].v_type != VAR_UNKNOWN)
10115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010116 before = get_tv_number_chk(&argvars[2], &error);
10117 if (error)
10118 return; /* type error; errmsg already given */
10119
Bram Moolenaar758711c2005-02-02 23:11:38 +000010120 if (before == l1->lv_len)
10121 item = NULL;
10122 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010123 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010124 item = list_find(l1, before);
10125 if (item == NULL)
10126 {
10127 EMSGN(_(e_listidx), before);
10128 return;
10129 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010130 }
10131 }
10132 else
10133 item = NULL;
10134 list_extend(l1, l2, item);
10135
Bram Moolenaare9a41262005-01-15 22:18:47 +000010136 copy_tv(&argvars[0], rettv);
10137 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010138 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010139 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10140 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010141 dict_T *d1, *d2;
10142 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010143 char_u *action;
10144 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010145 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010146 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010147
10148 d1 = argvars[0].vval.v_dict;
10149 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010150 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010151 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010152 {
10153 /* Check the third argument. */
10154 if (argvars[2].v_type != VAR_UNKNOWN)
10155 {
10156 static char *(av[]) = {"keep", "force", "error"};
10157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010158 action = get_tv_string_chk(&argvars[2]);
10159 if (action == NULL)
10160 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010161 for (i = 0; i < 3; ++i)
10162 if (STRCMP(action, av[i]) == 0)
10163 break;
10164 if (i == 3)
10165 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010166 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010167 return;
10168 }
10169 }
10170 else
10171 action = (char_u *)"force";
10172
10173 /* Go over all entries in the second dict and add them to the
10174 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010175 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010176 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010177 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010178 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010179 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010180 --todo;
10181 di1 = dict_find(d1, hi2->hi_key, -1);
10182 if (di1 == NULL)
10183 {
10184 di1 = dictitem_copy(HI2DI(hi2));
10185 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10186 dictitem_free(di1);
10187 }
10188 else if (*action == 'e')
10189 {
10190 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10191 break;
10192 }
10193 else if (*action == 'f')
10194 {
10195 clear_tv(&di1->di_tv);
10196 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10197 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010198 }
10199 }
10200
Bram Moolenaare9a41262005-01-15 22:18:47 +000010201 copy_tv(&argvars[0], rettv);
10202 }
10203 }
10204 else
10205 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010206}
10207
10208/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010209 * "feedkeys()" function
10210 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010211 static void
10212f_feedkeys(argvars, rettv)
10213 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010214 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010215{
10216 int remap = TRUE;
10217 char_u *keys, *flags;
10218 char_u nbuf[NUMBUFLEN];
10219 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010220 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010221
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010222 /* This is not allowed in the sandbox. If the commands would still be
10223 * executed in the sandbox it would be OK, but it probably happens later,
10224 * when "sandbox" is no longer set. */
10225 if (check_secure())
10226 return;
10227
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010228 keys = get_tv_string(&argvars[0]);
10229 if (*keys != NUL)
10230 {
10231 if (argvars[1].v_type != VAR_UNKNOWN)
10232 {
10233 flags = get_tv_string_buf(&argvars[1], nbuf);
10234 for ( ; *flags != NUL; ++flags)
10235 {
10236 switch (*flags)
10237 {
10238 case 'n': remap = FALSE; break;
10239 case 'm': remap = TRUE; break;
10240 case 't': typed = TRUE; break;
10241 }
10242 }
10243 }
10244
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010245 /* Need to escape K_SPECIAL and CSI before putting the string in the
10246 * typeahead buffer. */
10247 keys_esc = vim_strsave_escape_csi(keys);
10248 if (keys_esc != NULL)
10249 {
10250 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010251 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010252 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010253 if (vgetc_busy)
10254 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010255 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010256 }
10257}
10258
10259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 * "filereadable()" function
10261 */
10262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010263f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010264 typval_T *argvars;
10265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010267 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 char_u *p;
10269 int n;
10270
Bram Moolenaarc236c162008-07-13 17:41:49 +000010271#ifndef O_NONBLOCK
10272# define O_NONBLOCK 0
10273#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010274 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010275 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10276 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 {
10278 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010279 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 }
10281 else
10282 n = FALSE;
10283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010284 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285}
10286
10287/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010288 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 * rights to write into.
10290 */
10291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010292f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 typval_T *argvars;
10294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010296 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297}
10298
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010299static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010300
10301 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010302findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010303 typval_T *argvars;
10304 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010305 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010306{
10307#ifdef FEAT_SEARCHPATH
10308 char_u *fname;
10309 char_u *fresult = NULL;
10310 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10311 char_u *p;
10312 char_u pathbuf[NUMBUFLEN];
10313 int count = 1;
10314 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010315 int error = FALSE;
10316#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010317
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010318 rettv->vval.v_string = NULL;
10319 rettv->v_type = VAR_STRING;
10320
10321#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010322 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010323
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010324 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010326 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10327 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010328 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 else
10330 {
10331 if (*p != NUL)
10332 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010333
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010334 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010335 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010336 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010337 }
10338
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010339 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10340 error = TRUE;
10341
10342 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 do
10345 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010346 if (rettv->v_type == VAR_STRING)
10347 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010348 fresult = find_file_in_path_option(first ? fname : NULL,
10349 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010350 0, first, path,
10351 find_what,
10352 curbuf->b_ffname,
10353 find_what == FINDFILE_DIR
10354 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010355 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010356
10357 if (fresult != NULL && rettv->v_type == VAR_LIST)
10358 list_append_string(rettv->vval.v_list, fresult, -1);
10359
10360 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010361 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010362
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010363 if (rettv->v_type == VAR_STRING)
10364 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010365#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010366}
10367
Bram Moolenaar33570922005-01-25 22:26:29 +000010368static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10369static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010370
10371/*
10372 * Implementation of map() and filter().
10373 */
10374 static void
10375filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010376 typval_T *argvars;
10377 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010378 int map;
10379{
10380 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010381 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010382 listitem_T *li, *nli;
10383 list_T *l = NULL;
10384 dictitem_T *di;
10385 hashtab_T *ht;
10386 hashitem_T *hi;
10387 dict_T *d = NULL;
10388 typval_T save_val;
10389 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010390 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010391 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010392 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10393 char *arg_errmsg = (map ? N_("map() argument")
10394 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010395 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010396 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010397
Bram Moolenaare9a41262005-01-15 22:18:47 +000010398 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010399 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010400 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010401 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010402 return;
10403 }
10404 else if (argvars[0].v_type == VAR_DICT)
10405 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010406 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010407 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010408 return;
10409 }
10410 else
10411 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010412 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010413 return;
10414 }
10415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010416 expr = get_tv_string_buf_chk(&argvars[1], buf);
10417 /* On type errors, the preceding call has already displayed an error
10418 * message. Avoid a misleading error message for an empty string that
10419 * was not passed as argument. */
10420 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010422 prepare_vimvar(VV_VAL, &save_val);
10423 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010424
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010425 /* We reset "did_emsg" to be able to detect whether an error
10426 * occurred during evaluation of the expression. */
10427 save_did_emsg = did_emsg;
10428 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010429
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010430 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010431 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010433 vimvars[VV_KEY].vv_type = VAR_STRING;
10434
10435 ht = &d->dv_hashtab;
10436 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010437 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010438 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 if (!HASHITEM_EMPTY(hi))
10441 {
10442 --todo;
10443 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010444 if (tv_check_lock(di->di_tv.v_lock,
10445 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 break;
10447 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010448 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010449 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 break;
10451 if (!map && rem)
10452 dictitem_remove(d, di);
10453 clear_tv(&vimvars[VV_KEY].vv_tv);
10454 }
10455 }
10456 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 }
10458 else
10459 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010460 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 for (li = l->lv_first; li != NULL; li = nli)
10463 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010464 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010465 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010467 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010468 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010469 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010470 break;
10471 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010472 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010473 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010474 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010475 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010476
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010477 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010478 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010479
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010480 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010482
10483 copy_tv(&argvars[0], rettv);
10484}
10485
10486 static int
10487filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010488 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010489 char_u *expr;
10490 int map;
10491 int *remp;
10492{
Bram Moolenaar33570922005-01-25 22:26:29 +000010493 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010494 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010495 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010496
Bram Moolenaar33570922005-01-25 22:26:29 +000010497 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010498 s = expr;
10499 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010500 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010501 if (*s != NUL) /* check for trailing chars after expr */
10502 {
10503 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010504 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010505 }
10506 if (map)
10507 {
10508 /* map(): replace the list item value */
10509 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010510 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010511 *tv = rettv;
10512 }
10513 else
10514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010515 int error = FALSE;
10516
Bram Moolenaare9a41262005-01-15 22:18:47 +000010517 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010518 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010519 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010520 /* On type error, nothing has been removed; return FAIL to stop the
10521 * loop. The error message was given by get_tv_number_chk(). */
10522 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010523 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010524 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010525 retval = OK;
10526theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010527 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010528 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010529}
10530
10531/*
10532 * "filter()" function
10533 */
10534 static void
10535f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010536 typval_T *argvars;
10537 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010538{
10539 filter_map(argvars, rettv, FALSE);
10540}
10541
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010542/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010543 * "finddir({fname}[, {path}[, {count}]])" function
10544 */
10545 static void
10546f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010547 typval_T *argvars;
10548 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010549{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010550 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010551}
10552
10553/*
10554 * "findfile({fname}[, {path}[, {count}]])" function
10555 */
10556 static void
10557f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010558 typval_T *argvars;
10559 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010560{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010561 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010562}
10563
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010564#ifdef FEAT_FLOAT
10565/*
10566 * "float2nr({float})" function
10567 */
10568 static void
10569f_float2nr(argvars, rettv)
10570 typval_T *argvars;
10571 typval_T *rettv;
10572{
10573 float_T f;
10574
10575 if (get_float_arg(argvars, &f) == OK)
10576 {
10577 if (f < -0x7fffffff)
10578 rettv->vval.v_number = -0x7fffffff;
10579 else if (f > 0x7fffffff)
10580 rettv->vval.v_number = 0x7fffffff;
10581 else
10582 rettv->vval.v_number = (varnumber_T)f;
10583 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010584}
10585
10586/*
10587 * "floor({float})" function
10588 */
10589 static void
10590f_floor(argvars, rettv)
10591 typval_T *argvars;
10592 typval_T *rettv;
10593{
10594 float_T f;
10595
10596 rettv->v_type = VAR_FLOAT;
10597 if (get_float_arg(argvars, &f) == OK)
10598 rettv->vval.v_float = floor(f);
10599 else
10600 rettv->vval.v_float = 0.0;
10601}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010602
10603/*
10604 * "fmod()" function
10605 */
10606 static void
10607f_fmod(argvars, rettv)
10608 typval_T *argvars;
10609 typval_T *rettv;
10610{
10611 float_T fx, fy;
10612
10613 rettv->v_type = VAR_FLOAT;
10614 if (get_float_arg(argvars, &fx) == OK
10615 && get_float_arg(&argvars[1], &fy) == OK)
10616 rettv->vval.v_float = fmod(fx, fy);
10617 else
10618 rettv->vval.v_float = 0.0;
10619}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010620#endif
10621
Bram Moolenaar0d660222005-01-07 21:51:51 +000010622/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010623 * "fnameescape({string})" function
10624 */
10625 static void
10626f_fnameescape(argvars, rettv)
10627 typval_T *argvars;
10628 typval_T *rettv;
10629{
10630 rettv->vval.v_string = vim_strsave_fnameescape(
10631 get_tv_string(&argvars[0]), FALSE);
10632 rettv->v_type = VAR_STRING;
10633}
10634
10635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 * "fnamemodify({fname}, {mods})" function
10637 */
10638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010639f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010640 typval_T *argvars;
10641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642{
10643 char_u *fname;
10644 char_u *mods;
10645 int usedlen = 0;
10646 int len;
10647 char_u *fbuf = NULL;
10648 char_u buf[NUMBUFLEN];
10649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010650 fname = get_tv_string_chk(&argvars[0]);
10651 mods = get_tv_string_buf_chk(&argvars[1], buf);
10652 if (fname == NULL || mods == NULL)
10653 fname = NULL;
10654 else
10655 {
10656 len = (int)STRLEN(fname);
10657 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010662 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010664 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 vim_free(fbuf);
10666}
10667
Bram Moolenaar33570922005-01-25 22:26:29 +000010668static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669
10670/*
10671 * "foldclosed()" function
10672 */
10673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010674foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010675 typval_T *argvars;
10676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 int end;
10678{
10679#ifdef FEAT_FOLDING
10680 linenr_T lnum;
10681 linenr_T first, last;
10682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010683 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10685 {
10686 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10687 {
10688 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 return;
10693 }
10694 }
10695#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010696 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697}
10698
10699/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010700 * "foldclosed()" function
10701 */
10702 static void
10703f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010704 typval_T *argvars;
10705 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010706{
10707 foldclosed_both(argvars, rettv, FALSE);
10708}
10709
10710/*
10711 * "foldclosedend()" function
10712 */
10713 static void
10714f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010715 typval_T *argvars;
10716 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010717{
10718 foldclosed_both(argvars, rettv, TRUE);
10719}
10720
10721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 * "foldlevel()" function
10723 */
10724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010725f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010726 typval_T *argvars;
10727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728{
10729#ifdef FEAT_FOLDING
10730 linenr_T lnum;
10731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010732 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010734 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736}
10737
10738/*
10739 * "foldtext()" function
10740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010743 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745{
10746#ifdef FEAT_FOLDING
10747 linenr_T lnum;
10748 char_u *s;
10749 char_u *r;
10750 int len;
10751 char *txt;
10752#endif
10753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->v_type = VAR_STRING;
10755 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010757 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10758 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10759 <= curbuf->b_ml.ml_line_count
10760 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 {
10762 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010763 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10764 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 {
10766 if (!linewhite(lnum))
10767 break;
10768 ++lnum;
10769 }
10770
10771 /* Find interesting text in this line. */
10772 s = skipwhite(ml_get(lnum));
10773 /* skip C comment-start */
10774 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010775 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010777 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010778 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010779 {
10780 s = skipwhite(ml_get(lnum + 1));
10781 if (*s == '*')
10782 s = skipwhite(s + 1);
10783 }
10784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 txt = _("+-%s%3ld lines: ");
10786 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 + 20 /* for %3ld */
10789 + STRLEN(s))); /* concatenated */
10790 if (r != NULL)
10791 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010792 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10793 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10794 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 len = (int)STRLEN(r);
10796 STRCAT(r, s);
10797 /* remove 'foldmarker' and 'commentstring' */
10798 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010799 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 }
10801 }
10802#endif
10803}
10804
10805/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010806 * "foldtextresult(lnum)" function
10807 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010810 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010811 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010812{
10813#ifdef FEAT_FOLDING
10814 linenr_T lnum;
10815 char_u *text;
10816 char_u buf[51];
10817 foldinfo_T foldinfo;
10818 int fold_count;
10819#endif
10820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821 rettv->v_type = VAR_STRING;
10822 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010823#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010824 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010825 /* treat illegal types and illegal string values for {lnum} the same */
10826 if (lnum < 0)
10827 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010828 fold_count = foldedCount(curwin, lnum, &foldinfo);
10829 if (fold_count > 0)
10830 {
10831 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10832 &foldinfo, buf);
10833 if (text == buf)
10834 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010835 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010836 }
10837#endif
10838}
10839
10840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 * "foreground()" function
10842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010845 typval_T *argvars UNUSED;
10846 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848#ifdef FEAT_GUI
10849 if (gui.in_use)
10850 gui_mch_set_foreground();
10851#else
10852# ifdef WIN32
10853 win32_set_foreground();
10854# endif
10855#endif
10856}
10857
10858/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010859 * "function()" function
10860 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010863 typval_T *argvars;
10864 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010865{
10866 char_u *s;
10867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010868 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010869 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010870 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010871 /* Don't check an autoload name for existence here. */
10872 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010873 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010874 else
10875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010876 rettv->vval.v_string = vim_strsave(s);
10877 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010878 }
10879}
10880
10881/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010882 * "garbagecollect()" function
10883 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010884 static void
10885f_garbagecollect(argvars, rettv)
10886 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010887 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010888{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010889 /* This is postponed until we are back at the toplevel, because we may be
10890 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10891 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010892
10893 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10894 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010895}
10896
10897/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010898 * "get()" function
10899 */
10900 static void
10901f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010902 typval_T *argvars;
10903 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010904{
Bram Moolenaar33570922005-01-25 22:26:29 +000010905 listitem_T *li;
10906 list_T *l;
10907 dictitem_T *di;
10908 dict_T *d;
10909 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010910
Bram Moolenaare9a41262005-01-15 22:18:47 +000010911 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010912 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010913 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 int error = FALSE;
10916
10917 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10918 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010919 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010920 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010921 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010922 else if (argvars[0].v_type == VAR_DICT)
10923 {
10924 if ((d = argvars[0].vval.v_dict) != NULL)
10925 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010926 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010927 if (di != NULL)
10928 tv = &di->di_tv;
10929 }
10930 }
10931 else
10932 EMSG2(_(e_listdictarg), "get()");
10933
10934 if (tv == NULL)
10935 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010936 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937 copy_tv(&argvars[2], rettv);
10938 }
10939 else
10940 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010941}
10942
Bram Moolenaar342337a2005-07-21 21:11:17 +000010943static 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 +000010944
10945/*
10946 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010947 * Return a range (from start to end) of lines in rettv from the specified
10948 * buffer.
10949 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010950 */
10951 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010952get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010953 buf_T *buf;
10954 linenr_T start;
10955 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010956 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010957 typval_T *rettv;
10958{
10959 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010960
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010961 if (retlist && rettv_list_alloc(rettv) == FAIL)
10962 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010963
10964 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10965 return;
10966
10967 if (!retlist)
10968 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010969 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10970 p = ml_get_buf(buf, start, FALSE);
10971 else
10972 p = (char_u *)"";
10973
10974 rettv->v_type = VAR_STRING;
10975 rettv->vval.v_string = vim_strsave(p);
10976 }
10977 else
10978 {
10979 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010980 return;
10981
10982 if (start < 1)
10983 start = 1;
10984 if (end > buf->b_ml.ml_line_count)
10985 end = buf->b_ml.ml_line_count;
10986 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010987 if (list_append_string(rettv->vval.v_list,
10988 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010989 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010990 }
10991}
10992
10993/*
10994 * "getbufline()" function
10995 */
10996 static void
10997f_getbufline(argvars, rettv)
10998 typval_T *argvars;
10999 typval_T *rettv;
11000{
11001 linenr_T lnum;
11002 linenr_T end;
11003 buf_T *buf;
11004
11005 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11006 ++emsg_off;
11007 buf = get_buf_tv(&argvars[0]);
11008 --emsg_off;
11009
Bram Moolenaar661b1822005-07-28 22:36:45 +000011010 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011011 if (argvars[2].v_type == VAR_UNKNOWN)
11012 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011013 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011014 end = get_tv_lnum_buf(&argvars[2], buf);
11015
Bram Moolenaar342337a2005-07-21 21:11:17 +000011016 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011017}
11018
Bram Moolenaar0d660222005-01-07 21:51:51 +000011019/*
11020 * "getbufvar()" function
11021 */
11022 static void
11023f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011024 typval_T *argvars;
11025 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011026{
11027 buf_T *buf;
11028 buf_T *save_curbuf;
11029 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011030 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011031
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011032 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11033 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011034 ++emsg_off;
11035 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011036
11037 rettv->v_type = VAR_STRING;
11038 rettv->vval.v_string = NULL;
11039
11040 if (buf != NULL && varname != NULL)
11041 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011042 /* set curbuf to be our buf, temporarily */
11043 save_curbuf = curbuf;
11044 curbuf = buf;
11045
Bram Moolenaar0d660222005-01-07 21:51:51 +000011046 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011047 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011048 else if (STRCMP(varname, "changedtick") == 0)
11049 {
11050 rettv->v_type = VAR_NUMBER;
11051 rettv->vval.v_number = curbuf->b_changedtick;
11052 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011053 else
11054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011055 if (*varname == NUL)
11056 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11057 * scope prefix before the NUL byte is required by
11058 * find_var_in_ht(). */
11059 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011060 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011061 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011062 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011063 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011064 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011065
11066 /* restore previous notion of curbuf */
11067 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011068 }
11069
11070 --emsg_off;
11071}
11072
11073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 * "getchar()" function
11075 */
11076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011078 typval_T *argvars;
11079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080{
11081 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011082 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011084 /* Position the cursor. Needed after a message that ends in a space. */
11085 windgoto(msg_row, msg_col);
11086
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 ++no_mapping;
11088 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011089 for (;;)
11090 {
11091 if (argvars[0].v_type == VAR_UNKNOWN)
11092 /* getchar(): blocking wait. */
11093 n = safe_vgetc();
11094 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11095 /* getchar(1): only check if char avail */
11096 n = vpeekc();
11097 else if (error || vpeekc() == NUL)
11098 /* illegal argument or getchar(0) and no char avail: return zero */
11099 n = 0;
11100 else
11101 /* getchar(0) and char avail: return char */
11102 n = safe_vgetc();
11103 if (n == K_IGNORE)
11104 continue;
11105 break;
11106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 --no_mapping;
11108 --allow_keys;
11109
Bram Moolenaar219b8702006-11-01 14:32:36 +000011110 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11111 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11112 vimvars[VV_MOUSE_COL].vv_nr = 0;
11113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 if (IS_SPECIAL(n) || mod_mask != 0)
11116 {
11117 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11118 int i = 0;
11119
11120 /* Turn a special key into three bytes, plus modifier. */
11121 if (mod_mask != 0)
11122 {
11123 temp[i++] = K_SPECIAL;
11124 temp[i++] = KS_MODIFIER;
11125 temp[i++] = mod_mask;
11126 }
11127 if (IS_SPECIAL(n))
11128 {
11129 temp[i++] = K_SPECIAL;
11130 temp[i++] = K_SECOND(n);
11131 temp[i++] = K_THIRD(n);
11132 }
11133#ifdef FEAT_MBYTE
11134 else if (has_mbyte)
11135 i += (*mb_char2bytes)(n, temp + i);
11136#endif
11137 else
11138 temp[i++] = n;
11139 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 rettv->v_type = VAR_STRING;
11141 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011142
11143#ifdef FEAT_MOUSE
11144 if (n == K_LEFTMOUSE
11145 || n == K_LEFTMOUSE_NM
11146 || n == K_LEFTDRAG
11147 || n == K_LEFTRELEASE
11148 || n == K_LEFTRELEASE_NM
11149 || n == K_MIDDLEMOUSE
11150 || n == K_MIDDLEDRAG
11151 || n == K_MIDDLERELEASE
11152 || n == K_RIGHTMOUSE
11153 || n == K_RIGHTDRAG
11154 || n == K_RIGHTRELEASE
11155 || n == K_X1MOUSE
11156 || n == K_X1DRAG
11157 || n == K_X1RELEASE
11158 || n == K_X2MOUSE
11159 || n == K_X2DRAG
11160 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011161 || n == K_MOUSELEFT
11162 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011163 || n == K_MOUSEDOWN
11164 || n == K_MOUSEUP)
11165 {
11166 int row = mouse_row;
11167 int col = mouse_col;
11168 win_T *win;
11169 linenr_T lnum;
11170# ifdef FEAT_WINDOWS
11171 win_T *wp;
11172# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011173 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011174
11175 if (row >= 0 && col >= 0)
11176 {
11177 /* Find the window at the mouse coordinates and compute the
11178 * text position. */
11179 win = mouse_find_win(&row, &col);
11180 (void)mouse_comp_pos(win, &row, &col, &lnum);
11181# ifdef FEAT_WINDOWS
11182 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011183 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011184# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011185 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011186 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11187 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11188 }
11189 }
11190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 }
11192}
11193
11194/*
11195 * "getcharmod()" function
11196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011198f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011199 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203}
11204
11205/*
11206 * "getcmdline()" function
11207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209f_getcmdline(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 Moolenaarc70646c2005-01-04 21:52:38 +000011213 rettv->v_type = VAR_STRING;
11214 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215}
11216
11217/*
11218 * "getcmdpos()" function
11219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011222 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226}
11227
11228/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011229 * "getcmdtype()" function
11230 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011231 static void
11232f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011233 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011234 typval_T *rettv;
11235{
11236 rettv->v_type = VAR_STRING;
11237 rettv->vval.v_string = alloc(2);
11238 if (rettv->vval.v_string != NULL)
11239 {
11240 rettv->vval.v_string[0] = get_cmdline_type();
11241 rettv->vval.v_string[1] = NUL;
11242 }
11243}
11244
11245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 * "getcwd()" function
11247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011249f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011250 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011253 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011256 rettv->vval.v_string = NULL;
11257 cwd = alloc(MAXPATHL);
11258 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011260 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11261 {
11262 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011264 if (rettv->vval.v_string != NULL)
11265 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011267 }
11268 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 }
11270}
11271
11272/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011273 * "getfontname()" function
11274 */
11275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011277 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011278 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011279{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 rettv->v_type = VAR_STRING;
11281 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011282#ifdef FEAT_GUI
11283 if (gui.in_use)
11284 {
11285 GuiFont font;
11286 char_u *name = NULL;
11287
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011288 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011289 {
11290 /* Get the "Normal" font. Either the name saved by
11291 * hl_set_font_name() or from the font ID. */
11292 font = gui.norm_font;
11293 name = hl_get_font_name();
11294 }
11295 else
11296 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011298 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11299 return;
11300 font = gui_mch_get_font(name, FALSE);
11301 if (font == NOFONT)
11302 return; /* Invalid font name, return empty string. */
11303 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011304 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011305 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011306 gui_mch_free_font(font);
11307 }
11308#endif
11309}
11310
11311/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011312 * "getfperm({fname})" function
11313 */
11314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011315f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011316 typval_T *argvars;
11317 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011318{
11319 char_u *fname;
11320 struct stat st;
11321 char_u *perm = NULL;
11322 char_u flags[] = "rwx";
11323 int i;
11324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011325 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011327 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011328 if (mch_stat((char *)fname, &st) >= 0)
11329 {
11330 perm = vim_strsave((char_u *)"---------");
11331 if (perm != NULL)
11332 {
11333 for (i = 0; i < 9; i++)
11334 {
11335 if (st.st_mode & (1 << (8 - i)))
11336 perm[i] = flags[i % 3];
11337 }
11338 }
11339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011341}
11342
11343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 * "getfsize({fname})" function
11345 */
11346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011348 typval_T *argvars;
11349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350{
11351 char_u *fname;
11352 struct stat st;
11353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357
11358 if (mch_stat((char *)fname, &st) >= 0)
11359 {
11360 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011365
11366 /* non-perfect check for overflow */
11367 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11368 rettv->vval.v_number = -2;
11369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 }
11371 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373}
11374
11375/*
11376 * "getftime({fname})" function
11377 */
11378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 typval_T *argvars;
11381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382{
11383 char_u *fname;
11384 struct stat st;
11385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387
11388 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011391 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392}
11393
11394/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011395 * "getftype({fname})" function
11396 */
11397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011399 typval_T *argvars;
11400 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011401{
11402 char_u *fname;
11403 struct stat st;
11404 char_u *type = NULL;
11405 char *t;
11406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011410 if (mch_lstat((char *)fname, &st) >= 0)
11411 {
11412#ifdef S_ISREG
11413 if (S_ISREG(st.st_mode))
11414 t = "file";
11415 else if (S_ISDIR(st.st_mode))
11416 t = "dir";
11417# ifdef S_ISLNK
11418 else if (S_ISLNK(st.st_mode))
11419 t = "link";
11420# endif
11421# ifdef S_ISBLK
11422 else if (S_ISBLK(st.st_mode))
11423 t = "bdev";
11424# endif
11425# ifdef S_ISCHR
11426 else if (S_ISCHR(st.st_mode))
11427 t = "cdev";
11428# endif
11429# ifdef S_ISFIFO
11430 else if (S_ISFIFO(st.st_mode))
11431 t = "fifo";
11432# endif
11433# ifdef S_ISSOCK
11434 else if (S_ISSOCK(st.st_mode))
11435 t = "fifo";
11436# endif
11437 else
11438 t = "other";
11439#else
11440# ifdef S_IFMT
11441 switch (st.st_mode & S_IFMT)
11442 {
11443 case S_IFREG: t = "file"; break;
11444 case S_IFDIR: t = "dir"; break;
11445# ifdef S_IFLNK
11446 case S_IFLNK: t = "link"; break;
11447# endif
11448# ifdef S_IFBLK
11449 case S_IFBLK: t = "bdev"; break;
11450# endif
11451# ifdef S_IFCHR
11452 case S_IFCHR: t = "cdev"; break;
11453# endif
11454# ifdef S_IFIFO
11455 case S_IFIFO: t = "fifo"; break;
11456# endif
11457# ifdef S_IFSOCK
11458 case S_IFSOCK: t = "socket"; break;
11459# endif
11460 default: t = "other";
11461 }
11462# else
11463 if (mch_isdir(fname))
11464 t = "dir";
11465 else
11466 t = "file";
11467# endif
11468#endif
11469 type = vim_strsave((char_u *)t);
11470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011472}
11473
11474/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011475 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011476 */
11477 static void
11478f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011479 typval_T *argvars;
11480 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011481{
11482 linenr_T lnum;
11483 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011484 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011485
11486 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011487 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011488 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011489 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011490 retlist = FALSE;
11491 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011492 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011493 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011494 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011495 retlist = TRUE;
11496 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011497
Bram Moolenaar342337a2005-07-21 21:11:17 +000011498 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011499}
11500
11501/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011502 * "getmatches()" function
11503 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011504 static void
11505f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011506 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011507 typval_T *rettv;
11508{
11509#ifdef FEAT_SEARCH_EXTRA
11510 dict_T *dict;
11511 matchitem_T *cur = curwin->w_match_head;
11512
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011513 if (rettv_list_alloc(rettv) == OK)
11514 {
11515 while (cur != NULL)
11516 {
11517 dict = dict_alloc();
11518 if (dict == NULL)
11519 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011520 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11521 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11522 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11523 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11524 list_append_dict(rettv->vval.v_list, dict);
11525 cur = cur->next;
11526 }
11527 }
11528#endif
11529}
11530
11531/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011532 * "getpid()" function
11533 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011534 static void
11535f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011536 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011537 typval_T *rettv;
11538{
11539 rettv->vval.v_number = mch_get_pid();
11540}
11541
11542/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011543 * "getpos(string)" function
11544 */
11545 static void
11546f_getpos(argvars, rettv)
11547 typval_T *argvars;
11548 typval_T *rettv;
11549{
11550 pos_T *fp;
11551 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011552 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011553
11554 if (rettv_list_alloc(rettv) == OK)
11555 {
11556 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011557 fp = var2fpos(&argvars[0], TRUE, &fnum);
11558 if (fnum != -1)
11559 list_append_number(l, (varnumber_T)fnum);
11560 else
11561 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011562 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11563 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011564 list_append_number(l, (fp != NULL)
11565 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011566 : (varnumber_T)0);
11567 list_append_number(l,
11568#ifdef FEAT_VIRTUALEDIT
11569 (fp != NULL) ? (varnumber_T)fp->coladd :
11570#endif
11571 (varnumber_T)0);
11572 }
11573 else
11574 rettv->vval.v_number = FALSE;
11575}
11576
11577/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011578 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011579 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011580 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011581f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011582 typval_T *argvars UNUSED;
11583 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011584{
11585#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011586 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011587#endif
11588
Bram Moolenaar2641f772005-03-25 21:58:17 +000011589#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011590 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011591 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011592 wp = NULL;
11593 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11594 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011595 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011596 if (wp == NULL)
11597 return;
11598 }
11599
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011600 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011601 }
11602#endif
11603}
11604
11605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 * "getreg()" function
11607 */
11608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011610 typval_T *argvars;
11611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612{
11613 char_u *strregname;
11614 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011615 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011616 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011618 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011620 strregname = get_tv_string_chk(&argvars[0]);
11621 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011622 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011623 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011626 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 regname = (strregname == NULL ? '"' : *strregname);
11628 if (regname == 0)
11629 regname = '"';
11630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011631 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011632 rettv->vval.v_string = error ? NULL :
11633 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634}
11635
11636/*
11637 * "getregtype()" function
11638 */
11639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011640f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011641 typval_T *argvars;
11642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643{
11644 char_u *strregname;
11645 int regname;
11646 char_u buf[NUMBUFLEN + 2];
11647 long reglen = 0;
11648
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011649 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011650 {
11651 strregname = get_tv_string_chk(&argvars[0]);
11652 if (strregname == NULL) /* type error; errmsg already given */
11653 {
11654 rettv->v_type = VAR_STRING;
11655 rettv->vval.v_string = NULL;
11656 return;
11657 }
11658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 else
11660 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011661 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662
11663 regname = (strregname == NULL ? '"' : *strregname);
11664 if (regname == 0)
11665 regname = '"';
11666
11667 buf[0] = NUL;
11668 buf[1] = NUL;
11669 switch (get_reg_type(regname, &reglen))
11670 {
11671 case MLINE: buf[0] = 'V'; break;
11672 case MCHAR: buf[0] = 'v'; break;
11673#ifdef FEAT_VISUAL
11674 case MBLOCK:
11675 buf[0] = Ctrl_V;
11676 sprintf((char *)buf + 1, "%ld", reglen + 1);
11677 break;
11678#endif
11679 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011680 rettv->v_type = VAR_STRING;
11681 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682}
11683
11684/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011685 * "gettabvar()" function
11686 */
11687 static void
11688f_gettabvar(argvars, rettv)
11689 typval_T *argvars;
11690 typval_T *rettv;
11691{
11692 tabpage_T *tp;
11693 dictitem_T *v;
11694 char_u *varname;
11695
11696 rettv->v_type = VAR_STRING;
11697 rettv->vval.v_string = NULL;
11698
11699 varname = get_tv_string_chk(&argvars[1]);
11700 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11701 if (tp != NULL && varname != NULL)
11702 {
11703 /* look up the variable */
11704 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11705 if (v != NULL)
11706 copy_tv(&v->di_tv, rettv);
11707 }
11708}
11709
11710/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011711 * "gettabwinvar()" function
11712 */
11713 static void
11714f_gettabwinvar(argvars, rettv)
11715 typval_T *argvars;
11716 typval_T *rettv;
11717{
11718 getwinvar(argvars, rettv, 1);
11719}
11720
11721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 * "getwinposx()" function
11723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011725f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011726 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730#ifdef FEAT_GUI
11731 if (gui.in_use)
11732 {
11733 int x, y;
11734
11735 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011736 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 }
11738#endif
11739}
11740
11741/*
11742 * "getwinposy()" function
11743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011745f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011746 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011749 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750#ifdef FEAT_GUI
11751 if (gui.in_use)
11752 {
11753 int x, y;
11754
11755 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011756 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 }
11758#endif
11759}
11760
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011761/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011762 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011763 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011764 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011765find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011766 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011767 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011768{
11769#ifdef FEAT_WINDOWS
11770 win_T *wp;
11771#endif
11772 int nr;
11773
11774 nr = get_tv_number_chk(vp, NULL);
11775
11776#ifdef FEAT_WINDOWS
11777 if (nr < 0)
11778 return NULL;
11779 if (nr == 0)
11780 return curwin;
11781
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011782 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11783 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011784 if (--nr <= 0)
11785 break;
11786 return wp;
11787#else
11788 if (nr == 0 || nr == 1)
11789 return curwin;
11790 return NULL;
11791#endif
11792}
11793
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794/*
11795 * "getwinvar()" function
11796 */
11797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011798f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011799 typval_T *argvars;
11800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011802 getwinvar(argvars, rettv, 0);
11803}
11804
11805/*
11806 * getwinvar() and gettabwinvar()
11807 */
11808 static void
11809getwinvar(argvars, rettv, off)
11810 typval_T *argvars;
11811 typval_T *rettv;
11812 int off; /* 1 for gettabwinvar() */
11813{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 win_T *win, *oldcurwin;
11815 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011816 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011817 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011819#ifdef FEAT_WINDOWS
11820 if (off == 1)
11821 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11822 else
11823 tp = curtab;
11824#endif
11825 win = find_win_by_nr(&argvars[off], tp);
11826 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011827 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 rettv->v_type = VAR_STRING;
11830 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831
11832 if (win != NULL && varname != NULL)
11833 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011834 /* Set curwin to be our win, temporarily. Also set curbuf, so
11835 * that we can get buffer-local options. */
11836 oldcurwin = curwin;
11837 curwin = win;
11838 curbuf = win->w_buffer;
11839
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011841 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 else
11843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011844 if (*varname == NUL)
11845 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11846 * scope prefix before the NUL byte is required by
11847 * find_var_in_ht(). */
11848 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011850 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011852 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011854
11855 /* restore previous notion of curwin */
11856 curwin = oldcurwin;
11857 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 }
11859
11860 --emsg_off;
11861}
11862
11863/*
11864 * "glob()" function
11865 */
11866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011868 typval_T *argvars;
11869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011871 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011873 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011875 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011876 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011878 if (argvars[1].v_type != VAR_UNKNOWN)
11879 {
11880 if (get_tv_number_chk(&argvars[1], &error))
11881 options |= WILD_KEEP_ALL;
11882 if (argvars[2].v_type != VAR_UNKNOWN
11883 && get_tv_number_chk(&argvars[2], &error))
11884 {
11885 rettv->v_type = VAR_LIST;
11886 rettv->vval.v_list = NULL;
11887 }
11888 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011889 if (!error)
11890 {
11891 ExpandInit(&xpc);
11892 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011893 if (p_wic)
11894 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011895 if (rettv->v_type == VAR_STRING)
11896 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011897 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011898 else if (rettv_list_alloc(rettv) != FAIL)
11899 {
11900 int i;
11901
11902 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11903 NULL, options, WILD_ALL_KEEP);
11904 for (i = 0; i < xpc.xp_numfiles; i++)
11905 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11906
11907 ExpandCleanup(&xpc);
11908 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011909 }
11910 else
11911 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912}
11913
11914/*
11915 * "globpath()" function
11916 */
11917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011918f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011919 typval_T *argvars;
11920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011922 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011924 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011925 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011927 /* When the optional second argument is non-zero, don't remove matches
11928 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11929 if (argvars[2].v_type != VAR_UNKNOWN
11930 && get_tv_number_chk(&argvars[2], &error))
11931 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011932 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011933 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011934 rettv->vval.v_string = NULL;
11935 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011936 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11937 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938}
11939
11940/*
11941 * "has()" function
11942 */
11943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011944f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011945 typval_T *argvars;
11946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947{
11948 int i;
11949 char_u *name;
11950 int n = FALSE;
11951 static char *(has_list[]) =
11952 {
11953#ifdef AMIGA
11954 "amiga",
11955# ifdef FEAT_ARP
11956 "arp",
11957# endif
11958#endif
11959#ifdef __BEOS__
11960 "beos",
11961#endif
11962#ifdef MSDOS
11963# ifdef DJGPP
11964 "dos32",
11965# else
11966 "dos16",
11967# endif
11968#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011969#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970 "mac",
11971#endif
11972#if defined(MACOS_X_UNIX)
11973 "macunix",
11974#endif
11975#ifdef OS2
11976 "os2",
11977#endif
11978#ifdef __QNX__
11979 "qnx",
11980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981#ifdef UNIX
11982 "unix",
11983#endif
11984#ifdef VMS
11985 "vms",
11986#endif
11987#ifdef WIN16
11988 "win16",
11989#endif
11990#ifdef WIN32
11991 "win32",
11992#endif
11993#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11994 "win32unix",
11995#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011996#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 "win64",
11998#endif
11999#ifdef EBCDIC
12000 "ebcdic",
12001#endif
12002#ifndef CASE_INSENSITIVE_FILENAME
12003 "fname_case",
12004#endif
12005#ifdef FEAT_ARABIC
12006 "arabic",
12007#endif
12008#ifdef FEAT_AUTOCMD
12009 "autocmd",
12010#endif
12011#ifdef FEAT_BEVAL
12012 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012013# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12014 "balloon_multiline",
12015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016#endif
12017#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12018 "builtin_terms",
12019# ifdef ALL_BUILTIN_TCAPS
12020 "all_builtin_terms",
12021# endif
12022#endif
12023#ifdef FEAT_BYTEOFF
12024 "byte_offset",
12025#endif
12026#ifdef FEAT_CINDENT
12027 "cindent",
12028#endif
12029#ifdef FEAT_CLIENTSERVER
12030 "clientserver",
12031#endif
12032#ifdef FEAT_CLIPBOARD
12033 "clipboard",
12034#endif
12035#ifdef FEAT_CMDL_COMPL
12036 "cmdline_compl",
12037#endif
12038#ifdef FEAT_CMDHIST
12039 "cmdline_hist",
12040#endif
12041#ifdef FEAT_COMMENTS
12042 "comments",
12043#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012044#ifdef FEAT_CONCEAL
12045 "conceal",
12046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047#ifdef FEAT_CRYPT
12048 "cryptv",
12049#endif
12050#ifdef FEAT_CSCOPE
12051 "cscope",
12052#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012053#ifdef FEAT_CURSORBIND
12054 "cursorbind",
12055#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012056#ifdef CURSOR_SHAPE
12057 "cursorshape",
12058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059#ifdef DEBUG
12060 "debug",
12061#endif
12062#ifdef FEAT_CON_DIALOG
12063 "dialog_con",
12064#endif
12065#ifdef FEAT_GUI_DIALOG
12066 "dialog_gui",
12067#endif
12068#ifdef FEAT_DIFF
12069 "diff",
12070#endif
12071#ifdef FEAT_DIGRAPHS
12072 "digraphs",
12073#endif
12074#ifdef FEAT_DND
12075 "dnd",
12076#endif
12077#ifdef FEAT_EMACS_TAGS
12078 "emacs_tags",
12079#endif
12080 "eval", /* always present, of course! */
12081#ifdef FEAT_EX_EXTRA
12082 "ex_extra",
12083#endif
12084#ifdef FEAT_SEARCH_EXTRA
12085 "extra_search",
12086#endif
12087#ifdef FEAT_FKMAP
12088 "farsi",
12089#endif
12090#ifdef FEAT_SEARCHPATH
12091 "file_in_path",
12092#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012093#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012094 "filterpipe",
12095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096#ifdef FEAT_FIND_ID
12097 "find_in_path",
12098#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012099#ifdef FEAT_FLOAT
12100 "float",
12101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102#ifdef FEAT_FOLDING
12103 "folding",
12104#endif
12105#ifdef FEAT_FOOTER
12106 "footer",
12107#endif
12108#if !defined(USE_SYSTEM) && defined(UNIX)
12109 "fork",
12110#endif
12111#ifdef FEAT_GETTEXT
12112 "gettext",
12113#endif
12114#ifdef FEAT_GUI
12115 "gui",
12116#endif
12117#ifdef FEAT_GUI_ATHENA
12118# ifdef FEAT_GUI_NEXTAW
12119 "gui_neXtaw",
12120# else
12121 "gui_athena",
12122# endif
12123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124#ifdef FEAT_GUI_GTK
12125 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012128#ifdef FEAT_GUI_GNOME
12129 "gui_gnome",
12130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131#ifdef FEAT_GUI_MAC
12132 "gui_mac",
12133#endif
12134#ifdef FEAT_GUI_MOTIF
12135 "gui_motif",
12136#endif
12137#ifdef FEAT_GUI_PHOTON
12138 "gui_photon",
12139#endif
12140#ifdef FEAT_GUI_W16
12141 "gui_win16",
12142#endif
12143#ifdef FEAT_GUI_W32
12144 "gui_win32",
12145#endif
12146#ifdef FEAT_HANGULIN
12147 "hangul_input",
12148#endif
12149#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12150 "iconv",
12151#endif
12152#ifdef FEAT_INS_EXPAND
12153 "insert_expand",
12154#endif
12155#ifdef FEAT_JUMPLIST
12156 "jumplist",
12157#endif
12158#ifdef FEAT_KEYMAP
12159 "keymap",
12160#endif
12161#ifdef FEAT_LANGMAP
12162 "langmap",
12163#endif
12164#ifdef FEAT_LIBCALL
12165 "libcall",
12166#endif
12167#ifdef FEAT_LINEBREAK
12168 "linebreak",
12169#endif
12170#ifdef FEAT_LISP
12171 "lispindent",
12172#endif
12173#ifdef FEAT_LISTCMDS
12174 "listcmds",
12175#endif
12176#ifdef FEAT_LOCALMAP
12177 "localmap",
12178#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012179#ifdef FEAT_LUA
12180# ifndef DYNAMIC_LUA
12181 "lua",
12182# endif
12183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184#ifdef FEAT_MENU
12185 "menu",
12186#endif
12187#ifdef FEAT_SESSION
12188 "mksession",
12189#endif
12190#ifdef FEAT_MODIFY_FNAME
12191 "modify_fname",
12192#endif
12193#ifdef FEAT_MOUSE
12194 "mouse",
12195#endif
12196#ifdef FEAT_MOUSESHAPE
12197 "mouseshape",
12198#endif
12199#if defined(UNIX) || defined(VMS)
12200# ifdef FEAT_MOUSE_DEC
12201 "mouse_dec",
12202# endif
12203# ifdef FEAT_MOUSE_GPM
12204 "mouse_gpm",
12205# endif
12206# ifdef FEAT_MOUSE_JSB
12207 "mouse_jsbterm",
12208# endif
12209# ifdef FEAT_MOUSE_NET
12210 "mouse_netterm",
12211# endif
12212# ifdef FEAT_MOUSE_PTERM
12213 "mouse_pterm",
12214# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012215# ifdef FEAT_SYSMOUSE
12216 "mouse_sysmouse",
12217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218# ifdef FEAT_MOUSE_XTERM
12219 "mouse_xterm",
12220# endif
12221#endif
12222#ifdef FEAT_MBYTE
12223 "multi_byte",
12224#endif
12225#ifdef FEAT_MBYTE_IME
12226 "multi_byte_ime",
12227#endif
12228#ifdef FEAT_MULTI_LANG
12229 "multi_lang",
12230#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012231#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012232#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012233 "mzscheme",
12234#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236#ifdef FEAT_OLE
12237 "ole",
12238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239#ifdef FEAT_PATH_EXTRA
12240 "path_extra",
12241#endif
12242#ifdef FEAT_PERL
12243#ifndef DYNAMIC_PERL
12244 "perl",
12245#endif
12246#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012247#ifdef FEAT_PERSISTENT_UNDO
12248 "persistent_undo",
12249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250#ifdef FEAT_PYTHON
12251#ifndef DYNAMIC_PYTHON
12252 "python",
12253#endif
12254#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012255#ifdef FEAT_PYTHON3
12256#ifndef DYNAMIC_PYTHON3
12257 "python3",
12258#endif
12259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260#ifdef FEAT_POSTSCRIPT
12261 "postscript",
12262#endif
12263#ifdef FEAT_PRINTER
12264 "printer",
12265#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012266#ifdef FEAT_PROFILE
12267 "profile",
12268#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012269#ifdef FEAT_RELTIME
12270 "reltime",
12271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272#ifdef FEAT_QUICKFIX
12273 "quickfix",
12274#endif
12275#ifdef FEAT_RIGHTLEFT
12276 "rightleft",
12277#endif
12278#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12279 "ruby",
12280#endif
12281#ifdef FEAT_SCROLLBIND
12282 "scrollbind",
12283#endif
12284#ifdef FEAT_CMDL_INFO
12285 "showcmd",
12286 "cmdline_info",
12287#endif
12288#ifdef FEAT_SIGNS
12289 "signs",
12290#endif
12291#ifdef FEAT_SMARTINDENT
12292 "smartindent",
12293#endif
12294#ifdef FEAT_SNIFF
12295 "sniff",
12296#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012297#ifdef STARTUPTIME
12298 "startuptime",
12299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300#ifdef FEAT_STL_OPT
12301 "statusline",
12302#endif
12303#ifdef FEAT_SUN_WORKSHOP
12304 "sun_workshop",
12305#endif
12306#ifdef FEAT_NETBEANS_INTG
12307 "netbeans_intg",
12308#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012309#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012310 "spell",
12311#endif
12312#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313 "syntax",
12314#endif
12315#if defined(USE_SYSTEM) || !defined(UNIX)
12316 "system",
12317#endif
12318#ifdef FEAT_TAG_BINS
12319 "tag_binary",
12320#endif
12321#ifdef FEAT_TAG_OLDSTATIC
12322 "tag_old_static",
12323#endif
12324#ifdef FEAT_TAG_ANYWHITE
12325 "tag_any_white",
12326#endif
12327#ifdef FEAT_TCL
12328# ifndef DYNAMIC_TCL
12329 "tcl",
12330# endif
12331#endif
12332#ifdef TERMINFO
12333 "terminfo",
12334#endif
12335#ifdef FEAT_TERMRESPONSE
12336 "termresponse",
12337#endif
12338#ifdef FEAT_TEXTOBJ
12339 "textobjects",
12340#endif
12341#ifdef HAVE_TGETENT
12342 "tgetent",
12343#endif
12344#ifdef FEAT_TITLE
12345 "title",
12346#endif
12347#ifdef FEAT_TOOLBAR
12348 "toolbar",
12349#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012350#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12351 "unnamedplus",
12352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353#ifdef FEAT_USR_CMDS
12354 "user-commands", /* was accidentally included in 5.4 */
12355 "user_commands",
12356#endif
12357#ifdef FEAT_VIMINFO
12358 "viminfo",
12359#endif
12360#ifdef FEAT_VERTSPLIT
12361 "vertsplit",
12362#endif
12363#ifdef FEAT_VIRTUALEDIT
12364 "virtualedit",
12365#endif
12366#ifdef FEAT_VISUAL
12367 "visual",
12368#endif
12369#ifdef FEAT_VISUALEXTRA
12370 "visualextra",
12371#endif
12372#ifdef FEAT_VREPLACE
12373 "vreplace",
12374#endif
12375#ifdef FEAT_WILDIGN
12376 "wildignore",
12377#endif
12378#ifdef FEAT_WILDMENU
12379 "wildmenu",
12380#endif
12381#ifdef FEAT_WINDOWS
12382 "windows",
12383#endif
12384#ifdef FEAT_WAK
12385 "winaltkeys",
12386#endif
12387#ifdef FEAT_WRITEBACKUP
12388 "writebackup",
12389#endif
12390#ifdef FEAT_XIM
12391 "xim",
12392#endif
12393#ifdef FEAT_XFONTSET
12394 "xfontset",
12395#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012396#ifdef FEAT_XPM_W32
12397 "xpm_w32",
12398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399#ifdef USE_XSMP
12400 "xsmp",
12401#endif
12402#ifdef USE_XSMP_INTERACT
12403 "xsmp_interact",
12404#endif
12405#ifdef FEAT_XCLIPBOARD
12406 "xterm_clipboard",
12407#endif
12408#ifdef FEAT_XTERM_SAVE
12409 "xterm_save",
12410#endif
12411#if defined(UNIX) && defined(FEAT_X11)
12412 "X11",
12413#endif
12414 NULL
12415 };
12416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012417 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 for (i = 0; has_list[i] != NULL; ++i)
12419 if (STRICMP(name, has_list[i]) == 0)
12420 {
12421 n = TRUE;
12422 break;
12423 }
12424
12425 if (n == FALSE)
12426 {
12427 if (STRNICMP(name, "patch", 5) == 0)
12428 n = has_patch(atoi((char *)name + 5));
12429 else if (STRICMP(name, "vim_starting") == 0)
12430 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012431#ifdef FEAT_MBYTE
12432 else if (STRICMP(name, "multi_byte_encoding") == 0)
12433 n = has_mbyte;
12434#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012435#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12436 else if (STRICMP(name, "balloon_multiline") == 0)
12437 n = multiline_balloon_available();
12438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439#ifdef DYNAMIC_TCL
12440 else if (STRICMP(name, "tcl") == 0)
12441 n = tcl_enabled(FALSE);
12442#endif
12443#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12444 else if (STRICMP(name, "iconv") == 0)
12445 n = iconv_enabled(FALSE);
12446#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012447#ifdef DYNAMIC_LUA
12448 else if (STRICMP(name, "lua") == 0)
12449 n = lua_enabled(FALSE);
12450#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012451#ifdef DYNAMIC_MZSCHEME
12452 else if (STRICMP(name, "mzscheme") == 0)
12453 n = mzscheme_enabled(FALSE);
12454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455#ifdef DYNAMIC_RUBY
12456 else if (STRICMP(name, "ruby") == 0)
12457 n = ruby_enabled(FALSE);
12458#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012459#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460#ifdef DYNAMIC_PYTHON
12461 else if (STRICMP(name, "python") == 0)
12462 n = python_enabled(FALSE);
12463#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012464#endif
12465#ifdef FEAT_PYTHON3
12466#ifdef DYNAMIC_PYTHON3
12467 else if (STRICMP(name, "python3") == 0)
12468 n = python3_enabled(FALSE);
12469#endif
12470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471#ifdef DYNAMIC_PERL
12472 else if (STRICMP(name, "perl") == 0)
12473 n = perl_enabled(FALSE);
12474#endif
12475#ifdef FEAT_GUI
12476 else if (STRICMP(name, "gui_running") == 0)
12477 n = (gui.in_use || gui.starting);
12478# ifdef FEAT_GUI_W32
12479 else if (STRICMP(name, "gui_win32s") == 0)
12480 n = gui_is_win32s();
12481# endif
12482# ifdef FEAT_BROWSE
12483 else if (STRICMP(name, "browse") == 0)
12484 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12485# endif
12486#endif
12487#ifdef FEAT_SYN_HL
12488 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012489 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490#endif
12491#if defined(WIN3264)
12492 else if (STRICMP(name, "win95") == 0)
12493 n = mch_windows95();
12494#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012495#ifdef FEAT_NETBEANS_INTG
12496 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012497 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499 }
12500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502}
12503
12504/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012505 * "has_key()" function
12506 */
12507 static void
12508f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012509 typval_T *argvars;
12510 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012511{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012512 if (argvars[0].v_type != VAR_DICT)
12513 {
12514 EMSG(_(e_dictreq));
12515 return;
12516 }
12517 if (argvars[0].vval.v_dict == NULL)
12518 return;
12519
12520 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012521 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012522}
12523
12524/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012525 * "haslocaldir()" function
12526 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012527 static void
12528f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012529 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012530 typval_T *rettv;
12531{
12532 rettv->vval.v_number = (curwin->w_localdir != NULL);
12533}
12534
12535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536 * "hasmapto()" function
12537 */
12538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012539f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012540 typval_T *argvars;
12541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542{
12543 char_u *name;
12544 char_u *mode;
12545 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012546 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012548 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012549 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550 mode = (char_u *)"nvo";
12551 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012553 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012554 if (argvars[2].v_type != VAR_UNKNOWN)
12555 abbr = get_tv_number(&argvars[2]);
12556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557
Bram Moolenaar2c932302006-03-18 21:42:09 +000012558 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012559 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012561 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562}
12563
12564/*
12565 * "histadd()" function
12566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012568f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012569 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571{
12572#ifdef FEAT_CMDHIST
12573 int histype;
12574 char_u *str;
12575 char_u buf[NUMBUFLEN];
12576#endif
12577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012578 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579 if (check_restricted() || check_secure())
12580 return;
12581#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012582 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12583 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 if (histype >= 0)
12585 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012586 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587 if (*str != NUL)
12588 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012589 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592 return;
12593 }
12594 }
12595#endif
12596}
12597
12598/*
12599 * "histdel()" function
12600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012602f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012603 typval_T *argvars UNUSED;
12604 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605{
12606#ifdef FEAT_CMDHIST
12607 int n;
12608 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012609 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012611 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12612 if (str == NULL)
12613 n = 0;
12614 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012616 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012617 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012619 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012620 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 else
12622 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012623 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012624 get_tv_string_buf(&argvars[1], buf));
12625 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626#endif
12627}
12628
12629/*
12630 * "histget()" function
12631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012633f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012634 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636{
12637#ifdef FEAT_CMDHIST
12638 int type;
12639 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012640 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012642 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12643 if (str == NULL)
12644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012646 {
12647 type = get_histtype(str);
12648 if (argvars[1].v_type == VAR_UNKNOWN)
12649 idx = get_history_idx(type);
12650 else
12651 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12652 /* -1 on type error */
12653 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012658 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659}
12660
12661/*
12662 * "histnr()" function
12663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012665f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012666 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668{
12669 int i;
12670
12671#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672 char_u *history = get_tv_string_chk(&argvars[0]);
12673
12674 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675 if (i >= HIST_CMD && i < HIST_COUNT)
12676 i = get_history_idx(i);
12677 else
12678#endif
12679 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681}
12682
12683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684 * "highlightID(name)" function
12685 */
12686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012687f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012688 typval_T *argvars;
12689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012691 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692}
12693
12694/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012695 * "highlight_exists()" function
12696 */
12697 static void
12698f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012699 typval_T *argvars;
12700 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012701{
12702 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12703}
12704
12705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706 * "hostname()" function
12707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012709f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012710 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712{
12713 char_u hostname[256];
12714
12715 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012716 rettv->v_type = VAR_STRING;
12717 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718}
12719
12720/*
12721 * iconv() function
12722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012724f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012725 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727{
12728#ifdef FEAT_MBYTE
12729 char_u buf1[NUMBUFLEN];
12730 char_u buf2[NUMBUFLEN];
12731 char_u *from, *to, *str;
12732 vimconv_T vimconv;
12733#endif
12734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 rettv->v_type = VAR_STRING;
12736 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737
12738#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739 str = get_tv_string(&argvars[0]);
12740 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12741 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742 vimconv.vc_type = CONV_NONE;
12743 convert_setup(&vimconv, from, to);
12744
12745 /* If the encodings are equal, no conversion needed. */
12746 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012747 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750
12751 convert_setup(&vimconv, NULL, NULL);
12752 vim_free(from);
12753 vim_free(to);
12754#endif
12755}
12756
12757/*
12758 * "indent()" function
12759 */
12760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012762 typval_T *argvars;
12763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764{
12765 linenr_T lnum;
12766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012767 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772}
12773
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012774/*
12775 * "index()" function
12776 */
12777 static void
12778f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012779 typval_T *argvars;
12780 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012781{
Bram Moolenaar33570922005-01-25 22:26:29 +000012782 list_T *l;
12783 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012784 long idx = 0;
12785 int ic = FALSE;
12786
12787 rettv->vval.v_number = -1;
12788 if (argvars[0].v_type != VAR_LIST)
12789 {
12790 EMSG(_(e_listreq));
12791 return;
12792 }
12793 l = argvars[0].vval.v_list;
12794 if (l != NULL)
12795 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012796 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012797 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012799 int error = FALSE;
12800
Bram Moolenaar758711c2005-02-02 23:11:38 +000012801 /* Start at specified item. Use the cached index that list_find()
12802 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012803 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012804 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012805 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012806 ic = get_tv_number_chk(&argvars[3], &error);
12807 if (error)
12808 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012809 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012810
Bram Moolenaar758711c2005-02-02 23:11:38 +000012811 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012812 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012813 {
12814 rettv->vval.v_number = idx;
12815 break;
12816 }
12817 }
12818}
12819
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820static int inputsecret_flag = 0;
12821
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012822static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12823
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012825 * This function is used by f_input() and f_inputdialog() functions. The third
12826 * argument to f_input() specifies the type of completion to use at the
12827 * prompt. The third argument to f_inputdialog() specifies the value to return
12828 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829 */
12830 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012831get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012832 typval_T *argvars;
12833 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012834 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012836 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 char_u *p = NULL;
12838 int c;
12839 char_u buf[NUMBUFLEN];
12840 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012841 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012842 int xp_type = EXPAND_NOTHING;
12843 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012846 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847
12848#ifdef NO_CONSOLE_INPUT
12849 /* While starting up, there is no place to enter text. */
12850 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852#endif
12853
12854 cmd_silent = FALSE; /* Want to see the prompt. */
12855 if (prompt != NULL)
12856 {
12857 /* Only the part of the message after the last NL is considered as
12858 * prompt for the command line */
12859 p = vim_strrchr(prompt, '\n');
12860 if (p == NULL)
12861 p = prompt;
12862 else
12863 {
12864 ++p;
12865 c = *p;
12866 *p = NUL;
12867 msg_start();
12868 msg_clr_eos();
12869 msg_puts_attr(prompt, echo_attr);
12870 msg_didout = FALSE;
12871 msg_starthere();
12872 *p = c;
12873 }
12874 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012876 if (argvars[1].v_type != VAR_UNKNOWN)
12877 {
12878 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12879 if (defstr != NULL)
12880 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012882 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012883 {
12884 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012885 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012886 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012887
Bram Moolenaar4463f292005-09-25 22:20:24 +000012888 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012889
Bram Moolenaar4463f292005-09-25 22:20:24 +000012890 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12891 if (xp_name == NULL)
12892 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012893
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012894 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012895
Bram Moolenaar4463f292005-09-25 22:20:24 +000012896 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12897 &xp_arg) == FAIL)
12898 return;
12899 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012900 }
12901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012902 if (defstr != NULL)
12903 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012904 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12905 xp_type, xp_arg);
12906
12907 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012909 /* since the user typed this, no need to wait for return */
12910 need_wait_return = FALSE;
12911 msg_didout = FALSE;
12912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 cmd_silent = cmd_silent_save;
12914}
12915
12916/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012917 * "input()" function
12918 * Also handles inputsecret() when inputsecret is set.
12919 */
12920 static void
12921f_input(argvars, rettv)
12922 typval_T *argvars;
12923 typval_T *rettv;
12924{
12925 get_user_input(argvars, rettv, FALSE);
12926}
12927
12928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 * "inputdialog()" function
12930 */
12931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012932f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012933 typval_T *argvars;
12934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935{
12936#if defined(FEAT_GUI_TEXTDIALOG)
12937 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12938 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12939 {
12940 char_u *message;
12941 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012942 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012944 message = get_tv_string_chk(&argvars[0]);
12945 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012946 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012947 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 else
12949 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012950 if (message != NULL && defstr != NULL
12951 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012952 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012953 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954 else
12955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012956 if (message != NULL && defstr != NULL
12957 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012958 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012959 rettv->vval.v_string = vim_strsave(
12960 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012962 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012964 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 }
12966 else
12967#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012968 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969}
12970
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012971/*
12972 * "inputlist()" function
12973 */
12974 static void
12975f_inputlist(argvars, rettv)
12976 typval_T *argvars;
12977 typval_T *rettv;
12978{
12979 listitem_T *li;
12980 int selected;
12981 int mouse_used;
12982
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012983#ifdef NO_CONSOLE_INPUT
12984 /* While starting up, there is no place to enter text. */
12985 if (no_console_input())
12986 return;
12987#endif
12988 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12989 {
12990 EMSG2(_(e_listarg), "inputlist()");
12991 return;
12992 }
12993
12994 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012995 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012996 lines_left = Rows; /* avoid more prompt */
12997 msg_scroll = TRUE;
12998 msg_clr_eos();
12999
13000 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13001 {
13002 msg_puts(get_tv_string(&li->li_tv));
13003 msg_putchar('\n');
13004 }
13005
13006 /* Ask for choice. */
13007 selected = prompt_for_number(&mouse_used);
13008 if (mouse_used)
13009 selected -= lines_left;
13010
13011 rettv->vval.v_number = selected;
13012}
13013
13014
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13016
13017/*
13018 * "inputrestore()" function
13019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013021f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013022 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024{
13025 if (ga_userinput.ga_len > 0)
13026 {
13027 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13029 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013030 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 }
13032 else if (p_verbose > 1)
13033 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013034 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013035 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036 }
13037}
13038
13039/*
13040 * "inputsave()" function
13041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013044 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013047 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048 if (ga_grow(&ga_userinput, 1) == OK)
13049 {
13050 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13051 + ga_userinput.ga_len);
13052 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013053 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054 }
13055 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057}
13058
13059/*
13060 * "inputsecret()" function
13061 */
13062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013063f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013064 typval_T *argvars;
13065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066{
13067 ++cmdline_star;
13068 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013069 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070 --cmdline_star;
13071 --inputsecret_flag;
13072}
13073
13074/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013075 * "insert()" function
13076 */
13077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013078f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013079 typval_T *argvars;
13080 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013081{
13082 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013083 listitem_T *item;
13084 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013085 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013086
13087 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013088 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013089 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013090 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013091 {
13092 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013093 before = get_tv_number_chk(&argvars[2], &error);
13094 if (error)
13095 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013096
Bram Moolenaar758711c2005-02-02 23:11:38 +000013097 if (before == l->lv_len)
13098 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013099 else
13100 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013101 item = list_find(l, before);
13102 if (item == NULL)
13103 {
13104 EMSGN(_(e_listidx), before);
13105 l = NULL;
13106 }
13107 }
13108 if (l != NULL)
13109 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013110 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013111 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013112 }
13113 }
13114}
13115
13116/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013117 * "invert(expr)" function
13118 */
13119 static void
13120f_invert(argvars, rettv)
13121 typval_T *argvars;
13122 typval_T *rettv;
13123{
13124 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13125}
13126
13127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128 * "isdirectory()" function
13129 */
13130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013131f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013132 typval_T *argvars;
13133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013135 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136}
13137
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013138/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013139 * "islocked()" function
13140 */
13141 static void
13142f_islocked(argvars, rettv)
13143 typval_T *argvars;
13144 typval_T *rettv;
13145{
13146 lval_T lv;
13147 char_u *end;
13148 dictitem_T *di;
13149
13150 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013151 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13152 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013153 if (end != NULL && lv.ll_name != NULL)
13154 {
13155 if (*end != NUL)
13156 EMSG(_(e_trailing));
13157 else
13158 {
13159 if (lv.ll_tv == NULL)
13160 {
13161 if (check_changedtick(lv.ll_name))
13162 rettv->vval.v_number = 1; /* always locked */
13163 else
13164 {
13165 di = find_var(lv.ll_name, NULL);
13166 if (di != NULL)
13167 {
13168 /* Consider a variable locked when:
13169 * 1. the variable itself is locked
13170 * 2. the value of the variable is locked.
13171 * 3. the List or Dict value is locked.
13172 */
13173 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13174 || tv_islocked(&di->di_tv));
13175 }
13176 }
13177 }
13178 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013179 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013180 else if (lv.ll_newkey != NULL)
13181 EMSG2(_(e_dictkey), lv.ll_newkey);
13182 else if (lv.ll_list != NULL)
13183 /* List item. */
13184 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13185 else
13186 /* Dictionary item. */
13187 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13188 }
13189 }
13190
13191 clear_lval(&lv);
13192}
13193
Bram Moolenaar33570922005-01-25 22:26:29 +000013194static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013195
13196/*
13197 * Turn a dict into a list:
13198 * "what" == 0: list of keys
13199 * "what" == 1: list of values
13200 * "what" == 2: list of items
13201 */
13202 static void
13203dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013204 typval_T *argvars;
13205 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013206 int what;
13207{
Bram Moolenaar33570922005-01-25 22:26:29 +000013208 list_T *l2;
13209 dictitem_T *di;
13210 hashitem_T *hi;
13211 listitem_T *li;
13212 listitem_T *li2;
13213 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013214 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013215
Bram Moolenaar8c711452005-01-14 21:53:12 +000013216 if (argvars[0].v_type != VAR_DICT)
13217 {
13218 EMSG(_(e_dictreq));
13219 return;
13220 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013221 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013222 return;
13223
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013224 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013225 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013226
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013227 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013228 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013229 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013230 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013231 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013232 --todo;
13233 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013234
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013235 li = listitem_alloc();
13236 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013237 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013238 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013239
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013240 if (what == 0)
13241 {
13242 /* keys() */
13243 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013244 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013245 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13246 }
13247 else if (what == 1)
13248 {
13249 /* values() */
13250 copy_tv(&di->di_tv, &li->li_tv);
13251 }
13252 else
13253 {
13254 /* items() */
13255 l2 = list_alloc();
13256 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013257 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013258 li->li_tv.vval.v_list = l2;
13259 if (l2 == NULL)
13260 break;
13261 ++l2->lv_refcount;
13262
13263 li2 = listitem_alloc();
13264 if (li2 == NULL)
13265 break;
13266 list_append(l2, li2);
13267 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013268 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013269 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13270
13271 li2 = listitem_alloc();
13272 if (li2 == NULL)
13273 break;
13274 list_append(l2, li2);
13275 copy_tv(&di->di_tv, &li2->li_tv);
13276 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013277 }
13278 }
13279}
13280
13281/*
13282 * "items(dict)" function
13283 */
13284 static void
13285f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013286 typval_T *argvars;
13287 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013288{
13289 dict_list(argvars, rettv, 2);
13290}
13291
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013293 * "join()" function
13294 */
13295 static void
13296f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013297 typval_T *argvars;
13298 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013299{
13300 garray_T ga;
13301 char_u *sep;
13302
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013303 if (argvars[0].v_type != VAR_LIST)
13304 {
13305 EMSG(_(e_listreq));
13306 return;
13307 }
13308 if (argvars[0].vval.v_list == NULL)
13309 return;
13310 if (argvars[1].v_type == VAR_UNKNOWN)
13311 sep = (char_u *)" ";
13312 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013313 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013314
13315 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013316
13317 if (sep != NULL)
13318 {
13319 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013320 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013321 ga_append(&ga, NUL);
13322 rettv->vval.v_string = (char_u *)ga.ga_data;
13323 }
13324 else
13325 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013326}
13327
13328/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013329 * "keys()" function
13330 */
13331 static void
13332f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013333 typval_T *argvars;
13334 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013335{
13336 dict_list(argvars, rettv, 0);
13337}
13338
13339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 * "last_buffer_nr()" function.
13341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013343f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013344 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346{
13347 int n = 0;
13348 buf_T *buf;
13349
13350 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13351 if (n < buf->b_fnum)
13352 n = buf->b_fnum;
13353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013355}
13356
13357/*
13358 * "len()" function
13359 */
13360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013361f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 typval_T *argvars;
13363 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013364{
13365 switch (argvars[0].v_type)
13366 {
13367 case VAR_STRING:
13368 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013369 rettv->vval.v_number = (varnumber_T)STRLEN(
13370 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013371 break;
13372 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013373 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013374 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013375 case VAR_DICT:
13376 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13377 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013378 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013379 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013380 break;
13381 }
13382}
13383
Bram Moolenaar33570922005-01-25 22:26:29 +000013384static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013385
13386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013387libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013388 typval_T *argvars;
13389 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013390 int type;
13391{
13392#ifdef FEAT_LIBCALL
13393 char_u *string_in;
13394 char_u **string_result;
13395 int nr_result;
13396#endif
13397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013398 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013399 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013400 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013401
13402 if (check_restricted() || check_secure())
13403 return;
13404
13405#ifdef FEAT_LIBCALL
13406 /* The first two args must be strings, otherwise its meaningless */
13407 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13408 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013409 string_in = NULL;
13410 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013411 string_in = argvars[2].vval.v_string;
13412 if (type == VAR_NUMBER)
13413 string_result = NULL;
13414 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013415 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013416 if (mch_libcall(argvars[0].vval.v_string,
13417 argvars[1].vval.v_string,
13418 string_in,
13419 argvars[2].vval.v_number,
13420 string_result,
13421 &nr_result) == OK
13422 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013423 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013424 }
13425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426}
13427
13428/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013429 * "libcall()" function
13430 */
13431 static void
13432f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013433 typval_T *argvars;
13434 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013435{
13436 libcall_common(argvars, rettv, VAR_STRING);
13437}
13438
13439/*
13440 * "libcallnr()" function
13441 */
13442 static void
13443f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013444 typval_T *argvars;
13445 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013446{
13447 libcall_common(argvars, rettv, VAR_NUMBER);
13448}
13449
13450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451 * "line(string)" function
13452 */
13453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013454f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013455 typval_T *argvars;
13456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457{
13458 linenr_T lnum = 0;
13459 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013460 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013462 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463 if (fp != NULL)
13464 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013465 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466}
13467
13468/*
13469 * "line2byte(lnum)" function
13470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013472f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013473 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475{
13476#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478#else
13479 linenr_T lnum;
13480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013481 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013485 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13486 if (rettv->vval.v_number >= 0)
13487 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488#endif
13489}
13490
13491/*
13492 * "lispindent(lnum)" function
13493 */
13494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013495f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013496 typval_T *argvars;
13497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498{
13499#ifdef FEAT_LISP
13500 pos_T pos;
13501 linenr_T lnum;
13502
13503 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13506 {
13507 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013508 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 curwin->w_cursor = pos;
13510 }
13511 else
13512#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013513 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514}
13515
13516/*
13517 * "localtime()" function
13518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013521 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013524 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525}
13526
Bram Moolenaar33570922005-01-25 22:26:29 +000013527static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528
13529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013530get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013531 typval_T *argvars;
13532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 int exact;
13534{
13535 char_u *keys;
13536 char_u *which;
13537 char_u buf[NUMBUFLEN];
13538 char_u *keys_buf = NULL;
13539 char_u *rhs;
13540 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013541 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013542 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013543 mapblock_T *mp;
13544 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545
13546 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013547 rettv->v_type = VAR_STRING;
13548 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013550 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 if (*keys == NUL)
13552 return;
13553
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013554 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013556 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013557 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013558 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013559 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013560 if (argvars[3].v_type != VAR_UNKNOWN)
13561 get_dict = get_tv_number(&argvars[3]);
13562 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 else
13565 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013566 if (which == NULL)
13567 return;
13568
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 mode = get_map_mode(&which, 0);
13570
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013571 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013572 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013574
13575 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013577 /* Return a string. */
13578 if (rhs != NULL)
13579 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580
Bram Moolenaarbd743252010-10-20 21:23:33 +020013581 }
13582 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13583 {
13584 /* Return a dictionary. */
13585 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13586 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13587 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588
Bram Moolenaarbd743252010-10-20 21:23:33 +020013589 dict_add_nr_str(dict, "lhs", 0L, lhs);
13590 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13591 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13592 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13593 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13594 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13595 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13596 dict_add_nr_str(dict, "mode", 0L, mapmode);
13597
13598 vim_free(lhs);
13599 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600 }
13601}
13602
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013603#ifdef FEAT_FLOAT
13604/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013605 * "log()" function
13606 */
13607 static void
13608f_log(argvars, rettv)
13609 typval_T *argvars;
13610 typval_T *rettv;
13611{
13612 float_T f;
13613
13614 rettv->v_type = VAR_FLOAT;
13615 if (get_float_arg(argvars, &f) == OK)
13616 rettv->vval.v_float = log(f);
13617 else
13618 rettv->vval.v_float = 0.0;
13619}
13620
13621/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013622 * "log10()" function
13623 */
13624 static void
13625f_log10(argvars, rettv)
13626 typval_T *argvars;
13627 typval_T *rettv;
13628{
13629 float_T f;
13630
13631 rettv->v_type = VAR_FLOAT;
13632 if (get_float_arg(argvars, &f) == OK)
13633 rettv->vval.v_float = log10(f);
13634 else
13635 rettv->vval.v_float = 0.0;
13636}
13637#endif
13638
Bram Moolenaar1dced572012-04-05 16:54:08 +020013639#ifdef FEAT_LUA
13640/*
13641 * "luaeval()" function
13642 */
13643 static void
13644f_luaeval(argvars, rettv)
13645 typval_T *argvars;
13646 typval_T *rettv;
13647{
13648 char_u *str;
13649 char_u buf[NUMBUFLEN];
13650
13651 str = get_tv_string_buf(&argvars[0], buf);
13652 do_luaeval(str, argvars + 1, rettv);
13653}
13654#endif
13655
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013657 * "map()" function
13658 */
13659 static void
13660f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013661 typval_T *argvars;
13662 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013663{
13664 filter_map(argvars, rettv, TRUE);
13665}
13666
13667/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013668 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 */
13670 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013671f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013672 typval_T *argvars;
13673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013675 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676}
13677
13678/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013679 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 */
13681 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013682f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013683 typval_T *argvars;
13684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013686 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687}
13688
Bram Moolenaar33570922005-01-25 22:26:29 +000013689static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690
13691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013693 typval_T *argvars;
13694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 int type;
13696{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013697 char_u *str = NULL;
13698 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699 char_u *pat;
13700 regmatch_T regmatch;
13701 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013702 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703 char_u *save_cpo;
13704 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013705 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013706 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013707 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013708 list_T *l = NULL;
13709 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013710 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013711 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712
13713 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13714 save_cpo = p_cpo;
13715 p_cpo = (char_u *)"";
13716
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013717 rettv->vval.v_number = -1;
13718 if (type == 3)
13719 {
13720 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013721 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013722 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013723 }
13724 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726 rettv->v_type = VAR_STRING;
13727 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013730 if (argvars[0].v_type == VAR_LIST)
13731 {
13732 if ((l = argvars[0].vval.v_list) == NULL)
13733 goto theend;
13734 li = l->lv_first;
13735 }
13736 else
13737 expr = str = get_tv_string(&argvars[0]);
13738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013739 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13740 if (pat == NULL)
13741 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013742
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013743 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013745 int error = FALSE;
13746
13747 start = get_tv_number_chk(&argvars[2], &error);
13748 if (error)
13749 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013750 if (l != NULL)
13751 {
13752 li = list_find(l, start);
13753 if (li == NULL)
13754 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013755 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013756 }
13757 else
13758 {
13759 if (start < 0)
13760 start = 0;
13761 if (start > (long)STRLEN(str))
13762 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013763 /* When "count" argument is there ignore matches before "start",
13764 * otherwise skip part of the string. Differs when pattern is "^"
13765 * or "\<". */
13766 if (argvars[3].v_type != VAR_UNKNOWN)
13767 startcol = start;
13768 else
13769 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013770 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013771
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013772 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013773 nth = get_tv_number_chk(&argvars[3], &error);
13774 if (error)
13775 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 }
13777
13778 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13779 if (regmatch.regprog != NULL)
13780 {
13781 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013782
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013783 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013784 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013785 if (l != NULL)
13786 {
13787 if (li == NULL)
13788 {
13789 match = FALSE;
13790 break;
13791 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013792 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013793 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013794 if (str == NULL)
13795 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013796 }
13797
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013798 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013799
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013800 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013801 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013802 if (l == NULL && !match)
13803 break;
13804
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013805 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013806 if (l != NULL)
13807 {
13808 li = li->li_next;
13809 ++idx;
13810 }
13811 else
13812 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013813#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013814 startcol = (colnr_T)(regmatch.startp[0]
13815 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013816#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013817 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013818#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013819 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013820 }
13821
13822 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013824 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013825 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013826 int i;
13827
13828 /* return list with matched string and submatches */
13829 for (i = 0; i < NSUBEXP; ++i)
13830 {
13831 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013832 {
13833 if (list_append_string(rettv->vval.v_list,
13834 (char_u *)"", 0) == FAIL)
13835 break;
13836 }
13837 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013838 regmatch.startp[i],
13839 (int)(regmatch.endp[i] - regmatch.startp[i]))
13840 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013841 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013842 }
13843 }
13844 else if (type == 2)
13845 {
13846 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013847 if (l != NULL)
13848 copy_tv(&li->li_tv, rettv);
13849 else
13850 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013852 }
13853 else if (l != NULL)
13854 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 else
13856 {
13857 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013858 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859 (varnumber_T)(regmatch.startp[0] - str);
13860 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013861 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013863 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 }
13865 }
13866 vim_free(regmatch.regprog);
13867 }
13868
13869theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013870 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 p_cpo = save_cpo;
13872}
13873
13874/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013875 * "match()" function
13876 */
13877 static void
13878f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013879 typval_T *argvars;
13880 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013881{
13882 find_some_match(argvars, rettv, 1);
13883}
13884
13885/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013886 * "matchadd()" function
13887 */
13888 static void
13889f_matchadd(argvars, rettv)
13890 typval_T *argvars;
13891 typval_T *rettv;
13892{
13893#ifdef FEAT_SEARCH_EXTRA
13894 char_u buf[NUMBUFLEN];
13895 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13896 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13897 int prio = 10; /* default priority */
13898 int id = -1;
13899 int error = FALSE;
13900
13901 rettv->vval.v_number = -1;
13902
13903 if (grp == NULL || pat == NULL)
13904 return;
13905 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013906 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013907 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013908 if (argvars[3].v_type != VAR_UNKNOWN)
13909 id = get_tv_number_chk(&argvars[3], &error);
13910 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013911 if (error == TRUE)
13912 return;
13913 if (id >= 1 && id <= 3)
13914 {
13915 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13916 return;
13917 }
13918
13919 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13920#endif
13921}
13922
13923/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013924 * "matcharg()" function
13925 */
13926 static void
13927f_matcharg(argvars, rettv)
13928 typval_T *argvars;
13929 typval_T *rettv;
13930{
13931 if (rettv_list_alloc(rettv) == OK)
13932 {
13933#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013934 int id = get_tv_number(&argvars[0]);
13935 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013936
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013937 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013938 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013939 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13940 {
13941 list_append_string(rettv->vval.v_list,
13942 syn_id2name(m->hlg_id), -1);
13943 list_append_string(rettv->vval.v_list, m->pattern, -1);
13944 }
13945 else
13946 {
13947 list_append_string(rettv->vval.v_list, NUL, -1);
13948 list_append_string(rettv->vval.v_list, NUL, -1);
13949 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013950 }
13951#endif
13952 }
13953}
13954
13955/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013956 * "matchdelete()" function
13957 */
13958 static void
13959f_matchdelete(argvars, rettv)
13960 typval_T *argvars;
13961 typval_T *rettv;
13962{
13963#ifdef FEAT_SEARCH_EXTRA
13964 rettv->vval.v_number = match_delete(curwin,
13965 (int)get_tv_number(&argvars[0]), TRUE);
13966#endif
13967}
13968
13969/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013970 * "matchend()" function
13971 */
13972 static void
13973f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013974 typval_T *argvars;
13975 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013976{
13977 find_some_match(argvars, rettv, 0);
13978}
13979
13980/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013981 * "matchlist()" function
13982 */
13983 static void
13984f_matchlist(argvars, rettv)
13985 typval_T *argvars;
13986 typval_T *rettv;
13987{
13988 find_some_match(argvars, rettv, 3);
13989}
13990
13991/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013992 * "matchstr()" function
13993 */
13994 static void
13995f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013996 typval_T *argvars;
13997 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013998{
13999 find_some_match(argvars, rettv, 2);
14000}
14001
Bram Moolenaar33570922005-01-25 22:26:29 +000014002static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014003
14004 static void
14005max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014006 typval_T *argvars;
14007 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014008 int domax;
14009{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014010 long n = 0;
14011 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014012 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014013
14014 if (argvars[0].v_type == VAR_LIST)
14015 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014016 list_T *l;
14017 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014018
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014019 l = argvars[0].vval.v_list;
14020 if (l != NULL)
14021 {
14022 li = l->lv_first;
14023 if (li != NULL)
14024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014025 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014026 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014027 {
14028 li = li->li_next;
14029 if (li == NULL)
14030 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014031 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014032 if (domax ? i > n : i < n)
14033 n = i;
14034 }
14035 }
14036 }
14037 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014038 else if (argvars[0].v_type == VAR_DICT)
14039 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014040 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014041 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014042 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014043 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014044
14045 d = argvars[0].vval.v_dict;
14046 if (d != NULL)
14047 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014048 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014049 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014050 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014051 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014052 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014053 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014054 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014055 if (first)
14056 {
14057 n = i;
14058 first = FALSE;
14059 }
14060 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014061 n = i;
14062 }
14063 }
14064 }
14065 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014066 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014067 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014068 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014069}
14070
14071/*
14072 * "max()" function
14073 */
14074 static void
14075f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014076 typval_T *argvars;
14077 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014078{
14079 max_min(argvars, rettv, TRUE);
14080}
14081
14082/*
14083 * "min()" function
14084 */
14085 static void
14086f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014087 typval_T *argvars;
14088 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014089{
14090 max_min(argvars, rettv, FALSE);
14091}
14092
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014093static int mkdir_recurse __ARGS((char_u *dir, int prot));
14094
14095/*
14096 * Create the directory in which "dir" is located, and higher levels when
14097 * needed.
14098 */
14099 static int
14100mkdir_recurse(dir, prot)
14101 char_u *dir;
14102 int prot;
14103{
14104 char_u *p;
14105 char_u *updir;
14106 int r = FAIL;
14107
14108 /* Get end of directory name in "dir".
14109 * We're done when it's "/" or "c:/". */
14110 p = gettail_sep(dir);
14111 if (p <= get_past_head(dir))
14112 return OK;
14113
14114 /* If the directory exists we're done. Otherwise: create it.*/
14115 updir = vim_strnsave(dir, (int)(p - dir));
14116 if (updir == NULL)
14117 return FAIL;
14118 if (mch_isdir(updir))
14119 r = OK;
14120 else if (mkdir_recurse(updir, prot) == OK)
14121 r = vim_mkdir_emsg(updir, prot);
14122 vim_free(updir);
14123 return r;
14124}
14125
14126#ifdef vim_mkdir
14127/*
14128 * "mkdir()" function
14129 */
14130 static void
14131f_mkdir(argvars, rettv)
14132 typval_T *argvars;
14133 typval_T *rettv;
14134{
14135 char_u *dir;
14136 char_u buf[NUMBUFLEN];
14137 int prot = 0755;
14138
14139 rettv->vval.v_number = FAIL;
14140 if (check_restricted() || check_secure())
14141 return;
14142
14143 dir = get_tv_string_buf(&argvars[0], buf);
14144 if (argvars[1].v_type != VAR_UNKNOWN)
14145 {
14146 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147 prot = get_tv_number_chk(&argvars[2], NULL);
14148 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014149 mkdir_recurse(dir, prot);
14150 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014152}
14153#endif
14154
Bram Moolenaar0d660222005-01-07 21:51:51 +000014155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156 * "mode()" function
14157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014159f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014160 typval_T *argvars;
14161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014163 char_u buf[3];
14164
14165 buf[1] = NUL;
14166 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167
14168#ifdef FEAT_VISUAL
14169 if (VIsual_active)
14170 {
14171 if (VIsual_select)
14172 buf[0] = VIsual_mode + 's' - 'v';
14173 else
14174 buf[0] = VIsual_mode;
14175 }
14176 else
14177#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014178 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14179 || State == CONFIRM)
14180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014182 if (State == ASKMORE)
14183 buf[1] = 'm';
14184 else if (State == CONFIRM)
14185 buf[1] = '?';
14186 }
14187 else if (State == EXTERNCMD)
14188 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189 else if (State & INSERT)
14190 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014191#ifdef FEAT_VREPLACE
14192 if (State & VREPLACE_FLAG)
14193 {
14194 buf[0] = 'R';
14195 buf[1] = 'v';
14196 }
14197 else
14198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199 if (State & REPLACE_FLAG)
14200 buf[0] = 'R';
14201 else
14202 buf[0] = 'i';
14203 }
14204 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014207 if (exmode_active)
14208 buf[1] = 'v';
14209 }
14210 else if (exmode_active)
14211 {
14212 buf[0] = 'c';
14213 buf[1] = 'e';
14214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014218 if (finish_op)
14219 buf[1] = 'o';
14220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014222 /* Clear out the minor mode when the argument is not a non-zero number or
14223 * non-empty string. */
14224 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014225 buf[1] = NUL;
14226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014227 rettv->vval.v_string = vim_strsave(buf);
14228 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229}
14230
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014231#ifdef FEAT_MZSCHEME
14232/*
14233 * "mzeval()" function
14234 */
14235 static void
14236f_mzeval(argvars, rettv)
14237 typval_T *argvars;
14238 typval_T *rettv;
14239{
14240 char_u *str;
14241 char_u buf[NUMBUFLEN];
14242
14243 str = get_tv_string_buf(&argvars[0], buf);
14244 do_mzeval(str, rettv);
14245}
14246#endif
14247
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014249 * "nextnonblank()" function
14250 */
14251 static void
14252f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014253 typval_T *argvars;
14254 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014255{
14256 linenr_T lnum;
14257
14258 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014260 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014261 {
14262 lnum = 0;
14263 break;
14264 }
14265 if (*skipwhite(ml_get(lnum)) != NUL)
14266 break;
14267 }
14268 rettv->vval.v_number = lnum;
14269}
14270
14271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272 * "nr2char()" function
14273 */
14274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014275f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014276 typval_T *argvars;
14277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278{
14279 char_u buf[NUMBUFLEN];
14280
14281#ifdef FEAT_MBYTE
14282 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284 else
14285#endif
14286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014287 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 buf[1] = NUL;
14289 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014290 rettv->v_type = VAR_STRING;
14291 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014292}
14293
14294/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014295 * "or(expr, expr)" function
14296 */
14297 static void
14298f_or(argvars, rettv)
14299 typval_T *argvars;
14300 typval_T *rettv;
14301{
14302 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14303 | get_tv_number_chk(&argvars[1], NULL);
14304}
14305
14306/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014307 * "pathshorten()" function
14308 */
14309 static void
14310f_pathshorten(argvars, rettv)
14311 typval_T *argvars;
14312 typval_T *rettv;
14313{
14314 char_u *p;
14315
14316 rettv->v_type = VAR_STRING;
14317 p = get_tv_string_chk(&argvars[0]);
14318 if (p == NULL)
14319 rettv->vval.v_string = NULL;
14320 else
14321 {
14322 p = vim_strsave(p);
14323 rettv->vval.v_string = p;
14324 if (p != NULL)
14325 shorten_dir(p);
14326 }
14327}
14328
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014329#ifdef FEAT_FLOAT
14330/*
14331 * "pow()" function
14332 */
14333 static void
14334f_pow(argvars, rettv)
14335 typval_T *argvars;
14336 typval_T *rettv;
14337{
14338 float_T fx, fy;
14339
14340 rettv->v_type = VAR_FLOAT;
14341 if (get_float_arg(argvars, &fx) == OK
14342 && get_float_arg(&argvars[1], &fy) == OK)
14343 rettv->vval.v_float = pow(fx, fy);
14344 else
14345 rettv->vval.v_float = 0.0;
14346}
14347#endif
14348
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014349/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014350 * "prevnonblank()" function
14351 */
14352 static void
14353f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014354 typval_T *argvars;
14355 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014356{
14357 linenr_T lnum;
14358
14359 lnum = get_tv_lnum(argvars);
14360 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14361 lnum = 0;
14362 else
14363 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14364 --lnum;
14365 rettv->vval.v_number = lnum;
14366}
14367
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014368#ifdef HAVE_STDARG_H
14369/* This dummy va_list is here because:
14370 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14371 * - locally in the function results in a "used before set" warning
14372 * - using va_start() to initialize it gives "function with fixed args" error */
14373static va_list ap;
14374#endif
14375
Bram Moolenaar8c711452005-01-14 21:53:12 +000014376/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014377 * "printf()" function
14378 */
14379 static void
14380f_printf(argvars, rettv)
14381 typval_T *argvars;
14382 typval_T *rettv;
14383{
14384 rettv->v_type = VAR_STRING;
14385 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014386#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014387 {
14388 char_u buf[NUMBUFLEN];
14389 int len;
14390 char_u *s;
14391 int saved_did_emsg = did_emsg;
14392 char *fmt;
14393
14394 /* Get the required length, allocate the buffer and do it for real. */
14395 did_emsg = FALSE;
14396 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014397 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014398 if (!did_emsg)
14399 {
14400 s = alloc(len + 1);
14401 if (s != NULL)
14402 {
14403 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014404 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014405 }
14406 }
14407 did_emsg |= saved_did_emsg;
14408 }
14409#endif
14410}
14411
14412/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014413 * "pumvisible()" function
14414 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014415 static void
14416f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014417 typval_T *argvars UNUSED;
14418 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014419{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014420#ifdef FEAT_INS_EXPAND
14421 if (pum_visible())
14422 rettv->vval.v_number = 1;
14423#endif
14424}
14425
14426/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014427 * "range()" function
14428 */
14429 static void
14430f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014431 typval_T *argvars;
14432 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014433{
14434 long start;
14435 long end;
14436 long stride = 1;
14437 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014438 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014440 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014441 if (argvars[1].v_type == VAR_UNKNOWN)
14442 {
14443 end = start - 1;
14444 start = 0;
14445 }
14446 else
14447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014448 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014449 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014450 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014451 }
14452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014453 if (error)
14454 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014455 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014456 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014457 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014458 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014459 else
14460 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014461 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014462 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014463 if (list_append_number(rettv->vval.v_list,
14464 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014465 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014466 }
14467}
14468
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014469/*
14470 * "readfile()" function
14471 */
14472 static void
14473f_readfile(argvars, rettv)
14474 typval_T *argvars;
14475 typval_T *rettv;
14476{
14477 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014478 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014479 char_u *fname;
14480 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014481 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14482 int io_size = sizeof(buf);
14483 int readlen; /* size of last fread() */
14484 char_u *prev = NULL; /* previously read bytes, if any */
14485 long prevlen = 0; /* length of data in prev */
14486 long prevsize = 0; /* size of prev buffer */
14487 long maxline = MAXLNUM;
14488 long cnt = 0;
14489 char_u *p; /* position in buf */
14490 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014491
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014492 if (argvars[1].v_type != VAR_UNKNOWN)
14493 {
14494 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14495 binary = TRUE;
14496 if (argvars[2].v_type != VAR_UNKNOWN)
14497 maxline = get_tv_number(&argvars[2]);
14498 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014499
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014500 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014501 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014502
14503 /* Always open the file in binary mode, library functions have a mind of
14504 * their own about CR-LF conversion. */
14505 fname = get_tv_string(&argvars[0]);
14506 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14507 {
14508 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14509 return;
14510 }
14511
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014512 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014513 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014514 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014515
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014516 /* This for loop processes what was read, but is also entered at end
14517 * of file so that either:
14518 * - an incomplete line gets written
14519 * - a "binary" file gets an empty line at the end if it ends in a
14520 * newline. */
14521 for (p = buf, start = buf;
14522 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14523 ++p)
14524 {
14525 if (*p == '\n' || readlen <= 0)
14526 {
14527 listitem_T *li;
14528 char_u *s = NULL;
14529 long_u len = p - start;
14530
14531 /* Finished a line. Remove CRs before NL. */
14532 if (readlen > 0 && !binary)
14533 {
14534 while (len > 0 && start[len - 1] == '\r')
14535 --len;
14536 /* removal may cross back to the "prev" string */
14537 if (len == 0)
14538 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14539 --prevlen;
14540 }
14541 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014542 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014543 else
14544 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014545 /* Change "prev" buffer to be the right size. This way
14546 * the bytes are only copied once, and very long lines are
14547 * allocated only once. */
14548 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014549 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014550 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014551 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014552 prev = NULL; /* the list will own the string */
14553 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014554 }
14555 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014556 if (s == NULL)
14557 {
14558 do_outofmem_msg((long_u) prevlen + len + 1);
14559 failed = TRUE;
14560 break;
14561 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014562
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014563 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014564 {
14565 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014566 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014567 break;
14568 }
14569 li->li_tv.v_type = VAR_STRING;
14570 li->li_tv.v_lock = 0;
14571 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014572 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014573
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014574 start = p + 1; /* step over newline */
14575 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014576 break;
14577 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014578 else if (*p == NUL)
14579 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014580#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014581 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14582 * when finding the BF and check the previous two bytes. */
14583 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014584 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014585 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14586 * + 1, these may be in the "prev" string. */
14587 char_u back1 = p >= buf + 1 ? p[-1]
14588 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14589 char_u back2 = p >= buf + 2 ? p[-2]
14590 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14591 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014592
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014593 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014594 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014595 char_u *dest = p - 2;
14596
14597 /* Usually a BOM is at the beginning of a file, and so at
14598 * the beginning of a line; then we can just step over it.
14599 */
14600 if (start == dest)
14601 start = p + 1;
14602 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014603 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014604 /* have to shuffle buf to close gap */
14605 int adjust_prevlen = 0;
14606
14607 if (dest < buf)
14608 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014609 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014610 dest = buf;
14611 }
14612 if (readlen > p - buf + 1)
14613 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14614 readlen -= 3 - adjust_prevlen;
14615 prevlen -= adjust_prevlen;
14616 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014617 }
14618 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014619 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014620#endif
14621 } /* for */
14622
14623 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14624 break;
14625 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014626 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014627 /* There's part of a line in buf, store it in "prev". */
14628 if (p - start + prevlen >= prevsize)
14629 {
14630 /* need bigger "prev" buffer */
14631 char_u *newprev;
14632
14633 /* A common use case is ordinary text files and "prev" gets a
14634 * fragment of a line, so the first allocation is made
14635 * small, to avoid repeatedly 'allocing' large and
14636 * 'reallocing' small. */
14637 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014638 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014639 else
14640 {
14641 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014642 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014643 prevsize = grow50pc > growmin ? grow50pc : growmin;
14644 }
14645 if ((newprev = vim_realloc(prev, prevsize)) == NULL)
14646 {
14647 do_outofmem_msg((long_u)prevsize);
14648 failed = TRUE;
14649 break;
14650 }
14651 prev = newprev;
14652 }
14653 /* Add the line part to end of "prev". */
14654 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014655 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014656 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014657 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014658
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014659 /*
14660 * For a negative line count use only the lines at the end of the file,
14661 * free the rest.
14662 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014663 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014664 while (cnt > -maxline)
14665 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014666 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014667 --cnt;
14668 }
14669
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014670 if (failed)
14671 {
14672 list_free(rettv->vval.v_list, TRUE);
14673 /* readfile doc says an empty list is returned on error */
14674 rettv->vval.v_list = list_alloc();
14675 }
14676
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014677 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014678 fclose(fd);
14679}
14680
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014681#if defined(FEAT_RELTIME)
14682static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14683
14684/*
14685 * Convert a List to proftime_T.
14686 * Return FAIL when there is something wrong.
14687 */
14688 static int
14689list2proftime(arg, tm)
14690 typval_T *arg;
14691 proftime_T *tm;
14692{
14693 long n1, n2;
14694 int error = FALSE;
14695
14696 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14697 || arg->vval.v_list->lv_len != 2)
14698 return FAIL;
14699 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14700 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14701# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014702 tm->HighPart = n1;
14703 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014704# else
14705 tm->tv_sec = n1;
14706 tm->tv_usec = n2;
14707# endif
14708 return error ? FAIL : OK;
14709}
14710#endif /* FEAT_RELTIME */
14711
14712/*
14713 * "reltime()" function
14714 */
14715 static void
14716f_reltime(argvars, rettv)
14717 typval_T *argvars;
14718 typval_T *rettv;
14719{
14720#ifdef FEAT_RELTIME
14721 proftime_T res;
14722 proftime_T start;
14723
14724 if (argvars[0].v_type == VAR_UNKNOWN)
14725 {
14726 /* No arguments: get current time. */
14727 profile_start(&res);
14728 }
14729 else if (argvars[1].v_type == VAR_UNKNOWN)
14730 {
14731 if (list2proftime(&argvars[0], &res) == FAIL)
14732 return;
14733 profile_end(&res);
14734 }
14735 else
14736 {
14737 /* Two arguments: compute the difference. */
14738 if (list2proftime(&argvars[0], &start) == FAIL
14739 || list2proftime(&argvars[1], &res) == FAIL)
14740 return;
14741 profile_sub(&res, &start);
14742 }
14743
14744 if (rettv_list_alloc(rettv) == OK)
14745 {
14746 long n1, n2;
14747
14748# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014749 n1 = res.HighPart;
14750 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014751# else
14752 n1 = res.tv_sec;
14753 n2 = res.tv_usec;
14754# endif
14755 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14756 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14757 }
14758#endif
14759}
14760
14761/*
14762 * "reltimestr()" function
14763 */
14764 static void
14765f_reltimestr(argvars, rettv)
14766 typval_T *argvars;
14767 typval_T *rettv;
14768{
14769#ifdef FEAT_RELTIME
14770 proftime_T tm;
14771#endif
14772
14773 rettv->v_type = VAR_STRING;
14774 rettv->vval.v_string = NULL;
14775#ifdef FEAT_RELTIME
14776 if (list2proftime(&argvars[0], &tm) == OK)
14777 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14778#endif
14779}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014780
Bram Moolenaar0d660222005-01-07 21:51:51 +000014781#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14782static void make_connection __ARGS((void));
14783static int check_connection __ARGS((void));
14784
14785 static void
14786make_connection()
14787{
14788 if (X_DISPLAY == NULL
14789# ifdef FEAT_GUI
14790 && !gui.in_use
14791# endif
14792 )
14793 {
14794 x_force_connect = TRUE;
14795 setup_term_clip();
14796 x_force_connect = FALSE;
14797 }
14798}
14799
14800 static int
14801check_connection()
14802{
14803 make_connection();
14804 if (X_DISPLAY == NULL)
14805 {
14806 EMSG(_("E240: No connection to Vim server"));
14807 return FAIL;
14808 }
14809 return OK;
14810}
14811#endif
14812
14813#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014814static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014815
14816 static void
14817remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014818 typval_T *argvars;
14819 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014820 int expr;
14821{
14822 char_u *server_name;
14823 char_u *keys;
14824 char_u *r = NULL;
14825 char_u buf[NUMBUFLEN];
14826# ifdef WIN32
14827 HWND w;
14828# else
14829 Window w;
14830# endif
14831
14832 if (check_restricted() || check_secure())
14833 return;
14834
14835# ifdef FEAT_X11
14836 if (check_connection() == FAIL)
14837 return;
14838# endif
14839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014840 server_name = get_tv_string_chk(&argvars[0]);
14841 if (server_name == NULL)
14842 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014843 keys = get_tv_string_buf(&argvars[1], buf);
14844# ifdef WIN32
14845 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14846# else
14847 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14848 < 0)
14849# endif
14850 {
14851 if (r != NULL)
14852 EMSG(r); /* sending worked but evaluation failed */
14853 else
14854 EMSG2(_("E241: Unable to send to %s"), server_name);
14855 return;
14856 }
14857
14858 rettv->vval.v_string = r;
14859
14860 if (argvars[2].v_type != VAR_UNKNOWN)
14861 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014862 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014863 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014864 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014865
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014866 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014867 v.di_tv.v_type = VAR_STRING;
14868 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014869 idvar = get_tv_string_chk(&argvars[2]);
14870 if (idvar != NULL)
14871 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014872 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014873 }
14874}
14875#endif
14876
14877/*
14878 * "remote_expr()" function
14879 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014880 static void
14881f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014882 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014883 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014884{
14885 rettv->v_type = VAR_STRING;
14886 rettv->vval.v_string = NULL;
14887#ifdef FEAT_CLIENTSERVER
14888 remote_common(argvars, rettv, TRUE);
14889#endif
14890}
14891
14892/*
14893 * "remote_foreground()" function
14894 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014895 static void
14896f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014897 typval_T *argvars UNUSED;
14898 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014899{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014900#ifdef FEAT_CLIENTSERVER
14901# ifdef WIN32
14902 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014903 {
14904 char_u *server_name = get_tv_string_chk(&argvars[0]);
14905
14906 if (server_name != NULL)
14907 serverForeground(server_name);
14908 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014909# else
14910 /* Send a foreground() expression to the server. */
14911 argvars[1].v_type = VAR_STRING;
14912 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14913 argvars[2].v_type = VAR_UNKNOWN;
14914 remote_common(argvars, rettv, TRUE);
14915 vim_free(argvars[1].vval.v_string);
14916# endif
14917#endif
14918}
14919
Bram Moolenaar0d660222005-01-07 21:51:51 +000014920 static void
14921f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014922 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014923 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014924{
14925#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014926 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014927 char_u *s = NULL;
14928# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014929 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014930# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014931 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014932
14933 if (check_restricted() || check_secure())
14934 {
14935 rettv->vval.v_number = -1;
14936 return;
14937 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014938 serverid = get_tv_string_chk(&argvars[0]);
14939 if (serverid == NULL)
14940 {
14941 rettv->vval.v_number = -1;
14942 return; /* type error; errmsg already given */
14943 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014944# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014945 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014946 if (n == 0)
14947 rettv->vval.v_number = -1;
14948 else
14949 {
14950 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14951 rettv->vval.v_number = (s != NULL);
14952 }
14953# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014954 if (check_connection() == FAIL)
14955 return;
14956
14957 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014958 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014959# endif
14960
14961 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014963 char_u *retvar;
14964
Bram Moolenaar33570922005-01-25 22:26:29 +000014965 v.di_tv.v_type = VAR_STRING;
14966 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014967 retvar = get_tv_string_chk(&argvars[1]);
14968 if (retvar != NULL)
14969 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014970 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014971 }
14972#else
14973 rettv->vval.v_number = -1;
14974#endif
14975}
14976
Bram Moolenaar0d660222005-01-07 21:51:51 +000014977 static void
14978f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014979 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014980 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014981{
14982 char_u *r = NULL;
14983
14984#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014985 char_u *serverid = get_tv_string_chk(&argvars[0]);
14986
14987 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014988 {
14989# ifdef WIN32
14990 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014991 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014992
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014993 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014994 if (n != 0)
14995 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14996 if (r == NULL)
14997# else
14998 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014999 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015000# endif
15001 EMSG(_("E277: Unable to read a server reply"));
15002 }
15003#endif
15004 rettv->v_type = VAR_STRING;
15005 rettv->vval.v_string = r;
15006}
15007
15008/*
15009 * "remote_send()" function
15010 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011 static void
15012f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015013 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015014 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015015{
15016 rettv->v_type = VAR_STRING;
15017 rettv->vval.v_string = NULL;
15018#ifdef FEAT_CLIENTSERVER
15019 remote_common(argvars, rettv, FALSE);
15020#endif
15021}
15022
15023/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015024 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015025 */
15026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015027f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015028 typval_T *argvars;
15029 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015030{
Bram Moolenaar33570922005-01-25 22:26:29 +000015031 list_T *l;
15032 listitem_T *item, *item2;
15033 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015034 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015035 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015036 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015037 dict_T *d;
15038 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015039 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015040
Bram Moolenaar8c711452005-01-14 21:53:12 +000015041 if (argvars[0].v_type == VAR_DICT)
15042 {
15043 if (argvars[2].v_type != VAR_UNKNOWN)
15044 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015045 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015046 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015048 key = get_tv_string_chk(&argvars[1]);
15049 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015051 di = dict_find(d, key, -1);
15052 if (di == NULL)
15053 EMSG2(_(e_dictkey), key);
15054 else
15055 {
15056 *rettv = di->di_tv;
15057 init_tv(&di->di_tv);
15058 dictitem_remove(d, di);
15059 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015060 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015061 }
15062 }
15063 else if (argvars[0].v_type != VAR_LIST)
15064 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015065 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015066 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015068 int error = FALSE;
15069
15070 idx = get_tv_number_chk(&argvars[1], &error);
15071 if (error)
15072 ; /* type error: do nothing, errmsg already given */
15073 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015074 EMSGN(_(e_listidx), idx);
15075 else
15076 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015077 if (argvars[2].v_type == VAR_UNKNOWN)
15078 {
15079 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015080 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015081 *rettv = item->li_tv;
15082 vim_free(item);
15083 }
15084 else
15085 {
15086 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015087 end = get_tv_number_chk(&argvars[2], &error);
15088 if (error)
15089 ; /* type error: do nothing */
15090 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015091 EMSGN(_(e_listidx), end);
15092 else
15093 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015094 int cnt = 0;
15095
15096 for (li = item; li != NULL; li = li->li_next)
15097 {
15098 ++cnt;
15099 if (li == item2)
15100 break;
15101 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015102 if (li == NULL) /* didn't find "item2" after "item" */
15103 EMSG(_(e_invrange));
15104 else
15105 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015106 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015107 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015108 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015109 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015110 l->lv_first = item;
15111 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015112 item->li_prev = NULL;
15113 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015114 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015115 }
15116 }
15117 }
15118 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015119 }
15120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121}
15122
15123/*
15124 * "rename({from}, {to})" function
15125 */
15126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015127f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015128 typval_T *argvars;
15129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130{
15131 char_u buf[NUMBUFLEN];
15132
15133 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015134 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015136 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15137 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138}
15139
15140/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015141 * "repeat()" function
15142 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015143 static void
15144f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015145 typval_T *argvars;
15146 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015147{
15148 char_u *p;
15149 int n;
15150 int slen;
15151 int len;
15152 char_u *r;
15153 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015154
15155 n = get_tv_number(&argvars[1]);
15156 if (argvars[0].v_type == VAR_LIST)
15157 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015158 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015159 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015160 if (list_extend(rettv->vval.v_list,
15161 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015162 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015163 }
15164 else
15165 {
15166 p = get_tv_string(&argvars[0]);
15167 rettv->v_type = VAR_STRING;
15168 rettv->vval.v_string = NULL;
15169
15170 slen = (int)STRLEN(p);
15171 len = slen * n;
15172 if (len <= 0)
15173 return;
15174
15175 r = alloc(len + 1);
15176 if (r != NULL)
15177 {
15178 for (i = 0; i < n; i++)
15179 mch_memmove(r + i * slen, p, (size_t)slen);
15180 r[len] = NUL;
15181 }
15182
15183 rettv->vval.v_string = r;
15184 }
15185}
15186
15187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015188 * "resolve()" function
15189 */
15190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015191f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015192 typval_T *argvars;
15193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194{
15195 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015196#ifdef HAVE_READLINK
15197 char_u *buf = NULL;
15198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015200 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201#ifdef FEAT_SHORTCUT
15202 {
15203 char_u *v = NULL;
15204
15205 v = mch_resolve_shortcut(p);
15206 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015207 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015209 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210 }
15211#else
15212# ifdef HAVE_READLINK
15213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214 char_u *cpy;
15215 int len;
15216 char_u *remain = NULL;
15217 char_u *q;
15218 int is_relative_to_current = FALSE;
15219 int has_trailing_pathsep = FALSE;
15220 int limit = 100;
15221
15222 p = vim_strsave(p);
15223
15224 if (p[0] == '.' && (vim_ispathsep(p[1])
15225 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15226 is_relative_to_current = TRUE;
15227
15228 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015229 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015231 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015232 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234
15235 q = getnextcomp(p);
15236 if (*q != NUL)
15237 {
15238 /* Separate the first path component in "p", and keep the
15239 * remainder (beginning with the path separator). */
15240 remain = vim_strsave(q - 1);
15241 q[-1] = NUL;
15242 }
15243
Bram Moolenaard9462e32011-04-11 21:35:11 +020015244 buf = alloc(MAXPATHL + 1);
15245 if (buf == NULL)
15246 goto fail;
15247
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248 for (;;)
15249 {
15250 for (;;)
15251 {
15252 len = readlink((char *)p, (char *)buf, MAXPATHL);
15253 if (len <= 0)
15254 break;
15255 buf[len] = NUL;
15256
15257 if (limit-- == 0)
15258 {
15259 vim_free(p);
15260 vim_free(remain);
15261 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015262 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 goto fail;
15264 }
15265
15266 /* Ensure that the result will have a trailing path separator
15267 * if the argument has one. */
15268 if (remain == NULL && has_trailing_pathsep)
15269 add_pathsep(buf);
15270
15271 /* Separate the first path component in the link value and
15272 * concatenate the remainders. */
15273 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15274 if (*q != NUL)
15275 {
15276 if (remain == NULL)
15277 remain = vim_strsave(q - 1);
15278 else
15279 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015280 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281 if (cpy != NULL)
15282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283 vim_free(remain);
15284 remain = cpy;
15285 }
15286 }
15287 q[-1] = NUL;
15288 }
15289
15290 q = gettail(p);
15291 if (q > p && *q == NUL)
15292 {
15293 /* Ignore trailing path separator. */
15294 q[-1] = NUL;
15295 q = gettail(p);
15296 }
15297 if (q > p && !mch_isFullName(buf))
15298 {
15299 /* symlink is relative to directory of argument */
15300 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15301 if (cpy != NULL)
15302 {
15303 STRCPY(cpy, p);
15304 STRCPY(gettail(cpy), buf);
15305 vim_free(p);
15306 p = cpy;
15307 }
15308 }
15309 else
15310 {
15311 vim_free(p);
15312 p = vim_strsave(buf);
15313 }
15314 }
15315
15316 if (remain == NULL)
15317 break;
15318
15319 /* Append the first path component of "remain" to "p". */
15320 q = getnextcomp(remain + 1);
15321 len = q - remain - (*q != NUL);
15322 cpy = vim_strnsave(p, STRLEN(p) + len);
15323 if (cpy != NULL)
15324 {
15325 STRNCAT(cpy, remain, len);
15326 vim_free(p);
15327 p = cpy;
15328 }
15329 /* Shorten "remain". */
15330 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015331 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 else
15333 {
15334 vim_free(remain);
15335 remain = NULL;
15336 }
15337 }
15338
15339 /* If the result is a relative path name, make it explicitly relative to
15340 * the current directory if and only if the argument had this form. */
15341 if (!vim_ispathsep(*p))
15342 {
15343 if (is_relative_to_current
15344 && *p != NUL
15345 && !(p[0] == '.'
15346 && (p[1] == NUL
15347 || vim_ispathsep(p[1])
15348 || (p[1] == '.'
15349 && (p[2] == NUL
15350 || vim_ispathsep(p[2]))))))
15351 {
15352 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015353 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354 if (cpy != NULL)
15355 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 vim_free(p);
15357 p = cpy;
15358 }
15359 }
15360 else if (!is_relative_to_current)
15361 {
15362 /* Strip leading "./". */
15363 q = p;
15364 while (q[0] == '.' && vim_ispathsep(q[1]))
15365 q += 2;
15366 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015367 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368 }
15369 }
15370
15371 /* Ensure that the result will have no trailing path separator
15372 * if the argument had none. But keep "/" or "//". */
15373 if (!has_trailing_pathsep)
15374 {
15375 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015376 if (after_pathsep(p, q))
15377 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378 }
15379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015380 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 }
15382# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015383 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384# endif
15385#endif
15386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015387 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388
15389#ifdef HAVE_READLINK
15390fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015391 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015393 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394}
15395
15396/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015397 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398 */
15399 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015400f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015401 typval_T *argvars;
15402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403{
Bram Moolenaar33570922005-01-25 22:26:29 +000015404 list_T *l;
15405 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406
Bram Moolenaar0d660222005-01-07 21:51:51 +000015407 if (argvars[0].v_type != VAR_LIST)
15408 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015409 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015410 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015411 {
15412 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015413 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015414 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015415 while (li != NULL)
15416 {
15417 ni = li->li_prev;
15418 list_append(l, li);
15419 li = ni;
15420 }
15421 rettv->vval.v_list = l;
15422 rettv->v_type = VAR_LIST;
15423 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015424 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426}
15427
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015428#define SP_NOMOVE 0x01 /* don't move cursor */
15429#define SP_REPEAT 0x02 /* repeat to find outer pair */
15430#define SP_RETCOUNT 0x04 /* return matchcount */
15431#define SP_SETPCMARK 0x08 /* set previous context mark */
15432#define SP_START 0x10 /* accept match at start position */
15433#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15434#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015435
Bram Moolenaar33570922005-01-25 22:26:29 +000015436static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015437
15438/*
15439 * Get flags for a search function.
15440 * Possibly sets "p_ws".
15441 * Returns BACKWARD, FORWARD or zero (for an error).
15442 */
15443 static int
15444get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015445 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015446 int *flagsp;
15447{
15448 int dir = FORWARD;
15449 char_u *flags;
15450 char_u nbuf[NUMBUFLEN];
15451 int mask;
15452
15453 if (varp->v_type != VAR_UNKNOWN)
15454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015455 flags = get_tv_string_buf_chk(varp, nbuf);
15456 if (flags == NULL)
15457 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015458 while (*flags != NUL)
15459 {
15460 switch (*flags)
15461 {
15462 case 'b': dir = BACKWARD; break;
15463 case 'w': p_ws = TRUE; break;
15464 case 'W': p_ws = FALSE; break;
15465 default: mask = 0;
15466 if (flagsp != NULL)
15467 switch (*flags)
15468 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015469 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015470 case 'e': mask = SP_END; break;
15471 case 'm': mask = SP_RETCOUNT; break;
15472 case 'n': mask = SP_NOMOVE; break;
15473 case 'p': mask = SP_SUBPAT; break;
15474 case 'r': mask = SP_REPEAT; break;
15475 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015476 }
15477 if (mask == 0)
15478 {
15479 EMSG2(_(e_invarg2), flags);
15480 dir = 0;
15481 }
15482 else
15483 *flagsp |= mask;
15484 }
15485 if (dir == 0)
15486 break;
15487 ++flags;
15488 }
15489 }
15490 return dir;
15491}
15492
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015494 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015496 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015497search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015498 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015499 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015500 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015502 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503 char_u *pat;
15504 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015505 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506 int save_p_ws = p_ws;
15507 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015508 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015509 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015510 proftime_T tm;
15511#ifdef FEAT_RELTIME
15512 long time_limit = 0;
15513#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015514 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015515 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015517 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015518 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015519 if (dir == 0)
15520 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015521 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015522 if (flags & SP_START)
15523 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015524 if (flags & SP_END)
15525 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015526
Bram Moolenaar76929292008-01-06 19:07:36 +000015527 /* Optional arguments: line number to stop searching and timeout. */
15528 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015529 {
15530 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15531 if (lnum_stop < 0)
15532 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015533#ifdef FEAT_RELTIME
15534 if (argvars[3].v_type != VAR_UNKNOWN)
15535 {
15536 time_limit = get_tv_number_chk(&argvars[3], NULL);
15537 if (time_limit < 0)
15538 goto theend;
15539 }
15540#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015541 }
15542
Bram Moolenaar76929292008-01-06 19:07:36 +000015543#ifdef FEAT_RELTIME
15544 /* Set the time limit, if there is one. */
15545 profile_setlimit(time_limit, &tm);
15546#endif
15547
Bram Moolenaar231334e2005-07-25 20:46:57 +000015548 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015549 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015550 * Check to make sure only those flags are set.
15551 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15552 * flags cannot be set. Check for that condition also.
15553 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015554 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015555 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015557 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015558 goto theend;
15559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015561 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015562 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015563 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015564 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015566 if (flags & SP_SUBPAT)
15567 retval = subpatnum;
15568 else
15569 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015570 if (flags & SP_SETPCMARK)
15571 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015573 if (match_pos != NULL)
15574 {
15575 /* Store the match cursor position */
15576 match_pos->lnum = pos.lnum;
15577 match_pos->col = pos.col + 1;
15578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 /* "/$" will put the cursor after the end of the line, may need to
15580 * correct that here */
15581 check_cursor();
15582 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015583
15584 /* If 'n' flag is used: restore cursor position. */
15585 if (flags & SP_NOMOVE)
15586 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015587 else
15588 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015589theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015591
15592 return retval;
15593}
15594
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015595#ifdef FEAT_FLOAT
15596/*
15597 * "round({float})" function
15598 */
15599 static void
15600f_round(argvars, rettv)
15601 typval_T *argvars;
15602 typval_T *rettv;
15603{
15604 float_T f;
15605
15606 rettv->v_type = VAR_FLOAT;
15607 if (get_float_arg(argvars, &f) == OK)
15608 /* round() is not in C90, use ceil() or floor() instead. */
15609 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15610 else
15611 rettv->vval.v_float = 0.0;
15612}
15613#endif
15614
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015615/*
15616 * "search()" function
15617 */
15618 static void
15619f_search(argvars, rettv)
15620 typval_T *argvars;
15621 typval_T *rettv;
15622{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015623 int flags = 0;
15624
15625 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626}
15627
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015629 * "searchdecl()" function
15630 */
15631 static void
15632f_searchdecl(argvars, rettv)
15633 typval_T *argvars;
15634 typval_T *rettv;
15635{
15636 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015637 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015638 int error = FALSE;
15639 char_u *name;
15640
15641 rettv->vval.v_number = 1; /* default: FAIL */
15642
15643 name = get_tv_string_chk(&argvars[0]);
15644 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015645 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015646 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015647 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15648 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15649 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015650 if (!error && name != NULL)
15651 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015652 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015653}
15654
15655/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015656 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015658 static int
15659searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015660 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015661 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662{
15663 char_u *spat, *mpat, *epat;
15664 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 int dir;
15667 int flags = 0;
15668 char_u nbuf1[NUMBUFLEN];
15669 char_u nbuf2[NUMBUFLEN];
15670 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015671 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015672 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015673 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015676 spat = get_tv_string_chk(&argvars[0]);
15677 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15678 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15679 if (spat == NULL || mpat == NULL || epat == NULL)
15680 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 /* Handle the optional fourth argument: flags */
15683 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015684 if (dir == 0)
15685 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015686
15687 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015688 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15689 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015690 if ((flags & (SP_END | SP_SUBPAT)) != 0
15691 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015692 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015693 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015694 goto theend;
15695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015697 /* Using 'r' implies 'W', otherwise it doesn't work. */
15698 if (flags & SP_REPEAT)
15699 p_ws = FALSE;
15700
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015701 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015702 if (argvars[3].v_type == VAR_UNKNOWN
15703 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 skip = (char_u *)"";
15705 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015707 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015708 if (argvars[5].v_type != VAR_UNKNOWN)
15709 {
15710 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15711 if (lnum_stop < 0)
15712 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015713#ifdef FEAT_RELTIME
15714 if (argvars[6].v_type != VAR_UNKNOWN)
15715 {
15716 time_limit = get_tv_number_chk(&argvars[6], NULL);
15717 if (time_limit < 0)
15718 goto theend;
15719 }
15720#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015721 }
15722 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015723 if (skip == NULL)
15724 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015726 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015727 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015728
15729theend:
15730 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015731
15732 return retval;
15733}
15734
15735/*
15736 * "searchpair()" function
15737 */
15738 static void
15739f_searchpair(argvars, rettv)
15740 typval_T *argvars;
15741 typval_T *rettv;
15742{
15743 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15744}
15745
15746/*
15747 * "searchpairpos()" function
15748 */
15749 static void
15750f_searchpairpos(argvars, rettv)
15751 typval_T *argvars;
15752 typval_T *rettv;
15753{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015754 pos_T match_pos;
15755 int lnum = 0;
15756 int col = 0;
15757
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015758 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015759 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015760
15761 if (searchpair_cmn(argvars, &match_pos) > 0)
15762 {
15763 lnum = match_pos.lnum;
15764 col = match_pos.col;
15765 }
15766
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015767 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15768 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015769}
15770
15771/*
15772 * Search for a start/middle/end thing.
15773 * Used by searchpair(), see its documentation for the details.
15774 * Returns 0 or -1 for no match,
15775 */
15776 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015777do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15778 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015779 char_u *spat; /* start pattern */
15780 char_u *mpat; /* middle pattern */
15781 char_u *epat; /* end pattern */
15782 int dir; /* BACKWARD or FORWARD */
15783 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015784 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015785 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015786 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015787 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015788{
15789 char_u *save_cpo;
15790 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15791 long retval = 0;
15792 pos_T pos;
15793 pos_T firstpos;
15794 pos_T foundpos;
15795 pos_T save_cursor;
15796 pos_T save_pos;
15797 int n;
15798 int r;
15799 int nest = 1;
15800 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015801 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015802 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015803
15804 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15805 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015806 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015807
Bram Moolenaar76929292008-01-06 19:07:36 +000015808#ifdef FEAT_RELTIME
15809 /* Set the time limit, if there is one. */
15810 profile_setlimit(time_limit, &tm);
15811#endif
15812
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015813 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15814 * start/middle/end (pat3, for the top pair). */
15815 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15816 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15817 if (pat2 == NULL || pat3 == NULL)
15818 goto theend;
15819 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15820 if (*mpat == NUL)
15821 STRCPY(pat3, pat2);
15822 else
15823 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15824 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015825 if (flags & SP_START)
15826 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015827
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 save_cursor = curwin->w_cursor;
15829 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015830 clearpos(&firstpos);
15831 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832 pat = pat3;
15833 for (;;)
15834 {
15835 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015836 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15838 /* didn't find it or found the first match again: FAIL */
15839 break;
15840
15841 if (firstpos.lnum == 0)
15842 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015843 if (equalpos(pos, foundpos))
15844 {
15845 /* Found the same position again. Can happen with a pattern that
15846 * has "\zs" at the end and searching backwards. Advance one
15847 * character and try again. */
15848 if (dir == BACKWARD)
15849 decl(&pos);
15850 else
15851 incl(&pos);
15852 }
15853 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015855 /* clear the start flag to avoid getting stuck here */
15856 options &= ~SEARCH_START;
15857
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 /* If the skip pattern matches, ignore this match. */
15859 if (*skip != NUL)
15860 {
15861 save_pos = curwin->w_cursor;
15862 curwin->w_cursor = pos;
15863 r = eval_to_bool(skip, &err, NULL, FALSE);
15864 curwin->w_cursor = save_pos;
15865 if (err)
15866 {
15867 /* Evaluating {skip} caused an error, break here. */
15868 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015869 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 break;
15871 }
15872 if (r)
15873 continue;
15874 }
15875
15876 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15877 {
15878 /* Found end when searching backwards or start when searching
15879 * forward: nested pair. */
15880 ++nest;
15881 pat = pat2; /* nested, don't search for middle */
15882 }
15883 else
15884 {
15885 /* Found end when searching forward or start when searching
15886 * backward: end of (nested) pair; or found middle in outer pair. */
15887 if (--nest == 1)
15888 pat = pat3; /* outer level, search for middle */
15889 }
15890
15891 if (nest == 0)
15892 {
15893 /* Found the match: return matchcount or line number. */
15894 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015895 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015897 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015898 if (flags & SP_SETPCMARK)
15899 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 curwin->w_cursor = pos;
15901 if (!(flags & SP_REPEAT))
15902 break;
15903 nest = 1; /* search for next unmatched */
15904 }
15905 }
15906
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015907 if (match_pos != NULL)
15908 {
15909 /* Store the match cursor position */
15910 match_pos->lnum = curwin->w_cursor.lnum;
15911 match_pos->col = curwin->w_cursor.col + 1;
15912 }
15913
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015915 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916 curwin->w_cursor = save_cursor;
15917
15918theend:
15919 vim_free(pat2);
15920 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015921 if (p_cpo == empty_option)
15922 p_cpo = save_cpo;
15923 else
15924 /* Darn, evaluating the {skip} expression changed the value. */
15925 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015926
15927 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928}
15929
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015930/*
15931 * "searchpos()" function
15932 */
15933 static void
15934f_searchpos(argvars, rettv)
15935 typval_T *argvars;
15936 typval_T *rettv;
15937{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015938 pos_T match_pos;
15939 int lnum = 0;
15940 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015941 int n;
15942 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015943
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015944 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015945 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015946
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015947 n = search_cmn(argvars, &match_pos, &flags);
15948 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015949 {
15950 lnum = match_pos.lnum;
15951 col = match_pos.col;
15952 }
15953
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015954 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15955 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015956 if (flags & SP_SUBPAT)
15957 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015958}
15959
15960
Bram Moolenaar0d660222005-01-07 21:51:51 +000015961 static void
15962f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015963 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015966#ifdef FEAT_CLIENTSERVER
15967 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015968 char_u *server = get_tv_string_chk(&argvars[0]);
15969 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970
Bram Moolenaar0d660222005-01-07 21:51:51 +000015971 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015972 if (server == NULL || reply == NULL)
15973 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015974 if (check_restricted() || check_secure())
15975 return;
15976# ifdef FEAT_X11
15977 if (check_connection() == FAIL)
15978 return;
15979# endif
15980
15981 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015983 EMSG(_("E258: Unable to send to client"));
15984 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015985 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015986 rettv->vval.v_number = 0;
15987#else
15988 rettv->vval.v_number = -1;
15989#endif
15990}
15991
Bram Moolenaar0d660222005-01-07 21:51:51 +000015992 static void
15993f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015994 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015995 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015996{
15997 char_u *r = NULL;
15998
15999#ifdef FEAT_CLIENTSERVER
16000# ifdef WIN32
16001 r = serverGetVimNames();
16002# else
16003 make_connection();
16004 if (X_DISPLAY != NULL)
16005 r = serverGetVimNames(X_DISPLAY);
16006# endif
16007#endif
16008 rettv->v_type = VAR_STRING;
16009 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010}
16011
16012/*
16013 * "setbufvar()" function
16014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016016f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016017 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016018 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019{
16020 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016023 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 char_u nbuf[NUMBUFLEN];
16025
16026 if (check_restricted() || check_secure())
16027 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16029 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016030 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 varp = &argvars[2];
16032
16033 if (buf != NULL && varname != NULL && varp != NULL)
16034 {
16035 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037
16038 if (*varname == '&')
16039 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016040 long numval;
16041 char_u *strval;
16042 int error = FALSE;
16043
Bram Moolenaar071d4272004-06-13 20:20:40 +000016044 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016045 numval = get_tv_number_chk(varp, &error);
16046 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016047 if (!error && strval != NULL)
16048 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 }
16050 else
16051 {
16052 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16053 if (bufvarname != NULL)
16054 {
16055 STRCPY(bufvarname, "b:");
16056 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016057 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 vim_free(bufvarname);
16059 }
16060 }
16061
16062 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016065}
16066
16067/*
16068 * "setcmdpos()" function
16069 */
16070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016071f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016072 typval_T *argvars;
16073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016075 int pos = (int)get_tv_number(&argvars[0]) - 1;
16076
16077 if (pos >= 0)
16078 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079}
16080
16081/*
16082 * "setline()" function
16083 */
16084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016085f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 typval_T *argvars;
16087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088{
16089 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016090 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016091 list_T *l = NULL;
16092 listitem_T *li = NULL;
16093 long added = 0;
16094 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016096 lnum = get_tv_lnum(&argvars[0]);
16097 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016099 l = argvars[1].vval.v_list;
16100 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016102 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016103 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016104
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016105 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016106 for (;;)
16107 {
16108 if (l != NULL)
16109 {
16110 /* list argument, get next string */
16111 if (li == NULL)
16112 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016113 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016114 li = li->li_next;
16115 }
16116
16117 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016118 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016119 break;
16120 if (lnum <= curbuf->b_ml.ml_line_count)
16121 {
16122 /* existing line, replace it */
16123 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16124 {
16125 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016126 if (lnum == curwin->w_cursor.lnum)
16127 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016128 rettv->vval.v_number = 0; /* OK */
16129 }
16130 }
16131 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16132 {
16133 /* lnum is one past the last line, append the line */
16134 ++added;
16135 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16136 rettv->vval.v_number = 0; /* OK */
16137 }
16138
16139 if (l == NULL) /* only one string argument */
16140 break;
16141 ++lnum;
16142 }
16143
16144 if (added > 0)
16145 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146}
16147
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016148static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16149
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016151 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016152 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016153 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016154set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016155 win_T *wp UNUSED;
16156 typval_T *list_arg UNUSED;
16157 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016158 typval_T *rettv;
16159{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016160#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016161 char_u *act;
16162 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016163#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016164
Bram Moolenaar2641f772005-03-25 21:58:17 +000016165 rettv->vval.v_number = -1;
16166
16167#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016168 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016169 EMSG(_(e_listreq));
16170 else
16171 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016172 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016173
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016174 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016175 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016176 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016177 if (act == NULL)
16178 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016179 if (*act == 'a' || *act == 'r')
16180 action = *act;
16181 }
16182
Bram Moolenaarbc226b62010-08-09 22:14:48 +020016183 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016184 rettv->vval.v_number = 0;
16185 }
16186#endif
16187}
16188
16189/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016190 * "setloclist()" function
16191 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016192 static void
16193f_setloclist(argvars, rettv)
16194 typval_T *argvars;
16195 typval_T *rettv;
16196{
16197 win_T *win;
16198
16199 rettv->vval.v_number = -1;
16200
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016201 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016202 if (win != NULL)
16203 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16204}
16205
16206/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016207 * "setmatches()" function
16208 */
16209 static void
16210f_setmatches(argvars, rettv)
16211 typval_T *argvars;
16212 typval_T *rettv;
16213{
16214#ifdef FEAT_SEARCH_EXTRA
16215 list_T *l;
16216 listitem_T *li;
16217 dict_T *d;
16218
16219 rettv->vval.v_number = -1;
16220 if (argvars[0].v_type != VAR_LIST)
16221 {
16222 EMSG(_(e_listreq));
16223 return;
16224 }
16225 if ((l = argvars[0].vval.v_list) != NULL)
16226 {
16227
16228 /* To some extent make sure that we are dealing with a list from
16229 * "getmatches()". */
16230 li = l->lv_first;
16231 while (li != NULL)
16232 {
16233 if (li->li_tv.v_type != VAR_DICT
16234 || (d = li->li_tv.vval.v_dict) == NULL)
16235 {
16236 EMSG(_(e_invarg));
16237 return;
16238 }
16239 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16240 && dict_find(d, (char_u *)"pattern", -1) != NULL
16241 && dict_find(d, (char_u *)"priority", -1) != NULL
16242 && dict_find(d, (char_u *)"id", -1) != NULL))
16243 {
16244 EMSG(_(e_invarg));
16245 return;
16246 }
16247 li = li->li_next;
16248 }
16249
16250 clear_matches(curwin);
16251 li = l->lv_first;
16252 while (li != NULL)
16253 {
16254 d = li->li_tv.vval.v_dict;
16255 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16256 get_dict_string(d, (char_u *)"pattern", FALSE),
16257 (int)get_dict_number(d, (char_u *)"priority"),
16258 (int)get_dict_number(d, (char_u *)"id"));
16259 li = li->li_next;
16260 }
16261 rettv->vval.v_number = 0;
16262 }
16263#endif
16264}
16265
16266/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016267 * "setpos()" function
16268 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016269 static void
16270f_setpos(argvars, rettv)
16271 typval_T *argvars;
16272 typval_T *rettv;
16273{
16274 pos_T pos;
16275 int fnum;
16276 char_u *name;
16277
Bram Moolenaar08250432008-02-13 11:42:46 +000016278 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016279 name = get_tv_string_chk(argvars);
16280 if (name != NULL)
16281 {
16282 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16283 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016284 if (--pos.col < 0)
16285 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016286 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016287 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016288 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016289 if (fnum == curbuf->b_fnum)
16290 {
16291 curwin->w_cursor = pos;
16292 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016293 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016294 }
16295 else
16296 EMSG(_(e_invarg));
16297 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016298 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16299 {
16300 /* set mark */
16301 if (setmark_pos(name[1], &pos, fnum) == OK)
16302 rettv->vval.v_number = 0;
16303 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016304 else
16305 EMSG(_(e_invarg));
16306 }
16307 }
16308}
16309
16310/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016311 * "setqflist()" function
16312 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016313 static void
16314f_setqflist(argvars, rettv)
16315 typval_T *argvars;
16316 typval_T *rettv;
16317{
16318 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16319}
16320
16321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322 * "setreg()" function
16323 */
16324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016325f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016326 typval_T *argvars;
16327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328{
16329 int regname;
16330 char_u *strregname;
16331 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016332 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333 int append;
16334 char_u yank_type;
16335 long block_len;
16336
16337 block_len = -1;
16338 yank_type = MAUTO;
16339 append = FALSE;
16340
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016341 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016342 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016344 if (strregname == NULL)
16345 return; /* type error; errmsg already given */
16346 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347 if (regname == 0 || regname == '@')
16348 regname = '"';
16349 else if (regname == '=')
16350 return;
16351
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016352 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016354 stropt = get_tv_string_chk(&argvars[2]);
16355 if (stropt == NULL)
16356 return; /* type error */
16357 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358 switch (*stropt)
16359 {
16360 case 'a': case 'A': /* append */
16361 append = TRUE;
16362 break;
16363 case 'v': case 'c': /* character-wise selection */
16364 yank_type = MCHAR;
16365 break;
16366 case 'V': case 'l': /* line-wise selection */
16367 yank_type = MLINE;
16368 break;
16369#ifdef FEAT_VISUAL
16370 case 'b': case Ctrl_V: /* block-wise selection */
16371 yank_type = MBLOCK;
16372 if (VIM_ISDIGIT(stropt[1]))
16373 {
16374 ++stropt;
16375 block_len = getdigits(&stropt) - 1;
16376 --stropt;
16377 }
16378 break;
16379#endif
16380 }
16381 }
16382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016383 strval = get_tv_string_chk(&argvars[1]);
16384 if (strval != NULL)
16385 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016387 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388}
16389
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016390/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016391 * "settabvar()" function
16392 */
16393 static void
16394f_settabvar(argvars, rettv)
16395 typval_T *argvars;
16396 typval_T *rettv;
16397{
16398 tabpage_T *save_curtab;
16399 char_u *varname, *tabvarname;
16400 typval_T *varp;
16401 tabpage_T *tp;
16402
16403 rettv->vval.v_number = 0;
16404
16405 if (check_restricted() || check_secure())
16406 return;
16407
16408 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16409 varname = get_tv_string_chk(&argvars[1]);
16410 varp = &argvars[2];
16411
16412 if (tp != NULL && varname != NULL && varp != NULL)
16413 {
16414 save_curtab = curtab;
16415 goto_tabpage_tp(tp);
16416
16417 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16418 if (tabvarname != NULL)
16419 {
16420 STRCPY(tabvarname, "t:");
16421 STRCPY(tabvarname + 2, varname);
16422 set_var(tabvarname, varp, TRUE);
16423 vim_free(tabvarname);
16424 }
16425
16426 /* Restore current tabpage */
16427 if (valid_tabpage(save_curtab))
16428 goto_tabpage_tp(save_curtab);
16429 }
16430}
16431
16432/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016433 * "settabwinvar()" function
16434 */
16435 static void
16436f_settabwinvar(argvars, rettv)
16437 typval_T *argvars;
16438 typval_T *rettv;
16439{
16440 setwinvar(argvars, rettv, 1);
16441}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442
16443/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016444 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016447f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016448 typval_T *argvars;
16449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016451 setwinvar(argvars, rettv, 0);
16452}
16453
16454/*
16455 * "setwinvar()" and "settabwinvar()" functions
16456 */
16457 static void
16458setwinvar(argvars, rettv, off)
16459 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016460 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016461 int off;
16462{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 win_T *win;
16464#ifdef FEAT_WINDOWS
16465 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016466 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467#endif
16468 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016469 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016471 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472
16473 if (check_restricted() || check_secure())
16474 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016475
16476#ifdef FEAT_WINDOWS
16477 if (off == 1)
16478 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16479 else
16480 tp = curtab;
16481#endif
16482 win = find_win_by_nr(&argvars[off], tp);
16483 varname = get_tv_string_chk(&argvars[off + 1]);
16484 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485
16486 if (win != NULL && varname != NULL && varp != NULL)
16487 {
16488#ifdef FEAT_WINDOWS
16489 /* set curwin to be our win, temporarily */
16490 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016491 save_curtab = curtab;
16492 goto_tabpage_tp(tp);
16493 if (!win_valid(win))
16494 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 curwin = win;
16496 curbuf = curwin->w_buffer;
16497#endif
16498
16499 if (*varname == '&')
16500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016501 long numval;
16502 char_u *strval;
16503 int error = FALSE;
16504
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016506 numval = get_tv_number_chk(varp, &error);
16507 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016508 if (!error && strval != NULL)
16509 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510 }
16511 else
16512 {
16513 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16514 if (winvarname != NULL)
16515 {
16516 STRCPY(winvarname, "w:");
16517 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016518 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519 vim_free(winvarname);
16520 }
16521 }
16522
16523#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016524 /* Restore current tabpage and window, if still valid (autocomands can
16525 * make them invalid). */
16526 if (valid_tabpage(save_curtab))
16527 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 if (win_valid(save_curwin))
16529 {
16530 curwin = save_curwin;
16531 curbuf = curwin->w_buffer;
16532 }
16533#endif
16534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535}
16536
16537/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016538 * "shellescape({string})" function
16539 */
16540 static void
16541f_shellescape(argvars, rettv)
16542 typval_T *argvars;
16543 typval_T *rettv;
16544{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016545 rettv->vval.v_string = vim_strsave_shellescape(
16546 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016547 rettv->v_type = VAR_STRING;
16548}
16549
16550/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016551 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 */
16553 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016554f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016555 typval_T *argvars;
16556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016558 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559
Bram Moolenaar0d660222005-01-07 21:51:51 +000016560 p = get_tv_string(&argvars[0]);
16561 rettv->vval.v_string = vim_strsave(p);
16562 simplify_filename(rettv->vval.v_string); /* simplify in place */
16563 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564}
16565
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016566#ifdef FEAT_FLOAT
16567/*
16568 * "sin()" function
16569 */
16570 static void
16571f_sin(argvars, rettv)
16572 typval_T *argvars;
16573 typval_T *rettv;
16574{
16575 float_T f;
16576
16577 rettv->v_type = VAR_FLOAT;
16578 if (get_float_arg(argvars, &f) == OK)
16579 rettv->vval.v_float = sin(f);
16580 else
16581 rettv->vval.v_float = 0.0;
16582}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016583
16584/*
16585 * "sinh()" function
16586 */
16587 static void
16588f_sinh(argvars, rettv)
16589 typval_T *argvars;
16590 typval_T *rettv;
16591{
16592 float_T f;
16593
16594 rettv->v_type = VAR_FLOAT;
16595 if (get_float_arg(argvars, &f) == OK)
16596 rettv->vval.v_float = sinh(f);
16597 else
16598 rettv->vval.v_float = 0.0;
16599}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016600#endif
16601
Bram Moolenaar0d660222005-01-07 21:51:51 +000016602static int
16603#ifdef __BORLANDC__
16604 _RTLENTRYF
16605#endif
16606 item_compare __ARGS((const void *s1, const void *s2));
16607static int
16608#ifdef __BORLANDC__
16609 _RTLENTRYF
16610#endif
16611 item_compare2 __ARGS((const void *s1, const void *s2));
16612
16613static int item_compare_ic;
16614static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016615static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016616static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016617#define ITEM_COMPARE_FAIL 999
16618
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016620 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016622 static int
16623#ifdef __BORLANDC__
16624_RTLENTRYF
16625#endif
16626item_compare(s1, s2)
16627 const void *s1;
16628 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016630 char_u *p1, *p2;
16631 char_u *tofree1, *tofree2;
16632 int res;
16633 char_u numbuf1[NUMBUFLEN];
16634 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016636 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16637 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016638 if (p1 == NULL)
16639 p1 = (char_u *)"";
16640 if (p2 == NULL)
16641 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016642 if (item_compare_ic)
16643 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016645 res = STRCMP(p1, p2);
16646 vim_free(tofree1);
16647 vim_free(tofree2);
16648 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649}
16650
16651 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016652#ifdef __BORLANDC__
16653_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016655item_compare2(s1, s2)
16656 const void *s1;
16657 const void *s2;
16658{
16659 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016660 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016661 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016662 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016664 /* shortcut after failure in previous call; compare all items equal */
16665 if (item_compare_func_err)
16666 return 0;
16667
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016668 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16669 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016670 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16671 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016672
16673 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016674 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016675 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16676 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016677 clear_tv(&argv[0]);
16678 clear_tv(&argv[1]);
16679
16680 if (res == FAIL)
16681 res = ITEM_COMPARE_FAIL;
16682 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016683 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16684 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016685 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016686 clear_tv(&rettv);
16687 return res;
16688}
16689
16690/*
16691 * "sort({list})" function
16692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016694f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016695 typval_T *argvars;
16696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697{
Bram Moolenaar33570922005-01-25 22:26:29 +000016698 list_T *l;
16699 listitem_T *li;
16700 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016701 long len;
16702 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703
Bram Moolenaar0d660222005-01-07 21:51:51 +000016704 if (argvars[0].v_type != VAR_LIST)
16705 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706 else
16707 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016708 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016709 if (l == NULL || tv_check_lock(l->lv_lock,
16710 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016711 return;
16712 rettv->vval.v_list = l;
16713 rettv->v_type = VAR_LIST;
16714 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715
Bram Moolenaar0d660222005-01-07 21:51:51 +000016716 len = list_len(l);
16717 if (len <= 1)
16718 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719
Bram Moolenaar0d660222005-01-07 21:51:51 +000016720 item_compare_ic = FALSE;
16721 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016722 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016723 if (argvars[1].v_type != VAR_UNKNOWN)
16724 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016725 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016726 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016727 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016728 else
16729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016730 int error = FALSE;
16731
16732 i = get_tv_number_chk(&argvars[1], &error);
16733 if (error)
16734 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016735 if (i == 1)
16736 item_compare_ic = TRUE;
16737 else
16738 item_compare_func = get_tv_string(&argvars[1]);
16739 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016740
16741 if (argvars[2].v_type != VAR_UNKNOWN)
16742 {
16743 /* optional third argument: {dict} */
16744 if (argvars[2].v_type != VAR_DICT)
16745 {
16746 EMSG(_(e_dictreq));
16747 return;
16748 }
16749 item_compare_selfdict = argvars[2].vval.v_dict;
16750 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752
Bram Moolenaar0d660222005-01-07 21:51:51 +000016753 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016754 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016755 if (ptrs == NULL)
16756 return;
16757 i = 0;
16758 for (li = l->lv_first; li != NULL; li = li->li_next)
16759 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016761 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016762 /* test the compare function */
16763 if (item_compare_func != NULL
16764 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16765 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016766 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016768 {
16769 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016770 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016771 item_compare_func == NULL ? item_compare : item_compare2);
16772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016773 if (!item_compare_func_err)
16774 {
16775 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016776 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016777 l->lv_len = 0;
16778 for (i = 0; i < len; ++i)
16779 list_append(l, ptrs[i]);
16780 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016781 }
16782
16783 vim_free(ptrs);
16784 }
16785}
16786
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016787/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016788 * "soundfold({word})" function
16789 */
16790 static void
16791f_soundfold(argvars, rettv)
16792 typval_T *argvars;
16793 typval_T *rettv;
16794{
16795 char_u *s;
16796
16797 rettv->v_type = VAR_STRING;
16798 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016799#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016800 rettv->vval.v_string = eval_soundfold(s);
16801#else
16802 rettv->vval.v_string = vim_strsave(s);
16803#endif
16804}
16805
16806/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016807 * "spellbadword()" function
16808 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016809 static void
16810f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016811 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016812 typval_T *rettv;
16813{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016814 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016815 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016816 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016817
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016818 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016819 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016820
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016821#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016822 if (argvars[0].v_type == VAR_UNKNOWN)
16823 {
16824 /* Find the start and length of the badly spelled word. */
16825 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16826 if (len != 0)
16827 word = ml_get_cursor();
16828 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016829 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016830 {
16831 char_u *str = get_tv_string_chk(&argvars[0]);
16832 int capcol = -1;
16833
16834 if (str != NULL)
16835 {
16836 /* Check the argument for spelling. */
16837 while (*str != NUL)
16838 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016839 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016840 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016841 {
16842 word = str;
16843 break;
16844 }
16845 str += len;
16846 }
16847 }
16848 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016849#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016850
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016851 list_append_string(rettv->vval.v_list, word, len);
16852 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016853 attr == HLF_SPB ? "bad" :
16854 attr == HLF_SPR ? "rare" :
16855 attr == HLF_SPL ? "local" :
16856 attr == HLF_SPC ? "caps" :
16857 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016858}
16859
16860/*
16861 * "spellsuggest()" function
16862 */
16863 static void
16864f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016865 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016866 typval_T *rettv;
16867{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016868#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016869 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016870 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016871 int maxcount;
16872 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016873 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016874 listitem_T *li;
16875 int need_capital = FALSE;
16876#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016877
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016878 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016879 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016880
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016881#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016882 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016883 {
16884 str = get_tv_string(&argvars[0]);
16885 if (argvars[1].v_type != VAR_UNKNOWN)
16886 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016887 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016888 if (maxcount <= 0)
16889 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016890 if (argvars[2].v_type != VAR_UNKNOWN)
16891 {
16892 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16893 if (typeerr)
16894 return;
16895 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016896 }
16897 else
16898 maxcount = 25;
16899
Bram Moolenaar4770d092006-01-12 23:22:24 +000016900 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016901
16902 for (i = 0; i < ga.ga_len; ++i)
16903 {
16904 str = ((char_u **)ga.ga_data)[i];
16905
16906 li = listitem_alloc();
16907 if (li == NULL)
16908 vim_free(str);
16909 else
16910 {
16911 li->li_tv.v_type = VAR_STRING;
16912 li->li_tv.v_lock = 0;
16913 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016914 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016915 }
16916 }
16917 ga_clear(&ga);
16918 }
16919#endif
16920}
16921
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016923f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016924 typval_T *argvars;
16925 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016926{
16927 char_u *str;
16928 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016929 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 regmatch_T regmatch;
16931 char_u patbuf[NUMBUFLEN];
16932 char_u *save_cpo;
16933 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016934 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016935 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016936 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016937
16938 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16939 save_cpo = p_cpo;
16940 p_cpo = (char_u *)"";
16941
16942 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016943 if (argvars[1].v_type != VAR_UNKNOWN)
16944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016945 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16946 if (pat == NULL)
16947 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016948 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016949 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016950 }
16951 if (pat == NULL || *pat == NUL)
16952 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016953
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016954 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016956 if (typeerr)
16957 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16960 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016963 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016965 if (*str == NUL)
16966 match = FALSE; /* empty item at the end */
16967 else
16968 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969 if (match)
16970 end = regmatch.startp[0];
16971 else
16972 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016973 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16974 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016976 if (list_append_string(rettv->vval.v_list, str,
16977 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979 }
16980 if (!match)
16981 break;
16982 /* Advance to just after the match. */
16983 if (regmatch.endp[0] > str)
16984 col = 0;
16985 else
16986 {
16987 /* Don't get stuck at the same match. */
16988#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016989 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016990#else
16991 col = 1;
16992#endif
16993 }
16994 str = regmatch.endp[0];
16995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001}
17002
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017003#ifdef FEAT_FLOAT
17004/*
17005 * "sqrt()" function
17006 */
17007 static void
17008f_sqrt(argvars, rettv)
17009 typval_T *argvars;
17010 typval_T *rettv;
17011{
17012 float_T f;
17013
17014 rettv->v_type = VAR_FLOAT;
17015 if (get_float_arg(argvars, &f) == OK)
17016 rettv->vval.v_float = sqrt(f);
17017 else
17018 rettv->vval.v_float = 0.0;
17019}
17020
17021/*
17022 * "str2float()" function
17023 */
17024 static void
17025f_str2float(argvars, rettv)
17026 typval_T *argvars;
17027 typval_T *rettv;
17028{
17029 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17030
17031 if (*p == '+')
17032 p = skipwhite(p + 1);
17033 (void)string2float(p, &rettv->vval.v_float);
17034 rettv->v_type = VAR_FLOAT;
17035}
17036#endif
17037
Bram Moolenaar2c932302006-03-18 21:42:09 +000017038/*
17039 * "str2nr()" function
17040 */
17041 static void
17042f_str2nr(argvars, rettv)
17043 typval_T *argvars;
17044 typval_T *rettv;
17045{
17046 int base = 10;
17047 char_u *p;
17048 long n;
17049
17050 if (argvars[1].v_type != VAR_UNKNOWN)
17051 {
17052 base = get_tv_number(&argvars[1]);
17053 if (base != 8 && base != 10 && base != 16)
17054 {
17055 EMSG(_(e_invarg));
17056 return;
17057 }
17058 }
17059
17060 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017061 if (*p == '+')
17062 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017063 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17064 rettv->vval.v_number = n;
17065}
17066
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067#ifdef HAVE_STRFTIME
17068/*
17069 * "strftime({format}[, {time}])" function
17070 */
17071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017072f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017073 typval_T *argvars;
17074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075{
17076 char_u result_buf[256];
17077 struct tm *curtime;
17078 time_t seconds;
17079 char_u *p;
17080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017081 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017083 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017084 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 seconds = time(NULL);
17086 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017087 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 curtime = localtime(&seconds);
17089 /* MSVC returns NULL for an invalid value of seconds. */
17090 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017091 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 else
17093 {
17094# ifdef FEAT_MBYTE
17095 vimconv_T conv;
17096 char_u *enc;
17097
17098 conv.vc_type = CONV_NONE;
17099 enc = enc_locale();
17100 convert_setup(&conv, p_enc, enc);
17101 if (conv.vc_type != CONV_NONE)
17102 p = string_convert(&conv, p, NULL);
17103# endif
17104 if (p != NULL)
17105 (void)strftime((char *)result_buf, sizeof(result_buf),
17106 (char *)p, curtime);
17107 else
17108 result_buf[0] = NUL;
17109
17110# ifdef FEAT_MBYTE
17111 if (conv.vc_type != CONV_NONE)
17112 vim_free(p);
17113 convert_setup(&conv, enc, p_enc);
17114 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017115 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116 else
17117# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017118 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119
17120# ifdef FEAT_MBYTE
17121 /* Release conversion descriptors */
17122 convert_setup(&conv, NULL, NULL);
17123 vim_free(enc);
17124# endif
17125 }
17126}
17127#endif
17128
17129/*
17130 * "stridx()" function
17131 */
17132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017134 typval_T *argvars;
17135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136{
17137 char_u buf[NUMBUFLEN];
17138 char_u *needle;
17139 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017140 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017142 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017144 needle = get_tv_string_chk(&argvars[1]);
17145 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017146 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017147 if (needle == NULL || haystack == NULL)
17148 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149
Bram Moolenaar33570922005-01-25 22:26:29 +000017150 if (argvars[2].v_type != VAR_UNKNOWN)
17151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017152 int error = FALSE;
17153
17154 start_idx = get_tv_number_chk(&argvars[2], &error);
17155 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017156 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017157 if (start_idx >= 0)
17158 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017159 }
17160
17161 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17162 if (pos != NULL)
17163 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164}
17165
17166/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017167 * "string()" function
17168 */
17169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017170f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017171 typval_T *argvars;
17172 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017173{
17174 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017175 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017177 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017178 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017179 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017180 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017181 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182}
17183
17184/*
17185 * "strlen()" function
17186 */
17187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017188f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017189 typval_T *argvars;
17190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017192 rettv->vval.v_number = (varnumber_T)(STRLEN(
17193 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194}
17195
17196/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017197 * "strchars()" function
17198 */
17199 static void
17200f_strchars(argvars, rettv)
17201 typval_T *argvars;
17202 typval_T *rettv;
17203{
17204 char_u *s = get_tv_string(&argvars[0]);
17205#ifdef FEAT_MBYTE
17206 varnumber_T len = 0;
17207
17208 while (*s != NUL)
17209 {
17210 mb_cptr2char_adv(&s);
17211 ++len;
17212 }
17213 rettv->vval.v_number = len;
17214#else
17215 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17216#endif
17217}
17218
17219/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017220 * "strdisplaywidth()" function
17221 */
17222 static void
17223f_strdisplaywidth(argvars, rettv)
17224 typval_T *argvars;
17225 typval_T *rettv;
17226{
17227 char_u *s = get_tv_string(&argvars[0]);
17228 int col = 0;
17229
17230 if (argvars[1].v_type != VAR_UNKNOWN)
17231 col = get_tv_number(&argvars[1]);
17232
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017233 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017234}
17235
17236/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017237 * "strwidth()" function
17238 */
17239 static void
17240f_strwidth(argvars, rettv)
17241 typval_T *argvars;
17242 typval_T *rettv;
17243{
17244 char_u *s = get_tv_string(&argvars[0]);
17245
17246 rettv->vval.v_number = (varnumber_T)(
17247#ifdef FEAT_MBYTE
17248 mb_string2cells(s, -1)
17249#else
17250 STRLEN(s)
17251#endif
17252 );
17253}
17254
17255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256 * "strpart()" function
17257 */
17258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017259f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017260 typval_T *argvars;
17261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262{
17263 char_u *p;
17264 int n;
17265 int len;
17266 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017267 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017269 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270 slen = (int)STRLEN(p);
17271
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017272 n = get_tv_number_chk(&argvars[1], &error);
17273 if (error)
17274 len = 0;
17275 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017276 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277 else
17278 len = slen - n; /* default len: all bytes that are available. */
17279
17280 /*
17281 * Only return the overlap between the specified part and the actual
17282 * string.
17283 */
17284 if (n < 0)
17285 {
17286 len += n;
17287 n = 0;
17288 }
17289 else if (n > slen)
17290 n = slen;
17291 if (len < 0)
17292 len = 0;
17293 else if (n + len > slen)
17294 len = slen - n;
17295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017296 rettv->v_type = VAR_STRING;
17297 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298}
17299
17300/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017301 * "strridx()" function
17302 */
17303 static void
17304f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 typval_T *argvars;
17306 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017307{
17308 char_u buf[NUMBUFLEN];
17309 char_u *needle;
17310 char_u *haystack;
17311 char_u *rest;
17312 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017313 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017315 needle = get_tv_string_chk(&argvars[1]);
17316 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017317
17318 rettv->vval.v_number = -1;
17319 if (needle == NULL || haystack == NULL)
17320 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017321
17322 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017323 if (argvars[2].v_type != VAR_UNKNOWN)
17324 {
17325 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017326 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017327 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017328 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017329 }
17330 else
17331 end_idx = haystack_len;
17332
Bram Moolenaar0d660222005-01-07 21:51:51 +000017333 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017334 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017335 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017336 lastmatch = haystack + end_idx;
17337 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017338 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017339 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017340 for (rest = haystack; *rest != '\0'; ++rest)
17341 {
17342 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017343 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017344 break;
17345 lastmatch = rest;
17346 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017347 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017348
17349 if (lastmatch == NULL)
17350 rettv->vval.v_number = -1;
17351 else
17352 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17353}
17354
17355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356 * "strtrans()" function
17357 */
17358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017359f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017360 typval_T *argvars;
17361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017363 rettv->v_type = VAR_STRING;
17364 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365}
17366
17367/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017368 * "submatch()" function
17369 */
17370 static void
17371f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017372 typval_T *argvars;
17373 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017374{
17375 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017376 rettv->vval.v_string =
17377 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017378}
17379
17380/*
17381 * "substitute()" function
17382 */
17383 static void
17384f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017385 typval_T *argvars;
17386 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017387{
17388 char_u patbuf[NUMBUFLEN];
17389 char_u subbuf[NUMBUFLEN];
17390 char_u flagsbuf[NUMBUFLEN];
17391
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017392 char_u *str = get_tv_string_chk(&argvars[0]);
17393 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17394 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17395 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17396
Bram Moolenaar0d660222005-01-07 21:51:51 +000017397 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017398 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17399 rettv->vval.v_string = NULL;
17400 else
17401 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017402}
17403
17404/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017405 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017408f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017409 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411{
17412 int id = 0;
17413#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017414 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415 long col;
17416 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017417 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017419 lnum = get_tv_lnum(argvars); /* -1 on type error */
17420 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17421 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017423 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017424 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017425 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426#endif
17427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017428 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429}
17430
17431/*
17432 * "synIDattr(id, what [, mode])" function
17433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017435f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017436 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438{
17439 char_u *p = NULL;
17440#ifdef FEAT_SYN_HL
17441 int id;
17442 char_u *what;
17443 char_u *mode;
17444 char_u modebuf[NUMBUFLEN];
17445 int modec;
17446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017447 id = get_tv_number(&argvars[0]);
17448 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017449 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017451 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017453 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454 modec = 0; /* replace invalid with current */
17455 }
17456 else
17457 {
17458#ifdef FEAT_GUI
17459 if (gui.in_use)
17460 modec = 'g';
17461 else
17462#endif
17463 if (t_colors > 1)
17464 modec = 'c';
17465 else
17466 modec = 't';
17467 }
17468
17469
17470 switch (TOLOWER_ASC(what[0]))
17471 {
17472 case 'b':
17473 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17474 p = highlight_color(id, what, modec);
17475 else /* bold */
17476 p = highlight_has_attr(id, HL_BOLD, modec);
17477 break;
17478
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017479 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 p = highlight_color(id, what, modec);
17481 break;
17482
17483 case 'i':
17484 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17485 p = highlight_has_attr(id, HL_INVERSE, modec);
17486 else /* italic */
17487 p = highlight_has_attr(id, HL_ITALIC, modec);
17488 break;
17489
17490 case 'n': /* name */
17491 p = get_highlight_name(NULL, id - 1);
17492 break;
17493
17494 case 'r': /* reverse */
17495 p = highlight_has_attr(id, HL_INVERSE, modec);
17496 break;
17497
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017498 case 's':
17499 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17500 p = highlight_color(id, what, modec);
17501 else /* standout */
17502 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 break;
17504
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017505 case 'u':
17506 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17507 /* underline */
17508 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17509 else
17510 /* undercurl */
17511 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 break;
17513 }
17514
17515 if (p != NULL)
17516 p = vim_strsave(p);
17517#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017518 rettv->v_type = VAR_STRING;
17519 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520}
17521
17522/*
17523 * "synIDtrans(id)" function
17524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017526f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017527 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529{
17530 int id;
17531
17532#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017533 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534
17535 if (id > 0)
17536 id = syn_get_final_id(id);
17537 else
17538#endif
17539 id = 0;
17540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017541 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542}
17543
17544/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017545 * "synconcealed(lnum, col)" function
17546 */
17547 static void
17548f_synconcealed(argvars, rettv)
17549 typval_T *argvars UNUSED;
17550 typval_T *rettv;
17551{
17552#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17553 long lnum;
17554 long col;
17555 int syntax_flags = 0;
17556 int cchar;
17557 int matchid = 0;
17558 char_u str[NUMBUFLEN];
17559#endif
17560
17561 rettv->v_type = VAR_LIST;
17562 rettv->vval.v_list = NULL;
17563
17564#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17565 lnum = get_tv_lnum(argvars); /* -1 on type error */
17566 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17567
17568 vim_memset(str, NUL, sizeof(str));
17569
17570 if (rettv_list_alloc(rettv) != FAIL)
17571 {
17572 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17573 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17574 && curwin->w_p_cole > 0)
17575 {
17576 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17577 syntax_flags = get_syntax_info(&matchid);
17578
17579 /* get the conceal character */
17580 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17581 {
17582 cchar = syn_get_sub_char();
17583 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17584 cchar = lcs_conceal;
17585 if (cchar != NUL)
17586 {
17587# ifdef FEAT_MBYTE
17588 if (has_mbyte)
17589 (*mb_char2bytes)(cchar, str);
17590 else
17591# endif
17592 str[0] = cchar;
17593 }
17594 }
17595 }
17596
17597 list_append_number(rettv->vval.v_list,
17598 (syntax_flags & HL_CONCEAL) != 0);
17599 /* -1 to auto-determine strlen */
17600 list_append_string(rettv->vval.v_list, str, -1);
17601 list_append_number(rettv->vval.v_list, matchid);
17602 }
17603#endif
17604}
17605
17606/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017607 * "synstack(lnum, col)" function
17608 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017609 static void
17610f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017611 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017612 typval_T *rettv;
17613{
17614#ifdef FEAT_SYN_HL
17615 long lnum;
17616 long col;
17617 int i;
17618 int id;
17619#endif
17620
17621 rettv->v_type = VAR_LIST;
17622 rettv->vval.v_list = NULL;
17623
17624#ifdef FEAT_SYN_HL
17625 lnum = get_tv_lnum(argvars); /* -1 on type error */
17626 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17627
17628 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017629 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017630 && rettv_list_alloc(rettv) != FAIL)
17631 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017632 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017633 for (i = 0; ; ++i)
17634 {
17635 id = syn_get_stack_item(i);
17636 if (id < 0)
17637 break;
17638 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17639 break;
17640 }
17641 }
17642#endif
17643}
17644
17645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646 * "system()" function
17647 */
17648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017649f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017650 typval_T *argvars;
17651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017653 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017655 char_u *infile = NULL;
17656 char_u buf[NUMBUFLEN];
17657 int err = FALSE;
17658 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017660 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017661 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017662
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017663 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017664 {
17665 /*
17666 * Write the string to a temp file, to be used for input of the shell
17667 * command.
17668 */
17669 if ((infile = vim_tempname('i')) == NULL)
17670 {
17671 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017672 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017673 }
17674
17675 fd = mch_fopen((char *)infile, WRITEBIN);
17676 if (fd == NULL)
17677 {
17678 EMSG2(_(e_notopen), infile);
17679 goto done;
17680 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017681 p = get_tv_string_buf_chk(&argvars[1], buf);
17682 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017683 {
17684 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017685 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017686 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017687 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17688 err = TRUE;
17689 if (fclose(fd) != 0)
17690 err = TRUE;
17691 if (err)
17692 {
17693 EMSG(_("E677: Error writing temp file"));
17694 goto done;
17695 }
17696 }
17697
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017698 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17699 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017700
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701#ifdef USE_CR
17702 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017703 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 {
17705 char_u *s;
17706
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017707 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 {
17709 if (*s == CAR)
17710 *s = NL;
17711 }
17712 }
17713#else
17714# ifdef USE_CRNL
17715 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017716 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 {
17718 char_u *s, *d;
17719
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017720 d = res;
17721 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 {
17723 if (s[0] == CAR && s[1] == NL)
17724 ++s;
17725 *d++ = *s;
17726 }
17727 *d = NUL;
17728 }
17729# endif
17730#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017731
17732done:
17733 if (infile != NULL)
17734 {
17735 mch_remove(infile);
17736 vim_free(infile);
17737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017738 rettv->v_type = VAR_STRING;
17739 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740}
17741
17742/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017743 * "tabpagebuflist()" function
17744 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017745 static void
17746f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017747 typval_T *argvars UNUSED;
17748 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017749{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017750#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017751 tabpage_T *tp;
17752 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017753
17754 if (argvars[0].v_type == VAR_UNKNOWN)
17755 wp = firstwin;
17756 else
17757 {
17758 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17759 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017760 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017761 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017762 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017763 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017764 for (; wp != NULL; wp = wp->w_next)
17765 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017766 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017767 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017768 }
17769#endif
17770}
17771
17772
17773/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017774 * "tabpagenr()" function
17775 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017776 static void
17777f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017778 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017779 typval_T *rettv;
17780{
17781 int nr = 1;
17782#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017783 char_u *arg;
17784
17785 if (argvars[0].v_type != VAR_UNKNOWN)
17786 {
17787 arg = get_tv_string_chk(&argvars[0]);
17788 nr = 0;
17789 if (arg != NULL)
17790 {
17791 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017792 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017793 else
17794 EMSG2(_(e_invexpr2), arg);
17795 }
17796 }
17797 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017798 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017799#endif
17800 rettv->vval.v_number = nr;
17801}
17802
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017803
17804#ifdef FEAT_WINDOWS
17805static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17806
17807/*
17808 * Common code for tabpagewinnr() and winnr().
17809 */
17810 static int
17811get_winnr(tp, argvar)
17812 tabpage_T *tp;
17813 typval_T *argvar;
17814{
17815 win_T *twin;
17816 int nr = 1;
17817 win_T *wp;
17818 char_u *arg;
17819
17820 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17821 if (argvar->v_type != VAR_UNKNOWN)
17822 {
17823 arg = get_tv_string_chk(argvar);
17824 if (arg == NULL)
17825 nr = 0; /* type error; errmsg already given */
17826 else if (STRCMP(arg, "$") == 0)
17827 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17828 else if (STRCMP(arg, "#") == 0)
17829 {
17830 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17831 if (twin == NULL)
17832 nr = 0;
17833 }
17834 else
17835 {
17836 EMSG2(_(e_invexpr2), arg);
17837 nr = 0;
17838 }
17839 }
17840
17841 if (nr > 0)
17842 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17843 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017844 {
17845 if (wp == NULL)
17846 {
17847 /* didn't find it in this tabpage */
17848 nr = 0;
17849 break;
17850 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017851 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017852 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017853 return nr;
17854}
17855#endif
17856
17857/*
17858 * "tabpagewinnr()" function
17859 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017860 static void
17861f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017862 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017863 typval_T *rettv;
17864{
17865 int nr = 1;
17866#ifdef FEAT_WINDOWS
17867 tabpage_T *tp;
17868
17869 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17870 if (tp == NULL)
17871 nr = 0;
17872 else
17873 nr = get_winnr(tp, &argvars[1]);
17874#endif
17875 rettv->vval.v_number = nr;
17876}
17877
17878
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017879/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017880 * "tagfiles()" function
17881 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017882 static void
17883f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017884 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017885 typval_T *rettv;
17886{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017887 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017888 tagname_T tn;
17889 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017890
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017891 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017892 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017893 fname = alloc(MAXPATHL);
17894 if (fname == NULL)
17895 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017896
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017897 for (first = TRUE; ; first = FALSE)
17898 if (get_tagfname(&tn, first, fname) == FAIL
17899 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017900 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017901 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017902 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017903}
17904
17905/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017906 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017907 */
17908 static void
17909f_taglist(argvars, rettv)
17910 typval_T *argvars;
17911 typval_T *rettv;
17912{
17913 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017914
17915 tag_pattern = get_tv_string(&argvars[0]);
17916
17917 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017918 if (*tag_pattern == NUL)
17919 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017920
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017921 if (rettv_list_alloc(rettv) == OK)
17922 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017923}
17924
17925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926 * "tempname()" function
17927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017929f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017930 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932{
17933 static int x = 'A';
17934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017935 rettv->v_type = VAR_STRING;
17936 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937
17938 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17939 * names. Skip 'I' and 'O', they are used for shell redirection. */
17940 do
17941 {
17942 if (x == 'Z')
17943 x = '0';
17944 else if (x == '9')
17945 x = 'A';
17946 else
17947 {
17948#ifdef EBCDIC
17949 if (x == 'I')
17950 x = 'J';
17951 else if (x == 'R')
17952 x = 'S';
17953 else
17954#endif
17955 ++x;
17956 }
17957 } while (x == 'I' || x == 'O');
17958}
17959
17960/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017961 * "test(list)" function: Just checking the walls...
17962 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017963 static void
17964f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017965 typval_T *argvars UNUSED;
17966 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017967{
17968 /* Used for unit testing. Change the code below to your liking. */
17969#if 0
17970 listitem_T *li;
17971 list_T *l;
17972 char_u *bad, *good;
17973
17974 if (argvars[0].v_type != VAR_LIST)
17975 return;
17976 l = argvars[0].vval.v_list;
17977 if (l == NULL)
17978 return;
17979 li = l->lv_first;
17980 if (li == NULL)
17981 return;
17982 bad = get_tv_string(&li->li_tv);
17983 li = li->li_next;
17984 if (li == NULL)
17985 return;
17986 good = get_tv_string(&li->li_tv);
17987 rettv->vval.v_number = test_edit_score(bad, good);
17988#endif
17989}
17990
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017991#ifdef FEAT_FLOAT
17992/*
17993 * "tan()" function
17994 */
17995 static void
17996f_tan(argvars, rettv)
17997 typval_T *argvars;
17998 typval_T *rettv;
17999{
18000 float_T f;
18001
18002 rettv->v_type = VAR_FLOAT;
18003 if (get_float_arg(argvars, &f) == OK)
18004 rettv->vval.v_float = tan(f);
18005 else
18006 rettv->vval.v_float = 0.0;
18007}
18008
18009/*
18010 * "tanh()" function
18011 */
18012 static void
18013f_tanh(argvars, rettv)
18014 typval_T *argvars;
18015 typval_T *rettv;
18016{
18017 float_T f;
18018
18019 rettv->v_type = VAR_FLOAT;
18020 if (get_float_arg(argvars, &f) == OK)
18021 rettv->vval.v_float = tanh(f);
18022 else
18023 rettv->vval.v_float = 0.0;
18024}
18025#endif
18026
Bram Moolenaard52d9742005-08-21 22:20:28 +000018027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028 * "tolower(string)" function
18029 */
18030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018031f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018032 typval_T *argvars;
18033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034{
18035 char_u *p;
18036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018037 p = vim_strsave(get_tv_string(&argvars[0]));
18038 rettv->v_type = VAR_STRING;
18039 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040
18041 if (p != NULL)
18042 while (*p != NUL)
18043 {
18044#ifdef FEAT_MBYTE
18045 int l;
18046
18047 if (enc_utf8)
18048 {
18049 int c, lc;
18050
18051 c = utf_ptr2char(p);
18052 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018053 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 /* TODO: reallocate string when byte count changes. */
18055 if (utf_char2len(lc) == l)
18056 utf_char2bytes(lc, p);
18057 p += l;
18058 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018059 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 p += l; /* skip multi-byte character */
18061 else
18062#endif
18063 {
18064 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18065 ++p;
18066 }
18067 }
18068}
18069
18070/*
18071 * "toupper(string)" function
18072 */
18073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018074f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018075 typval_T *argvars;
18076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018078 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018079 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080}
18081
18082/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018083 * "tr(string, fromstr, tostr)" function
18084 */
18085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018086f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018087 typval_T *argvars;
18088 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018089{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018090 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018091 char_u *fromstr;
18092 char_u *tostr;
18093 char_u *p;
18094#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018095 int inlen;
18096 int fromlen;
18097 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018098 int idx;
18099 char_u *cpstr;
18100 int cplen;
18101 int first = TRUE;
18102#endif
18103 char_u buf[NUMBUFLEN];
18104 char_u buf2[NUMBUFLEN];
18105 garray_T ga;
18106
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018107 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018108 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18109 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018110
18111 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018112 rettv->v_type = VAR_STRING;
18113 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018114 if (fromstr == NULL || tostr == NULL)
18115 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018116 ga_init2(&ga, (int)sizeof(char), 80);
18117
18118#ifdef FEAT_MBYTE
18119 if (!has_mbyte)
18120#endif
18121 /* not multi-byte: fromstr and tostr must be the same length */
18122 if (STRLEN(fromstr) != STRLEN(tostr))
18123 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018124#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018125error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018126#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018127 EMSG2(_(e_invarg2), fromstr);
18128 ga_clear(&ga);
18129 return;
18130 }
18131
18132 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018133 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018134 {
18135#ifdef FEAT_MBYTE
18136 if (has_mbyte)
18137 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018138 inlen = (*mb_ptr2len)(in_str);
18139 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018140 cplen = inlen;
18141 idx = 0;
18142 for (p = fromstr; *p != NUL; p += fromlen)
18143 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018144 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018145 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018146 {
18147 for (p = tostr; *p != NUL; p += tolen)
18148 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018149 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018150 if (idx-- == 0)
18151 {
18152 cplen = tolen;
18153 cpstr = p;
18154 break;
18155 }
18156 }
18157 if (*p == NUL) /* tostr is shorter than fromstr */
18158 goto error;
18159 break;
18160 }
18161 ++idx;
18162 }
18163
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018164 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018165 {
18166 /* Check that fromstr and tostr have the same number of
18167 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018168 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018169 first = FALSE;
18170 for (p = tostr; *p != NUL; p += tolen)
18171 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018172 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018173 --idx;
18174 }
18175 if (idx != 0)
18176 goto error;
18177 }
18178
18179 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018180 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018181 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018182
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018183 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018184 }
18185 else
18186#endif
18187 {
18188 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018189 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018190 if (p != NULL)
18191 ga_append(&ga, tostr[p - fromstr]);
18192 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018193 ga_append(&ga, *in_str);
18194 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018195 }
18196 }
18197
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018198 /* add a terminating NUL */
18199 ga_grow(&ga, 1);
18200 ga_append(&ga, NUL);
18201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018202 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018203}
18204
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018205#ifdef FEAT_FLOAT
18206/*
18207 * "trunc({float})" function
18208 */
18209 static void
18210f_trunc(argvars, rettv)
18211 typval_T *argvars;
18212 typval_T *rettv;
18213{
18214 float_T f;
18215
18216 rettv->v_type = VAR_FLOAT;
18217 if (get_float_arg(argvars, &f) == OK)
18218 /* trunc() is not in C90, use floor() or ceil() instead. */
18219 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18220 else
18221 rettv->vval.v_float = 0.0;
18222}
18223#endif
18224
Bram Moolenaar8299df92004-07-10 09:47:34 +000018225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226 * "type(expr)" function
18227 */
18228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018229f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018230 typval_T *argvars;
18231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018233 int n;
18234
18235 switch (argvars[0].v_type)
18236 {
18237 case VAR_NUMBER: n = 0; break;
18238 case VAR_STRING: n = 1; break;
18239 case VAR_FUNC: n = 2; break;
18240 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018241 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018242#ifdef FEAT_FLOAT
18243 case VAR_FLOAT: n = 5; break;
18244#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018245 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18246 }
18247 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248}
18249
18250/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018251 * "undofile(name)" function
18252 */
18253 static void
18254f_undofile(argvars, rettv)
18255 typval_T *argvars;
18256 typval_T *rettv;
18257{
18258 rettv->v_type = VAR_STRING;
18259#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018260 {
18261 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
18262
18263 if (ffname != NULL)
18264 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18265 vim_free(ffname);
18266 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018267#else
18268 rettv->vval.v_string = NULL;
18269#endif
18270}
18271
18272/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018273 * "undotree()" function
18274 */
18275 static void
18276f_undotree(argvars, rettv)
18277 typval_T *argvars UNUSED;
18278 typval_T *rettv;
18279{
18280 if (rettv_dict_alloc(rettv) == OK)
18281 {
18282 dict_T *dict = rettv->vval.v_dict;
18283 list_T *list;
18284
Bram Moolenaar730cde92010-06-27 05:18:54 +020018285 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018286 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018287 dict_add_nr_str(dict, "save_last",
18288 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018289 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18290 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018291 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018292
18293 list = list_alloc();
18294 if (list != NULL)
18295 {
18296 u_eval_tree(curbuf->b_u_oldhead, list);
18297 dict_add_list(dict, "entries", list);
18298 }
18299 }
18300}
18301
18302/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018303 * "values(dict)" function
18304 */
18305 static void
18306f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018307 typval_T *argvars;
18308 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018309{
18310 dict_list(argvars, rettv, 1);
18311}
18312
18313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 * "virtcol(string)" function
18315 */
18316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018317f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018318 typval_T *argvars;
18319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320{
18321 colnr_T vcol = 0;
18322 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018323 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018325 fp = var2fpos(&argvars[0], FALSE, &fnum);
18326 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18327 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328 {
18329 getvvcol(curwin, fp, NULL, NULL, &vcol);
18330 ++vcol;
18331 }
18332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018333 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334}
18335
18336/*
18337 * "visualmode()" function
18338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018340f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018341 typval_T *argvars UNUSED;
18342 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343{
18344#ifdef FEAT_VISUAL
18345 char_u str[2];
18346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018347 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348 str[0] = curbuf->b_visual_mode_eval;
18349 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018350 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351
18352 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018353 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355#endif
18356}
18357
18358/*
18359 * "winbufnr(nr)" function
18360 */
18361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018362f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018363 typval_T *argvars;
18364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365{
18366 win_T *wp;
18367
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018368 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018370 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018372 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373}
18374
18375/*
18376 * "wincol()" function
18377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018379f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018380 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382{
18383 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018384 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385}
18386
18387/*
18388 * "winheight(nr)" function
18389 */
18390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018391f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018392 typval_T *argvars;
18393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394{
18395 win_T *wp;
18396
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018397 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018399 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018401 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402}
18403
18404/*
18405 * "winline()" function
18406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018408f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018409 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411{
18412 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018413 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414}
18415
18416/*
18417 * "winnr()" function
18418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018420f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018421 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423{
18424 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018425
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018427 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430}
18431
18432/*
18433 * "winrestcmd()" function
18434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018436f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018437 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439{
18440#ifdef FEAT_WINDOWS
18441 win_T *wp;
18442 int winnr = 1;
18443 garray_T ga;
18444 char_u buf[50];
18445
18446 ga_init2(&ga, (int)sizeof(char), 70);
18447 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18448 {
18449 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18450 ga_concat(&ga, buf);
18451# ifdef FEAT_VERTSPLIT
18452 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18453 ga_concat(&ga, buf);
18454# endif
18455 ++winnr;
18456 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018457 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018459 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018461 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018463 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464}
18465
18466/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018467 * "winrestview()" function
18468 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018469 static void
18470f_winrestview(argvars, rettv)
18471 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018472 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018473{
18474 dict_T *dict;
18475
18476 if (argvars[0].v_type != VAR_DICT
18477 || (dict = argvars[0].vval.v_dict) == NULL)
18478 EMSG(_(e_invarg));
18479 else
18480 {
18481 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18482 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18483#ifdef FEAT_VIRTUALEDIT
18484 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18485#endif
18486 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018487 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018488
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018489 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018490#ifdef FEAT_DIFF
18491 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18492#endif
18493 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18494 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18495
18496 check_cursor();
18497 changed_cline_bef_curs();
18498 invalidate_botline();
18499 redraw_later(VALID);
18500
18501 if (curwin->w_topline == 0)
18502 curwin->w_topline = 1;
18503 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18504 curwin->w_topline = curbuf->b_ml.ml_line_count;
18505#ifdef FEAT_DIFF
18506 check_topfill(curwin, TRUE);
18507#endif
18508 }
18509}
18510
18511/*
18512 * "winsaveview()" function
18513 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018514 static void
18515f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018516 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018517 typval_T *rettv;
18518{
18519 dict_T *dict;
18520
Bram Moolenaara800b422010-06-27 01:15:55 +020018521 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018522 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018523 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018524
18525 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18526 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18527#ifdef FEAT_VIRTUALEDIT
18528 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18529#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018530 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018531 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18532
18533 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18534#ifdef FEAT_DIFF
18535 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18536#endif
18537 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18538 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18539}
18540
18541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 * "winwidth(nr)" function
18543 */
18544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018545f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018546 typval_T *argvars;
18547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548{
18549 win_T *wp;
18550
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018551 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554 else
18555#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018556 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018558 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559#endif
18560}
18561
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018563 * "writefile()" function
18564 */
18565 static void
18566f_writefile(argvars, rettv)
18567 typval_T *argvars;
18568 typval_T *rettv;
18569{
18570 int binary = FALSE;
18571 char_u *fname;
18572 FILE *fd;
18573 listitem_T *li;
18574 char_u *s;
18575 int ret = 0;
18576 int c;
18577
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018578 if (check_restricted() || check_secure())
18579 return;
18580
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018581 if (argvars[0].v_type != VAR_LIST)
18582 {
18583 EMSG2(_(e_listarg), "writefile()");
18584 return;
18585 }
18586 if (argvars[0].vval.v_list == NULL)
18587 return;
18588
18589 if (argvars[2].v_type != VAR_UNKNOWN
18590 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18591 binary = TRUE;
18592
18593 /* Always open the file in binary mode, library functions have a mind of
18594 * their own about CR-LF conversion. */
18595 fname = get_tv_string(&argvars[1]);
18596 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18597 {
18598 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18599 ret = -1;
18600 }
18601 else
18602 {
18603 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18604 li = li->li_next)
18605 {
18606 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18607 {
18608 if (*s == '\n')
18609 c = putc(NUL, fd);
18610 else
18611 c = putc(*s, fd);
18612 if (c == EOF)
18613 {
18614 ret = -1;
18615 break;
18616 }
18617 }
18618 if (!binary || li->li_next != NULL)
18619 if (putc('\n', fd) == EOF)
18620 {
18621 ret = -1;
18622 break;
18623 }
18624 if (ret < 0)
18625 {
18626 EMSG(_(e_write));
18627 break;
18628 }
18629 }
18630 fclose(fd);
18631 }
18632
18633 rettv->vval.v_number = ret;
18634}
18635
18636/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018637 * "xor(expr, expr)" function
18638 */
18639 static void
18640f_xor(argvars, rettv)
18641 typval_T *argvars;
18642 typval_T *rettv;
18643{
18644 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18645 ^ get_tv_number_chk(&argvars[1], NULL);
18646}
18647
18648
18649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018651 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 */
18653 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018654var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018655 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018656 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018657 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018659 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018661 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662
Bram Moolenaara5525202006-03-02 22:52:09 +000018663 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018664 if (varp->v_type == VAR_LIST)
18665 {
18666 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018667 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018668 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018669 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018670
18671 l = varp->vval.v_list;
18672 if (l == NULL)
18673 return NULL;
18674
18675 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018676 pos.lnum = list_find_nr(l, 0L, &error);
18677 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018678 return NULL; /* invalid line number */
18679
18680 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018681 pos.col = list_find_nr(l, 1L, &error);
18682 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018683 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018684 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018685
18686 /* We accept "$" for the column number: last column. */
18687 li = list_find(l, 1L);
18688 if (li != NULL && li->li_tv.v_type == VAR_STRING
18689 && li->li_tv.vval.v_string != NULL
18690 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18691 pos.col = len + 1;
18692
Bram Moolenaara5525202006-03-02 22:52:09 +000018693 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018694 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018695 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018696 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018697
Bram Moolenaara5525202006-03-02 22:52:09 +000018698#ifdef FEAT_VIRTUALEDIT
18699 /* Get the virtual offset. Defaults to zero. */
18700 pos.coladd = list_find_nr(l, 2L, &error);
18701 if (error)
18702 pos.coladd = 0;
18703#endif
18704
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018705 return &pos;
18706 }
18707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018708 name = get_tv_string_chk(varp);
18709 if (name == NULL)
18710 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018711 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018713#ifdef FEAT_VISUAL
18714 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18715 {
18716 if (VIsual_active)
18717 return &VIsual;
18718 return &curwin->w_cursor;
18719 }
18720#endif
18721 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018723 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18725 return NULL;
18726 return pp;
18727 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018728
18729#ifdef FEAT_VIRTUALEDIT
18730 pos.coladd = 0;
18731#endif
18732
Bram Moolenaar477933c2007-07-17 14:32:23 +000018733 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018734 {
18735 pos.col = 0;
18736 if (name[1] == '0') /* "w0": first visible line */
18737 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018738 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018739 pos.lnum = curwin->w_topline;
18740 return &pos;
18741 }
18742 else if (name[1] == '$') /* "w$": last visible line */
18743 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018744 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018745 pos.lnum = curwin->w_botline - 1;
18746 return &pos;
18747 }
18748 }
18749 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018751 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 {
18753 pos.lnum = curbuf->b_ml.ml_line_count;
18754 pos.col = 0;
18755 }
18756 else
18757 {
18758 pos.lnum = curwin->w_cursor.lnum;
18759 pos.col = (colnr_T)STRLEN(ml_get_curline());
18760 }
18761 return &pos;
18762 }
18763 return NULL;
18764}
18765
18766/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018767 * Convert list in "arg" into a position and optional file number.
18768 * When "fnump" is NULL there is no file number, only 3 items.
18769 * Note that the column is passed on as-is, the caller may want to decrement
18770 * it to use 1 for the first column.
18771 * Return FAIL when conversion is not possible, doesn't check the position for
18772 * validity.
18773 */
18774 static int
18775list2fpos(arg, posp, fnump)
18776 typval_T *arg;
18777 pos_T *posp;
18778 int *fnump;
18779{
18780 list_T *l = arg->vval.v_list;
18781 long i = 0;
18782 long n;
18783
Bram Moolenaarbde35262006-07-23 20:12:24 +000018784 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18785 * when "fnump" isn't NULL and "coladd" is optional. */
18786 if (arg->v_type != VAR_LIST
18787 || l == NULL
18788 || l->lv_len < (fnump == NULL ? 2 : 3)
18789 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018790 return FAIL;
18791
18792 if (fnump != NULL)
18793 {
18794 n = list_find_nr(l, i++, NULL); /* fnum */
18795 if (n < 0)
18796 return FAIL;
18797 if (n == 0)
18798 n = curbuf->b_fnum; /* current buffer */
18799 *fnump = n;
18800 }
18801
18802 n = list_find_nr(l, i++, NULL); /* lnum */
18803 if (n < 0)
18804 return FAIL;
18805 posp->lnum = n;
18806
18807 n = list_find_nr(l, i++, NULL); /* col */
18808 if (n < 0)
18809 return FAIL;
18810 posp->col = n;
18811
18812#ifdef FEAT_VIRTUALEDIT
18813 n = list_find_nr(l, i, NULL);
18814 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018815 posp->coladd = 0;
18816 else
18817 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018818#endif
18819
18820 return OK;
18821}
18822
18823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 * Get the length of an environment variable name.
18825 * Advance "arg" to the first character after the name.
18826 * Return 0 for error.
18827 */
18828 static int
18829get_env_len(arg)
18830 char_u **arg;
18831{
18832 char_u *p;
18833 int len;
18834
18835 for (p = *arg; vim_isIDc(*p); ++p)
18836 ;
18837 if (p == *arg) /* no name found */
18838 return 0;
18839
18840 len = (int)(p - *arg);
18841 *arg = p;
18842 return len;
18843}
18844
18845/*
18846 * Get the length of the name of a function or internal variable.
18847 * "arg" is advanced to the first non-white character after the name.
18848 * Return 0 if something is wrong.
18849 */
18850 static int
18851get_id_len(arg)
18852 char_u **arg;
18853{
18854 char_u *p;
18855 int len;
18856
18857 /* Find the end of the name. */
18858 for (p = *arg; eval_isnamec(*p); ++p)
18859 ;
18860 if (p == *arg) /* no name found */
18861 return 0;
18862
18863 len = (int)(p - *arg);
18864 *arg = skipwhite(p);
18865
18866 return len;
18867}
18868
18869/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018870 * Get the length of the name of a variable or function.
18871 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018873 * Return -1 if curly braces expansion failed.
18874 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 * If the name contains 'magic' {}'s, expand them and return the
18876 * expanded name in an allocated string via 'alias' - caller must free.
18877 */
18878 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018879get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 char_u **arg;
18881 char_u **alias;
18882 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018883 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884{
18885 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886 char_u *p;
18887 char_u *expr_start;
18888 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889
18890 *alias = NULL; /* default to no alias */
18891
18892 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18893 && (*arg)[2] == (int)KE_SNR)
18894 {
18895 /* hard coded <SNR>, already translated */
18896 *arg += 3;
18897 return get_id_len(arg) + 3;
18898 }
18899 len = eval_fname_script(*arg);
18900 if (len > 0)
18901 {
18902 /* literal "<SID>", "s:" or "<SNR>" */
18903 *arg += len;
18904 }
18905
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018907 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018909 p = find_name_end(*arg, &expr_start, &expr_end,
18910 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 if (expr_start != NULL)
18912 {
18913 char_u *temp_string;
18914
18915 if (!evaluate)
18916 {
18917 len += (int)(p - *arg);
18918 *arg = skipwhite(p);
18919 return len;
18920 }
18921
18922 /*
18923 * Include any <SID> etc in the expanded string:
18924 * Thus the -len here.
18925 */
18926 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18927 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018928 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929 *alias = temp_string;
18930 *arg = skipwhite(p);
18931 return (int)STRLEN(temp_string);
18932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933
18934 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018935 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936 EMSG2(_(e_invexpr2), *arg);
18937
18938 return len;
18939}
18940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018941/*
18942 * Find the end of a variable or function name, taking care of magic braces.
18943 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18944 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018945 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018946 * Return a pointer to just after the name. Equal to "arg" if there is no
18947 * valid name.
18948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018950find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951 char_u *arg;
18952 char_u **expr_start;
18953 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018954 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018956 int mb_nest = 0;
18957 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 char_u *p;
18959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018960 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018962 *expr_start = NULL;
18963 *expr_end = NULL;
18964 }
18965
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018966 /* Quick check for valid starting character. */
18967 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18968 return arg;
18969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018970 for (p = arg; *p != NUL
18971 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018972 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018973 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018974 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018975 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018976 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018977 if (*p == '\'')
18978 {
18979 /* skip over 'string' to avoid counting [ and ] inside it. */
18980 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18981 ;
18982 if (*p == NUL)
18983 break;
18984 }
18985 else if (*p == '"')
18986 {
18987 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18988 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18989 if (*p == '\\' && p[1] != NUL)
18990 ++p;
18991 if (*p == NUL)
18992 break;
18993 }
18994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018995 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018997 if (*p == '[')
18998 ++br_nest;
18999 else if (*p == ']')
19000 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019003 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019005 if (*p == '{')
19006 {
19007 mb_nest++;
19008 if (expr_start != NULL && *expr_start == NULL)
19009 *expr_start = p;
19010 }
19011 else if (*p == '}')
19012 {
19013 mb_nest--;
19014 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19015 *expr_end = p;
19016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 }
19019
19020 return p;
19021}
19022
19023/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019024 * Expands out the 'magic' {}'s in a variable/function name.
19025 * Note that this can call itself recursively, to deal with
19026 * constructs like foo{bar}{baz}{bam}
19027 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19028 * "in_start" ^
19029 * "expr_start" ^
19030 * "expr_end" ^
19031 * "in_end" ^
19032 *
19033 * Returns a new allocated string, which the caller must free.
19034 * Returns NULL for failure.
19035 */
19036 static char_u *
19037make_expanded_name(in_start, expr_start, expr_end, in_end)
19038 char_u *in_start;
19039 char_u *expr_start;
19040 char_u *expr_end;
19041 char_u *in_end;
19042{
19043 char_u c1;
19044 char_u *retval = NULL;
19045 char_u *temp_result;
19046 char_u *nextcmd = NULL;
19047
19048 if (expr_end == NULL || in_end == NULL)
19049 return NULL;
19050 *expr_start = NUL;
19051 *expr_end = NUL;
19052 c1 = *in_end;
19053 *in_end = NUL;
19054
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019055 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019056 if (temp_result != NULL && nextcmd == NULL)
19057 {
19058 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19059 + (in_end - expr_end) + 1));
19060 if (retval != NULL)
19061 {
19062 STRCPY(retval, in_start);
19063 STRCAT(retval, temp_result);
19064 STRCAT(retval, expr_end + 1);
19065 }
19066 }
19067 vim_free(temp_result);
19068
19069 *in_end = c1; /* put char back for error messages */
19070 *expr_start = '{';
19071 *expr_end = '}';
19072
19073 if (retval != NULL)
19074 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019075 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019076 if (expr_start != NULL)
19077 {
19078 /* Further expansion! */
19079 temp_result = make_expanded_name(retval, expr_start,
19080 expr_end, temp_result);
19081 vim_free(retval);
19082 retval = temp_result;
19083 }
19084 }
19085
19086 return retval;
19087}
19088
19089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019091 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092 */
19093 static int
19094eval_isnamec(c)
19095 int c;
19096{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019097 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19098}
19099
19100/*
19101 * Return TRUE if character "c" can be used as the first character in a
19102 * variable or function name (excluding '{' and '}').
19103 */
19104 static int
19105eval_isnamec1(c)
19106 int c;
19107{
19108 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109}
19110
19111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 * Set number v: variable to "val".
19113 */
19114 void
19115set_vim_var_nr(idx, val)
19116 int idx;
19117 long val;
19118{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019119 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120}
19121
19122/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019123 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124 */
19125 long
19126get_vim_var_nr(idx)
19127 int idx;
19128{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019129 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130}
19131
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019132/*
19133 * Get string v: variable value. Uses a static buffer, can only be used once.
19134 */
19135 char_u *
19136get_vim_var_str(idx)
19137 int idx;
19138{
19139 return get_tv_string(&vimvars[idx].vv_tv);
19140}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019141
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019143 * Get List v: variable value. Caller must take care of reference count when
19144 * needed.
19145 */
19146 list_T *
19147get_vim_var_list(idx)
19148 int idx;
19149{
19150 return vimvars[idx].vv_list;
19151}
19152
19153/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019154 * Set v:char to character "c".
19155 */
19156 void
19157set_vim_var_char(c)
19158 int c;
19159{
19160#ifdef FEAT_MBYTE
19161 char_u buf[MB_MAXBYTES];
19162#else
19163 char_u buf[2];
19164#endif
19165
19166#ifdef FEAT_MBYTE
19167 if (has_mbyte)
19168 buf[(*mb_char2bytes)(c, buf)] = NUL;
19169 else
19170#endif
19171 {
19172 buf[0] = c;
19173 buf[1] = NUL;
19174 }
19175 set_vim_var_string(VV_CHAR, buf, -1);
19176}
19177
19178/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019179 * Set v:count to "count" and v:count1 to "count1".
19180 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181 */
19182 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019183set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 long count;
19185 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019186 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019188 if (set_prevcount)
19189 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019190 vimvars[VV_COUNT].vv_nr = count;
19191 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192}
19193
19194/*
19195 * Set string v: variable to a copy of "val".
19196 */
19197 void
19198set_vim_var_string(idx, val, len)
19199 int idx;
19200 char_u *val;
19201 int len; /* length of "val" to use or -1 (whole string) */
19202{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019203 /* Need to do this (at least) once, since we can't initialize a union.
19204 * Will always be invoked when "v:progname" is set. */
19205 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19206
Bram Moolenaare9a41262005-01-15 22:18:47 +000019207 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019209 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019211 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019213 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214}
19215
19216/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019217 * Set List v: variable to "val".
19218 */
19219 void
19220set_vim_var_list(idx, val)
19221 int idx;
19222 list_T *val;
19223{
19224 list_unref(vimvars[idx].vv_list);
19225 vimvars[idx].vv_list = val;
19226 if (val != NULL)
19227 ++val->lv_refcount;
19228}
19229
19230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 * Set v:register if needed.
19232 */
19233 void
19234set_reg_var(c)
19235 int c;
19236{
19237 char_u regname;
19238
19239 if (c == 0 || c == ' ')
19240 regname = '"';
19241 else
19242 regname = c;
19243 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019244 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 set_vim_var_string(VV_REG, &regname, 1);
19246}
19247
19248/*
19249 * Get or set v:exception. If "oldval" == NULL, return the current value.
19250 * Otherwise, restore the value to "oldval" and return NULL.
19251 * Must always be called in pairs to save and restore v:exception! Does not
19252 * take care of memory allocations.
19253 */
19254 char_u *
19255v_exception(oldval)
19256 char_u *oldval;
19257{
19258 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019259 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260
Bram Moolenaare9a41262005-01-15 22:18:47 +000019261 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 return NULL;
19263}
19264
19265/*
19266 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19267 * Otherwise, restore the value to "oldval" and return NULL.
19268 * Must always be called in pairs to save and restore v:throwpoint! Does not
19269 * take care of memory allocations.
19270 */
19271 char_u *
19272v_throwpoint(oldval)
19273 char_u *oldval;
19274{
19275 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019276 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277
Bram Moolenaare9a41262005-01-15 22:18:47 +000019278 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279 return NULL;
19280}
19281
19282#if defined(FEAT_AUTOCMD) || defined(PROTO)
19283/*
19284 * Set v:cmdarg.
19285 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19286 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19287 * Must always be called in pairs!
19288 */
19289 char_u *
19290set_cmdarg(eap, oldarg)
19291 exarg_T *eap;
19292 char_u *oldarg;
19293{
19294 char_u *oldval;
19295 char_u *newval;
19296 unsigned len;
19297
Bram Moolenaare9a41262005-01-15 22:18:47 +000019298 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019299 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019301 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019302 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019303 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 }
19305
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019306 if (eap->force_bin == FORCE_BIN)
19307 len = 6;
19308 else if (eap->force_bin == FORCE_NOBIN)
19309 len = 8;
19310 else
19311 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019312
19313 if (eap->read_edit)
19314 len += 7;
19315
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019316 if (eap->force_ff != 0)
19317 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19318# ifdef FEAT_MBYTE
19319 if (eap->force_enc != 0)
19320 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019321 if (eap->bad_char != 0)
19322 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019323# endif
19324
19325 newval = alloc(len + 1);
19326 if (newval == NULL)
19327 return NULL;
19328
19329 if (eap->force_bin == FORCE_BIN)
19330 sprintf((char *)newval, " ++bin");
19331 else if (eap->force_bin == FORCE_NOBIN)
19332 sprintf((char *)newval, " ++nobin");
19333 else
19334 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019335
19336 if (eap->read_edit)
19337 STRCAT(newval, " ++edit");
19338
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019339 if (eap->force_ff != 0)
19340 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19341 eap->cmd + eap->force_ff);
19342# ifdef FEAT_MBYTE
19343 if (eap->force_enc != 0)
19344 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19345 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019346 if (eap->bad_char == BAD_KEEP)
19347 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19348 else if (eap->bad_char == BAD_DROP)
19349 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19350 else if (eap->bad_char != 0)
19351 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019352# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019353 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019354 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355}
19356#endif
19357
19358/*
19359 * Get the value of internal variable "name".
19360 * Return OK or FAIL.
19361 */
19362 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019363get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 char_u *name;
19365 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019366 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019367 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368{
19369 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019370 typval_T *tv = NULL;
19371 typval_T atv;
19372 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374
19375 /* truncate the name, so that we can use strcmp() */
19376 cc = name[len];
19377 name[len] = NUL;
19378
19379 /*
19380 * Check for "b:changedtick".
19381 */
19382 if (STRCMP(name, "b:changedtick") == 0)
19383 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019384 atv.v_type = VAR_NUMBER;
19385 atv.vval.v_number = curbuf->b_changedtick;
19386 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 }
19388
19389 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390 * Check for user-defined variables.
19391 */
19392 else
19393 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019394 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019395 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019396 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 }
19398
Bram Moolenaare9a41262005-01-15 22:18:47 +000019399 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019401 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019402 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403 ret = FAIL;
19404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019405 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019406 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407
19408 name[len] = cc;
19409
19410 return ret;
19411}
19412
19413/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019414 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19415 * Also handle function call with Funcref variable: func(expr)
19416 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19417 */
19418 static int
19419handle_subscript(arg, rettv, evaluate, verbose)
19420 char_u **arg;
19421 typval_T *rettv;
19422 int evaluate; /* do more than finding the end */
19423 int verbose; /* give error messages */
19424{
19425 int ret = OK;
19426 dict_T *selfdict = NULL;
19427 char_u *s;
19428 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019429 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019430
19431 while (ret == OK
19432 && (**arg == '['
19433 || (**arg == '.' && rettv->v_type == VAR_DICT)
19434 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19435 && !vim_iswhite(*(*arg - 1)))
19436 {
19437 if (**arg == '(')
19438 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019439 /* need to copy the funcref so that we can clear rettv */
19440 functv = *rettv;
19441 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019442
19443 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019444 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019445 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019446 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19447 &len, evaluate, selfdict);
19448
19449 /* Clear the funcref afterwards, so that deleting it while
19450 * evaluating the arguments is possible (see test55). */
19451 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019452
19453 /* Stop the expression evaluation when immediately aborting on
19454 * error, or when an interrupt occurred or an exception was thrown
19455 * but not caught. */
19456 if (aborting())
19457 {
19458 if (ret == OK)
19459 clear_tv(rettv);
19460 ret = FAIL;
19461 }
19462 dict_unref(selfdict);
19463 selfdict = NULL;
19464 }
19465 else /* **arg == '[' || **arg == '.' */
19466 {
19467 dict_unref(selfdict);
19468 if (rettv->v_type == VAR_DICT)
19469 {
19470 selfdict = rettv->vval.v_dict;
19471 if (selfdict != NULL)
19472 ++selfdict->dv_refcount;
19473 }
19474 else
19475 selfdict = NULL;
19476 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19477 {
19478 clear_tv(rettv);
19479 ret = FAIL;
19480 }
19481 }
19482 }
19483 dict_unref(selfdict);
19484 return ret;
19485}
19486
19487/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019488 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019489 * value).
19490 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019491 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019492alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019493{
Bram Moolenaar33570922005-01-25 22:26:29 +000019494 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019495}
19496
19497/*
19498 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499 * The string "s" must have been allocated, it is consumed.
19500 * Return NULL for out of memory, the variable otherwise.
19501 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019502 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019503alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 char_u *s;
19505{
Bram Moolenaar33570922005-01-25 22:26:29 +000019506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019508 rettv = alloc_tv();
19509 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019511 rettv->v_type = VAR_STRING;
19512 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 }
19514 else
19515 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019516 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517}
19518
19519/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019520 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019522 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019524 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525{
19526 if (varp != NULL)
19527 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019528 switch (varp->v_type)
19529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019530 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019531 func_unref(varp->vval.v_string);
19532 /*FALLTHROUGH*/
19533 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019534 vim_free(varp->vval.v_string);
19535 break;
19536 case VAR_LIST:
19537 list_unref(varp->vval.v_list);
19538 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019539 case VAR_DICT:
19540 dict_unref(varp->vval.v_dict);
19541 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019542 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019543#ifdef FEAT_FLOAT
19544 case VAR_FLOAT:
19545#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019546 case VAR_UNKNOWN:
19547 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019548 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019549 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019550 break;
19551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 vim_free(varp);
19553 }
19554}
19555
19556/*
19557 * Free the memory for a variable value and set the value to NULL or 0.
19558 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019559 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019560clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019561 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562{
19563 if (varp != NULL)
19564 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019565 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019567 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019568 func_unref(varp->vval.v_string);
19569 /*FALLTHROUGH*/
19570 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019571 vim_free(varp->vval.v_string);
19572 varp->vval.v_string = NULL;
19573 break;
19574 case VAR_LIST:
19575 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019576 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019577 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019578 case VAR_DICT:
19579 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019580 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019581 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019582 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019583 varp->vval.v_number = 0;
19584 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019585#ifdef FEAT_FLOAT
19586 case VAR_FLOAT:
19587 varp->vval.v_float = 0.0;
19588 break;
19589#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019590 case VAR_UNKNOWN:
19591 break;
19592 default:
19593 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019595 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 }
19597}
19598
19599/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019600 * Set the value of a variable to NULL without freeing items.
19601 */
19602 static void
19603init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019604 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019605{
19606 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019607 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019608}
19609
19610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 * Get the number value of a variable.
19612 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019613 * For incompatible types, return 0.
19614 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19615 * caller of incompatible types: it sets *denote to TRUE if "denote"
19616 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 */
19618 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019619get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019620 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019622 int error = FALSE;
19623
19624 return get_tv_number_chk(varp, &error); /* return 0L on error */
19625}
19626
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019627 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019628get_tv_number_chk(varp, denote)
19629 typval_T *varp;
19630 int *denote;
19631{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019632 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019634 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019636 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019637 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019638#ifdef FEAT_FLOAT
19639 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019640 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019641 break;
19642#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019643 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019644 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019645 break;
19646 case VAR_STRING:
19647 if (varp->vval.v_string != NULL)
19648 vim_str2nr(varp->vval.v_string, NULL, NULL,
19649 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019650 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019651 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019652 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019653 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019654 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019655 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019656 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019657 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019658 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019659 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019661 if (denote == NULL) /* useful for values that must be unsigned */
19662 n = -1;
19663 else
19664 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019665 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666}
19667
19668/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019669 * Get the lnum from the first argument.
19670 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019671 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 */
19673 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019674get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019675 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676{
Bram Moolenaar33570922005-01-25 22:26:29 +000019677 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 linenr_T lnum;
19679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019680 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 if (lnum == 0) /* no valid number, try using line() */
19682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019683 rettv.v_type = VAR_NUMBER;
19684 f_line(argvars, &rettv);
19685 lnum = rettv.vval.v_number;
19686 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 }
19688 return lnum;
19689}
19690
19691/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019692 * Get the lnum from the first argument.
19693 * Also accepts "$", then "buf" is used.
19694 * Returns 0 on error.
19695 */
19696 static linenr_T
19697get_tv_lnum_buf(argvars, buf)
19698 typval_T *argvars;
19699 buf_T *buf;
19700{
19701 if (argvars[0].v_type == VAR_STRING
19702 && argvars[0].vval.v_string != NULL
19703 && argvars[0].vval.v_string[0] == '$'
19704 && buf != NULL)
19705 return buf->b_ml.ml_line_count;
19706 return get_tv_number_chk(&argvars[0], NULL);
19707}
19708
19709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710 * Get the string value of a variable.
19711 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019712 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19713 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 * If the String variable has never been set, return an empty string.
19715 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019716 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19717 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 */
19719 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019720get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019721 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722{
19723 static char_u mybuf[NUMBUFLEN];
19724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019725 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726}
19727
19728 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019729get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019730 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019731 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019733 char_u *res = get_tv_string_buf_chk(varp, buf);
19734
19735 return res != NULL ? res : (char_u *)"";
19736}
19737
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019738 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019739get_tv_string_chk(varp)
19740 typval_T *varp;
19741{
19742 static char_u mybuf[NUMBUFLEN];
19743
19744 return get_tv_string_buf_chk(varp, mybuf);
19745}
19746
19747 static char_u *
19748get_tv_string_buf_chk(varp, buf)
19749 typval_T *varp;
19750 char_u *buf;
19751{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019752 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019754 case VAR_NUMBER:
19755 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19756 return buf;
19757 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019758 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019759 break;
19760 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019761 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019762 break;
19763 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019764 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019765 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019766#ifdef FEAT_FLOAT
19767 case VAR_FLOAT:
19768 EMSG(_("E806: using Float as a String"));
19769 break;
19770#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019771 case VAR_STRING:
19772 if (varp->vval.v_string != NULL)
19773 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019774 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019775 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019776 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019777 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019779 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780}
19781
19782/*
19783 * Find variable "name" in the list of variables.
19784 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019785 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019786 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019787 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019789 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019790find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019792 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019795 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796
Bram Moolenaara7043832005-01-21 11:56:39 +000019797 ht = find_var_ht(name, &varname);
19798 if (htp != NULL)
19799 *htp = ht;
19800 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019802 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803}
19804
19805/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019807 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019809 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019810find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019811 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019812 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019813 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019814{
Bram Moolenaar33570922005-01-25 22:26:29 +000019815 hashitem_T *hi;
19816
19817 if (*varname == NUL)
19818 {
19819 /* Must be something like "s:", otherwise "ht" would be NULL. */
19820 switch (varname[-2])
19821 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019822 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019823 case 'g': return &globvars_var;
19824 case 'v': return &vimvars_var;
19825 case 'b': return &curbuf->b_bufvar;
19826 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019827#ifdef FEAT_WINDOWS
19828 case 't': return &curtab->tp_winvar;
19829#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019830 case 'l': return current_funccal == NULL
19831 ? NULL : &current_funccal->l_vars_var;
19832 case 'a': return current_funccal == NULL
19833 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019834 }
19835 return NULL;
19836 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019837
19838 hi = hash_find(ht, varname);
19839 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019840 {
19841 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019842 * worked find the variable again. Don't auto-load a script if it was
19843 * loaded already, otherwise it would be loaded every time when
19844 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019845 if (ht == &globvarht && !writing)
19846 {
19847 /* Note: script_autoload() may make "hi" invalid. It must either
19848 * be obtained again or not used. */
19849 if (!script_autoload(varname, FALSE) || aborting())
19850 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019851 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019852 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019853 if (HASHITEM_EMPTY(hi))
19854 return NULL;
19855 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019856 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019857}
19858
19859/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019860 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019861 * Set "varname" to the start of name without ':'.
19862 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019863 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019864find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 char_u *name;
19866 char_u **varname;
19867{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019868 hashitem_T *hi;
19869
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870 if (name[1] != ':')
19871 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019872 /* The name must not start with a colon or #. */
19873 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 return NULL;
19875 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019876
19877 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019878 hi = hash_find(&compat_hashtab, name);
19879 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019880 return &compat_hashtab;
19881
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019883 return &globvarht; /* global variable */
19884 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 }
19886 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019887 if (*name == 'g') /* global variable */
19888 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019889 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19890 */
19891 if (vim_strchr(name + 2, ':') != NULL
19892 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019893 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019895 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019897 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019898#ifdef FEAT_WINDOWS
19899 if (*name == 't') /* tab page variable */
19900 return &curtab->tp_vars.dv_hashtab;
19901#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019902 if (*name == 'v') /* v: variable */
19903 return &vimvarht;
19904 if (*name == 'a' && current_funccal != NULL) /* function argument */
19905 return &current_funccal->l_avars.dv_hashtab;
19906 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19907 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 if (*name == 's' /* script variable */
19909 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19910 return &SCRIPT_VARS(current_SID);
19911 return NULL;
19912}
19913
19914/*
19915 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019916 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917 * Returns NULL when it doesn't exist.
19918 */
19919 char_u *
19920get_var_value(name)
19921 char_u *name;
19922{
Bram Moolenaar33570922005-01-25 22:26:29 +000019923 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924
Bram Moolenaara7043832005-01-21 11:56:39 +000019925 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 if (v == NULL)
19927 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019928 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929}
19930
19931/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019932 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 * sourcing this script and when executing functions defined in the script.
19934 */
19935 void
19936new_script_vars(id)
19937 scid_T id;
19938{
Bram Moolenaara7043832005-01-21 11:56:39 +000019939 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019940 hashtab_T *ht;
19941 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019942
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19944 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019945 /* Re-allocating ga_data means that an ht_array pointing to
19946 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019947 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019948 for (i = 1; i <= ga_scripts.ga_len; ++i)
19949 {
19950 ht = &SCRIPT_VARS(i);
19951 if (ht->ht_mask == HT_INIT_SIZE - 1)
19952 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019953 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019954 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019955 }
19956
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 while (ga_scripts.ga_len < id)
19958 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019959 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019960 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019961 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 }
19964 }
19965}
19966
19967/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019968 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19969 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 */
19971 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019972init_var_dict(dict, dict_var)
19973 dict_T *dict;
19974 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975{
Bram Moolenaar33570922005-01-25 22:26:29 +000019976 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019977 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019978 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019979 dict_var->di_tv.vval.v_dict = dict;
19980 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019981 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019982 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19983 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984}
19985
19986/*
19987 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019988 * Frees all allocated variables and the value they contain.
19989 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 */
19991 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019992vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019993 hashtab_T *ht;
19994{
19995 vars_clear_ext(ht, TRUE);
19996}
19997
19998/*
19999 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20000 */
20001 static void
20002vars_clear_ext(ht, free_val)
20003 hashtab_T *ht;
20004 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005{
Bram Moolenaara7043832005-01-21 11:56:39 +000020006 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020007 hashitem_T *hi;
20008 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009
Bram Moolenaar33570922005-01-25 22:26:29 +000020010 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020011 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020012 for (hi = ht->ht_array; todo > 0; ++hi)
20013 {
20014 if (!HASHITEM_EMPTY(hi))
20015 {
20016 --todo;
20017
Bram Moolenaar33570922005-01-25 22:26:29 +000020018 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020019 * ht_array might change then. hash_clear() takes care of it
20020 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020021 v = HI2DI(hi);
20022 if (free_val)
20023 clear_tv(&v->di_tv);
20024 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20025 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020026 }
20027 }
20028 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020029 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030}
20031
Bram Moolenaara7043832005-01-21 11:56:39 +000020032/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020033 * Delete a variable from hashtab "ht" at item "hi".
20034 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020037delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020038 hashtab_T *ht;
20039 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040{
Bram Moolenaar33570922005-01-25 22:26:29 +000020041 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020042
20043 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020044 clear_tv(&di->di_tv);
20045 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046}
20047
20048/*
20049 * List the value of one internal variable.
20050 */
20051 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020052list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020053 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020055 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020057 char_u *tofree;
20058 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020059 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020060
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020061 current_copyID += COPYID_INC;
20062 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020063 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020064 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020065 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066}
20067
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020069list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070 char_u *prefix;
20071 char_u *name;
20072 int type;
20073 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020074 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075{
Bram Moolenaar31859182007-08-14 20:41:13 +000020076 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20077 msg_start();
20078 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 if (name != NULL) /* "a:" vars don't have a name stored */
20080 msg_puts(name);
20081 msg_putchar(' ');
20082 msg_advance(22);
20083 if (type == VAR_NUMBER)
20084 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020085 else if (type == VAR_FUNC)
20086 msg_putchar('*');
20087 else if (type == VAR_LIST)
20088 {
20089 msg_putchar('[');
20090 if (*string == '[')
20091 ++string;
20092 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020093 else if (type == VAR_DICT)
20094 {
20095 msg_putchar('{');
20096 if (*string == '{')
20097 ++string;
20098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 else
20100 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020101
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020103
20104 if (type == VAR_FUNC)
20105 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020106 if (*first)
20107 {
20108 msg_clr_eos();
20109 *first = FALSE;
20110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111}
20112
20113/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020114 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 * If the variable already exists, the value is updated.
20116 * Otherwise the variable is created.
20117 */
20118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020119set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020121 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020122 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123{
Bram Moolenaar33570922005-01-25 22:26:29 +000020124 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020126 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020128 ht = find_var_ht(name, &varname);
20129 if (ht == NULL || *varname == NUL)
20130 {
20131 EMSG2(_(e_illvar), name);
20132 return;
20133 }
20134 v = find_var_in_ht(ht, varname, TRUE);
20135
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020136 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20137 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020138
Bram Moolenaar33570922005-01-25 22:26:29 +000020139 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020141 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020142 if (var_check_ro(v->di_flags, name)
20143 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020144 return;
20145 if (v->di_tv.v_type != tv->v_type
20146 && !((v->di_tv.v_type == VAR_STRING
20147 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020148 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020149 || tv->v_type == VAR_NUMBER))
20150#ifdef FEAT_FLOAT
20151 && !((v->di_tv.v_type == VAR_NUMBER
20152 || v->di_tv.v_type == VAR_FLOAT)
20153 && (tv->v_type == VAR_NUMBER
20154 || tv->v_type == VAR_FLOAT))
20155#endif
20156 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020157 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020158 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020159 return;
20160 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020161
20162 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020163 * Handle setting internal v: variables separately: we don't change
20164 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020165 */
20166 if (ht == &vimvarht)
20167 {
20168 if (v->di_tv.v_type == VAR_STRING)
20169 {
20170 vim_free(v->di_tv.vval.v_string);
20171 if (copy || tv->v_type != VAR_STRING)
20172 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20173 else
20174 {
20175 /* Take over the string to avoid an extra alloc/free. */
20176 v->di_tv.vval.v_string = tv->vval.v_string;
20177 tv->vval.v_string = NULL;
20178 }
20179 }
20180 else if (v->di_tv.v_type != VAR_NUMBER)
20181 EMSG2(_(e_intern2), "set_var()");
20182 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020183 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020184 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020185 if (STRCMP(varname, "searchforward") == 0)
20186 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20187 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020188 return;
20189 }
20190
20191 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192 }
20193 else /* add a new variable */
20194 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020195 /* Can't add "v:" variable. */
20196 if (ht == &vimvarht)
20197 {
20198 EMSG2(_(e_illvar), name);
20199 return;
20200 }
20201
Bram Moolenaar92124a32005-06-17 22:03:40 +000020202 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020203 if (!valid_varname(varname))
20204 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020205
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020206 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20207 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020208 if (v == NULL)
20209 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020210 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020211 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020213 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 return;
20215 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020216 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020218
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020219 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020220 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020221 else
20222 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020224 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020225 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227}
20228
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020229/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020230 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020231 * Also give an error message.
20232 */
20233 static int
20234var_check_ro(flags, name)
20235 int flags;
20236 char_u *name;
20237{
20238 if (flags & DI_FLAGS_RO)
20239 {
20240 EMSG2(_(e_readonlyvar), name);
20241 return TRUE;
20242 }
20243 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20244 {
20245 EMSG2(_(e_readonlysbx), name);
20246 return TRUE;
20247 }
20248 return FALSE;
20249}
20250
20251/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020252 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20253 * Also give an error message.
20254 */
20255 static int
20256var_check_fixed(flags, name)
20257 int flags;
20258 char_u *name;
20259{
20260 if (flags & DI_FLAGS_FIX)
20261 {
20262 EMSG2(_("E795: Cannot delete variable %s"), name);
20263 return TRUE;
20264 }
20265 return FALSE;
20266}
20267
20268/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020269 * Check if a funcref is assigned to a valid variable name.
20270 * Return TRUE and give an error if not.
20271 */
20272 static int
20273var_check_func_name(name, new_var)
20274 char_u *name; /* points to start of variable name */
20275 int new_var; /* TRUE when creating the variable */
20276{
20277 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20278 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20279 ? name[2] : name[0]))
20280 {
20281 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20282 name);
20283 return TRUE;
20284 }
20285 /* Don't allow hiding a function. When "v" is not NULL we might be
20286 * assigning another function to the same var, the type is checked
20287 * below. */
20288 if (new_var && function_exists(name))
20289 {
20290 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20291 name);
20292 return TRUE;
20293 }
20294 return FALSE;
20295}
20296
20297/*
20298 * Check if a variable name is valid.
20299 * Return FALSE and give an error if not.
20300 */
20301 static int
20302valid_varname(varname)
20303 char_u *varname;
20304{
20305 char_u *p;
20306
20307 for (p = varname; *p != NUL; ++p)
20308 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20309 && *p != AUTOLOAD_CHAR)
20310 {
20311 EMSG2(_(e_illvar), varname);
20312 return FALSE;
20313 }
20314 return TRUE;
20315}
20316
20317/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020318 * Return TRUE if typeval "tv" is set to be locked (immutable).
20319 * Also give an error message, using "name".
20320 */
20321 static int
20322tv_check_lock(lock, name)
20323 int lock;
20324 char_u *name;
20325{
20326 if (lock & VAR_LOCKED)
20327 {
20328 EMSG2(_("E741: Value is locked: %s"),
20329 name == NULL ? (char_u *)_("Unknown") : name);
20330 return TRUE;
20331 }
20332 if (lock & VAR_FIXED)
20333 {
20334 EMSG2(_("E742: Cannot change value of %s"),
20335 name == NULL ? (char_u *)_("Unknown") : name);
20336 return TRUE;
20337 }
20338 return FALSE;
20339}
20340
20341/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020342 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020343 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020344 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020345 * It is OK for "from" and "to" to point to the same item. This is used to
20346 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020347 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020348 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020349copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020350 typval_T *from;
20351 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020353 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020354 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020355 switch (from->v_type)
20356 {
20357 case VAR_NUMBER:
20358 to->vval.v_number = from->vval.v_number;
20359 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020360#ifdef FEAT_FLOAT
20361 case VAR_FLOAT:
20362 to->vval.v_float = from->vval.v_float;
20363 break;
20364#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020365 case VAR_STRING:
20366 case VAR_FUNC:
20367 if (from->vval.v_string == NULL)
20368 to->vval.v_string = NULL;
20369 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020370 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020371 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020372 if (from->v_type == VAR_FUNC)
20373 func_ref(to->vval.v_string);
20374 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020375 break;
20376 case VAR_LIST:
20377 if (from->vval.v_list == NULL)
20378 to->vval.v_list = NULL;
20379 else
20380 {
20381 to->vval.v_list = from->vval.v_list;
20382 ++to->vval.v_list->lv_refcount;
20383 }
20384 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020385 case VAR_DICT:
20386 if (from->vval.v_dict == NULL)
20387 to->vval.v_dict = NULL;
20388 else
20389 {
20390 to->vval.v_dict = from->vval.v_dict;
20391 ++to->vval.v_dict->dv_refcount;
20392 }
20393 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020394 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020395 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020396 break;
20397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398}
20399
20400/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020401 * Make a copy of an item.
20402 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020403 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20404 * reference to an already copied list/dict can be used.
20405 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020406 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020407 static int
20408item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 typval_T *from;
20410 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020411 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020412 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020413{
20414 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020415 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020416
Bram Moolenaar33570922005-01-25 22:26:29 +000020417 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020418 {
20419 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020420 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020421 }
20422 ++recurse;
20423
20424 switch (from->v_type)
20425 {
20426 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020427#ifdef FEAT_FLOAT
20428 case VAR_FLOAT:
20429#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020430 case VAR_STRING:
20431 case VAR_FUNC:
20432 copy_tv(from, to);
20433 break;
20434 case VAR_LIST:
20435 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020436 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020437 if (from->vval.v_list == NULL)
20438 to->vval.v_list = NULL;
20439 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20440 {
20441 /* use the copy made earlier */
20442 to->vval.v_list = from->vval.v_list->lv_copylist;
20443 ++to->vval.v_list->lv_refcount;
20444 }
20445 else
20446 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20447 if (to->vval.v_list == NULL)
20448 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020449 break;
20450 case VAR_DICT:
20451 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020452 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020453 if (from->vval.v_dict == NULL)
20454 to->vval.v_dict = NULL;
20455 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20456 {
20457 /* use the copy made earlier */
20458 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20459 ++to->vval.v_dict->dv_refcount;
20460 }
20461 else
20462 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20463 if (to->vval.v_dict == NULL)
20464 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020465 break;
20466 default:
20467 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020468 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020469 }
20470 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020471 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020472}
20473
20474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 * ":echo expr1 ..." print each argument separated with a space, add a
20476 * newline at the end.
20477 * ":echon expr1 ..." print each argument plain.
20478 */
20479 void
20480ex_echo(eap)
20481 exarg_T *eap;
20482{
20483 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020484 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020485 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486 char_u *p;
20487 int needclr = TRUE;
20488 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020489 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490
20491 if (eap->skip)
20492 ++emsg_skip;
20493 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20494 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020495 /* If eval1() causes an error message the text from the command may
20496 * still need to be cleared. E.g., "echo 22,44". */
20497 need_clr_eos = needclr;
20498
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020500 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501 {
20502 /*
20503 * Report the invalid expression unless the expression evaluation
20504 * has been cancelled due to an aborting error, an interrupt, or an
20505 * exception.
20506 */
20507 if (!aborting())
20508 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020509 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 break;
20511 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020512 need_clr_eos = FALSE;
20513
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 if (!eap->skip)
20515 {
20516 if (atstart)
20517 {
20518 atstart = FALSE;
20519 /* Call msg_start() after eval1(), evaluating the expression
20520 * may cause a message to appear. */
20521 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020522 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020523 /* Mark the saved text as finishing the line, so that what
20524 * follows is displayed on a new line when scrolling back
20525 * at the more prompt. */
20526 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529 }
20530 else if (eap->cmdidx == CMD_echo)
20531 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020532 current_copyID += COPYID_INC;
20533 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020534 if (p != NULL)
20535 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020537 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020539 if (*p != TAB && needclr)
20540 {
20541 /* remove any text still there from the command */
20542 msg_clr_eos();
20543 needclr = FALSE;
20544 }
20545 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 }
20547 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020548 {
20549#ifdef FEAT_MBYTE
20550 if (has_mbyte)
20551 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020552 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020553
20554 (void)msg_outtrans_len_attr(p, i, echo_attr);
20555 p += i - 1;
20556 }
20557 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020558#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020559 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020562 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020564 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 arg = skipwhite(arg);
20566 }
20567 eap->nextcmd = check_nextcmd(arg);
20568
20569 if (eap->skip)
20570 --emsg_skip;
20571 else
20572 {
20573 /* remove text that may still be there from the command */
20574 if (needclr)
20575 msg_clr_eos();
20576 if (eap->cmdidx == CMD_echo)
20577 msg_end();
20578 }
20579}
20580
20581/*
20582 * ":echohl {name}".
20583 */
20584 void
20585ex_echohl(eap)
20586 exarg_T *eap;
20587{
20588 int id;
20589
20590 id = syn_name2id(eap->arg);
20591 if (id == 0)
20592 echo_attr = 0;
20593 else
20594 echo_attr = syn_id2attr(id);
20595}
20596
20597/*
20598 * ":execute expr1 ..." execute the result of an expression.
20599 * ":echomsg expr1 ..." Print a message
20600 * ":echoerr expr1 ..." Print an error
20601 * Each gets spaces around each argument and a newline at the end for
20602 * echo commands
20603 */
20604 void
20605ex_execute(eap)
20606 exarg_T *eap;
20607{
20608 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020609 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 int ret = OK;
20611 char_u *p;
20612 garray_T ga;
20613 int len;
20614 int save_did_emsg;
20615
20616 ga_init2(&ga, 1, 80);
20617
20618 if (eap->skip)
20619 ++emsg_skip;
20620 while (*arg != NUL && *arg != '|' && *arg != '\n')
20621 {
20622 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020623 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 {
20625 /*
20626 * Report the invalid expression unless the expression evaluation
20627 * has been cancelled due to an aborting error, an interrupt, or an
20628 * exception.
20629 */
20630 if (!aborting())
20631 EMSG2(_(e_invexpr2), p);
20632 ret = FAIL;
20633 break;
20634 }
20635
20636 if (!eap->skip)
20637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020638 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639 len = (int)STRLEN(p);
20640 if (ga_grow(&ga, len + 2) == FAIL)
20641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020642 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 ret = FAIL;
20644 break;
20645 }
20646 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 ga.ga_len += len;
20650 }
20651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020652 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 arg = skipwhite(arg);
20654 }
20655
20656 if (ret != FAIL && ga.ga_data != NULL)
20657 {
20658 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020659 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020661 out_flush();
20662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 else if (eap->cmdidx == CMD_echoerr)
20664 {
20665 /* We don't want to abort following commands, restore did_emsg. */
20666 save_did_emsg = did_emsg;
20667 EMSG((char_u *)ga.ga_data);
20668 if (!force_abort)
20669 did_emsg = save_did_emsg;
20670 }
20671 else if (eap->cmdidx == CMD_execute)
20672 do_cmdline((char_u *)ga.ga_data,
20673 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20674 }
20675
20676 ga_clear(&ga);
20677
20678 if (eap->skip)
20679 --emsg_skip;
20680
20681 eap->nextcmd = check_nextcmd(arg);
20682}
20683
20684/*
20685 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20686 * "arg" points to the "&" or '+' when called, to "option" when returning.
20687 * Returns NULL when no option name found. Otherwise pointer to the char
20688 * after the option name.
20689 */
20690 static char_u *
20691find_option_end(arg, opt_flags)
20692 char_u **arg;
20693 int *opt_flags;
20694{
20695 char_u *p = *arg;
20696
20697 ++p;
20698 if (*p == 'g' && p[1] == ':')
20699 {
20700 *opt_flags = OPT_GLOBAL;
20701 p += 2;
20702 }
20703 else if (*p == 'l' && p[1] == ':')
20704 {
20705 *opt_flags = OPT_LOCAL;
20706 p += 2;
20707 }
20708 else
20709 *opt_flags = 0;
20710
20711 if (!ASCII_ISALPHA(*p))
20712 return NULL;
20713 *arg = p;
20714
20715 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20716 p += 4; /* termcap option */
20717 else
20718 while (ASCII_ISALPHA(*p))
20719 ++p;
20720 return p;
20721}
20722
20723/*
20724 * ":function"
20725 */
20726 void
20727ex_function(eap)
20728 exarg_T *eap;
20729{
20730 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020731 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732 int j;
20733 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 char_u *name = NULL;
20736 char_u *p;
20737 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020738 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739 garray_T newargs;
20740 garray_T newlines;
20741 int varargs = FALSE;
20742 int mustend = FALSE;
20743 int flags = 0;
20744 ufunc_T *fp;
20745 int indent;
20746 int nesting;
20747 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020748 dictitem_T *v;
20749 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020750 static int func_nr = 0; /* number for nameless function */
20751 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020752 hashtab_T *ht;
20753 int todo;
20754 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020755 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756
20757 /*
20758 * ":function" without argument: list functions.
20759 */
20760 if (ends_excmd(*eap->arg))
20761 {
20762 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020763 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020764 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020765 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020766 {
20767 if (!HASHITEM_EMPTY(hi))
20768 {
20769 --todo;
20770 fp = HI2UF(hi);
20771 if (!isdigit(*fp->uf_name))
20772 list_func_head(fp, FALSE);
20773 }
20774 }
20775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 eap->nextcmd = check_nextcmd(eap->arg);
20777 return;
20778 }
20779
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020780 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020781 * ":function /pat": list functions matching pattern.
20782 */
20783 if (*eap->arg == '/')
20784 {
20785 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20786 if (!eap->skip)
20787 {
20788 regmatch_T regmatch;
20789
20790 c = *p;
20791 *p = NUL;
20792 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20793 *p = c;
20794 if (regmatch.regprog != NULL)
20795 {
20796 regmatch.rm_ic = p_ic;
20797
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020798 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020799 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20800 {
20801 if (!HASHITEM_EMPTY(hi))
20802 {
20803 --todo;
20804 fp = HI2UF(hi);
20805 if (!isdigit(*fp->uf_name)
20806 && vim_regexec(&regmatch, fp->uf_name, 0))
20807 list_func_head(fp, FALSE);
20808 }
20809 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020810 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020811 }
20812 }
20813 if (*p == '/')
20814 ++p;
20815 eap->nextcmd = check_nextcmd(p);
20816 return;
20817 }
20818
20819 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020820 * Get the function name. There are these situations:
20821 * func normal function name
20822 * "name" == func, "fudi.fd_dict" == NULL
20823 * dict.func new dictionary entry
20824 * "name" == NULL, "fudi.fd_dict" set,
20825 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20826 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020827 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020828 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20829 * dict.func existing dict entry that's not a Funcref
20830 * "name" == NULL, "fudi.fd_dict" set,
20831 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020834 name = trans_function_name(&p, eap->skip, 0, &fudi);
20835 paren = (vim_strchr(p, '(') != NULL);
20836 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 {
20838 /*
20839 * Return on an invalid expression in braces, unless the expression
20840 * evaluation has been cancelled due to an aborting error, an
20841 * interrupt, or an exception.
20842 */
20843 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020844 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020845 if (!eap->skip && fudi.fd_newkey != NULL)
20846 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020847 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 else
20851 eap->skip = TRUE;
20852 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020853
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854 /* An error in a function call during evaluation of an expression in magic
20855 * braces should not cause the function not to be defined. */
20856 saved_did_emsg = did_emsg;
20857 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858
20859 /*
20860 * ":function func" with only function name: list function.
20861 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020862 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 {
20864 if (!ends_excmd(*skipwhite(p)))
20865 {
20866 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020867 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 }
20869 eap->nextcmd = check_nextcmd(p);
20870 if (eap->nextcmd != NULL)
20871 *p = NUL;
20872 if (!eap->skip && !got_int)
20873 {
20874 fp = find_func(name);
20875 if (fp != NULL)
20876 {
20877 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020878 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020880 if (FUNCLINE(fp, j) == NULL)
20881 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 msg_putchar('\n');
20883 msg_outnum((long)(j + 1));
20884 if (j < 9)
20885 msg_putchar(' ');
20886 if (j < 99)
20887 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020888 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 out_flush(); /* show a line at a time */
20890 ui_breakcheck();
20891 }
20892 if (!got_int)
20893 {
20894 msg_putchar('\n');
20895 msg_puts((char_u *)" endfunction");
20896 }
20897 }
20898 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020899 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020901 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 }
20903
20904 /*
20905 * ":function name(arg1, arg2)" Define function.
20906 */
20907 p = skipwhite(p);
20908 if (*p != '(')
20909 {
20910 if (!eap->skip)
20911 {
20912 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020913 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 }
20915 /* attempt to continue by skipping some text */
20916 if (vim_strchr(p, '(') != NULL)
20917 p = vim_strchr(p, '(');
20918 }
20919 p = skipwhite(p + 1);
20920
20921 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20922 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20923
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020924 if (!eap->skip)
20925 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020926 /* Check the name of the function. Unless it's a dictionary function
20927 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020928 if (name != NULL)
20929 arg = name;
20930 else
20931 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020932 if (arg != NULL && (fudi.fd_di == NULL
20933 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020934 {
20935 if (*arg == K_SPECIAL)
20936 j = 3;
20937 else
20938 j = 0;
20939 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20940 : eval_isnamec(arg[j])))
20941 ++j;
20942 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020943 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020944 }
20945 }
20946
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 /*
20948 * Isolate the arguments: "arg1, arg2, ...)"
20949 */
20950 while (*p != ')')
20951 {
20952 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20953 {
20954 varargs = TRUE;
20955 p += 3;
20956 mustend = TRUE;
20957 }
20958 else
20959 {
20960 arg = p;
20961 while (ASCII_ISALNUM(*p) || *p == '_')
20962 ++p;
20963 if (arg == p || isdigit(*arg)
20964 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20965 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20966 {
20967 if (!eap->skip)
20968 EMSG2(_("E125: Illegal argument: %s"), arg);
20969 break;
20970 }
20971 if (ga_grow(&newargs, 1) == FAIL)
20972 goto erret;
20973 c = *p;
20974 *p = NUL;
20975 arg = vim_strsave(arg);
20976 if (arg == NULL)
20977 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020978
20979 /* Check for duplicate argument name. */
20980 for (i = 0; i < newargs.ga_len; ++i)
20981 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
20982 {
20983 EMSG2(_("E853: Duplicate argument name: %s"), arg);
20984 goto erret;
20985 }
20986
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20988 *p = c;
20989 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990 if (*p == ',')
20991 ++p;
20992 else
20993 mustend = TRUE;
20994 }
20995 p = skipwhite(p);
20996 if (mustend && *p != ')')
20997 {
20998 if (!eap->skip)
20999 EMSG2(_(e_invarg2), eap->arg);
21000 break;
21001 }
21002 }
21003 ++p; /* skip the ')' */
21004
Bram Moolenaare9a41262005-01-15 22:18:47 +000021005 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 for (;;)
21007 {
21008 p = skipwhite(p);
21009 if (STRNCMP(p, "range", 5) == 0)
21010 {
21011 flags |= FC_RANGE;
21012 p += 5;
21013 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021014 else if (STRNCMP(p, "dict", 4) == 0)
21015 {
21016 flags |= FC_DICT;
21017 p += 4;
21018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 else if (STRNCMP(p, "abort", 5) == 0)
21020 {
21021 flags |= FC_ABORT;
21022 p += 5;
21023 }
21024 else
21025 break;
21026 }
21027
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021028 /* When there is a line break use what follows for the function body.
21029 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21030 if (*p == '\n')
21031 line_arg = p + 1;
21032 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 EMSG(_(e_trailing));
21034
21035 /*
21036 * Read the body of the function, until ":endfunction" is found.
21037 */
21038 if (KeyTyped)
21039 {
21040 /* Check if the function already exists, don't let the user type the
21041 * whole function before telling him it doesn't work! For a script we
21042 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021043 if (!eap->skip && !eap->forceit)
21044 {
21045 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21046 EMSG(_(e_funcdict));
21047 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021048 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021051 if (!eap->skip && did_emsg)
21052 goto erret;
21053
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054 msg_putchar('\n'); /* don't overwrite the function name */
21055 cmdline_row = msg_row;
21056 }
21057
21058 indent = 2;
21059 nesting = 0;
21060 for (;;)
21061 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021062 if (KeyTyped)
21063 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021064 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021065 sourcing_lnum_off = sourcing_lnum;
21066
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021067 if (line_arg != NULL)
21068 {
21069 /* Use eap->arg, split up in parts by line breaks. */
21070 theline = line_arg;
21071 p = vim_strchr(theline, '\n');
21072 if (p == NULL)
21073 line_arg += STRLEN(line_arg);
21074 else
21075 {
21076 *p = NUL;
21077 line_arg = p + 1;
21078 }
21079 }
21080 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081 theline = getcmdline(':', 0L, indent);
21082 else
21083 theline = eap->getline(':', eap->cookie, indent);
21084 if (KeyTyped)
21085 lines_left = Rows - 1;
21086 if (theline == NULL)
21087 {
21088 EMSG(_("E126: Missing :endfunction"));
21089 goto erret;
21090 }
21091
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021092 /* Detect line continuation: sourcing_lnum increased more than one. */
21093 if (sourcing_lnum > sourcing_lnum_off + 1)
21094 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21095 else
21096 sourcing_lnum_off = 0;
21097
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 if (skip_until != NULL)
21099 {
21100 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21101 * don't check for ":endfunc". */
21102 if (STRCMP(theline, skip_until) == 0)
21103 {
21104 vim_free(skip_until);
21105 skip_until = NULL;
21106 }
21107 }
21108 else
21109 {
21110 /* skip ':' and blanks*/
21111 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21112 ;
21113
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021114 /* Check for "endfunction". */
21115 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021117 if (line_arg == NULL)
21118 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 break;
21120 }
21121
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021122 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 * at "end". */
21124 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21125 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021126 else if (STRNCMP(p, "if", 2) == 0
21127 || STRNCMP(p, "wh", 2) == 0
21128 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 || STRNCMP(p, "try", 3) == 0)
21130 indent += 2;
21131
21132 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021133 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021135 if (*p == '!')
21136 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 p += eval_fname_script(p);
21138 if (ASCII_ISALPHA(*p))
21139 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021140 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 if (*skipwhite(p) == '(')
21142 {
21143 ++nesting;
21144 indent += 2;
21145 }
21146 }
21147 }
21148
21149 /* Check for ":append" or ":insert". */
21150 p = skip_range(p, NULL);
21151 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21152 || (p[0] == 'i'
21153 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21154 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21155 skip_until = vim_strsave((char_u *)".");
21156
21157 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21158 arg = skipwhite(skiptowhite(p));
21159 if (arg[0] == '<' && arg[1] =='<'
21160 && ((p[0] == 'p' && p[1] == 'y'
21161 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21162 || (p[0] == 'p' && p[1] == 'e'
21163 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21164 || (p[0] == 't' && p[1] == 'c'
21165 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021166 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21167 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21169 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021170 || (p[0] == 'm' && p[1] == 'z'
21171 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 ))
21173 {
21174 /* ":python <<" continues until a dot, like ":append" */
21175 p = skipwhite(arg + 2);
21176 if (*p == NUL)
21177 skip_until = vim_strsave((char_u *)".");
21178 else
21179 skip_until = vim_strsave(p);
21180 }
21181 }
21182
21183 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021184 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021185 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021186 if (line_arg == NULL)
21187 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021189 }
21190
21191 /* Copy the line to newly allocated memory. get_one_sourceline()
21192 * allocates 250 bytes per line, this saves 80% on average. The cost
21193 * is an extra alloc/free. */
21194 p = vim_strsave(theline);
21195 if (p != NULL)
21196 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021197 if (line_arg == NULL)
21198 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021199 theline = p;
21200 }
21201
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021202 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21203
21204 /* Add NULL lines for continuation lines, so that the line count is
21205 * equal to the index in the growarray. */
21206 while (sourcing_lnum_off-- > 0)
21207 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021208
21209 /* Check for end of eap->arg. */
21210 if (line_arg != NULL && *line_arg == NUL)
21211 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 }
21213
21214 /* Don't define the function when skipping commands or when an error was
21215 * detected. */
21216 if (eap->skip || did_emsg)
21217 goto erret;
21218
21219 /*
21220 * If there are no errors, add the function
21221 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021222 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021223 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021224 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021225 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021226 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021227 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021228 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021229 goto erret;
21230 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021231
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021232 fp = find_func(name);
21233 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021235 if (!eap->forceit)
21236 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021237 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021238 goto erret;
21239 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021240 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021241 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021242 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021243 name);
21244 goto erret;
21245 }
21246 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021247 ga_clear_strings(&(fp->uf_args));
21248 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021249 vim_free(name);
21250 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 }
21253 else
21254 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021255 char numbuf[20];
21256
21257 fp = NULL;
21258 if (fudi.fd_newkey == NULL && !eap->forceit)
21259 {
21260 EMSG(_(e_funcdict));
21261 goto erret;
21262 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021263 if (fudi.fd_di == NULL)
21264 {
21265 /* Can't add a function to a locked dictionary */
21266 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21267 goto erret;
21268 }
21269 /* Can't change an existing function if it is locked */
21270 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21271 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021272
21273 /* Give the function a sequential number. Can only be used with a
21274 * Funcref! */
21275 vim_free(name);
21276 sprintf(numbuf, "%d", ++func_nr);
21277 name = vim_strsave((char_u *)numbuf);
21278 if (name == NULL)
21279 goto erret;
21280 }
21281
21282 if (fp == NULL)
21283 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021284 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021285 {
21286 int slen, plen;
21287 char_u *scriptname;
21288
21289 /* Check that the autoload name matches the script name. */
21290 j = FAIL;
21291 if (sourcing_name != NULL)
21292 {
21293 scriptname = autoload_name(name);
21294 if (scriptname != NULL)
21295 {
21296 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021297 plen = (int)STRLEN(p);
21298 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021299 if (slen > plen && fnamecmp(p,
21300 sourcing_name + slen - plen) == 0)
21301 j = OK;
21302 vim_free(scriptname);
21303 }
21304 }
21305 if (j == FAIL)
21306 {
21307 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21308 goto erret;
21309 }
21310 }
21311
21312 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 if (fp == NULL)
21314 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021315
21316 if (fudi.fd_dict != NULL)
21317 {
21318 if (fudi.fd_di == NULL)
21319 {
21320 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021321 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021322 if (fudi.fd_di == NULL)
21323 {
21324 vim_free(fp);
21325 goto erret;
21326 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021327 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21328 {
21329 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021330 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021331 goto erret;
21332 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021333 }
21334 else
21335 /* overwrite existing dict entry */
21336 clear_tv(&fudi.fd_di->di_tv);
21337 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021338 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021339 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021340 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021341
21342 /* behave like "dict" was used */
21343 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021344 }
21345
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021347 STRCPY(fp->uf_name, name);
21348 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021350 fp->uf_args = newargs;
21351 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021352#ifdef FEAT_PROFILE
21353 fp->uf_tml_count = NULL;
21354 fp->uf_tml_total = NULL;
21355 fp->uf_tml_self = NULL;
21356 fp->uf_profiling = FALSE;
21357 if (prof_def_func())
21358 func_do_profile(fp);
21359#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021360 fp->uf_varargs = varargs;
21361 fp->uf_flags = flags;
21362 fp->uf_calls = 0;
21363 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021364 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365
21366erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 ga_clear_strings(&newargs);
21368 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021369ret_free:
21370 vim_free(skip_until);
21371 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374}
21375
21376/*
21377 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021378 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021380 * flags:
21381 * TFN_INT: internal function name OK
21382 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 * Advances "pp" to just after the function name (if no error).
21384 */
21385 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021386trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 char_u **pp;
21388 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021389 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021390 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021392 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 char_u *start;
21394 char_u *end;
21395 int lead;
21396 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021398 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021399
21400 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021401 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021403
21404 /* Check for hard coded <SNR>: already translated function ID (from a user
21405 * command). */
21406 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21407 && (*pp)[2] == (int)KE_SNR)
21408 {
21409 *pp += 3;
21410 len = get_id_len(pp) + 3;
21411 return vim_strnsave(start, len);
21412 }
21413
21414 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21415 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021417 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021419
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021420 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21421 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021422 if (end == start)
21423 {
21424 if (!skip)
21425 EMSG(_("E129: Function name required"));
21426 goto theend;
21427 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021428 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021429 {
21430 /*
21431 * Report an invalid expression in braces, unless the expression
21432 * evaluation has been cancelled due to an aborting error, an
21433 * interrupt, or an exception.
21434 */
21435 if (!aborting())
21436 {
21437 if (end != NULL)
21438 EMSG2(_(e_invarg2), start);
21439 }
21440 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021441 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021442 goto theend;
21443 }
21444
21445 if (lv.ll_tv != NULL)
21446 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021447 if (fdp != NULL)
21448 {
21449 fdp->fd_dict = lv.ll_dict;
21450 fdp->fd_newkey = lv.ll_newkey;
21451 lv.ll_newkey = NULL;
21452 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021453 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021454 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21455 {
21456 name = vim_strsave(lv.ll_tv->vval.v_string);
21457 *pp = end;
21458 }
21459 else
21460 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021461 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21462 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021463 EMSG(_(e_funcref));
21464 else
21465 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021466 name = NULL;
21467 }
21468 goto theend;
21469 }
21470
21471 if (lv.ll_name == NULL)
21472 {
21473 /* Error found, but continue after the function name. */
21474 *pp = end;
21475 goto theend;
21476 }
21477
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021478 /* Check if the name is a Funcref. If so, use the value. */
21479 if (lv.ll_exp_name != NULL)
21480 {
21481 len = (int)STRLEN(lv.ll_exp_name);
21482 name = deref_func_name(lv.ll_exp_name, &len);
21483 if (name == lv.ll_exp_name)
21484 name = NULL;
21485 }
21486 else
21487 {
21488 len = (int)(end - *pp);
21489 name = deref_func_name(*pp, &len);
21490 if (name == *pp)
21491 name = NULL;
21492 }
21493 if (name != NULL)
21494 {
21495 name = vim_strsave(name);
21496 *pp = end;
21497 goto theend;
21498 }
21499
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021500 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021501 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021502 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021503 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21504 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21505 {
21506 /* When there was "s:" already or the name expanded to get a
21507 * leading "s:" then remove it. */
21508 lv.ll_name += 2;
21509 len -= 2;
21510 lead = 2;
21511 }
21512 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021513 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021514 {
21515 if (lead == 2) /* skip over "s:" */
21516 lv.ll_name += 2;
21517 len = (int)(end - lv.ll_name);
21518 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021519
21520 /*
21521 * Copy the function name to allocated memory.
21522 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21523 * Accept <SNR>123_name() outside a script.
21524 */
21525 if (skip)
21526 lead = 0; /* do nothing */
21527 else if (lead > 0)
21528 {
21529 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021530 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21531 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021532 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021533 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021534 if (current_SID <= 0)
21535 {
21536 EMSG(_(e_usingsid));
21537 goto theend;
21538 }
21539 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21540 lead += (int)STRLEN(sid_buf);
21541 }
21542 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021543 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021544 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021545 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021546 goto theend;
21547 }
21548 name = alloc((unsigned)(len + lead + 1));
21549 if (name != NULL)
21550 {
21551 if (lead > 0)
21552 {
21553 name[0] = K_SPECIAL;
21554 name[1] = KS_EXTRA;
21555 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021556 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021557 STRCPY(name + 3, sid_buf);
21558 }
21559 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21560 name[len + lead] = NUL;
21561 }
21562 *pp = end;
21563
21564theend:
21565 clear_lval(&lv);
21566 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567}
21568
21569/*
21570 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21571 * Return 2 if "p" starts with "s:".
21572 * Return 0 otherwise.
21573 */
21574 static int
21575eval_fname_script(p)
21576 char_u *p;
21577{
21578 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21579 || STRNICMP(p + 1, "SNR>", 4) == 0))
21580 return 5;
21581 if (p[0] == 's' && p[1] == ':')
21582 return 2;
21583 return 0;
21584}
21585
21586/*
21587 * Return TRUE if "p" starts with "<SID>" or "s:".
21588 * Only works if eval_fname_script() returned non-zero for "p"!
21589 */
21590 static int
21591eval_fname_sid(p)
21592 char_u *p;
21593{
21594 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21595}
21596
21597/*
21598 * List the head of the function: "name(arg1, arg2)".
21599 */
21600 static void
21601list_func_head(fp, indent)
21602 ufunc_T *fp;
21603 int indent;
21604{
21605 int j;
21606
21607 msg_start();
21608 if (indent)
21609 MSG_PUTS(" ");
21610 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021611 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 {
21613 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021614 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 }
21616 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021617 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021619 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 {
21621 if (j)
21622 MSG_PUTS(", ");
21623 msg_puts(FUNCARG(fp, j));
21624 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021625 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 {
21627 if (j)
21628 MSG_PUTS(", ");
21629 MSG_PUTS("...");
21630 }
21631 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021632 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021633 if (p_verbose > 0)
21634 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635}
21636
21637/*
21638 * Find a function by name, return pointer to it in ufuncs.
21639 * Return NULL for unknown function.
21640 */
21641 static ufunc_T *
21642find_func(name)
21643 char_u *name;
21644{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021645 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021647 hi = hash_find(&func_hashtab, name);
21648 if (!HASHITEM_EMPTY(hi))
21649 return HI2UF(hi);
21650 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651}
21652
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021653#if defined(EXITFREE) || defined(PROTO)
21654 void
21655free_all_functions()
21656{
21657 hashitem_T *hi;
21658
21659 /* Need to start all over every time, because func_free() may change the
21660 * hash table. */
21661 while (func_hashtab.ht_used > 0)
21662 for (hi = func_hashtab.ht_array; ; ++hi)
21663 if (!HASHITEM_EMPTY(hi))
21664 {
21665 func_free(HI2UF(hi));
21666 break;
21667 }
21668}
21669#endif
21670
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021671/*
21672 * Return TRUE if a function "name" exists.
21673 */
21674 static int
21675function_exists(name)
21676 char_u *name;
21677{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021678 char_u *nm = name;
21679 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021680 int n = FALSE;
21681
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021682 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021683 nm = skipwhite(nm);
21684
21685 /* Only accept "funcname", "funcname ", "funcname (..." and
21686 * "funcname(...", not "funcname!...". */
21687 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021688 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021689 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021690 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021691 else
21692 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021693 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021694 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021695 return n;
21696}
21697
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021698/*
21699 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021700 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021701 */
21702 static int
21703builtin_function(name)
21704 char_u *name;
21705{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021706 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21707 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021708}
21709
Bram Moolenaar05159a02005-02-26 23:04:13 +000021710#if defined(FEAT_PROFILE) || defined(PROTO)
21711/*
21712 * Start profiling function "fp".
21713 */
21714 static void
21715func_do_profile(fp)
21716 ufunc_T *fp;
21717{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021718 int len = fp->uf_lines.ga_len;
21719
21720 if (len == 0)
21721 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021722 fp->uf_tm_count = 0;
21723 profile_zero(&fp->uf_tm_self);
21724 profile_zero(&fp->uf_tm_total);
21725 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021726 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021727 if (fp->uf_tml_total == NULL)
21728 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021729 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021730 if (fp->uf_tml_self == NULL)
21731 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021732 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021733 fp->uf_tml_idx = -1;
21734 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21735 || fp->uf_tml_self == NULL)
21736 return; /* out of memory */
21737
21738 fp->uf_profiling = TRUE;
21739}
21740
21741/*
21742 * Dump the profiling results for all functions in file "fd".
21743 */
21744 void
21745func_dump_profile(fd)
21746 FILE *fd;
21747{
21748 hashitem_T *hi;
21749 int todo;
21750 ufunc_T *fp;
21751 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021752 ufunc_T **sorttab;
21753 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021754
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021755 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021756 if (todo == 0)
21757 return; /* nothing to dump */
21758
Bram Moolenaar73830342005-02-28 22:48:19 +000021759 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21760
Bram Moolenaar05159a02005-02-26 23:04:13 +000021761 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21762 {
21763 if (!HASHITEM_EMPTY(hi))
21764 {
21765 --todo;
21766 fp = HI2UF(hi);
21767 if (fp->uf_profiling)
21768 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021769 if (sorttab != NULL)
21770 sorttab[st_len++] = fp;
21771
Bram Moolenaar05159a02005-02-26 23:04:13 +000021772 if (fp->uf_name[0] == K_SPECIAL)
21773 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21774 else
21775 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21776 if (fp->uf_tm_count == 1)
21777 fprintf(fd, "Called 1 time\n");
21778 else
21779 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21780 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21781 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21782 fprintf(fd, "\n");
21783 fprintf(fd, "count total (s) self (s)\n");
21784
21785 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21786 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021787 if (FUNCLINE(fp, i) == NULL)
21788 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021789 prof_func_line(fd, fp->uf_tml_count[i],
21790 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021791 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21792 }
21793 fprintf(fd, "\n");
21794 }
21795 }
21796 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021797
21798 if (sorttab != NULL && st_len > 0)
21799 {
21800 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21801 prof_total_cmp);
21802 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21803 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21804 prof_self_cmp);
21805 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21806 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021807
21808 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021809}
Bram Moolenaar73830342005-02-28 22:48:19 +000021810
21811 static void
21812prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21813 FILE *fd;
21814 ufunc_T **sorttab;
21815 int st_len;
21816 char *title;
21817 int prefer_self; /* when equal print only self time */
21818{
21819 int i;
21820 ufunc_T *fp;
21821
21822 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21823 fprintf(fd, "count total (s) self (s) function\n");
21824 for (i = 0; i < 20 && i < st_len; ++i)
21825 {
21826 fp = sorttab[i];
21827 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21828 prefer_self);
21829 if (fp->uf_name[0] == K_SPECIAL)
21830 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21831 else
21832 fprintf(fd, " %s()\n", fp->uf_name);
21833 }
21834 fprintf(fd, "\n");
21835}
21836
21837/*
21838 * Print the count and times for one function or function line.
21839 */
21840 static void
21841prof_func_line(fd, count, total, self, prefer_self)
21842 FILE *fd;
21843 int count;
21844 proftime_T *total;
21845 proftime_T *self;
21846 int prefer_self; /* when equal print only self time */
21847{
21848 if (count > 0)
21849 {
21850 fprintf(fd, "%5d ", count);
21851 if (prefer_self && profile_equal(total, self))
21852 fprintf(fd, " ");
21853 else
21854 fprintf(fd, "%s ", profile_msg(total));
21855 if (!prefer_self && profile_equal(total, self))
21856 fprintf(fd, " ");
21857 else
21858 fprintf(fd, "%s ", profile_msg(self));
21859 }
21860 else
21861 fprintf(fd, " ");
21862}
21863
21864/*
21865 * Compare function for total time sorting.
21866 */
21867 static int
21868#ifdef __BORLANDC__
21869_RTLENTRYF
21870#endif
21871prof_total_cmp(s1, s2)
21872 const void *s1;
21873 const void *s2;
21874{
21875 ufunc_T *p1, *p2;
21876
21877 p1 = *(ufunc_T **)s1;
21878 p2 = *(ufunc_T **)s2;
21879 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21880}
21881
21882/*
21883 * Compare function for self time sorting.
21884 */
21885 static int
21886#ifdef __BORLANDC__
21887_RTLENTRYF
21888#endif
21889prof_self_cmp(s1, s2)
21890 const void *s1;
21891 const void *s2;
21892{
21893 ufunc_T *p1, *p2;
21894
21895 p1 = *(ufunc_T **)s1;
21896 p2 = *(ufunc_T **)s2;
21897 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21898}
21899
Bram Moolenaar05159a02005-02-26 23:04:13 +000021900#endif
21901
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021902/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021903 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021904 * Return TRUE if a package was loaded.
21905 */
21906 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021907script_autoload(name, reload)
21908 char_u *name;
21909 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021910{
21911 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021912 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021913 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021914 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021915
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021916 /* Return quickly when autoload disabled. */
21917 if (no_autoload)
21918 return FALSE;
21919
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021920 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021921 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021922 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021923 return FALSE;
21924
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021925 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021926
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021927 /* Find the name in the list of previously loaded package names. Skip
21928 * "autoload/", it's always the same. */
21929 for (i = 0; i < ga_loaded.ga_len; ++i)
21930 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21931 break;
21932 if (!reload && i < ga_loaded.ga_len)
21933 ret = FALSE; /* was loaded already */
21934 else
21935 {
21936 /* Remember the name if it wasn't loaded already. */
21937 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21938 {
21939 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21940 tofree = NULL;
21941 }
21942
21943 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021944 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021945 ret = TRUE;
21946 }
21947
21948 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021949 return ret;
21950}
21951
21952/*
21953 * Return the autoload script name for a function or variable name.
21954 * Returns NULL when out of memory.
21955 */
21956 static char_u *
21957autoload_name(name)
21958 char_u *name;
21959{
21960 char_u *p;
21961 char_u *scriptname;
21962
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021963 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021964 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21965 if (scriptname == NULL)
21966 return FALSE;
21967 STRCPY(scriptname, "autoload/");
21968 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021969 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021970 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021971 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021972 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021973 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021974}
21975
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21977
21978/*
21979 * Function given to ExpandGeneric() to obtain the list of user defined
21980 * function names.
21981 */
21982 char_u *
21983get_user_func_name(xp, idx)
21984 expand_T *xp;
21985 int idx;
21986{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021987 static long_u done;
21988 static hashitem_T *hi;
21989 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990
21991 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021993 done = 0;
21994 hi = func_hashtab.ht_array;
21995 }
21996 if (done < func_hashtab.ht_used)
21997 {
21998 if (done++ > 0)
21999 ++hi;
22000 while (HASHITEM_EMPTY(hi))
22001 ++hi;
22002 fp = HI2UF(hi);
22003
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022004 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022005 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022006
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022007 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22008 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009
22010 cat_func_name(IObuff, fp);
22011 if (xp->xp_context != EXPAND_USER_FUNC)
22012 {
22013 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022014 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015 STRCAT(IObuff, ")");
22016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 return IObuff;
22018 }
22019 return NULL;
22020}
22021
22022#endif /* FEAT_CMDL_COMPL */
22023
22024/*
22025 * Copy the function name of "fp" to buffer "buf".
22026 * "buf" must be able to hold the function name plus three bytes.
22027 * Takes care of script-local function names.
22028 */
22029 static void
22030cat_func_name(buf, fp)
22031 char_u *buf;
22032 ufunc_T *fp;
22033{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022034 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 {
22036 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022037 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 }
22039 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022040 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041}
22042
22043/*
22044 * ":delfunction {name}"
22045 */
22046 void
22047ex_delfunction(eap)
22048 exarg_T *eap;
22049{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022050 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051 char_u *p;
22052 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022053 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054
22055 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022056 name = trans_function_name(&p, eap->skip, 0, &fudi);
22057 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022059 {
22060 if (fudi.fd_dict != NULL && !eap->skip)
22061 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 if (!ends_excmd(*skipwhite(p)))
22065 {
22066 vim_free(name);
22067 EMSG(_(e_trailing));
22068 return;
22069 }
22070 eap->nextcmd = check_nextcmd(p);
22071 if (eap->nextcmd != NULL)
22072 *p = NUL;
22073
22074 if (!eap->skip)
22075 fp = find_func(name);
22076 vim_free(name);
22077
22078 if (!eap->skip)
22079 {
22080 if (fp == NULL)
22081 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022082 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 return;
22084 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022085 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086 {
22087 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22088 return;
22089 }
22090
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022091 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022093 /* Delete the dict item that refers to the function, it will
22094 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022095 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022097 else
22098 func_free(fp);
22099 }
22100}
22101
22102/*
22103 * Free a function and remove it from the list of functions.
22104 */
22105 static void
22106func_free(fp)
22107 ufunc_T *fp;
22108{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022109 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022110
22111 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022112 ga_clear_strings(&(fp->uf_args));
22113 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022114#ifdef FEAT_PROFILE
22115 vim_free(fp->uf_tml_count);
22116 vim_free(fp->uf_tml_total);
22117 vim_free(fp->uf_tml_self);
22118#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022119
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022120 /* remove the function from the function hashtable */
22121 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22122 if (HASHITEM_EMPTY(hi))
22123 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022124 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022125 hash_remove(&func_hashtab, hi);
22126
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022127 vim_free(fp);
22128}
22129
22130/*
22131 * Unreference a Function: decrement the reference count and free it when it
22132 * becomes zero. Only for numbered functions.
22133 */
22134 static void
22135func_unref(name)
22136 char_u *name;
22137{
22138 ufunc_T *fp;
22139
22140 if (name != NULL && isdigit(*name))
22141 {
22142 fp = find_func(name);
22143 if (fp == NULL)
22144 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022145 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022146 {
22147 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022148 * when "uf_calls" becomes zero. */
22149 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022150 func_free(fp);
22151 }
22152 }
22153}
22154
22155/*
22156 * Count a reference to a Function.
22157 */
22158 static void
22159func_ref(name)
22160 char_u *name;
22161{
22162 ufunc_T *fp;
22163
22164 if (name != NULL && isdigit(*name))
22165 {
22166 fp = find_func(name);
22167 if (fp == NULL)
22168 EMSG2(_(e_intern2), "func_ref()");
22169 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022170 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 }
22172}
22173
22174/*
22175 * Call a user function.
22176 */
22177 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022178call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179 ufunc_T *fp; /* pointer to function */
22180 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022181 typval_T *argvars; /* arguments */
22182 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022183 linenr_T firstline; /* first line of range */
22184 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022185 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186{
Bram Moolenaar33570922005-01-25 22:26:29 +000022187 char_u *save_sourcing_name;
22188 linenr_T save_sourcing_lnum;
22189 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022190 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022191 int save_did_emsg;
22192 static int depth = 0;
22193 dictitem_T *v;
22194 int fixvar_idx = 0; /* index in fixvar[] */
22195 int i;
22196 int ai;
22197 char_u numbuf[NUMBUFLEN];
22198 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022199#ifdef FEAT_PROFILE
22200 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022201 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203
22204 /* If depth of calling is getting too high, don't execute the function */
22205 if (depth >= p_mfd)
22206 {
22207 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022208 rettv->v_type = VAR_NUMBER;
22209 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 return;
22211 }
22212 ++depth;
22213
22214 line_breakcheck(); /* check for CTRL-C hit */
22215
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022216 fc = (funccall_T *)alloc(sizeof(funccall_T));
22217 fc->caller = current_funccal;
22218 current_funccal = fc;
22219 fc->func = fp;
22220 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022221 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022222 fc->linenr = 0;
22223 fc->returned = FALSE;
22224 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022226 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22227 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228
Bram Moolenaar33570922005-01-25 22:26:29 +000022229 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022230 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022231 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22232 * each argument variable and saves a lot of time.
22233 */
22234 /*
22235 * Init l: variables.
22236 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022237 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000022238 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022239 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022240 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22241 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022242 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022243 name = v->di_key;
22244 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022245 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022246 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022247 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022248 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022249 v->di_tv.vval.v_dict = selfdict;
22250 ++selfdict->dv_refcount;
22251 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022252
Bram Moolenaar33570922005-01-25 22:26:29 +000022253 /*
22254 * Init a: variables.
22255 * Set a:0 to "argcount".
22256 * Set a:000 to a list with room for the "..." arguments.
22257 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022258 init_var_dict(&fc->l_avars, &fc->l_avars_var);
22259 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022260 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022261 /* Use "name" to avoid a warning from some compiler that checks the
22262 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022263 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022264 name = v->di_key;
22265 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022266 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022267 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022268 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022269 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022270 v->di_tv.vval.v_list = &fc->l_varlist;
22271 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22272 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22273 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022274
22275 /*
22276 * Set a:firstline to "firstline" and a:lastline to "lastline".
22277 * Set a:name to named arguments.
22278 * Set a:N to the "..." arguments.
22279 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022280 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022281 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022282 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022283 (varnumber_T)lastline);
22284 for (i = 0; i < argcount; ++i)
22285 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022286 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022287 if (ai < 0)
22288 /* named argument a:name */
22289 name = FUNCARG(fp, i);
22290 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022291 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022292 /* "..." argument a:1, a:2, etc. */
22293 sprintf((char *)numbuf, "%d", ai + 1);
22294 name = numbuf;
22295 }
22296 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22297 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022298 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022299 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22300 }
22301 else
22302 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022303 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22304 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022305 if (v == NULL)
22306 break;
22307 v->di_flags = DI_FLAGS_RO;
22308 }
22309 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022310 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022311
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022312 /* Note: the values are copied directly to avoid alloc/free.
22313 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022314 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022315 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022316
22317 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22318 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022319 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22320 fc->l_listitems[ai].li_tv = argvars[i];
22321 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022322 }
22323 }
22324
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325 /* Don't redraw while executing the function. */
22326 ++RedrawingDisabled;
22327 save_sourcing_name = sourcing_name;
22328 save_sourcing_lnum = sourcing_lnum;
22329 sourcing_lnum = 1;
22330 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022331 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332 if (sourcing_name != NULL)
22333 {
22334 if (save_sourcing_name != NULL
22335 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22336 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22337 else
22338 STRCPY(sourcing_name, "function ");
22339 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22340
22341 if (p_verbose >= 12)
22342 {
22343 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022344 verbose_enter_scroll();
22345
Bram Moolenaar555b2802005-05-19 21:08:39 +000022346 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347 if (p_verbose >= 14)
22348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022350 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022351 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022352 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353
22354 msg_puts((char_u *)"(");
22355 for (i = 0; i < argcount; ++i)
22356 {
22357 if (i > 0)
22358 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022359 if (argvars[i].v_type == VAR_NUMBER)
22360 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 else
22362 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022363 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22364 if (s != NULL)
22365 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022366 if (vim_strsize(s) > MSG_BUF_CLEN)
22367 {
22368 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22369 s = buf;
22370 }
22371 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022372 vim_free(tofree);
22373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 }
22375 }
22376 msg_puts((char_u *)")");
22377 }
22378 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022379
22380 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 --no_wait_return;
22382 }
22383 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022384#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022385 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022386 {
22387 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22388 func_do_profile(fp);
22389 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022390 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022391 {
22392 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022393 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022394 profile_zero(&fp->uf_tm_children);
22395 }
22396 script_prof_save(&wait_start);
22397 }
22398#endif
22399
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022401 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402 save_did_emsg = did_emsg;
22403 did_emsg = FALSE;
22404
22405 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022406 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22408
22409 --RedrawingDisabled;
22410
22411 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022412 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022414 clear_tv(rettv);
22415 rettv->v_type = VAR_NUMBER;
22416 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417 }
22418
Bram Moolenaar05159a02005-02-26 23:04:13 +000022419#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022420 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022421 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022422 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022423 profile_end(&call_start);
22424 profile_sub_wait(&wait_start, &call_start);
22425 profile_add(&fp->uf_tm_total, &call_start);
22426 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022427 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022428 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022429 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22430 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022431 }
22432 }
22433#endif
22434
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435 /* when being verbose, mention the return value */
22436 if (p_verbose >= 12)
22437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022439 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022442 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022443 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022444 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022445 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022446 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022448 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022449 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022450 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022451 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022452
Bram Moolenaar555b2802005-05-19 21:08:39 +000022453 /* The value may be very long. Skip the middle part, so that we
22454 * have some idea how it starts and ends. smsg() would always
22455 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022456 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022457 if (s != NULL)
22458 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022459 if (vim_strsize(s) > MSG_BUF_CLEN)
22460 {
22461 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22462 s = buf;
22463 }
22464 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022465 vim_free(tofree);
22466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 }
22468 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022469
22470 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471 --no_wait_return;
22472 }
22473
22474 vim_free(sourcing_name);
22475 sourcing_name = save_sourcing_name;
22476 sourcing_lnum = save_sourcing_lnum;
22477 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022478#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022479 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022480 script_prof_restore(&wait_start);
22481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482
22483 if (p_verbose >= 12 && sourcing_name != NULL)
22484 {
22485 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022486 verbose_enter_scroll();
22487
Bram Moolenaar555b2802005-05-19 21:08:39 +000022488 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022490
22491 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 --no_wait_return;
22493 }
22494
22495 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022496 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022498
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022499 /* If the a:000 list and the l: and a: dicts are not referenced we can
22500 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022501 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22502 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22503 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22504 {
22505 free_funccal(fc, FALSE);
22506 }
22507 else
22508 {
22509 hashitem_T *hi;
22510 listitem_T *li;
22511 int todo;
22512
22513 /* "fc" is still in use. This can happen when returning "a:000" or
22514 * assigning "l:" to a global variable.
22515 * Link "fc" in the list for garbage collection later. */
22516 fc->caller = previous_funccal;
22517 previous_funccal = fc;
22518
22519 /* Make a copy of the a: variables, since we didn't do that above. */
22520 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22521 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22522 {
22523 if (!HASHITEM_EMPTY(hi))
22524 {
22525 --todo;
22526 v = HI2DI(hi);
22527 copy_tv(&v->di_tv, &v->di_tv);
22528 }
22529 }
22530
22531 /* Make a copy of the a:000 items, since we didn't do that above. */
22532 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22533 copy_tv(&li->li_tv, &li->li_tv);
22534 }
22535}
22536
22537/*
22538 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022539 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022540 */
22541 static int
22542can_free_funccal(fc, copyID)
22543 funccall_T *fc;
22544 int copyID;
22545{
22546 return (fc->l_varlist.lv_copyID != copyID
22547 && fc->l_vars.dv_copyID != copyID
22548 && fc->l_avars.dv_copyID != copyID);
22549}
22550
22551/*
22552 * Free "fc" and what it contains.
22553 */
22554 static void
22555free_funccal(fc, free_val)
22556 funccall_T *fc;
22557 int free_val; /* a: vars were allocated */
22558{
22559 listitem_T *li;
22560
22561 /* The a: variables typevals may not have been allocated, only free the
22562 * allocated variables. */
22563 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22564
22565 /* free all l: variables */
22566 vars_clear(&fc->l_vars.dv_hashtab);
22567
22568 /* Free the a:000 variables if they were allocated. */
22569 if (free_val)
22570 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22571 clear_tv(&li->li_tv);
22572
22573 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574}
22575
22576/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022577 * Add a number variable "name" to dict "dp" with value "nr".
22578 */
22579 static void
22580add_nr_var(dp, v, name, nr)
22581 dict_T *dp;
22582 dictitem_T *v;
22583 char *name;
22584 varnumber_T nr;
22585{
22586 STRCPY(v->di_key, name);
22587 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22588 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22589 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022590 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022591 v->di_tv.vval.v_number = nr;
22592}
22593
22594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 * ":return [expr]"
22596 */
22597 void
22598ex_return(eap)
22599 exarg_T *eap;
22600{
22601 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022602 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603 int returning = FALSE;
22604
22605 if (current_funccal == NULL)
22606 {
22607 EMSG(_("E133: :return not inside a function"));
22608 return;
22609 }
22610
22611 if (eap->skip)
22612 ++emsg_skip;
22613
22614 eap->nextcmd = NULL;
22615 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022616 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 {
22618 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022619 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022621 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 }
22623 /* It's safer to return also on error. */
22624 else if (!eap->skip)
22625 {
22626 /*
22627 * Return unless the expression evaluation has been cancelled due to an
22628 * aborting error, an interrupt, or an exception.
22629 */
22630 if (!aborting())
22631 returning = do_return(eap, FALSE, TRUE, NULL);
22632 }
22633
22634 /* When skipping or the return gets pending, advance to the next command
22635 * in this line (!returning). Otherwise, ignore the rest of the line.
22636 * Following lines will be ignored by get_func_line(). */
22637 if (returning)
22638 eap->nextcmd = NULL;
22639 else if (eap->nextcmd == NULL) /* no argument */
22640 eap->nextcmd = check_nextcmd(arg);
22641
22642 if (eap->skip)
22643 --emsg_skip;
22644}
22645
22646/*
22647 * Return from a function. Possibly makes the return pending. Also called
22648 * for a pending return at the ":endtry" or after returning from an extra
22649 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022650 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022651 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 * FALSE when the return gets pending.
22653 */
22654 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022655do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 exarg_T *eap;
22657 int reanimate;
22658 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022659 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660{
22661 int idx;
22662 struct condstack *cstack = eap->cstack;
22663
22664 if (reanimate)
22665 /* Undo the return. */
22666 current_funccal->returned = FALSE;
22667
22668 /*
22669 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22670 * not in its finally clause (which then is to be executed next) is found.
22671 * In this case, make the ":return" pending for execution at the ":endtry".
22672 * Otherwise, return normally.
22673 */
22674 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22675 if (idx >= 0)
22676 {
22677 cstack->cs_pending[idx] = CSTP_RETURN;
22678
22679 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022680 /* A pending return again gets pending. "rettv" points to an
22681 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022683 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 else
22685 {
22686 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022687 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022688 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022689 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022691 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 {
22693 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022694 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022695 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696 else
22697 EMSG(_(e_outofmem));
22698 }
22699 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022700 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701
22702 if (reanimate)
22703 {
22704 /* The pending return value could be overwritten by a ":return"
22705 * without argument in a finally clause; reset the default
22706 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022707 current_funccal->rettv->v_type = VAR_NUMBER;
22708 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 }
22710 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022711 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 }
22713 else
22714 {
22715 current_funccal->returned = TRUE;
22716
22717 /* If the return is carried out now, store the return value. For
22718 * a return immediately after reanimation, the value is already
22719 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022720 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022722 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022723 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022725 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 }
22727 }
22728
22729 return idx < 0;
22730}
22731
22732/*
22733 * Free the variable with a pending return value.
22734 */
22735 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022736discard_pending_return(rettv)
22737 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738{
Bram Moolenaar33570922005-01-25 22:26:29 +000022739 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740}
22741
22742/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022743 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 * is an allocated string. Used by report_pending() for verbose messages.
22745 */
22746 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022747get_return_cmd(rettv)
22748 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022750 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022751 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022752 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022754 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022755 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022756 if (s == NULL)
22757 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022758
22759 STRCPY(IObuff, ":return ");
22760 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22761 if (STRLEN(s) + 8 >= IOSIZE)
22762 STRCPY(IObuff + IOSIZE - 4, "...");
22763 vim_free(tofree);
22764 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765}
22766
22767/*
22768 * Get next function line.
22769 * Called by do_cmdline() to get the next line.
22770 * Returns allocated string, or NULL for end of function.
22771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 char_u *
22773get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022774 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022776 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777{
Bram Moolenaar33570922005-01-25 22:26:29 +000022778 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022779 ufunc_T *fp = fcp->func;
22780 char_u *retval;
22781 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782
22783 /* If breakpoints have been added/deleted need to check for it. */
22784 if (fcp->dbg_tick != debug_tick)
22785 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022786 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022787 sourcing_lnum);
22788 fcp->dbg_tick = debug_tick;
22789 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022790#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022791 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022792 func_line_end(cookie);
22793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794
Bram Moolenaar05159a02005-02-26 23:04:13 +000022795 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022796 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22797 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798 retval = NULL;
22799 else
22800 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022801 /* Skip NULL lines (continuation lines). */
22802 while (fcp->linenr < gap->ga_len
22803 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22804 ++fcp->linenr;
22805 if (fcp->linenr >= gap->ga_len)
22806 retval = NULL;
22807 else
22808 {
22809 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22810 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022811#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022812 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022813 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022814#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816 }
22817
22818 /* Did we encounter a breakpoint? */
22819 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22820 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022821 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022822 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022823 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824 sourcing_lnum);
22825 fcp->dbg_tick = debug_tick;
22826 }
22827
22828 return retval;
22829}
22830
Bram Moolenaar05159a02005-02-26 23:04:13 +000022831#if defined(FEAT_PROFILE) || defined(PROTO)
22832/*
22833 * Called when starting to read a function line.
22834 * "sourcing_lnum" must be correct!
22835 * When skipping lines it may not actually be executed, but we won't find out
22836 * until later and we need to store the time now.
22837 */
22838 void
22839func_line_start(cookie)
22840 void *cookie;
22841{
22842 funccall_T *fcp = (funccall_T *)cookie;
22843 ufunc_T *fp = fcp->func;
22844
22845 if (fp->uf_profiling && sourcing_lnum >= 1
22846 && sourcing_lnum <= fp->uf_lines.ga_len)
22847 {
22848 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022849 /* Skip continuation lines. */
22850 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22851 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022852 fp->uf_tml_execed = FALSE;
22853 profile_start(&fp->uf_tml_start);
22854 profile_zero(&fp->uf_tml_children);
22855 profile_get_wait(&fp->uf_tml_wait);
22856 }
22857}
22858
22859/*
22860 * Called when actually executing a function line.
22861 */
22862 void
22863func_line_exec(cookie)
22864 void *cookie;
22865{
22866 funccall_T *fcp = (funccall_T *)cookie;
22867 ufunc_T *fp = fcp->func;
22868
22869 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22870 fp->uf_tml_execed = TRUE;
22871}
22872
22873/*
22874 * Called when done with a function line.
22875 */
22876 void
22877func_line_end(cookie)
22878 void *cookie;
22879{
22880 funccall_T *fcp = (funccall_T *)cookie;
22881 ufunc_T *fp = fcp->func;
22882
22883 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22884 {
22885 if (fp->uf_tml_execed)
22886 {
22887 ++fp->uf_tml_count[fp->uf_tml_idx];
22888 profile_end(&fp->uf_tml_start);
22889 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022890 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022891 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22892 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022893 }
22894 fp->uf_tml_idx = -1;
22895 }
22896}
22897#endif
22898
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899/*
22900 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022901 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 */
22903 int
22904func_has_ended(cookie)
22905 void *cookie;
22906{
Bram Moolenaar33570922005-01-25 22:26:29 +000022907 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908
22909 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22910 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022911 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 || fcp->returned);
22913}
22914
22915/*
22916 * return TRUE if cookie indicates a function which "abort"s on errors.
22917 */
22918 int
22919func_has_abort(cookie)
22920 void *cookie;
22921{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022922 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923}
22924
22925#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22926typedef enum
22927{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022928 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22929 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22930 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931} var_flavour_T;
22932
22933static var_flavour_T var_flavour __ARGS((char_u *varname));
22934
22935 static var_flavour_T
22936var_flavour(varname)
22937 char_u *varname;
22938{
22939 char_u *p = varname;
22940
22941 if (ASCII_ISUPPER(*p))
22942 {
22943 while (*(++p))
22944 if (ASCII_ISLOWER(*p))
22945 return VAR_FLAVOUR_SESSION;
22946 return VAR_FLAVOUR_VIMINFO;
22947 }
22948 else
22949 return VAR_FLAVOUR_DEFAULT;
22950}
22951#endif
22952
22953#if defined(FEAT_VIMINFO) || defined(PROTO)
22954/*
22955 * Restore global vars that start with a capital from the viminfo file
22956 */
22957 int
22958read_viminfo_varlist(virp, writing)
22959 vir_T *virp;
22960 int writing;
22961{
22962 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022963 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022964 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965
22966 if (!writing && (find_viminfo_parameter('!') != NULL))
22967 {
22968 tab = vim_strchr(virp->vir_line + 1, '\t');
22969 if (tab != NULL)
22970 {
22971 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022972 switch (*tab)
22973 {
22974 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022975#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022976 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022977#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022978 case 'D': type = VAR_DICT; break;
22979 case 'L': type = VAR_LIST; break;
22980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981
22982 tab = vim_strchr(tab, '\t');
22983 if (tab != NULL)
22984 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022985 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022986 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022987 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022989#ifdef FEAT_FLOAT
22990 else if (type == VAR_FLOAT)
22991 (void)string2float(tab + 1, &tv.vval.v_float);
22992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022994 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022995 if (type == VAR_DICT || type == VAR_LIST)
22996 {
22997 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22998
22999 if (etv == NULL)
23000 /* Failed to parse back the dict or list, use it as a
23001 * string. */
23002 tv.v_type = VAR_STRING;
23003 else
23004 {
23005 vim_free(tv.vval.v_string);
23006 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023007 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023008 }
23009 }
23010
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023011 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023012
23013 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023014 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023015 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23016 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 }
23018 }
23019 }
23020
23021 return viminfo_readline(virp);
23022}
23023
23024/*
23025 * Write global vars that start with a capital to the viminfo file
23026 */
23027 void
23028write_viminfo_varlist(fp)
23029 FILE *fp;
23030{
Bram Moolenaar33570922005-01-25 22:26:29 +000023031 hashitem_T *hi;
23032 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023033 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023034 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023035 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023036 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023037 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038
23039 if (find_viminfo_parameter('!') == NULL)
23040 return;
23041
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023042 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023043
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023044 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023045 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023047 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023048 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023049 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023050 this_var = HI2DI(hi);
23051 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023052 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023053 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023054 {
23055 case VAR_STRING: s = "STR"; break;
23056 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023057#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023058 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023059#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023060 case VAR_DICT: s = "DIC"; break;
23061 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023062 default: continue;
23063 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023064 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023065 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023066 if (p != NULL)
23067 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023068 vim_free(tofree);
23069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070 }
23071 }
23072}
23073#endif
23074
23075#if defined(FEAT_SESSION) || defined(PROTO)
23076 int
23077store_session_globals(fd)
23078 FILE *fd;
23079{
Bram Moolenaar33570922005-01-25 22:26:29 +000023080 hashitem_T *hi;
23081 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023082 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083 char_u *p, *t;
23084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023085 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023086 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023088 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023090 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023091 this_var = HI2DI(hi);
23092 if ((this_var->di_tv.v_type == VAR_NUMBER
23093 || this_var->di_tv.v_type == VAR_STRING)
23094 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023095 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023096 /* Escape special characters with a backslash. Turn a LF and
23097 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023098 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023099 (char_u *)"\\\"\n\r");
23100 if (p == NULL) /* out of memory */
23101 break;
23102 for (t = p; *t != NUL; ++t)
23103 if (*t == '\n')
23104 *t = 'n';
23105 else if (*t == '\r')
23106 *t = 'r';
23107 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023108 this_var->di_key,
23109 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23110 : ' ',
23111 p,
23112 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23113 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023114 || put_eol(fd) == FAIL)
23115 {
23116 vim_free(p);
23117 return FAIL;
23118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119 vim_free(p);
23120 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023121#ifdef FEAT_FLOAT
23122 else if (this_var->di_tv.v_type == VAR_FLOAT
23123 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23124 {
23125 float_T f = this_var->di_tv.vval.v_float;
23126 int sign = ' ';
23127
23128 if (f < 0)
23129 {
23130 f = -f;
23131 sign = '-';
23132 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023133 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023134 this_var->di_key, sign, f) < 0)
23135 || put_eol(fd) == FAIL)
23136 return FAIL;
23137 }
23138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139 }
23140 }
23141 return OK;
23142}
23143#endif
23144
Bram Moolenaar661b1822005-07-28 22:36:45 +000023145/*
23146 * Display script name where an item was last set.
23147 * Should only be invoked when 'verbose' is non-zero.
23148 */
23149 void
23150last_set_msg(scriptID)
23151 scid_T scriptID;
23152{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023153 char_u *p;
23154
Bram Moolenaar661b1822005-07-28 22:36:45 +000023155 if (scriptID != 0)
23156 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023157 p = home_replace_save(NULL, get_scriptname(scriptID));
23158 if (p != NULL)
23159 {
23160 verbose_enter();
23161 MSG_PUTS(_("\n\tLast set from "));
23162 MSG_PUTS(p);
23163 vim_free(p);
23164 verbose_leave();
23165 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023166 }
23167}
23168
Bram Moolenaard812df62008-11-09 12:46:09 +000023169/*
23170 * List v:oldfiles in a nice way.
23171 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023172 void
23173ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023174 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023175{
23176 list_T *l = vimvars[VV_OLDFILES].vv_list;
23177 listitem_T *li;
23178 int nr = 0;
23179
23180 if (l == NULL)
23181 msg((char_u *)_("No old files"));
23182 else
23183 {
23184 msg_start();
23185 msg_scroll = TRUE;
23186 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23187 {
23188 msg_outnum((long)++nr);
23189 MSG_PUTS(": ");
23190 msg_outtrans(get_tv_string(&li->li_tv));
23191 msg_putchar('\n');
23192 out_flush(); /* output one line at a time */
23193 ui_breakcheck();
23194 }
23195 /* Assume "got_int" was set to truncate the listing. */
23196 got_int = FALSE;
23197
23198#ifdef FEAT_BROWSE_CMD
23199 if (cmdmod.browse)
23200 {
23201 quit_more = FALSE;
23202 nr = prompt_for_number(FALSE);
23203 msg_starthere();
23204 if (nr > 0)
23205 {
23206 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23207 (long)nr);
23208
23209 if (p != NULL)
23210 {
23211 p = expand_env_save(p);
23212 eap->arg = p;
23213 eap->cmdidx = CMD_edit;
23214 cmdmod.browse = FALSE;
23215 do_exedit(eap, NULL);
23216 vim_free(p);
23217 }
23218 }
23219 }
23220#endif
23221 }
23222}
23223
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224#endif /* FEAT_EVAL */
23225
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023227#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228
23229#ifdef WIN3264
23230/*
23231 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23232 */
23233static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23234static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23235static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23236
23237/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023238 * Get the short path (8.3) for the filename in "fnamep".
23239 * Only works for a valid file name.
23240 * When the path gets longer "fnamep" is changed and the allocated buffer
23241 * is put in "bufp".
23242 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23243 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244 */
23245 static int
23246get_short_pathname(fnamep, bufp, fnamelen)
23247 char_u **fnamep;
23248 char_u **bufp;
23249 int *fnamelen;
23250{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023251 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 char_u *newbuf;
23253
23254 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255 l = GetShortPathName(*fnamep, *fnamep, len);
23256 if (l > len - 1)
23257 {
23258 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023259 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 newbuf = vim_strnsave(*fnamep, l);
23261 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263
23264 vim_free(*bufp);
23265 *fnamep = *bufp = newbuf;
23266
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023267 /* Really should always succeed, as the buffer is big enough. */
23268 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 }
23270
23271 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023272 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273}
23274
23275/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023276 * Get the short path (8.3) for the filename in "fname". The converted
23277 * path is returned in "bufp".
23278 *
23279 * Some of the directories specified in "fname" may not exist. This function
23280 * will shorten the existing directories at the beginning of the path and then
23281 * append the remaining non-existing path.
23282 *
23283 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023284 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023285 * bufp - Pointer to an allocated buffer for the filename.
23286 * fnamelen - Length of the filename pointed to by fname
23287 *
23288 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289 */
23290 static int
23291shortpath_for_invalid_fname(fname, bufp, fnamelen)
23292 char_u **fname;
23293 char_u **bufp;
23294 int *fnamelen;
23295{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023296 char_u *short_fname, *save_fname, *pbuf_unused;
23297 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023299 int old_len, len;
23300 int new_len, sfx_len;
23301 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302
23303 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023304 old_len = *fnamelen;
23305 save_fname = vim_strnsave(*fname, old_len);
23306 pbuf_unused = NULL;
23307 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023309 endp = save_fname + old_len - 1; /* Find the end of the copy */
23310 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023312 /*
23313 * Try shortening the supplied path till it succeeds by removing one
23314 * directory at a time from the tail of the path.
23315 */
23316 len = 0;
23317 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023318 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023319 /* go back one path-separator */
23320 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23321 --endp;
23322 if (endp <= save_fname)
23323 break; /* processed the complete path */
23324
23325 /*
23326 * Replace the path separator with a NUL and try to shorten the
23327 * resulting path.
23328 */
23329 ch = *endp;
23330 *endp = 0;
23331 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023332 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023333 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23334 {
23335 retval = FAIL;
23336 goto theend;
23337 }
23338 *endp = ch; /* preserve the string */
23339
23340 if (len > 0)
23341 break; /* successfully shortened the path */
23342
23343 /* failed to shorten the path. Skip the path separator */
23344 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345 }
23346
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023347 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023349 /*
23350 * Succeeded in shortening the path. Now concatenate the shortened
23351 * path with the remaining path at the tail.
23352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023354 /* Compute the length of the new path. */
23355 sfx_len = (int)(save_endp - endp) + 1;
23356 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023357
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023358 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023359 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023360 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023362 /* There is not enough space in the currently allocated string,
23363 * copy it to a buffer big enough. */
23364 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023365 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023366 {
23367 retval = FAIL;
23368 goto theend;
23369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 }
23371 else
23372 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023373 /* Transfer short_fname to the main buffer (it's big enough),
23374 * unless get_short_pathname() did its work in-place. */
23375 *fname = *bufp = save_fname;
23376 if (short_fname != save_fname)
23377 vim_strncpy(save_fname, short_fname, len);
23378 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023380
23381 /* concat the not-shortened part of the path */
23382 vim_strncpy(*fname + len, endp, sfx_len);
23383 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023384 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023385
23386theend:
23387 vim_free(pbuf_unused);
23388 vim_free(save_fname);
23389
23390 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391}
23392
23393/*
23394 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023395 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 */
23397 static int
23398shortpath_for_partial(fnamep, bufp, fnamelen)
23399 char_u **fnamep;
23400 char_u **bufp;
23401 int *fnamelen;
23402{
23403 int sepcount, len, tflen;
23404 char_u *p;
23405 char_u *pbuf, *tfname;
23406 int hasTilde;
23407
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023408 /* Count up the path separators from the RHS.. so we know which part
23409 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023411 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023412 if (vim_ispathsep(*p))
23413 ++sepcount;
23414
23415 /* Need full path first (use expand_env() to remove a "~/") */
23416 hasTilde = (**fnamep == '~');
23417 if (hasTilde)
23418 pbuf = tfname = expand_env_save(*fnamep);
23419 else
23420 pbuf = tfname = FullName_save(*fnamep, FALSE);
23421
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023422 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023424 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23425 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426
23427 if (len == 0)
23428 {
23429 /* Don't have a valid filename, so shorten the rest of the
23430 * path if we can. This CAN give us invalid 8.3 filenames, but
23431 * there's not a lot of point in guessing what it might be.
23432 */
23433 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023434 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23435 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023436 }
23437
23438 /* Count the paths backward to find the beginning of the desired string. */
23439 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023440 {
23441#ifdef FEAT_MBYTE
23442 if (has_mbyte)
23443 p -= mb_head_off(tfname, p);
23444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445 if (vim_ispathsep(*p))
23446 {
23447 if (sepcount == 0 || (hasTilde && sepcount == 1))
23448 break;
23449 else
23450 sepcount --;
23451 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453 if (hasTilde)
23454 {
23455 --p;
23456 if (p >= tfname)
23457 *p = '~';
23458 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023459 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460 }
23461 else
23462 ++p;
23463
23464 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23465 vim_free(*bufp);
23466 *fnamelen = (int)STRLEN(p);
23467 *bufp = pbuf;
23468 *fnamep = p;
23469
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023470 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471}
23472#endif /* WIN3264 */
23473
23474/*
23475 * Adjust a filename, according to a string of modifiers.
23476 * *fnamep must be NUL terminated when called. When returning, the length is
23477 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023478 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479 * When there is an error, *fnamep is set to NULL.
23480 */
23481 int
23482modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23483 char_u *src; /* string with modifiers */
23484 int *usedlen; /* characters after src that are used */
23485 char_u **fnamep; /* file name so far */
23486 char_u **bufp; /* buffer for allocated file name or NULL */
23487 int *fnamelen; /* length of fnamep */
23488{
23489 int valid = 0;
23490 char_u *tail;
23491 char_u *s, *p, *pbuf;
23492 char_u dirname[MAXPATHL];
23493 int c;
23494 int has_fullname = 0;
23495#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023496 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497 int has_shortname = 0;
23498#endif
23499
23500repeat:
23501 /* ":p" - full path/file_name */
23502 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23503 {
23504 has_fullname = 1;
23505
23506 valid |= VALID_PATH;
23507 *usedlen += 2;
23508
23509 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23510 if ((*fnamep)[0] == '~'
23511#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23512 && ((*fnamep)[1] == '/'
23513# ifdef BACKSLASH_IN_FILENAME
23514 || (*fnamep)[1] == '\\'
23515# endif
23516 || (*fnamep)[1] == NUL)
23517
23518#endif
23519 )
23520 {
23521 *fnamep = expand_env_save(*fnamep);
23522 vim_free(*bufp); /* free any allocated file name */
23523 *bufp = *fnamep;
23524 if (*fnamep == NULL)
23525 return -1;
23526 }
23527
23528 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023529 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 {
23531 if (vim_ispathsep(*p)
23532 && p[1] == '.'
23533 && (p[2] == NUL
23534 || vim_ispathsep(p[2])
23535 || (p[2] == '.'
23536 && (p[3] == NUL || vim_ispathsep(p[3])))))
23537 break;
23538 }
23539
23540 /* FullName_save() is slow, don't use it when not needed. */
23541 if (*p != NUL || !vim_isAbsName(*fnamep))
23542 {
23543 *fnamep = FullName_save(*fnamep, *p != NUL);
23544 vim_free(*bufp); /* free any allocated file name */
23545 *bufp = *fnamep;
23546 if (*fnamep == NULL)
23547 return -1;
23548 }
23549
23550 /* Append a path separator to a directory. */
23551 if (mch_isdir(*fnamep))
23552 {
23553 /* Make room for one or two extra characters. */
23554 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23555 vim_free(*bufp); /* free any allocated file name */
23556 *bufp = *fnamep;
23557 if (*fnamep == NULL)
23558 return -1;
23559 add_pathsep(*fnamep);
23560 }
23561 }
23562
23563 /* ":." - path relative to the current directory */
23564 /* ":~" - path relative to the home directory */
23565 /* ":8" - shortname path - postponed till after */
23566 while (src[*usedlen] == ':'
23567 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23568 {
23569 *usedlen += 2;
23570 if (c == '8')
23571 {
23572#ifdef WIN3264
23573 has_shortname = 1; /* Postpone this. */
23574#endif
23575 continue;
23576 }
23577 pbuf = NULL;
23578 /* Need full path first (use expand_env() to remove a "~/") */
23579 if (!has_fullname)
23580 {
23581 if (c == '.' && **fnamep == '~')
23582 p = pbuf = expand_env_save(*fnamep);
23583 else
23584 p = pbuf = FullName_save(*fnamep, FALSE);
23585 }
23586 else
23587 p = *fnamep;
23588
23589 has_fullname = 0;
23590
23591 if (p != NULL)
23592 {
23593 if (c == '.')
23594 {
23595 mch_dirname(dirname, MAXPATHL);
23596 s = shorten_fname(p, dirname);
23597 if (s != NULL)
23598 {
23599 *fnamep = s;
23600 if (pbuf != NULL)
23601 {
23602 vim_free(*bufp); /* free any allocated file name */
23603 *bufp = pbuf;
23604 pbuf = NULL;
23605 }
23606 }
23607 }
23608 else
23609 {
23610 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23611 /* Only replace it when it starts with '~' */
23612 if (*dirname == '~')
23613 {
23614 s = vim_strsave(dirname);
23615 if (s != NULL)
23616 {
23617 *fnamep = s;
23618 vim_free(*bufp);
23619 *bufp = s;
23620 }
23621 }
23622 }
23623 vim_free(pbuf);
23624 }
23625 }
23626
23627 tail = gettail(*fnamep);
23628 *fnamelen = (int)STRLEN(*fnamep);
23629
23630 /* ":h" - head, remove "/file_name", can be repeated */
23631 /* Don't remove the first "/" or "c:\" */
23632 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23633 {
23634 valid |= VALID_HEAD;
23635 *usedlen += 2;
23636 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023637 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023638 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 *fnamelen = (int)(tail - *fnamep);
23640#ifdef VMS
23641 if (*fnamelen > 0)
23642 *fnamelen += 1; /* the path separator is part of the path */
23643#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023644 if (*fnamelen == 0)
23645 {
23646 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23647 p = vim_strsave((char_u *)".");
23648 if (p == NULL)
23649 return -1;
23650 vim_free(*bufp);
23651 *bufp = *fnamep = tail = p;
23652 *fnamelen = 1;
23653 }
23654 else
23655 {
23656 while (tail > s && !after_pathsep(s, tail))
23657 mb_ptr_back(*fnamep, tail);
23658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 }
23660
23661 /* ":8" - shortname */
23662 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23663 {
23664 *usedlen += 2;
23665#ifdef WIN3264
23666 has_shortname = 1;
23667#endif
23668 }
23669
23670#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023671 /*
23672 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 */
23674 if (has_shortname)
23675 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023676 /* Copy the string if it is shortened by :h and when it wasn't copied
23677 * yet, because we are going to change it in place. Avoids changing
23678 * the buffer name for "%:8". */
23679 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 {
23681 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023682 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683 return -1;
23684 vim_free(*bufp);
23685 *bufp = *fnamep = p;
23686 }
23687
23688 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023689 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023690 if (!has_fullname && !vim_isAbsName(*fnamep))
23691 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023692 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693 return -1;
23694 }
23695 else
23696 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023697 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698
Bram Moolenaardc935552011-08-17 15:23:23 +020023699 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023700 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023701 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 return -1;
23703
23704 if (l == 0)
23705 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023706 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023708 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023709 return -1;
23710 }
23711 *fnamelen = l;
23712 }
23713 }
23714#endif /* WIN3264 */
23715
23716 /* ":t" - tail, just the basename */
23717 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23718 {
23719 *usedlen += 2;
23720 *fnamelen -= (int)(tail - *fnamep);
23721 *fnamep = tail;
23722 }
23723
23724 /* ":e" - extension, can be repeated */
23725 /* ":r" - root, without extension, can be repeated */
23726 while (src[*usedlen] == ':'
23727 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23728 {
23729 /* find a '.' in the tail:
23730 * - for second :e: before the current fname
23731 * - otherwise: The last '.'
23732 */
23733 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23734 s = *fnamep - 2;
23735 else
23736 s = *fnamep + *fnamelen - 1;
23737 for ( ; s > tail; --s)
23738 if (s[0] == '.')
23739 break;
23740 if (src[*usedlen + 1] == 'e') /* :e */
23741 {
23742 if (s > tail)
23743 {
23744 *fnamelen += (int)(*fnamep - (s + 1));
23745 *fnamep = s + 1;
23746#ifdef VMS
23747 /* cut version from the extension */
23748 s = *fnamep + *fnamelen - 1;
23749 for ( ; s > *fnamep; --s)
23750 if (s[0] == ';')
23751 break;
23752 if (s > *fnamep)
23753 *fnamelen = s - *fnamep;
23754#endif
23755 }
23756 else if (*fnamep <= tail)
23757 *fnamelen = 0;
23758 }
23759 else /* :r */
23760 {
23761 if (s > tail) /* remove one extension */
23762 *fnamelen = (int)(s - *fnamep);
23763 }
23764 *usedlen += 2;
23765 }
23766
23767 /* ":s?pat?foo?" - substitute */
23768 /* ":gs?pat?foo?" - global substitute */
23769 if (src[*usedlen] == ':'
23770 && (src[*usedlen + 1] == 's'
23771 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23772 {
23773 char_u *str;
23774 char_u *pat;
23775 char_u *sub;
23776 int sep;
23777 char_u *flags;
23778 int didit = FALSE;
23779
23780 flags = (char_u *)"";
23781 s = src + *usedlen + 2;
23782 if (src[*usedlen + 1] == 'g')
23783 {
23784 flags = (char_u *)"g";
23785 ++s;
23786 }
23787
23788 sep = *s++;
23789 if (sep)
23790 {
23791 /* find end of pattern */
23792 p = vim_strchr(s, sep);
23793 if (p != NULL)
23794 {
23795 pat = vim_strnsave(s, (int)(p - s));
23796 if (pat != NULL)
23797 {
23798 s = p + 1;
23799 /* find end of substitution */
23800 p = vim_strchr(s, sep);
23801 if (p != NULL)
23802 {
23803 sub = vim_strnsave(s, (int)(p - s));
23804 str = vim_strnsave(*fnamep, *fnamelen);
23805 if (sub != NULL && str != NULL)
23806 {
23807 *usedlen = (int)(p + 1 - src);
23808 s = do_string_sub(str, pat, sub, flags);
23809 if (s != NULL)
23810 {
23811 *fnamep = s;
23812 *fnamelen = (int)STRLEN(s);
23813 vim_free(*bufp);
23814 *bufp = s;
23815 didit = TRUE;
23816 }
23817 }
23818 vim_free(sub);
23819 vim_free(str);
23820 }
23821 vim_free(pat);
23822 }
23823 }
23824 /* after using ":s", repeat all the modifiers */
23825 if (didit)
23826 goto repeat;
23827 }
23828 }
23829
23830 return valid;
23831}
23832
23833/*
23834 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23835 * "flags" can be "g" to do a global substitute.
23836 * Returns an allocated string, NULL for error.
23837 */
23838 char_u *
23839do_string_sub(str, pat, sub, flags)
23840 char_u *str;
23841 char_u *pat;
23842 char_u *sub;
23843 char_u *flags;
23844{
23845 int sublen;
23846 regmatch_T regmatch;
23847 int i;
23848 int do_all;
23849 char_u *tail;
23850 garray_T ga;
23851 char_u *ret;
23852 char_u *save_cpo;
23853
23854 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23855 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023856 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857
23858 ga_init2(&ga, 1, 200);
23859
23860 do_all = (flags[0] == 'g');
23861
23862 regmatch.rm_ic = p_ic;
23863 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23864 if (regmatch.regprog != NULL)
23865 {
23866 tail = str;
23867 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23868 {
23869 /*
23870 * Get some space for a temporary buffer to do the substitution
23871 * into. It will contain:
23872 * - The text up to where the match is.
23873 * - The substituted text.
23874 * - The text after the match.
23875 */
23876 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23877 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23878 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23879 {
23880 ga_clear(&ga);
23881 break;
23882 }
23883
23884 /* copy the text up to where the match is */
23885 i = (int)(regmatch.startp[0] - tail);
23886 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23887 /* add the substituted text */
23888 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23889 + ga.ga_len + i, TRUE, TRUE, FALSE);
23890 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 /* avoid getting stuck on a match with an empty string */
23892 if (tail == regmatch.endp[0])
23893 {
23894 if (*tail == NUL)
23895 break;
23896 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23897 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 }
23899 else
23900 {
23901 tail = regmatch.endp[0];
23902 if (*tail == NUL)
23903 break;
23904 }
23905 if (!do_all)
23906 break;
23907 }
23908
23909 if (ga.ga_data != NULL)
23910 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23911
23912 vim_free(regmatch.regprog);
23913 }
23914
23915 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23916 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023917 if (p_cpo == empty_option)
23918 p_cpo = save_cpo;
23919 else
23920 /* Darn, evaluating {sub} expression changed the value. */
23921 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922
23923 return ret;
23924}
23925
23926#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */