blob: a46789fdf0567f3eab76a3961bd8ff1e9e93d3e1 [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 Moolenaarf193fff2006-04-27 00:02:13 +0000355 {VV_NAME("char", VAR_STRING), VV_RO},
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));
383#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
384static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
385#endif
386static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
387static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
388static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
390static void list_glob_vars __ARGS((int *first));
391static void list_buf_vars __ARGS((int *first));
392static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000393#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000394static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_vim_vars __ARGS((int *first));
397static void list_script_vars __ARGS((int *first));
398static void list_func_vars __ARGS((int *first));
399static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
401static int check_changedtick __ARGS((char_u *arg));
402static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
403static void clear_lval __ARGS((lval_T *lp));
404static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
405static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
406static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
407static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static listitem_T *listitem_alloc __ARGS((void));
431static void listitem_free __ARGS((listitem_T *item));
432static void listitem_remove __ARGS((list_T *l, listitem_T *item));
433static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100434static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
435static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
436static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000438static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000441static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
443static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
444static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000445static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000446static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *list2string __ARGS((typval_T *tv, int copyID));
448static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000449static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000450static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
451static void set_ref_in_list __ARGS((list_T *l, int copyID));
452static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200453static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000455static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
457static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000458static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000460static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000462static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
463static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static int string2float __ARGS((char_u *text, float_T *value));
467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
469static int find_internal_func __ARGS((char_u *name));
470static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
471static 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 +0200472static 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 +0000473static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000474static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000475
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#ifdef FEAT_FLOAT
477static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200478static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200488static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#ifdef FEAT_FLOAT
502static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
503#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000504static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000507static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000510static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000511static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
513#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000516#ifdef FEAT_FLOAT
517static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200518static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
523static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200534#ifdef FEAT_FLOAT
535static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
536#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000539static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#ifdef FEAT_FLOAT
546static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000549#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000550static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000559static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000561static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000567static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000575static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000576static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000577static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000578static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200581static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000582static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000590static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000604static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000610static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200623static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000631static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000632static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000634static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000638#ifdef vim_mkdir
639static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100642#ifdef FEAT_MZSCHEME
643static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000647static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000648#ifdef FEAT_FLOAT
649static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000652static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000653static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000689static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
692static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200693static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000696static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000697static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
701static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
703#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000704static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200705static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706#ifdef HAVE_STRFTIME
707static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
709static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200715static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000722static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200723static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000726static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000728static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000729static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000731static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200732#ifdef FEAT_FLOAT
733static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
740static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200743static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200744static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000754static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000759static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000760static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static int get_env_len __ARGS((char_u **arg));
762static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000763static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000764static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
765#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
766#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
767 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000768static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000770static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
772static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static typval_T *alloc_tv __ARGS((void));
774static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static void init_tv __ARGS((typval_T *varp));
776static long get_tv_number __ARGS((typval_T *varp));
777static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000778static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static char_u *get_tv_string __ARGS((typval_T *varp));
780static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000781static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000783static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
785static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
786static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000787static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
788static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
790static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000791static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000792static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000793static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000794static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
795static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
796static int eval_fname_script __ARGS((char_u *p));
797static int eval_fname_sid __ARGS((char_u *p));
798static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static ufunc_T *find_func __ARGS((char_u *name));
800static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000801static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802#ifdef FEAT_PROFILE
803static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000804static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
805static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
806static int
807# ifdef __BORLANDC__
808 _RTLENTRYF
809# endif
810 prof_total_cmp __ARGS((const void *s1, const void *s2));
811static int
812# ifdef __BORLANDC__
813 _RTLENTRYF
814# endif
815 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000817static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000818static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000819static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000820static void func_free __ARGS((ufunc_T *fp));
821static void func_unref __ARGS((char_u *name));
822static void func_ref __ARGS((char_u *name));
823static 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 +0000824static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
825static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000827static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
828static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000829static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000830static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000831static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000832
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200833
834#ifdef EBCDIC
835static int compare_func_name __ARGS((const void *s1, const void *s2));
836static void sortFunctions __ARGS(());
837#endif
838
839
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000840/* Character used as separated in autoload function/variable names. */
841#define AUTOLOAD_CHAR '#'
842
Bram Moolenaar33570922005-01-25 22:26:29 +0000843/*
844 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000845 */
846 void
847eval_init()
848{
Bram Moolenaar33570922005-01-25 22:26:29 +0000849 int i;
850 struct vimvar *p;
851
852 init_var_dict(&globvardict, &globvars_var);
853 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000854 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000855 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000856
857 for (i = 0; i < VV_LEN; ++i)
858 {
859 p = &vimvars[i];
860 STRCPY(p->vv_di.di_key, p->vv_name);
861 if (p->vv_flags & VV_RO)
862 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
863 else if (p->vv_flags & VV_RO_SBX)
864 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
865 else
866 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000867
868 /* add to v: scope dict, unless the value is not always available */
869 if (p->vv_type != VAR_UNKNOWN)
870 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000871 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000872 /* add to compat scope dict */
873 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000875 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200876
877#ifdef EBCDIC
878 /*
879 * Sort the function table, to enable binary sort.
880 */
881 sortFunctions();
882#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000883}
884
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000885#if defined(EXITFREE) || defined(PROTO)
886 void
887eval_clear()
888{
889 int i;
890 struct vimvar *p;
891
892 for (i = 0; i < VV_LEN; ++i)
893 {
894 p = &vimvars[i];
895 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000896 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000897 vim_free(p->vv_str);
898 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000899 }
900 else if (p->vv_di.di_tv.v_type == VAR_LIST)
901 {
902 list_unref(p->vv_list);
903 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000904 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905 }
906 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000907 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 hash_clear(&compat_hashtab);
909
Bram Moolenaard9fba312005-06-26 22:34:35 +0000910 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911
912 /* global variables */
913 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000915 /* autoloaded script names */
916 ga_clear_strings(&ga_loaded);
917
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200918 /* script-local variables */
919 for (i = 1; i <= ga_scripts.ga_len; ++i)
920 {
921 vars_clear(&SCRIPT_VARS(i));
922 vim_free(SCRIPT_SV(i));
923 }
924 ga_clear(&ga_scripts);
925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000926 /* unreferenced lists and dicts */
927 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000928
929 /* functions */
930 free_all_functions();
931 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932}
933#endif
934
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * Return the name of the executed function.
937 */
938 char_u *
939func_name(cookie)
940 void *cookie;
941{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943}
944
945/*
946 * Return the address holding the next breakpoint line for a funccall cookie.
947 */
948 linenr_T *
949func_breakpoint(cookie)
950 void *cookie;
951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the debug tick for a funccall cookie.
957 */
958 int *
959func_dbg_tick(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the nesting level for a funccall cookie.
967 */
968 int
969func_level(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000976funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000978/* pointer to list of previously used funccal, still around because some
979 * item in it is still being used. */
980funccall_T *previous_funccal = NULL;
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Return TRUE when a function was ended by a ":return" command.
984 */
985 int
986current_func_returned()
987{
988 return current_funccal->returned;
989}
990
991
992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 * Set an internal variable to a string value. Creates the variable if it does
994 * not already exist.
995 */
996 void
997set_internal_string_var(name, value)
998 char_u *name;
999 char_u *value;
1000{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001002 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 val = vim_strsave(value);
1005 if (val != NULL)
1006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001007 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014}
1015
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001017static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018static char_u *redir_endp = NULL;
1019static char_u *redir_varname = NULL;
1020
1021/*
1022 * Start recording command output to a variable
1023 * Returns OK if successfully completed the setup. FAIL otherwise.
1024 */
1025 int
1026var_redir_start(name, append)
1027 char_u *name;
1028 int append; /* append to an existing variable */
1029{
1030 int save_emsg;
1031 int err;
1032 typval_T tv;
1033
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001034 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001035 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 {
1037 EMSG(_(e_invarg));
1038 return FAIL;
1039 }
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 redir_varname = vim_strsave(name);
1043 if (redir_varname == NULL)
1044 return FAIL;
1045
1046 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1047 if (redir_lval == NULL)
1048 {
1049 var_redir_stop();
1050 return FAIL;
1051 }
1052
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001053 /* The output is stored in growarray "redir_ga" until redirection ends. */
1054 ga_init2(&redir_ga, (int)sizeof(char), 500);
1055
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001057 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1058 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1060 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001061 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp != NULL && *redir_endp != NUL)
1063 /* Trailing characters are present after the variable name */
1064 EMSG(_(e_trailing));
1065 else
1066 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 var_redir_stop();
1069 return FAIL;
1070 }
1071
1072 /* check if we can write to the variable: set it to or append an empty
1073 * string */
1074 save_emsg = did_emsg;
1075 did_emsg = FALSE;
1076 tv.v_type = VAR_STRING;
1077 tv.vval.v_string = (char_u *)"";
1078 if (append)
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1080 else
1081 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001082 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001084 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (err)
1086 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096 * Append "value[value_len]" to the variable set by var_redir_start().
1097 * The actual appending is postponed until redirection ends, because the value
1098 * appended may in fact be the string we write to, changing it may cause freed
1099 * memory to be used:
1100 * :redir => foo
1101 * :let foo
1102 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 */
1104 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001109 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110
1111 if (redir_lval == NULL)
1112 return;
1113
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 {
1121 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 }
1124 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126}
1127
1128/*
1129 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001130 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
1133var_redir_stop()
1134{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 typval_T tv;
1136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_lval != NULL)
1138 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 /* If there was no error: assign the text to the variable. */
1140 if (redir_endp != NULL)
1141 {
1142 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1143 tv.v_type = VAR_STRING;
1144 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001145 /* Call get_lval() again, if it's inside a Dict or List it may
1146 * have changed. */
1147 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1148 FALSE, FALSE, FALSE, FNE_CHECK_START);
1149 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1150 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1151 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* free the collected output */
1155 vim_free(redir_ga.ga_data);
1156 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 vim_free(redir_lval);
1159 redir_lval = NULL;
1160 }
1161 vim_free(redir_varname);
1162 redir_varname = NULL;
1163}
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165# if defined(FEAT_MBYTE) || defined(PROTO)
1166 int
1167eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1168 char_u *enc_from;
1169 char_u *enc_to;
1170 char_u *fname_from;
1171 char_u *fname_to;
1172{
1173 int err = FALSE;
1174
1175 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1176 set_vim_var_string(VV_CC_TO, enc_to, -1);
1177 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1178 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1179 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1180 err = TRUE;
1181 set_vim_var_string(VV_CC_FROM, NULL, -1);
1182 set_vim_var_string(VV_CC_TO, NULL, -1);
1183 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1184 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1185
1186 if (err)
1187 return FAIL;
1188 return OK;
1189}
1190# endif
1191
1192# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1193 int
1194eval_printexpr(fname, args)
1195 char_u *fname;
1196 char_u *args;
1197{
1198 int err = FALSE;
1199
1200 set_vim_var_string(VV_FNAME_IN, fname, -1);
1201 set_vim_var_string(VV_CMDARG, args, -1);
1202 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1203 err = TRUE;
1204 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1205 set_vim_var_string(VV_CMDARG, NULL, -1);
1206
1207 if (err)
1208 {
1209 mch_remove(fname);
1210 return FAIL;
1211 }
1212 return OK;
1213}
1214# endif
1215
1216# if defined(FEAT_DIFF) || defined(PROTO)
1217 void
1218eval_diff(origfile, newfile, outfile)
1219 char_u *origfile;
1220 char_u *newfile;
1221 char_u *outfile;
1222{
1223 int err = FALSE;
1224
1225 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1226 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1227 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1228 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1229 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1230 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1231 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1232}
1233
1234 void
1235eval_patch(origfile, difffile, outfile)
1236 char_u *origfile;
1237 char_u *difffile;
1238 char_u *outfile;
1239{
1240 int err;
1241
1242 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1243 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1244 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1245 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1246 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1248 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1249}
1250# endif
1251
1252/*
1253 * Top level evaluation function, returning a boolean.
1254 * Sets "error" to TRUE if there was an error.
1255 * Return TRUE or FALSE.
1256 */
1257 int
1258eval_to_bool(arg, error, nextcmd, skip)
1259 char_u *arg;
1260 int *error;
1261 char_u **nextcmd;
1262 int skip; /* only parse, don't execute */
1263{
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 int retval = FALSE;
1266
1267 if (skip)
1268 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
1272 {
1273 *error = FALSE;
1274 if (!skip)
1275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001276 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 }
1280 if (skip)
1281 --emsg_skip;
1282
1283 return retval;
1284}
1285
1286/*
1287 * Top level evaluation function, returning a string. If "skip" is TRUE,
1288 * only parsing to "nextcmd" is done, without reporting errors. Return
1289 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1290 */
1291 char_u *
1292eval_to_string_skip(arg, nextcmd, skip)
1293 char_u *arg;
1294 char_u **nextcmd;
1295 int skip; /* only parse, don't execute */
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 char_u *retval;
1299
1300 if (skip)
1301 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 retval = NULL;
1304 else
1305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 retval = vim_strsave(get_tv_string(&tv));
1307 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 if (skip)
1310 --emsg_skip;
1311
1312 return retval;
1313}
1314
1315/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001316 * Skip over an expression at "*pp".
1317 * Return FAIL for an error, OK otherwise.
1318 */
1319 int
1320skip_expr(pp)
1321 char_u **pp;
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324
1325 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327}
1328
1329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001331 * When "convert" is TRUE convert a List into a sequence of lines and convert
1332 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Return pointer to allocated memory, or NULL for failure.
1334 */
1335 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *arg;
1338 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001343 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 retval = NULL;
1350 else
1351 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 {
1354 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001355 if (tv.vval.v_list != NULL)
1356 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001357 ga_append(&ga, NUL);
1358 retval = (char_u *)ga.ga_data;
1359 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001360#ifdef FEAT_FLOAT
1361 else if (convert && tv.v_type == VAR_FLOAT)
1362 {
1363 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1364 retval = vim_strsave(numbuf);
1365 }
1366#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001367 else
1368 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001369 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 }
1371
1372 return retval;
1373}
1374
1375/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001376 * Call eval_to_string() without using current local variables and using
1377 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 */
1379 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001380eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 char_u *arg;
1382 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
1385 char_u *retval;
1386 void *save_funccalp;
1387
1388 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001389 if (use_sandbox)
1390 ++sandbox;
1391 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001392 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 if (use_sandbox)
1394 --sandbox;
1395 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 restore_funccal(save_funccalp);
1397 return retval;
1398}
1399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400/*
1401 * Top level evaluation function, returning a number.
1402 * Evaluates "expr" silently.
1403 * Returns -1 for an error.
1404 */
1405 int
1406eval_to_number(expr)
1407 char_u *expr;
1408{
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001411 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
1413 ++emsg_off;
1414
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001415 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 retval = -1;
1417 else
1418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001419 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001420 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 }
1422 --emsg_off;
1423
1424 return retval;
1425}
1426
Bram Moolenaara40058a2005-07-11 22:42:07 +00001427/*
1428 * Prepare v: variable "idx" to be used.
1429 * Save the current typeval in "save_tv".
1430 * When not used yet add the variable to the v: hashtable.
1431 */
1432 static void
1433prepare_vimvar(idx, save_tv)
1434 int idx;
1435 typval_T *save_tv;
1436{
1437 *save_tv = vimvars[idx].vv_tv;
1438 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1439 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1440}
1441
1442/*
1443 * Restore v: variable "idx" to typeval "save_tv".
1444 * When no longer defined, remove the variable from the v: hashtable.
1445 */
1446 static void
1447restore_vimvar(idx, save_tv)
1448 int idx;
1449 typval_T *save_tv;
1450{
1451 hashitem_T *hi;
1452
Bram Moolenaara40058a2005-07-11 22:42:07 +00001453 vimvars[idx].vv_tv = *save_tv;
1454 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1455 {
1456 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1457 if (HASHITEM_EMPTY(hi))
1458 EMSG2(_(e_intern2), "restore_vimvar()");
1459 else
1460 hash_remove(&vimvarht, hi);
1461 }
1462}
1463
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001464#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001465/*
1466 * Evaluate an expression to a list with suggestions.
1467 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001468 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001469 */
1470 list_T *
1471eval_spell_expr(badword, expr)
1472 char_u *badword;
1473 char_u *expr;
1474{
1475 typval_T save_val;
1476 typval_T rettv;
1477 list_T *list = NULL;
1478 char_u *p = skipwhite(expr);
1479
1480 /* Set "v:val" to the bad word. */
1481 prepare_vimvar(VV_VAL, &save_val);
1482 vimvars[VV_VAL].vv_type = VAR_STRING;
1483 vimvars[VV_VAL].vv_str = badword;
1484 if (p_verbose == 0)
1485 ++emsg_off;
1486
1487 if (eval1(&p, &rettv, TRUE) == OK)
1488 {
1489 if (rettv.v_type != VAR_LIST)
1490 clear_tv(&rettv);
1491 else
1492 list = rettv.vval.v_list;
1493 }
1494
1495 if (p_verbose == 0)
1496 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001497 restore_vimvar(VV_VAL, &save_val);
1498
1499 return list;
1500}
1501
1502/*
1503 * "list" is supposed to contain two items: a word and a number. Return the
1504 * word in "pp" and the number as the return value.
1505 * Return -1 if anything isn't right.
1506 * Used to get the good word and score from the eval_spell_expr() result.
1507 */
1508 int
1509get_spellword(list, pp)
1510 list_T *list;
1511 char_u **pp;
1512{
1513 listitem_T *li;
1514
1515 li = list->lv_first;
1516 if (li == NULL)
1517 return -1;
1518 *pp = get_tv_string(&li->li_tv);
1519
1520 li = li->li_next;
1521 if (li == NULL)
1522 return -1;
1523 return get_tv_number(&li->li_tv);
1524}
1525#endif
1526
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001527/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001528 * Top level evaluation function.
1529 * Returns an allocated typval_T with the result.
1530 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001531 */
1532 typval_T *
1533eval_expr(arg, nextcmd)
1534 char_u *arg;
1535 char_u **nextcmd;
1536{
1537 typval_T *tv;
1538
1539 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001540 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001541 {
1542 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001543 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544 }
1545
1546 return tv;
1547}
1548
1549
Bram Moolenaar4f688582007-07-24 12:34:30 +00001550#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1551 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001554 * Uses argv[argc] for the function arguments. Only Number and String
1555 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 static int
1559call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 char_u *func;
1561 int argc;
1562 char_u **argv;
1563 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
Bram Moolenaar33570922005-01-25 22:26:29 +00001566 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 long n;
1568 int len;
1569 int i;
1570 int doesrange;
1571 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001574 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
1578 for (i = 0; i < argc; i++)
1579 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001580 /* Pass a NULL or empty argument as an empty string */
1581 if (argv[i] == NULL || *argv[i] == NUL)
1582 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001583 argvars[i].v_type = VAR_STRING;
1584 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001585 continue;
1586 }
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 /* Recognize a number argument, the others must be strings. */
1589 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1590 if (len != 0 && len == (int)STRLEN(argv[i]))
1591 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001592 argvars[i].v_type = VAR_NUMBER;
1593 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595 else
1596 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001597 argvars[i].v_type = VAR_STRING;
1598 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600 }
1601
1602 if (safe)
1603 {
1604 save_funccalp = save_funccal();
1605 ++sandbox;
1606 }
1607
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001608 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1609 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (safe)
1613 {
1614 --sandbox;
1615 restore_funccal(save_funccalp);
1616 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 vim_free(argvars);
1618
1619 if (ret == FAIL)
1620 clear_tv(rettv);
1621
1622 return ret;
1623}
1624
Bram Moolenaar4f688582007-07-24 12:34:30 +00001625# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001627 * Call vimL function "func" and return the result as a string.
1628 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629 * Uses argv[argc] for the function arguments.
1630 */
1631 void *
1632call_func_retstr(func, argc, argv, safe)
1633 char_u *func;
1634 int argc;
1635 char_u **argv;
1636 int safe; /* use the sandbox */
1637{
1638 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001639 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640
1641 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1642 return NULL;
1643
1644 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001645 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 return retval;
1647}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001648# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649
Bram Moolenaar4f688582007-07-24 12:34:30 +00001650# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001652 * Call vimL function "func" and return the result as a number.
1653 * Returns -1 when calling the function fails.
1654 * Uses argv[argc] for the function arguments.
1655 */
1656 long
1657call_func_retnr(func, argc, argv, safe)
1658 char_u *func;
1659 int argc;
1660 char_u **argv;
1661 int safe; /* use the sandbox */
1662{
1663 typval_T rettv;
1664 long retval;
1665
1666 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1667 return -1;
1668
1669 retval = get_tv_number_chk(&rettv, NULL);
1670 clear_tv(&rettv);
1671 return retval;
1672}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001673# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001674
1675/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001676 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001678 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 */
1680 void *
1681call_func_retlist(func, argc, argv, safe)
1682 char_u *func;
1683 int argc;
1684 char_u **argv;
1685 int safe; /* use the sandbox */
1686{
1687 typval_T rettv;
1688
1689 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1690 return NULL;
1691
1692 if (rettv.v_type != VAR_LIST)
1693 {
1694 clear_tv(&rettv);
1695 return NULL;
1696 }
1697
1698 return rettv.vval.v_list;
1699}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700#endif
1701
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703/*
1704 * Save the current function call pointer, and set it to NULL.
1705 * Used when executing autocommands and for ":source".
1706 */
1707 void *
1708save_funccal()
1709{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001710 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 current_funccal = NULL;
1713 return (void *)fc;
1714}
1715
1716 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001717restore_funccal(vfc)
1718 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001720 funccall_T *fc = (funccall_T *)vfc;
1721
1722 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723}
1724
Bram Moolenaar05159a02005-02-26 23:04:13 +00001725#if defined(FEAT_PROFILE) || defined(PROTO)
1726/*
1727 * Prepare profiling for entering a child or something else that is not
1728 * counted for the script/function itself.
1729 * Should always be called in pair with prof_child_exit().
1730 */
1731 void
1732prof_child_enter(tm)
1733 proftime_T *tm; /* place to store waittime */
1734{
1735 funccall_T *fc = current_funccal;
1736
1737 if (fc != NULL && fc->func->uf_profiling)
1738 profile_start(&fc->prof_child);
1739 script_prof_save(tm);
1740}
1741
1742/*
1743 * Take care of time spent in a child.
1744 * Should always be called after prof_child_enter().
1745 */
1746 void
1747prof_child_exit(tm)
1748 proftime_T *tm; /* where waittime was stored */
1749{
1750 funccall_T *fc = current_funccal;
1751
1752 if (fc != NULL && fc->func->uf_profiling)
1753 {
1754 profile_end(&fc->prof_child);
1755 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1756 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1757 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1758 }
1759 script_prof_restore(tm);
1760}
1761#endif
1762
1763
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764#ifdef FEAT_FOLDING
1765/*
1766 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1767 * it in "*cp". Doesn't give error messages.
1768 */
1769 int
1770eval_foldexpr(arg, cp)
1771 char_u *arg;
1772 int *cp;
1773{
Bram Moolenaar33570922005-01-25 22:26:29 +00001774 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 int retval;
1776 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001777 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1778 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001781 if (use_sandbox)
1782 ++sandbox;
1783 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 retval = 0;
1787 else
1788 {
1789 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001790 if (tv.v_type == VAR_NUMBER)
1791 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001792 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 retval = 0;
1794 else
1795 {
1796 /* If the result is a string, check if there is a non-digit before
1797 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (!VIM_ISDIGIT(*s) && *s != '-')
1800 *cp = *s++;
1801 retval = atol((char *)s);
1802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 }
1805 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001806 if (use_sandbox)
1807 --sandbox;
1808 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
1810 return retval;
1811}
1812#endif
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001815 * ":let" list all variable values
1816 * ":let var1 var2" list variable values
1817 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001818 * ":let var += expr" assignment command.
1819 * ":let var -= expr" assignment command.
1820 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 */
1823 void
1824ex_let(eap)
1825 exarg_T *eap;
1826{
1827 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001829 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 int var_count = 0;
1832 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001833 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001834 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001835 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
Bram Moolenaardb552d602006-03-23 22:59:57 +00001837 argend = skip_var_list(arg, &var_count, &semicolon);
1838 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001840 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1841 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001842 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001845 /*
1846 * ":let" without "=": list variables
1847 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 if (*arg == '[')
1849 EMSG(_(e_invarg));
1850 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001852 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001856 list_glob_vars(&first);
1857 list_buf_vars(&first);
1858 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001859#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001861#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001862 list_script_vars(&first);
1863 list_func_vars(&first);
1864 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 eap->nextcmd = check_nextcmd(arg);
1867 }
1868 else
1869 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001870 op[0] = '=';
1871 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001872 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 {
1874 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1875 op[0] = expr[-1]; /* +=, -= or .= */
1876 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (eap->skip)
1880 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (eap->skip)
1883 {
1884 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 --emsg_skip;
1887 }
1888 else if (i != FAIL)
1889 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 }
1894 }
1895}
1896
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897/*
1898 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1899 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 * When "nextchars" is not NULL it points to a string with characters that
1901 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1902 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 * Returns OK or FAIL;
1904 */
1905 static int
1906ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1907 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001908 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 int copy; /* copy values from "tv", don't move */
1910 int semicolon; /* from skip_var_list() */
1911 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913{
1914 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001915 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 listitem_T *item;
1918 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919
1920 if (*arg != '[')
1921 {
1922 /*
1923 * ":let var = expr" or ":for var in list"
1924 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926 return FAIL;
1927 return OK;
1928 }
1929
1930 /*
1931 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1932 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001933 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 {
1935 EMSG(_(e_listreq));
1936 return FAIL;
1937 }
1938
1939 i = list_len(l);
1940 if (semicolon == 0 && var_count < i)
1941 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001942 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 return FAIL;
1944 }
1945 if (var_count - semicolon > i)
1946 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001947 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 return FAIL;
1949 }
1950
1951 item = l->lv_first;
1952 while (*arg != ']')
1953 {
1954 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001955 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 item = item->li_next;
1957 if (arg == NULL)
1958 return FAIL;
1959
1960 arg = skipwhite(arg);
1961 if (*arg == ';')
1962 {
1963 /* Put the rest of the list (may be empty) in the var after ';'.
1964 * Create a new list for this. */
1965 l = list_alloc();
1966 if (l == NULL)
1967 return FAIL;
1968 while (item != NULL)
1969 {
1970 list_append_tv(l, &item->li_tv);
1971 item = item->li_next;
1972 }
1973
1974 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001975 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976 ltv.vval.v_list = l;
1977 l->lv_refcount = 1;
1978
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001979 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1980 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 clear_tv(&ltv);
1982 if (arg == NULL)
1983 return FAIL;
1984 break;
1985 }
1986 else if (*arg != ',' && *arg != ']')
1987 {
1988 EMSG2(_(e_intern2), "ex_let_vars()");
1989 return FAIL;
1990 }
1991 }
1992
1993 return OK;
1994}
1995
1996/*
1997 * Skip over assignable variable "var" or list of variables "[var, var]".
1998 * Used for ":let varvar = expr" and ":for varvar in expr".
1999 * For "[var, var]" increment "*var_count" for each variable.
2000 * for "[var, var; var]" set "semicolon".
2001 * Return NULL for an error.
2002 */
2003 static char_u *
2004skip_var_list(arg, var_count, semicolon)
2005 char_u *arg;
2006 int *var_count;
2007 int *semicolon;
2008{
2009 char_u *p, *s;
2010
2011 if (*arg == '[')
2012 {
2013 /* "[var, var]": find the matching ']'. */
2014 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002015 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 {
2017 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2018 s = skip_var_one(p);
2019 if (s == p)
2020 {
2021 EMSG2(_(e_invarg2), p);
2022 return NULL;
2023 }
2024 ++*var_count;
2025
2026 p = skipwhite(s);
2027 if (*p == ']')
2028 break;
2029 else if (*p == ';')
2030 {
2031 if (*semicolon == 1)
2032 {
2033 EMSG(_("Double ; in list of variables"));
2034 return NULL;
2035 }
2036 *semicolon = 1;
2037 }
2038 else if (*p != ',')
2039 {
2040 EMSG2(_(e_invarg2), p);
2041 return NULL;
2042 }
2043 }
2044 return p + 1;
2045 }
2046 else
2047 return skip_var_one(arg);
2048}
2049
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002050/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002051 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002052 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 static char_u *
2055skip_var_one(arg)
2056 char_u *arg;
2057{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002058 if (*arg == '@' && arg[1] != NUL)
2059 return arg + 2;
2060 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2061 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062}
2063
Bram Moolenaara7043832005-01-21 11:56:39 +00002064/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002065 * List variables for hashtab "ht" with prefix "prefix".
2066 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002069list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002070 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002072 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002073 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002074{
Bram Moolenaar33570922005-01-25 22:26:29 +00002075 hashitem_T *hi;
2076 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002077 int todo;
2078
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002079 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2081 {
2082 if (!HASHITEM_EMPTY(hi))
2083 {
2084 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002085 di = HI2DI(hi);
2086 if (empty || di->di_tv.v_type != VAR_STRING
2087 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002088 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 }
2090 }
2091}
2092
2093/*
2094 * List global variables.
2095 */
2096 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097list_glob_vars(first)
2098 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002101}
2102
2103/*
2104 * List buffer variables.
2105 */
2106 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107list_buf_vars(first)
2108 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002110 char_u numbuf[NUMBUFLEN];
2111
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002112 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2113 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002114
2115 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2117 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002118}
2119
2120/*
2121 * List window variables.
2122 */
2123 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124list_win_vars(first)
2125 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002126{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2128 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002129}
2130
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002131#ifdef FEAT_WINDOWS
2132/*
2133 * List tab page variables.
2134 */
2135 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136list_tab_vars(first)
2137 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002138{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2140 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002141}
2142#endif
2143
Bram Moolenaara7043832005-01-21 11:56:39 +00002144/*
2145 * List Vim variables.
2146 */
2147 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148list_vim_vars(first)
2149 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152}
2153
2154/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002155 * List script-local variables, if there is a script.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_script_vars(first)
2159 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002160{
2161 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2163 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164}
2165
2166/*
2167 * List function variables, if there is a function.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_func_vars(first)
2171 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002172{
2173 if (current_funccal != NULL)
2174 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176}
2177
2178/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 * List variables in "arg".
2180 */
2181 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 exarg_T *eap;
2184 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186{
2187 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002188 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190 char_u *name_start;
2191 char_u *arg_subsc;
2192 char_u *tofree;
2193 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194
2195 while (!ends_excmd(*arg) && !got_int)
2196 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002199 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2201 {
2202 emsg_severe = TRUE;
2203 EMSG(_(e_trailing));
2204 break;
2205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 /* get_name_len() takes care of expanding curly braces */
2210 name_start = name = arg;
2211 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2212 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 /* This is mainly to keep test 49 working: when expanding
2215 * curly braces fails overrule the exception error message. */
2216 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 emsg_severe = TRUE;
2219 EMSG2(_(e_invarg2), arg);
2220 break;
2221 }
2222 error = TRUE;
2223 }
2224 else
2225 {
2226 if (tofree != NULL)
2227 name = tofree;
2228 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 else
2231 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 /* handle d.key, l[idx], f(expr) */
2233 arg_subsc = arg;
2234 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002235 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002237 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002242 case 'g': list_glob_vars(first); break;
2243 case 'b': list_buf_vars(first); break;
2244 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002247#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002248 case 'v': list_vim_vars(first); break;
2249 case 's': list_script_vars(first); break;
2250 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 default:
2252 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002254 }
2255 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 {
2257 char_u numbuf[NUMBUFLEN];
2258 char_u *tf;
2259 int c;
2260 char_u *s;
2261
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002262 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 c = *arg;
2264 *arg = NUL;
2265 list_one_var_a((char_u *)"",
2266 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 tv.v_type,
2268 s == NULL ? (char_u *)"" : s,
2269 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 *arg = c;
2271 vim_free(tf);
2272 }
2273 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002274 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 }
2276 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277
2278 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280
2281 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
2283
2284 return arg;
2285}
2286
2287/*
2288 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2289 * Returns a pointer to the char just after the var name.
2290 * Returns NULL if there is an error.
2291 */
2292 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002295 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002297 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002298 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299{
2300 int c1;
2301 char_u *name;
2302 char_u *p;
2303 char_u *arg_end = NULL;
2304 int len;
2305 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307
2308 /*
2309 * ":let $VAR = expr": Set environment variable.
2310 */
2311 if (*arg == '$')
2312 {
2313 /* Find the end of the name. */
2314 ++arg;
2315 name = arg;
2316 len = get_env_len(&arg);
2317 if (len == 0)
2318 EMSG2(_(e_invarg2), name - 1);
2319 else
2320 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002321 if (op != NULL && (*op == '+' || *op == '-'))
2322 EMSG2(_(e_letwrong), op);
2323 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002324 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002326 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 {
2328 c1 = name[len];
2329 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002330 p = get_tv_string_chk(tv);
2331 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002332 {
2333 int mustfree = FALSE;
2334 char_u *s = vim_getenv(name, &mustfree);
2335
2336 if (s != NULL)
2337 {
2338 p = tofree = concat_str(s, p);
2339 if (mustfree)
2340 vim_free(s);
2341 }
2342 }
2343 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002344 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 if (STRICMP(name, "HOME") == 0)
2347 init_homedir();
2348 else if (didset_vim && STRICMP(name, "VIM") == 0)
2349 didset_vim = FALSE;
2350 else if (didset_vimruntime
2351 && STRICMP(name, "VIMRUNTIME") == 0)
2352 didset_vimruntime = FALSE;
2353 arg_end = arg;
2354 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 }
2358 }
2359 }
2360
2361 /*
2362 * ":let &option = expr": Set option value.
2363 * ":let &l:option = expr": Set local option value.
2364 * ":let &g:option = expr": Set global option value.
2365 */
2366 else if (*arg == '&')
2367 {
2368 /* Find the end of the name. */
2369 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002370 if (p == NULL || (endchars != NULL
2371 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 EMSG(_(e_letunexp));
2373 else
2374 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002375 long n;
2376 int opt_type;
2377 long numval;
2378 char_u *stringval = NULL;
2379 char_u *s;
2380
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 c1 = *p;
2382 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383
2384 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002385 s = get_tv_string_chk(tv); /* != NULL if number or string */
2386 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 {
2388 opt_type = get_option_value(arg, &numval,
2389 &stringval, opt_flags);
2390 if ((opt_type == 1 && *op == '.')
2391 || (opt_type == 0 && *op != '.'))
2392 EMSG2(_(e_letwrong), op);
2393 else
2394 {
2395 if (opt_type == 1) /* number */
2396 {
2397 if (*op == '+')
2398 n = numval + n;
2399 else
2400 n = numval - n;
2401 }
2402 else if (opt_type == 0 && stringval != NULL) /* string */
2403 {
2404 s = concat_str(stringval, s);
2405 vim_free(stringval);
2406 stringval = s;
2407 }
2408 }
2409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 if (s != NULL)
2411 {
2412 set_option_value(arg, n, s, opt_flags);
2413 arg_end = p;
2414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 }
2418 }
2419
2420 /*
2421 * ":let @r = expr": Set register contents.
2422 */
2423 else if (*arg == '@')
2424 {
2425 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 if (op != NULL && (*op == '+' || *op == '-'))
2427 EMSG2(_(e_letwrong), op);
2428 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002429 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 EMSG(_(e_letunexp));
2431 else
2432 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002433 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 char_u *s;
2435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002436 p = get_tv_string_chk(tv);
2437 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002439 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 if (s != NULL)
2441 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002442 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 vim_free(s);
2444 }
2445 }
2446 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002449 arg_end = arg + 1;
2450 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002451 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 }
2453 }
2454
2455 /*
2456 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002459 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002461 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002463 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2467 EMSG(_(e_letunexp));
2468 else
2469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 arg_end = p;
2472 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 }
2476
2477 else
2478 EMSG2(_(e_invarg2), arg);
2479
2480 return arg_end;
2481}
2482
2483/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2485 */
2486 static int
2487check_changedtick(arg)
2488 char_u *arg;
2489{
2490 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2491 {
2492 EMSG2(_(e_readonlyvar), arg);
2493 return TRUE;
2494 }
2495 return FALSE;
2496}
2497
2498/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 * Get an lval: variable, Dict item or List item that can be assigned a value
2500 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2501 * "name.key", "name.key[expr]" etc.
2502 * Indexing only works if "name" is an existing List or Dictionary.
2503 * "name" points to the start of the name.
2504 * If "rettv" is not NULL it points to the value to be assigned.
2505 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2506 * wrong; must end in space or cmd separator.
2507 *
2508 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002509 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 */
2512 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002513get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002515 typval_T *rettv;
2516 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 int unlet;
2518 int skip;
2519 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002520 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 char_u *expr_start, *expr_end;
2524 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 dictitem_T *v;
2526 typval_T var1;
2527 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536
2537 if (skip)
2538 {
2539 /* When skipping just find the end of the name. */
2540 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002541 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 }
2543
2544 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002545 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 if (expr_start != NULL)
2547 {
2548 /* Don't expand the name when we already know there is an error. */
2549 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2550 && *p != '[' && *p != '.')
2551 {
2552 EMSG(_(e_trailing));
2553 return NULL;
2554 }
2555
2556 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2557 if (lp->ll_exp_name == NULL)
2558 {
2559 /* Report an invalid expression in braces, unless the
2560 * expression evaluation has been cancelled due to an
2561 * aborting error, an interrupt, or an exception. */
2562 if (!aborting() && !quiet)
2563 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002564 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 EMSG2(_(e_invarg2), name);
2566 return NULL;
2567 }
2568 }
2569 lp->ll_name = lp->ll_exp_name;
2570 }
2571 else
2572 lp->ll_name = name;
2573
2574 /* Without [idx] or .key we are done. */
2575 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2576 return p;
2577
2578 cc = *p;
2579 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002580 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 if (v == NULL && !quiet)
2582 EMSG2(_(e_undefvar), lp->ll_name);
2583 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 if (v == NULL)
2585 return NULL;
2586
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 /*
2588 * Loop until no more [idx] or .key is following.
2589 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002590 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2594 && !(lp->ll_tv->v_type == VAR_DICT
2595 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (!quiet)
2598 EMSG(_("E689: Can only index a List or Dictionary"));
2599 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (!quiet)
2604 EMSG(_("E708: [:] must come last"));
2605 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002607
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 len = -1;
2609 if (*p == '.')
2610 {
2611 key = p + 1;
2612 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2613 ;
2614 if (len == 0)
2615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 if (!quiet)
2617 EMSG(_(e_emptykey));
2618 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 }
2620 p = key + len;
2621 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002622 else
2623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002624 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 if (*p == ':')
2627 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 else
2629 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 empty1 = FALSE;
2631 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002633 if (get_tv_string_chk(&var1) == NULL)
2634 {
2635 /* not a number or string */
2636 clear_tv(&var1);
2637 return NULL;
2638 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 }
2640
2641 /* Optionally get the second index [ :expr]. */
2642 if (*p == ':')
2643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002648 if (!empty1)
2649 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002651 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (rettv != NULL && (rettv->v_type != VAR_LIST
2653 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
2656 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 if (!empty1)
2658 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 }
2661 p = skipwhite(p + 1);
2662 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 else
2665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2668 {
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 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002673 if (get_tv_string_chk(&var2) == NULL)
2674 {
2675 /* not a number or string */
2676 if (!empty1)
2677 clear_tv(&var1);
2678 clear_tv(&var2);
2679 return NULL;
2680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (!quiet)
2690 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (!empty1)
2692 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 }
2697
2698 /* Skip to past ']'. */
2699 ++p;
2700 }
2701
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 {
2704 if (len == -1)
2705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002707 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (*key == NUL)
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (!quiet)
2711 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 }
2715 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002716 lp->ll_list = NULL;
2717 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002718 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002721 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (len == -1)
2727 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 }
2730 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 if (len == -1)
2735 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 p = NULL;
2738 break;
2739 }
2740 if (len == -1)
2741 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 }
2744 else
2745 {
2746 /*
2747 * Get the number and item for the only or first index of the List.
2748 */
2749 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 else
2752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002753 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 clear_tv(&var1);
2755 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002756 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 lp->ll_list = lp->ll_tv->vval.v_list;
2758 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2759 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002761 if (lp->ll_n1 < 0)
2762 {
2763 lp->ll_n1 = 0;
2764 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2765 }
2766 }
2767 if (lp->ll_li == NULL)
2768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 }
2773
2774 /*
2775 * May need to find the item or absolute index for the second
2776 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 * When no index given: "lp->ll_empty2" is TRUE.
2778 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002782 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 }
2791
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2793 if (lp->ll_n1 < 0)
2794 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2795 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002797 }
2798
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002801 }
2802
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 return p;
2804}
2805
2806/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002807 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 */
2809 static void
2810clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002811 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812{
2813 vim_free(lp->ll_exp_name);
2814 vim_free(lp->ll_newkey);
2815}
2816
2817/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002818 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 */
2822 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002823set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002824 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002826 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002828 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829{
2830 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002831 listitem_T *ri;
2832 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833
2834 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002837 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 cc = *endp;
2839 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002840 if (op != NULL && *op != '=')
2841 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002842 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002844 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002845 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002846 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002847 {
2848 if (tv_op(&tv, rettv, op) == OK)
2849 set_var(lp->ll_name, &tv, FALSE);
2850 clear_tv(&tv);
2851 }
2852 }
2853 else
2854 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002856 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002858 else if (tv_check_lock(lp->ll_newkey == NULL
2859 ? lp->ll_tv->v_lock
2860 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2861 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 else if (lp->ll_range)
2863 {
2864 /*
2865 * Assign the List values to the list items.
2866 */
2867 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002868 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002869 if (op != NULL && *op != '=')
2870 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2871 else
2872 {
2873 clear_tv(&lp->ll_li->li_tv);
2874 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2875 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 ri = ri->li_next;
2877 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2878 break;
2879 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002882 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 ri = NULL;
2885 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002886 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002887 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 lp->ll_li = lp->ll_li->li_next;
2889 ++lp->ll_n1;
2890 }
2891 if (ri != NULL)
2892 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002893 else if (lp->ll_empty2
2894 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 : lp->ll_n1 != lp->ll_n2)
2896 EMSG(_("E711: List value has not enough items"));
2897 }
2898 else
2899 {
2900 /*
2901 * Assign to a List or Dictionary item.
2902 */
2903 if (lp->ll_newkey != NULL)
2904 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905 if (op != NULL && *op != '=')
2906 {
2907 EMSG2(_(e_letwrong), op);
2908 return;
2909 }
2910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002912 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 if (di == NULL)
2914 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002915 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2916 {
2917 vim_free(di);
2918 return;
2919 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002921 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 else if (op != NULL && *op != '=')
2923 {
2924 tv_op(lp->ll_tv, rettv, op);
2925 return;
2926 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002929
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 /*
2931 * Assign the value to the variable or list item.
2932 */
2933 if (copy)
2934 copy_tv(rettv, lp->ll_tv);
2935 else
2936 {
2937 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002938 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 }
2941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002942}
2943
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002944/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2946 * Returns OK or FAIL.
2947 */
2948 static int
2949tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 typval_T *tv1;
2951 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 char_u *op;
2953{
2954 long n;
2955 char_u numbuf[NUMBUFLEN];
2956 char_u *s;
2957
2958 /* Can't do anything with a Funcref or a Dict on the right. */
2959 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2960 {
2961 switch (tv1->v_type)
2962 {
2963 case VAR_DICT:
2964 case VAR_FUNC:
2965 break;
2966
2967 case VAR_LIST:
2968 if (*op != '+' || tv2->v_type != VAR_LIST)
2969 break;
2970 /* List += List */
2971 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2972 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2973 return OK;
2974
2975 case VAR_NUMBER:
2976 case VAR_STRING:
2977 if (tv2->v_type == VAR_LIST)
2978 break;
2979 if (*op == '+' || *op == '-')
2980 {
2981 /* nr += nr or nr -= nr*/
2982 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002983#ifdef FEAT_FLOAT
2984 if (tv2->v_type == VAR_FLOAT)
2985 {
2986 float_T f = n;
2987
2988 if (*op == '+')
2989 f += tv2->vval.v_float;
2990 else
2991 f -= tv2->vval.v_float;
2992 clear_tv(tv1);
2993 tv1->v_type = VAR_FLOAT;
2994 tv1->vval.v_float = f;
2995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002997#endif
2998 {
2999 if (*op == '+')
3000 n += get_tv_number(tv2);
3001 else
3002 n -= get_tv_number(tv2);
3003 clear_tv(tv1);
3004 tv1->v_type = VAR_NUMBER;
3005 tv1->vval.v_number = n;
3006 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003007 }
3008 else
3009 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003010 if (tv2->v_type == VAR_FLOAT)
3011 break;
3012
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 /* str .= str */
3014 s = get_tv_string(tv1);
3015 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3016 clear_tv(tv1);
3017 tv1->v_type = VAR_STRING;
3018 tv1->vval.v_string = s;
3019 }
3020 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003021
3022#ifdef FEAT_FLOAT
3023 case VAR_FLOAT:
3024 {
3025 float_T f;
3026
3027 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3028 && tv2->v_type != VAR_NUMBER
3029 && tv2->v_type != VAR_STRING))
3030 break;
3031 if (tv2->v_type == VAR_FLOAT)
3032 f = tv2->vval.v_float;
3033 else
3034 f = get_tv_number(tv2);
3035 if (*op == '+')
3036 tv1->vval.v_float += f;
3037 else
3038 tv1->vval.v_float -= f;
3039 }
3040 return OK;
3041#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003042 }
3043 }
3044
3045 EMSG2(_(e_letwrong), op);
3046 return FAIL;
3047}
3048
3049/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003050 * Add a watcher to a list.
3051 */
3052 static void
3053list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003054 list_T *l;
3055 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003056{
3057 lw->lw_next = l->lv_watch;
3058 l->lv_watch = lw;
3059}
3060
3061/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003062 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003063 * No warning when it isn't found...
3064 */
3065 static void
3066list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003067 list_T *l;
3068 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003069{
Bram Moolenaar33570922005-01-25 22:26:29 +00003070 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003071
3072 lwp = &l->lv_watch;
3073 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3074 {
3075 if (lw == lwrem)
3076 {
3077 *lwp = lw->lw_next;
3078 break;
3079 }
3080 lwp = &lw->lw_next;
3081 }
3082}
3083
3084/*
3085 * Just before removing an item from a list: advance watchers to the next
3086 * item.
3087 */
3088 static void
3089list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003090 list_T *l;
3091 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092{
Bram Moolenaar33570922005-01-25 22:26:29 +00003093 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003094
3095 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3096 if (lw->lw_item == item)
3097 lw->lw_item = item->li_next;
3098}
3099
3100/*
3101 * Evaluate the expression used in a ":for var in expr" command.
3102 * "arg" points to "var".
3103 * Set "*errp" to TRUE for an error, FALSE otherwise;
3104 * Return a pointer that holds the info. Null when there is an error.
3105 */
3106 void *
3107eval_for_line(arg, errp, nextcmdp, skip)
3108 char_u *arg;
3109 int *errp;
3110 char_u **nextcmdp;
3111 int skip;
3112{
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 typval_T tv;
3116 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117
3118 *errp = TRUE; /* default: there is an error */
3119
Bram Moolenaar33570922005-01-25 22:26:29 +00003120 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121 if (fi == NULL)
3122 return NULL;
3123
3124 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3125 if (expr == NULL)
3126 return fi;
3127
3128 expr = skipwhite(expr);
3129 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3130 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003131 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132 return fi;
3133 }
3134
3135 if (skip)
3136 ++emsg_skip;
3137 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3138 {
3139 *errp = FALSE;
3140 if (!skip)
3141 {
3142 l = tv.vval.v_list;
3143 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003144 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003146 clear_tv(&tv);
3147 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148 else
3149 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003150 /* No need to increment the refcount, it's already set for the
3151 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003152 fi->fi_list = l;
3153 list_add_watch(l, &fi->fi_lw);
3154 fi->fi_lw.lw_item = l->lv_first;
3155 }
3156 }
3157 }
3158 if (skip)
3159 --emsg_skip;
3160
3161 return fi;
3162}
3163
3164/*
3165 * Use the first item in a ":for" list. Advance to the next.
3166 * Assign the values to the variable (list). "arg" points to the first one.
3167 * Return TRUE when a valid item was found, FALSE when at end of list or
3168 * something wrong.
3169 */
3170 int
3171next_for_item(fi_void, arg)
3172 void *fi_void;
3173 char_u *arg;
3174{
Bram Moolenaar33570922005-01-25 22:26:29 +00003175 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003177 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178
3179 item = fi->fi_lw.lw_item;
3180 if (item == NULL)
3181 result = FALSE;
3182 else
3183 {
3184 fi->fi_lw.lw_item = item->li_next;
3185 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3186 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3187 }
3188 return result;
3189}
3190
3191/*
3192 * Free the structure used to store info used by ":for".
3193 */
3194 void
3195free_for_info(fi_void)
3196 void *fi_void;
3197{
Bram Moolenaar33570922005-01-25 22:26:29 +00003198 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003200 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003201 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203 list_unref(fi->fi_list);
3204 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 vim_free(fi);
3206}
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3209
3210 void
3211set_context_for_expression(xp, arg, cmdidx)
3212 expand_T *xp;
3213 char_u *arg;
3214 cmdidx_T cmdidx;
3215{
3216 int got_eq = FALSE;
3217 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 if (cmdidx == CMD_let)
3221 {
3222 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003223 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224 {
3225 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003226 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 {
3228 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 if (vim_iswhite(*p))
3231 break;
3232 }
3233 return;
3234 }
3235 }
3236 else
3237 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3238 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 while ((xp->xp_pattern = vim_strpbrk(arg,
3240 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3241 {
3242 c = *xp->xp_pattern;
3243 if (c == '&')
3244 {
3245 c = xp->xp_pattern[1];
3246 if (c == '&')
3247 {
3248 ++xp->xp_pattern;
3249 xp->xp_context = cmdidx != CMD_let || got_eq
3250 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3251 }
3252 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003255 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3256 xp->xp_pattern += 2;
3257
3258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 }
3260 else if (c == '$')
3261 {
3262 /* environment variable */
3263 xp->xp_context = EXPAND_ENV_VARS;
3264 }
3265 else if (c == '=')
3266 {
3267 got_eq = TRUE;
3268 xp->xp_context = EXPAND_EXPRESSION;
3269 }
3270 else if (c == '<'
3271 && xp->xp_context == EXPAND_FUNCTIONS
3272 && vim_strchr(xp->xp_pattern, '(') == NULL)
3273 {
3274 /* Function name can start with "<SNR>" */
3275 break;
3276 }
3277 else if (cmdidx != CMD_let || got_eq)
3278 {
3279 if (c == '"') /* string */
3280 {
3281 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3282 if (c == '\\' && xp->xp_pattern[1] != NUL)
3283 ++xp->xp_pattern;
3284 xp->xp_context = EXPAND_NOTHING;
3285 }
3286 else if (c == '\'') /* literal string */
3287 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003288 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3290 /* skip */ ;
3291 xp->xp_context = EXPAND_NOTHING;
3292 }
3293 else if (c == '|')
3294 {
3295 if (xp->xp_pattern[1] == '|')
3296 {
3297 ++xp->xp_pattern;
3298 xp->xp_context = EXPAND_EXPRESSION;
3299 }
3300 else
3301 xp->xp_context = EXPAND_COMMANDS;
3302 }
3303 else
3304 xp->xp_context = EXPAND_EXPRESSION;
3305 }
3306 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 /* Doesn't look like something valid, expand as an expression
3308 * anyway. */
3309 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 arg = xp->xp_pattern;
3311 if (*arg != NUL)
3312 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3313 /* skip */ ;
3314 }
3315 xp->xp_pattern = arg;
3316}
3317
3318#endif /* FEAT_CMDL_COMPL */
3319
3320/*
3321 * ":1,25call func(arg1, arg2)" function call.
3322 */
3323 void
3324ex_call(eap)
3325 exarg_T *eap;
3326{
3327 char_u *arg = eap->arg;
3328 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003330 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003332 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 linenr_T lnum;
3334 int doesrange;
3335 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003336 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003338 if (eap->skip)
3339 {
3340 /* trans_function_name() doesn't work well when skipping, use eval0()
3341 * instead to skip to any following command, e.g. for:
3342 * :if 0 | call dict.foo().bar() | endif */
3343 eval0(eap->arg, &rettv, &eap->nextcmd, FALSE);
3344 return;
3345 }
3346
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003347 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003348 if (fudi.fd_newkey != NULL)
3349 {
3350 /* Still need to give an error message for missing key. */
3351 EMSG2(_(e_dictkey), fudi.fd_newkey);
3352 vim_free(fudi.fd_newkey);
3353 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003354 if (tofree == NULL)
3355 return;
3356
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003357 /* Increase refcount on dictionary, it could get deleted when evaluating
3358 * the arguments. */
3359 if (fudi.fd_dict != NULL)
3360 ++fudi.fd_dict->dv_refcount;
3361
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003362 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003363 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003364 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
Bram Moolenaar532c7802005-01-27 14:44:31 +00003366 /* Skip white space to allow ":call func ()". Not good, but required for
3367 * backward compatibility. */
3368 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003369 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370
3371 if (*startarg != '(')
3372 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003373 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 goto end;
3375 }
3376
3377 /*
3378 * When skipping, evaluate the function once, to find the end of the
3379 * arguments.
3380 * When the function takes a range, this is discovered after the first
3381 * call, and the loop is broken.
3382 */
3383 if (eap->skip)
3384 {
3385 ++emsg_skip;
3386 lnum = eap->line2; /* do it once, also with an invalid range */
3387 }
3388 else
3389 lnum = eap->line1;
3390 for ( ; lnum <= eap->line2; ++lnum)
3391 {
3392 if (!eap->skip && eap->addr_count > 0)
3393 {
3394 curwin->w_cursor.lnum = lnum;
3395 curwin->w_cursor.col = 0;
3396 }
3397 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003398 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003399 eap->line1, eap->line2, &doesrange,
3400 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
3402 failed = TRUE;
3403 break;
3404 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003405
3406 /* Handle a function returning a Funcref, Dictionary or List. */
3407 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3408 {
3409 failed = TRUE;
3410 break;
3411 }
3412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003413 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (doesrange || eap->skip)
3415 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003416
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003418 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003419 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003420 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 if (aborting())
3422 break;
3423 }
3424 if (eap->skip)
3425 --emsg_skip;
3426
3427 if (!failed)
3428 {
3429 /* Check for trailing illegal characters and a following command. */
3430 if (!ends_excmd(*arg))
3431 {
3432 emsg_severe = TRUE;
3433 EMSG(_(e_trailing));
3434 }
3435 else
3436 eap->nextcmd = check_nextcmd(arg);
3437 }
3438
3439end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003440 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003441 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442}
3443
3444/*
3445 * ":unlet[!] var1 ... " command.
3446 */
3447 void
3448ex_unlet(eap)
3449 exarg_T *eap;
3450{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003451 ex_unletlock(eap, eap->arg, 0);
3452}
3453
3454/*
3455 * ":lockvar" and ":unlockvar" commands
3456 */
3457 void
3458ex_lockvar(eap)
3459 exarg_T *eap;
3460{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003462 int deep = 2;
3463
3464 if (eap->forceit)
3465 deep = -1;
3466 else if (vim_isdigit(*arg))
3467 {
3468 deep = getdigits(&arg);
3469 arg = skipwhite(arg);
3470 }
3471
3472 ex_unletlock(eap, arg, deep);
3473}
3474
3475/*
3476 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3477 */
3478 static void
3479ex_unletlock(eap, argstart, deep)
3480 exarg_T *eap;
3481 char_u *argstart;
3482 int deep;
3483{
3484 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003487 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
3489 do
3490 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003491 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003492 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3493 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003494 if (lv.ll_name == NULL)
3495 error = TRUE; /* error but continue parsing */
3496 if (name_end == NULL || (!vim_iswhite(*name_end)
3497 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003499 if (name_end != NULL)
3500 {
3501 emsg_severe = TRUE;
3502 EMSG(_(e_trailing));
3503 }
3504 if (!(eap->skip || error))
3505 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 break;
3507 }
3508
3509 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003510 {
3511 if (eap->cmdidx == CMD_unlet)
3512 {
3513 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3514 error = TRUE;
3515 }
3516 else
3517 {
3518 if (do_lock_var(&lv, name_end, deep,
3519 eap->cmdidx == CMD_lockvar) == FAIL)
3520 error = TRUE;
3521 }
3522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003524 if (!eap->skip)
3525 clear_lval(&lv);
3526
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 arg = skipwhite(name_end);
3528 } while (!ends_excmd(*arg));
3529
3530 eap->nextcmd = check_nextcmd(arg);
3531}
3532
Bram Moolenaar8c711452005-01-14 21:53:12 +00003533 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003534do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003535 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003536 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 int forceit;
3538{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539 int ret = OK;
3540 int cc;
3541
3542 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003543 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003544 cc = *name_end;
3545 *name_end = NUL;
3546
3547 /* Normal name or expanded name. */
3548 if (check_changedtick(lp->ll_name))
3549 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003550 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003551 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003552 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003553 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3555 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 else if (lp->ll_range)
3557 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003558 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003559
3560 /* Delete a range of List items. */
3561 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3562 {
3563 li = lp->ll_li->li_next;
3564 listitem_remove(lp->ll_list, lp->ll_li);
3565 lp->ll_li = li;
3566 ++lp->ll_n1;
3567 }
3568 }
3569 else
3570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003572 /* unlet a List item. */
3573 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003574 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003575 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003576 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003577 }
3578
3579 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580}
3581
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582/*
3583 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 */
3586 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590{
Bram Moolenaar33570922005-01-25 22:26:29 +00003591 hashtab_T *ht;
3592 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003593 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003594 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaar33570922005-01-25 22:26:29 +00003596 ht = find_var_ht(name, &varname);
3597 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003599 hi = hash_find(ht, varname);
3600 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003601 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003602 di = HI2DI(hi);
3603 if (var_check_fixed(di->di_flags, name)
3604 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605 return FAIL;
3606 delete_var(ht, hi);
3607 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610 if (forceit)
3611 return OK;
3612 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 return FAIL;
3614}
3615
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616/*
3617 * Lock or unlock variable indicated by "lp".
3618 * "deep" is the levels to go (-1 for unlimited);
3619 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3620 */
3621 static int
3622do_lock_var(lp, name_end, deep, lock)
3623 lval_T *lp;
3624 char_u *name_end;
3625 int deep;
3626 int lock;
3627{
3628 int ret = OK;
3629 int cc;
3630 dictitem_T *di;
3631
3632 if (deep == 0) /* nothing to do */
3633 return OK;
3634
3635 if (lp->ll_tv == NULL)
3636 {
3637 cc = *name_end;
3638 *name_end = NUL;
3639
3640 /* Normal name or expanded name. */
3641 if (check_changedtick(lp->ll_name))
3642 ret = FAIL;
3643 else
3644 {
3645 di = find_var(lp->ll_name, NULL);
3646 if (di == NULL)
3647 ret = FAIL;
3648 else
3649 {
3650 if (lock)
3651 di->di_flags |= DI_FLAGS_LOCK;
3652 else
3653 di->di_flags &= ~DI_FLAGS_LOCK;
3654 item_lock(&di->di_tv, deep, lock);
3655 }
3656 }
3657 *name_end = cc;
3658 }
3659 else if (lp->ll_range)
3660 {
3661 listitem_T *li = lp->ll_li;
3662
3663 /* (un)lock a range of List items. */
3664 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3665 {
3666 item_lock(&li->li_tv, deep, lock);
3667 li = li->li_next;
3668 ++lp->ll_n1;
3669 }
3670 }
3671 else if (lp->ll_list != NULL)
3672 /* (un)lock a List item. */
3673 item_lock(&lp->ll_li->li_tv, deep, lock);
3674 else
3675 /* un(lock) a Dictionary item. */
3676 item_lock(&lp->ll_di->di_tv, deep, lock);
3677
3678 return ret;
3679}
3680
3681/*
3682 * Lock or unlock an item. "deep" is nr of levels to go.
3683 */
3684 static void
3685item_lock(tv, deep, lock)
3686 typval_T *tv;
3687 int deep;
3688 int lock;
3689{
3690 static int recurse = 0;
3691 list_T *l;
3692 listitem_T *li;
3693 dict_T *d;
3694 hashitem_T *hi;
3695 int todo;
3696
3697 if (recurse >= DICT_MAXNEST)
3698 {
3699 EMSG(_("E743: variable nested too deep for (un)lock"));
3700 return;
3701 }
3702 if (deep == 0)
3703 return;
3704 ++recurse;
3705
3706 /* lock/unlock the item itself */
3707 if (lock)
3708 tv->v_lock |= VAR_LOCKED;
3709 else
3710 tv->v_lock &= ~VAR_LOCKED;
3711
3712 switch (tv->v_type)
3713 {
3714 case VAR_LIST:
3715 if ((l = tv->vval.v_list) != NULL)
3716 {
3717 if (lock)
3718 l->lv_lock |= VAR_LOCKED;
3719 else
3720 l->lv_lock &= ~VAR_LOCKED;
3721 if (deep < 0 || deep > 1)
3722 /* recursive: lock/unlock the items the List contains */
3723 for (li = l->lv_first; li != NULL; li = li->li_next)
3724 item_lock(&li->li_tv, deep - 1, lock);
3725 }
3726 break;
3727 case VAR_DICT:
3728 if ((d = tv->vval.v_dict) != NULL)
3729 {
3730 if (lock)
3731 d->dv_lock |= VAR_LOCKED;
3732 else
3733 d->dv_lock &= ~VAR_LOCKED;
3734 if (deep < 0 || deep > 1)
3735 {
3736 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003737 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003738 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3739 {
3740 if (!HASHITEM_EMPTY(hi))
3741 {
3742 --todo;
3743 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3744 }
3745 }
3746 }
3747 }
3748 }
3749 --recurse;
3750}
3751
Bram Moolenaara40058a2005-07-11 22:42:07 +00003752/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003753 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3754 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003755 */
3756 static int
3757tv_islocked(tv)
3758 typval_T *tv;
3759{
3760 return (tv->v_lock & VAR_LOCKED)
3761 || (tv->v_type == VAR_LIST
3762 && tv->vval.v_list != NULL
3763 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3764 || (tv->v_type == VAR_DICT
3765 && tv->vval.v_dict != NULL
3766 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3767}
3768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3770/*
3771 * Delete all "menutrans_" variables.
3772 */
3773 void
3774del_menutrans_vars()
3775{
Bram Moolenaar33570922005-01-25 22:26:29 +00003776 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003777 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003780 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003781 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 {
3783 if (!HASHITEM_EMPTY(hi))
3784 {
3785 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3787 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003788 }
3789 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003790 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791}
3792#endif
3793
3794#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3795
3796/*
3797 * Local string buffer for the next two functions to store a variable name
3798 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3799 * get_user_var_name().
3800 */
3801
3802static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3803
3804static char_u *varnamebuf = NULL;
3805static int varnamebuflen = 0;
3806
3807/*
3808 * Function to concatenate a prefix and a variable name.
3809 */
3810 static char_u *
3811cat_prefix_varname(prefix, name)
3812 int prefix;
3813 char_u *name;
3814{
3815 int len;
3816
3817 len = (int)STRLEN(name) + 3;
3818 if (len > varnamebuflen)
3819 {
3820 vim_free(varnamebuf);
3821 len += 10; /* some additional space */
3822 varnamebuf = alloc(len);
3823 if (varnamebuf == NULL)
3824 {
3825 varnamebuflen = 0;
3826 return NULL;
3827 }
3828 varnamebuflen = len;
3829 }
3830 *varnamebuf = prefix;
3831 varnamebuf[1] = ':';
3832 STRCPY(varnamebuf + 2, name);
3833 return varnamebuf;
3834}
3835
3836/*
3837 * Function given to ExpandGeneric() to obtain the list of user defined
3838 * (global/buffer/window/built-in) variable names.
3839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 char_u *
3841get_user_var_name(xp, idx)
3842 expand_T *xp;
3843 int idx;
3844{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003845 static long_u gdone;
3846 static long_u bdone;
3847 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003848#ifdef FEAT_WINDOWS
3849 static long_u tdone;
3850#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003851 static int vidx;
3852 static hashitem_T *hi;
3853 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
3855 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003856 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003857 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003858#ifdef FEAT_WINDOWS
3859 tdone = 0;
3860#endif
3861 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003862
3863 /* Global variables */
3864 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003866 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003867 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003868 else
3869 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003870 while (HASHITEM_EMPTY(hi))
3871 ++hi;
3872 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3873 return cat_prefix_varname('g', hi->hi_key);
3874 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003876
3877 /* b: variables */
3878 ht = &curbuf->b_vars.dv_hashtab;
3879 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003881 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003882 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003883 else
3884 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003885 while (HASHITEM_EMPTY(hi))
3886 ++hi;
3887 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003889 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003891 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 return (char_u *)"b:changedtick";
3893 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003894
3895 /* w: variables */
3896 ht = &curwin->w_vars.dv_hashtab;
3897 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003899 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003900 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003901 else
3902 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003903 while (HASHITEM_EMPTY(hi))
3904 ++hi;
3905 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003907
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003908#ifdef FEAT_WINDOWS
3909 /* t: variables */
3910 ht = &curtab->tp_vars.dv_hashtab;
3911 if (tdone < ht->ht_used)
3912 {
3913 if (tdone++ == 0)
3914 hi = ht->ht_array;
3915 else
3916 ++hi;
3917 while (HASHITEM_EMPTY(hi))
3918 ++hi;
3919 return cat_prefix_varname('t', hi->hi_key);
3920 }
3921#endif
3922
Bram Moolenaar33570922005-01-25 22:26:29 +00003923 /* v: variables */
3924 if (vidx < VV_LEN)
3925 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927 vim_free(varnamebuf);
3928 varnamebuf = NULL;
3929 varnamebuflen = 0;
3930 return NULL;
3931}
3932
3933#endif /* FEAT_CMDL_COMPL */
3934
3935/*
3936 * types for expressions.
3937 */
3938typedef enum
3939{
3940 TYPE_UNKNOWN = 0
3941 , TYPE_EQUAL /* == */
3942 , TYPE_NEQUAL /* != */
3943 , TYPE_GREATER /* > */
3944 , TYPE_GEQUAL /* >= */
3945 , TYPE_SMALLER /* < */
3946 , TYPE_SEQUAL /* <= */
3947 , TYPE_MATCH /* =~ */
3948 , TYPE_NOMATCH /* !~ */
3949} exptype_T;
3950
3951/*
3952 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3955 */
3956
3957/*
3958 * Handle zero level expression.
3959 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003960 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003961 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 * Return OK or FAIL.
3963 */
3964 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003965eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 char_u **nextcmd;
3969 int evaluate;
3970{
3971 int ret;
3972 char_u *p;
3973
3974 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003975 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 if (ret == FAIL || !ends_excmd(*p))
3977 {
3978 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003979 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 /*
3981 * Report the invalid expression unless the expression evaluation has
3982 * been cancelled due to an aborting error, an interrupt, or an
3983 * exception.
3984 */
3985 if (!aborting())
3986 EMSG2(_(e_invexpr2), arg);
3987 ret = FAIL;
3988 }
3989 if (nextcmd != NULL)
3990 *nextcmd = check_nextcmd(p);
3991
3992 return ret;
3993}
3994
3995/*
3996 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003997 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 *
3999 * "arg" must point to the first non-white of the expression.
4000 * "arg" is advanced to the next non-white after the recognized expression.
4001 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004002 * Note: "rettv.v_lock" is not set.
4003 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 * Return OK or FAIL.
4005 */
4006 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 int evaluate;
4011{
4012 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014
4015 /*
4016 * Get the first variable.
4017 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 return FAIL;
4020
4021 if ((*arg)[0] == '?')
4022 {
4023 result = FALSE;
4024 if (evaluate)
4025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004026 int error = FALSE;
4027
4028 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004030 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004031 if (error)
4032 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034
4035 /*
4036 * Get the second variable.
4037 */
4038 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004039 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 return FAIL;
4041
4042 /*
4043 * Check for the ":".
4044 */
4045 if ((*arg)[0] != ':')
4046 {
4047 EMSG(_("E109: Missing ':' after '?'"));
4048 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 return FAIL;
4051 }
4052
4053 /*
4054 * Get the third variable.
4055 */
4056 *arg = skipwhite(*arg + 1);
4057 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4058 {
4059 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004060 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return FAIL;
4062 }
4063 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
4066
4067 return OK;
4068}
4069
4070/*
4071 * Handle first level expression:
4072 * expr2 || expr2 || expr2 logical OR
4073 *
4074 * "arg" must point to the first non-white of the expression.
4075 * "arg" is advanced to the next non-white after the recognized expression.
4076 *
4077 * Return OK or FAIL.
4078 */
4079 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 int evaluate;
4084{
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 long result;
4087 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
4090 /*
4091 * Get the first variable.
4092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 return FAIL;
4095
4096 /*
4097 * Repeat until there is no following "||".
4098 */
4099 first = TRUE;
4100 result = FALSE;
4101 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4102 {
4103 if (evaluate && first)
4104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004108 if (error)
4109 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 first = FALSE;
4111 }
4112
4113 /*
4114 * Get the second variable.
4115 */
4116 *arg = skipwhite(*arg + 2);
4117 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4118 return FAIL;
4119
4120 /*
4121 * Compute the result.
4122 */
4123 if (evaluate && !result)
4124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004125 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004128 if (error)
4129 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131 if (evaluate)
4132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 rettv->v_type = VAR_NUMBER;
4134 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 }
4137
4138 return OK;
4139}
4140
4141/*
4142 * Handle second level expression:
4143 * expr3 && expr3 && expr3 logical AND
4144 *
4145 * "arg" must point to the first non-white of the expression.
4146 * "arg" is advanced to the next non-white after the recognized expression.
4147 *
4148 * Return OK or FAIL.
4149 */
4150 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 int evaluate;
4155{
Bram Moolenaar33570922005-01-25 22:26:29 +00004156 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 long result;
4158 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004159 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 /*
4162 * Get the first variable.
4163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 return FAIL;
4166
4167 /*
4168 * Repeat until there is no following "&&".
4169 */
4170 first = TRUE;
4171 result = TRUE;
4172 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4173 {
4174 if (evaluate && first)
4175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004176 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 if (error)
4180 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 first = FALSE;
4182 }
4183
4184 /*
4185 * Get the second variable.
4186 */
4187 *arg = skipwhite(*arg + 2);
4188 if (eval4(arg, &var2, evaluate && result) == FAIL)
4189 return FAIL;
4190
4191 /*
4192 * Compute the result.
4193 */
4194 if (evaluate && result)
4195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 if (error)
4200 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202 if (evaluate)
4203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 rettv->v_type = VAR_NUMBER;
4205 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207 }
4208
4209 return OK;
4210}
4211
4212/*
4213 * Handle third level expression:
4214 * var1 == var2
4215 * var1 =~ var2
4216 * var1 != var2
4217 * var1 !~ var2
4218 * var1 > var2
4219 * var1 >= var2
4220 * var1 < var2
4221 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004222 * var1 is var2
4223 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 *
4225 * "arg" must point to the first non-white of the expression.
4226 * "arg" is advanced to the next non-white after the recognized expression.
4227 *
4228 * Return OK or FAIL.
4229 */
4230 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 int evaluate;
4235{
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 char_u *p;
4238 int i;
4239 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004240 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 int len = 2;
4242 long n1, n2;
4243 char_u *s1, *s2;
4244 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4245 regmatch_T regmatch;
4246 int ic;
4247 char_u *save_cpo;
4248
4249 /*
4250 * Get the first variable.
4251 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return FAIL;
4254
4255 p = *arg;
4256 switch (p[0])
4257 {
4258 case '=': if (p[1] == '=')
4259 type = TYPE_EQUAL;
4260 else if (p[1] == '~')
4261 type = TYPE_MATCH;
4262 break;
4263 case '!': if (p[1] == '=')
4264 type = TYPE_NEQUAL;
4265 else if (p[1] == '~')
4266 type = TYPE_NOMATCH;
4267 break;
4268 case '>': if (p[1] != '=')
4269 {
4270 type = TYPE_GREATER;
4271 len = 1;
4272 }
4273 else
4274 type = TYPE_GEQUAL;
4275 break;
4276 case '<': if (p[1] != '=')
4277 {
4278 type = TYPE_SMALLER;
4279 len = 1;
4280 }
4281 else
4282 type = TYPE_SEQUAL;
4283 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004284 case 'i': if (p[1] == 's')
4285 {
4286 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4287 len = 5;
4288 if (!vim_isIDc(p[len]))
4289 {
4290 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4291 type_is = TRUE;
4292 }
4293 }
4294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296
4297 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004298 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 */
4300 if (type != TYPE_UNKNOWN)
4301 {
4302 /* extra question mark appended: ignore case */
4303 if (p[len] == '?')
4304 {
4305 ic = TRUE;
4306 ++len;
4307 }
4308 /* extra '#' appended: match case */
4309 else if (p[len] == '#')
4310 {
4311 ic = FALSE;
4312 ++len;
4313 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004314 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 else
4316 ic = p_ic;
4317
4318 /*
4319 * Get the second variable.
4320 */
4321 *arg = skipwhite(p + len);
4322 if (eval5(arg, &var2, evaluate) == FAIL)
4323 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 return FAIL;
4326 }
4327
4328 if (evaluate)
4329 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004330 if (type_is && rettv->v_type != var2.v_type)
4331 {
4332 /* For "is" a different type always means FALSE, for "notis"
4333 * it means TRUE. */
4334 n1 = (type == TYPE_NEQUAL);
4335 }
4336 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4337 {
4338 if (type_is)
4339 {
4340 n1 = (rettv->v_type == var2.v_type
4341 && rettv->vval.v_list == var2.vval.v_list);
4342 if (type == TYPE_NEQUAL)
4343 n1 = !n1;
4344 }
4345 else if (rettv->v_type != var2.v_type
4346 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4347 {
4348 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004349 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004350 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004351 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004352 clear_tv(rettv);
4353 clear_tv(&var2);
4354 return FAIL;
4355 }
4356 else
4357 {
4358 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004359 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4360 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004361 if (type == TYPE_NEQUAL)
4362 n1 = !n1;
4363 }
4364 }
4365
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004366 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4367 {
4368 if (type_is)
4369 {
4370 n1 = (rettv->v_type == var2.v_type
4371 && rettv->vval.v_dict == var2.vval.v_dict);
4372 if (type == TYPE_NEQUAL)
4373 n1 = !n1;
4374 }
4375 else if (rettv->v_type != var2.v_type
4376 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4377 {
4378 if (rettv->v_type != var2.v_type)
4379 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4380 else
4381 EMSG(_("E736: Invalid operation for Dictionary"));
4382 clear_tv(rettv);
4383 clear_tv(&var2);
4384 return FAIL;
4385 }
4386 else
4387 {
4388 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004389 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4390 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004391 if (type == TYPE_NEQUAL)
4392 n1 = !n1;
4393 }
4394 }
4395
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4397 {
4398 if (rettv->v_type != var2.v_type
4399 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4400 {
4401 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004402 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004403 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004404 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004405 clear_tv(rettv);
4406 clear_tv(&var2);
4407 return FAIL;
4408 }
4409 else
4410 {
4411 /* Compare two Funcrefs for being equal or unequal. */
4412 if (rettv->vval.v_string == NULL
4413 || var2.vval.v_string == NULL)
4414 n1 = FALSE;
4415 else
4416 n1 = STRCMP(rettv->vval.v_string,
4417 var2.vval.v_string) == 0;
4418 if (type == TYPE_NEQUAL)
4419 n1 = !n1;
4420 }
4421 }
4422
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004423#ifdef FEAT_FLOAT
4424 /*
4425 * If one of the two variables is a float, compare as a float.
4426 * When using "=~" or "!~", always compare as string.
4427 */
4428 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4429 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4430 {
4431 float_T f1, f2;
4432
4433 if (rettv->v_type == VAR_FLOAT)
4434 f1 = rettv->vval.v_float;
4435 else
4436 f1 = get_tv_number(rettv);
4437 if (var2.v_type == VAR_FLOAT)
4438 f2 = var2.vval.v_float;
4439 else
4440 f2 = get_tv_number(&var2);
4441 n1 = FALSE;
4442 switch (type)
4443 {
4444 case TYPE_EQUAL: n1 = (f1 == f2); break;
4445 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4446 case TYPE_GREATER: n1 = (f1 > f2); break;
4447 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4448 case TYPE_SMALLER: n1 = (f1 < f2); break;
4449 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4450 case TYPE_UNKNOWN:
4451 case TYPE_MATCH:
4452 case TYPE_NOMATCH: break; /* avoid gcc warning */
4453 }
4454 }
4455#endif
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 /*
4458 * If one of the two variables is a number, compare as a number.
4459 * When using "=~" or "!~", always compare as string.
4460 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004461 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004464 n1 = get_tv_number(rettv);
4465 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 switch (type)
4467 {
4468 case TYPE_EQUAL: n1 = (n1 == n2); break;
4469 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4470 case TYPE_GREATER: n1 = (n1 > n2); break;
4471 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4472 case TYPE_SMALLER: n1 = (n1 < n2); break;
4473 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4474 case TYPE_UNKNOWN:
4475 case TYPE_MATCH:
4476 case TYPE_NOMATCH: break; /* avoid gcc warning */
4477 }
4478 }
4479 else
4480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 s1 = get_tv_string_buf(rettv, buf1);
4482 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4484 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4485 else
4486 i = 0;
4487 n1 = FALSE;
4488 switch (type)
4489 {
4490 case TYPE_EQUAL: n1 = (i == 0); break;
4491 case TYPE_NEQUAL: n1 = (i != 0); break;
4492 case TYPE_GREATER: n1 = (i > 0); break;
4493 case TYPE_GEQUAL: n1 = (i >= 0); break;
4494 case TYPE_SMALLER: n1 = (i < 0); break;
4495 case TYPE_SEQUAL: n1 = (i <= 0); break;
4496
4497 case TYPE_MATCH:
4498 case TYPE_NOMATCH:
4499 /* avoid 'l' flag in 'cpoptions' */
4500 save_cpo = p_cpo;
4501 p_cpo = (char_u *)"";
4502 regmatch.regprog = vim_regcomp(s2,
4503 RE_MAGIC + RE_STRING);
4504 regmatch.rm_ic = ic;
4505 if (regmatch.regprog != NULL)
4506 {
4507 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4508 vim_free(regmatch.regprog);
4509 if (type == TYPE_NOMATCH)
4510 n1 = !n1;
4511 }
4512 p_cpo = save_cpo;
4513 break;
4514
4515 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4516 }
4517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004518 clear_tv(rettv);
4519 clear_tv(&var2);
4520 rettv->v_type = VAR_NUMBER;
4521 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523 }
4524
4525 return OK;
4526}
4527
4528/*
4529 * Handle fourth level expression:
4530 * + number addition
4531 * - number subtraction
4532 * . string concatenation
4533 *
4534 * "arg" must point to the first non-white of the expression.
4535 * "arg" is advanced to the next non-white after the recognized expression.
4536 *
4537 * Return OK or FAIL.
4538 */
4539 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004540eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 int evaluate;
4544{
Bram Moolenaar33570922005-01-25 22:26:29 +00004545 typval_T var2;
4546 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 int op;
4548 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004549#ifdef FEAT_FLOAT
4550 float_T f1 = 0, f2 = 0;
4551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 char_u *s1, *s2;
4553 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4554 char_u *p;
4555
4556 /*
4557 * Get the first variable.
4558 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004559 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 return FAIL;
4561
4562 /*
4563 * Repeat computing, until no '+', '-' or '.' is following.
4564 */
4565 for (;;)
4566 {
4567 op = **arg;
4568 if (op != '+' && op != '-' && op != '.')
4569 break;
4570
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004571 if ((op != '+' || rettv->v_type != VAR_LIST)
4572#ifdef FEAT_FLOAT
4573 && (op == '.' || rettv->v_type != VAR_FLOAT)
4574#endif
4575 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004576 {
4577 /* For "list + ...", an illegal use of the first operand as
4578 * a number cannot be determined before evaluating the 2nd
4579 * operand: if this is also a list, all is ok.
4580 * For "something . ...", "something - ..." or "non-list + ...",
4581 * we know that the first operand needs to be a string or number
4582 * without evaluating the 2nd operand. So check before to avoid
4583 * side effects after an error. */
4584 if (evaluate && get_tv_string_chk(rettv) == NULL)
4585 {
4586 clear_tv(rettv);
4587 return FAIL;
4588 }
4589 }
4590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 /*
4592 * Get the second variable.
4593 */
4594 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004595 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004597 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 return FAIL;
4599 }
4600
4601 if (evaluate)
4602 {
4603 /*
4604 * Compute the result.
4605 */
4606 if (op == '.')
4607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004608 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4609 s2 = get_tv_string_buf_chk(&var2, buf2);
4610 if (s2 == NULL) /* type error ? */
4611 {
4612 clear_tv(rettv);
4613 clear_tv(&var2);
4614 return FAIL;
4615 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004616 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 clear_tv(rettv);
4618 rettv->v_type = VAR_STRING;
4619 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004621 else if (op == '+' && rettv->v_type == VAR_LIST
4622 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 {
4624 /* concatenate Lists */
4625 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4626 &var3) == FAIL)
4627 {
4628 clear_tv(rettv);
4629 clear_tv(&var2);
4630 return FAIL;
4631 }
4632 clear_tv(rettv);
4633 *rettv = var3;
4634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 else
4636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004637 int error = FALSE;
4638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004639#ifdef FEAT_FLOAT
4640 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004641 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004642 f1 = rettv->vval.v_float;
4643 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004644 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004645 else
4646#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004647 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004648 n1 = get_tv_number_chk(rettv, &error);
4649 if (error)
4650 {
4651 /* This can only happen for "list + non-list". For
4652 * "non-list + ..." or "something - ...", we returned
4653 * before evaluating the 2nd operand. */
4654 clear_tv(rettv);
4655 return FAIL;
4656 }
4657#ifdef FEAT_FLOAT
4658 if (var2.v_type == VAR_FLOAT)
4659 f1 = n1;
4660#endif
4661 }
4662#ifdef FEAT_FLOAT
4663 if (var2.v_type == VAR_FLOAT)
4664 {
4665 f2 = var2.vval.v_float;
4666 n2 = 0;
4667 }
4668 else
4669#endif
4670 {
4671 n2 = get_tv_number_chk(&var2, &error);
4672 if (error)
4673 {
4674 clear_tv(rettv);
4675 clear_tv(&var2);
4676 return FAIL;
4677 }
4678#ifdef FEAT_FLOAT
4679 if (rettv->v_type == VAR_FLOAT)
4680 f2 = n2;
4681#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004682 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004683 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004684
4685#ifdef FEAT_FLOAT
4686 /* If there is a float on either side the result is a float. */
4687 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4688 {
4689 if (op == '+')
4690 f1 = f1 + f2;
4691 else
4692 f1 = f1 - f2;
4693 rettv->v_type = VAR_FLOAT;
4694 rettv->vval.v_float = f1;
4695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004697#endif
4698 {
4699 if (op == '+')
4700 n1 = n1 + n2;
4701 else
4702 n1 = n1 - n2;
4703 rettv->v_type = VAR_NUMBER;
4704 rettv->vval.v_number = n1;
4705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709 }
4710 return OK;
4711}
4712
4713/*
4714 * Handle fifth level expression:
4715 * * number multiplication
4716 * / number division
4717 * % number modulo
4718 *
4719 * "arg" must point to the first non-white of the expression.
4720 * "arg" is advanced to the next non-white after the recognized expression.
4721 *
4722 * Return OK or FAIL.
4723 */
4724 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004725eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004729 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
Bram Moolenaar33570922005-01-25 22:26:29 +00004731 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 int op;
4733 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734#ifdef FEAT_FLOAT
4735 int use_float = FALSE;
4736 float_T f1 = 0, f2;
4737#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004738 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
4740 /*
4741 * Get the first variable.
4742 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004743 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 return FAIL;
4745
4746 /*
4747 * Repeat computing, until no '*', '/' or '%' is following.
4748 */
4749 for (;;)
4750 {
4751 op = **arg;
4752 if (op != '*' && op != '/' && op != '%')
4753 break;
4754
4755 if (evaluate)
4756 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757#ifdef FEAT_FLOAT
4758 if (rettv->v_type == VAR_FLOAT)
4759 {
4760 f1 = rettv->vval.v_float;
4761 use_float = TRUE;
4762 n1 = 0;
4763 }
4764 else
4765#endif
4766 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004767 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004768 if (error)
4769 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 else
4772 n1 = 0;
4773
4774 /*
4775 * Get the second variable.
4776 */
4777 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004778 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 return FAIL;
4780
4781 if (evaluate)
4782 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783#ifdef FEAT_FLOAT
4784 if (var2.v_type == VAR_FLOAT)
4785 {
4786 if (!use_float)
4787 {
4788 f1 = n1;
4789 use_float = TRUE;
4790 }
4791 f2 = var2.vval.v_float;
4792 n2 = 0;
4793 }
4794 else
4795#endif
4796 {
4797 n2 = get_tv_number_chk(&var2, &error);
4798 clear_tv(&var2);
4799 if (error)
4800 return FAIL;
4801#ifdef FEAT_FLOAT
4802 if (use_float)
4803 f2 = n2;
4804#endif
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 /*
4808 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811#ifdef FEAT_FLOAT
4812 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814 if (op == '*')
4815 f1 = f1 * f2;
4816 else if (op == '/')
4817 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004818# ifdef VMS
4819 /* VMS crashes on divide by zero, work around it */
4820 if (f2 == 0.0)
4821 {
4822 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004823 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004824 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004825 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004826 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004827 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004828 }
4829 else
4830 f1 = f1 / f2;
4831# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004832 /* We rely on the floating point library to handle divide
4833 * by zero to result in "inf" and not a crash. */
4834 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004835# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004838 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004839 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840 return FAIL;
4841 }
4842 rettv->v_type = VAR_FLOAT;
4843 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
4845 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004848 if (op == '*')
4849 n1 = n1 * n2;
4850 else if (op == '/')
4851 {
4852 if (n2 == 0) /* give an error message? */
4853 {
4854 if (n1 == 0)
4855 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4856 else if (n1 < 0)
4857 n1 = -0x7fffffffL;
4858 else
4859 n1 = 0x7fffffffL;
4860 }
4861 else
4862 n1 = n1 / n2;
4863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004865 {
4866 if (n2 == 0) /* give an error message? */
4867 n1 = 0;
4868 else
4869 n1 = n1 % n2;
4870 }
4871 rettv->v_type = VAR_NUMBER;
4872 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
4875 }
4876
4877 return OK;
4878}
4879
4880/*
4881 * Handle sixth level expression:
4882 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004883 * "string" string constant
4884 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 * &option-name option value
4886 * @r register contents
4887 * identifier variable value
4888 * function() function call
4889 * $VAR environment variable
4890 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004891 * [expr, expr] List
4892 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 *
4894 * Also handle:
4895 * ! in front logical NOT
4896 * - in front unary minus
4897 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004898 * trailing [] subscript in String or List
4899 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 *
4901 * "arg" must point to the first non-white of the expression.
4902 * "arg" is advanced to the next non-white after the recognized expression.
4903 *
4904 * Return OK or FAIL.
4905 */
4906 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004907eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004911 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 long n;
4914 int len;
4915 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 char_u *start_leader, *end_leader;
4917 int ret = OK;
4918 char_u *alias;
4919
4920 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004922 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004924 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926 /*
4927 * Skip '!' and '-' characters. They are handled later.
4928 */
4929 start_leader = *arg;
4930 while (**arg == '!' || **arg == '-' || **arg == '+')
4931 *arg = skipwhite(*arg + 1);
4932 end_leader = *arg;
4933
4934 switch (**arg)
4935 {
4936 /*
4937 * Number constant.
4938 */
4939 case '0':
4940 case '1':
4941 case '2':
4942 case '3':
4943 case '4':
4944 case '5':
4945 case '6':
4946 case '7':
4947 case '8':
4948 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949 {
4950#ifdef FEAT_FLOAT
4951 char_u *p = skipdigits(*arg + 1);
4952 int get_float = FALSE;
4953
4954 /* We accept a float when the format matches
4955 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004956 * strict to avoid backwards compatibility problems.
4957 * Don't look for a float after the "." operator, so that
4958 * ":let vers = 1.2.3" doesn't fail. */
4959 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 get_float = TRUE;
4962 p = skipdigits(p + 2);
4963 if (*p == 'e' || *p == 'E')
4964 {
4965 ++p;
4966 if (*p == '-' || *p == '+')
4967 ++p;
4968 if (!vim_isdigit(*p))
4969 get_float = FALSE;
4970 else
4971 p = skipdigits(p + 1);
4972 }
4973 if (ASCII_ISALPHA(*p) || *p == '.')
4974 get_float = FALSE;
4975 }
4976 if (get_float)
4977 {
4978 float_T f;
4979
4980 *arg += string2float(*arg, &f);
4981 if (evaluate)
4982 {
4983 rettv->v_type = VAR_FLOAT;
4984 rettv->vval.v_float = f;
4985 }
4986 }
4987 else
4988#endif
4989 {
4990 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4991 *arg += len;
4992 if (evaluate)
4993 {
4994 rettv->v_type = VAR_NUMBER;
4995 rettv->vval.v_number = n;
4996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000
5001 /*
5002 * String constant: "string".
5003 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 break;
5006
5007 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005008 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005010 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 break;
5012
5013 /*
5014 * List: [expr, expr]
5015 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005016 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 break;
5018
5019 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 * Dictionary: {key: val, key: val}
5021 */
5022 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5023 break;
5024
5025 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005026 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005028 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 break;
5030
5031 /*
5032 * Environment variable: $VAR.
5033 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005034 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 break;
5036
5037 /*
5038 * Register contents: @r.
5039 */
5040 case '@': ++*arg;
5041 if (evaluate)
5042 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005043 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005044 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046 if (**arg != NUL)
5047 ++*arg;
5048 break;
5049
5050 /*
5051 * nested expression: (expression).
5052 */
5053 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (**arg == ')')
5056 ++*arg;
5057 else if (ret == OK)
5058 {
5059 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005060 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 ret = FAIL;
5062 }
5063 break;
5064
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 break;
5067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068
5069 if (ret == NOTDONE)
5070 {
5071 /*
5072 * Must be a variable or function name.
5073 * Can also be a curly-braces kind of name: {expr}.
5074 */
5075 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005076 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 if (alias != NULL)
5078 s = alias;
5079
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005080 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 ret = FAIL;
5082 else
5083 {
5084 if (**arg == '(') /* recursive! */
5085 {
5086 /* If "s" is the name of a variable of type VAR_FUNC
5087 * use its contents. */
5088 s = deref_func_name(s, &len);
5089
5090 /* Invoke the function. */
5091 ret = get_func_tv(s, len, rettv, arg,
5092 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005093 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 /* Stop the expression evaluation when immediately
5095 * aborting on error, or when an interrupt occurred or
5096 * an exception was thrown but not caught. */
5097 if (aborting())
5098 {
5099 if (ret == OK)
5100 clear_tv(rettv);
5101 ret = FAIL;
5102 }
5103 }
5104 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005105 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005106 else
5107 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 }
5109
5110 if (alias != NULL)
5111 vim_free(alias);
5112 }
5113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 *arg = skipwhite(*arg);
5115
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005116 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5117 * expr(expr). */
5118 if (ret == OK)
5119 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
5121 /*
5122 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5123 */
5124 if (ret == OK && evaluate && end_leader > start_leader)
5125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005127 int val = 0;
5128#ifdef FEAT_FLOAT
5129 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005130
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005131 if (rettv->v_type == VAR_FLOAT)
5132 f = rettv->vval.v_float;
5133 else
5134#endif
5135 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005136 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005138 clear_tv(rettv);
5139 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005141 else
5142 {
5143 while (end_leader > start_leader)
5144 {
5145 --end_leader;
5146 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005147 {
5148#ifdef FEAT_FLOAT
5149 if (rettv->v_type == VAR_FLOAT)
5150 f = !f;
5151 else
5152#endif
5153 val = !val;
5154 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005155 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005156 {
5157#ifdef FEAT_FLOAT
5158 if (rettv->v_type == VAR_FLOAT)
5159 f = -f;
5160 else
5161#endif
5162 val = -val;
5163 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005164 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005165#ifdef FEAT_FLOAT
5166 if (rettv->v_type == VAR_FLOAT)
5167 {
5168 clear_tv(rettv);
5169 rettv->vval.v_float = f;
5170 }
5171 else
5172#endif
5173 {
5174 clear_tv(rettv);
5175 rettv->v_type = VAR_NUMBER;
5176 rettv->vval.v_number = val;
5177 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180
5181 return ret;
5182}
5183
5184/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005185 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5186 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5188 */
5189 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005190eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005192 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005194 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195{
5196 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005197 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005198 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 long len = -1;
5200 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005204 if (rettv->v_type == VAR_FUNC
5205#ifdef FEAT_FLOAT
5206 || rettv->v_type == VAR_FLOAT
5207#endif
5208 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005209 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005210 if (verbose)
5211 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005212 return FAIL;
5213 }
5214
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005216 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 /*
5218 * dict.name
5219 */
5220 key = *arg + 1;
5221 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5222 ;
5223 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226 }
5227 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 /*
5230 * something[idx]
5231 *
5232 * Get the (first) variable from inside the [].
5233 */
5234 *arg = skipwhite(*arg + 1);
5235 if (**arg == ':')
5236 empty1 = TRUE;
5237 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5238 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005239 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5240 {
5241 /* not a number or string */
5242 clear_tv(&var1);
5243 return FAIL;
5244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245
5246 /*
5247 * Get the second variable from inside the [:].
5248 */
5249 if (**arg == ':')
5250 {
5251 range = TRUE;
5252 *arg = skipwhite(*arg + 1);
5253 if (**arg == ']')
5254 empty2 = TRUE;
5255 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005257 if (!empty1)
5258 clear_tv(&var1);
5259 return FAIL;
5260 }
5261 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5262 {
5263 /* not a number or string */
5264 if (!empty1)
5265 clear_tv(&var1);
5266 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 return FAIL;
5268 }
5269 }
5270
5271 /* Check for the ']'. */
5272 if (**arg != ']')
5273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 if (verbose)
5275 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 clear_tv(&var1);
5277 if (range)
5278 clear_tv(&var2);
5279 return FAIL;
5280 }
5281 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 }
5283
5284 if (evaluate)
5285 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 n1 = 0;
5287 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005289 n1 = get_tv_number(&var1);
5290 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 }
5292 if (range)
5293 {
5294 if (empty2)
5295 n2 = -1;
5296 else
5297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005298 n2 = get_tv_number(&var2);
5299 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 }
5301 }
5302
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005303 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 {
5305 case VAR_NUMBER:
5306 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005307 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 len = (long)STRLEN(s);
5309 if (range)
5310 {
5311 /* The resulting variable is a substring. If the indexes
5312 * are out of range the result is empty. */
5313 if (n1 < 0)
5314 {
5315 n1 = len + n1;
5316 if (n1 < 0)
5317 n1 = 0;
5318 }
5319 if (n2 < 0)
5320 n2 = len + n2;
5321 else if (n2 >= len)
5322 n2 = len;
5323 if (n1 >= len || n2 < 0 || n1 > n2)
5324 s = NULL;
5325 else
5326 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5327 }
5328 else
5329 {
5330 /* The resulting variable is a string of a single
5331 * character. If the index is too big or negative the
5332 * result is empty. */
5333 if (n1 >= len || n1 < 0)
5334 s = NULL;
5335 else
5336 s = vim_strnsave(s + n1, 1);
5337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005338 clear_tv(rettv);
5339 rettv->v_type = VAR_STRING;
5340 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 break;
5342
5343 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005344 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 if (n1 < 0)
5346 n1 = len + n1;
5347 if (!empty1 && (n1 < 0 || n1 >= len))
5348 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005349 /* For a range we allow invalid values and return an empty
5350 * list. A list index out of range is an error. */
5351 if (!range)
5352 {
5353 if (verbose)
5354 EMSGN(_(e_listidx), n1);
5355 return FAIL;
5356 }
5357 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 }
5359 if (range)
5360 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005361 list_T *l;
5362 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363
5364 if (n2 < 0)
5365 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005366 else if (n2 >= len)
5367 n2 = len - 1;
5368 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005369 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 l = list_alloc();
5371 if (l == NULL)
5372 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005373 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 n1 <= n2; ++n1)
5375 {
5376 if (list_append_tv(l, &item->li_tv) == FAIL)
5377 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005378 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 return FAIL;
5380 }
5381 item = item->li_next;
5382 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005383 clear_tv(rettv);
5384 rettv->v_type = VAR_LIST;
5385 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005386 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 }
5388 else
5389 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005390 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005391 clear_tv(rettv);
5392 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 }
5394 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395
5396 case VAR_DICT:
5397 if (range)
5398 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005399 if (verbose)
5400 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401 if (len == -1)
5402 clear_tv(&var1);
5403 return FAIL;
5404 }
5405 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005406 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407
5408 if (len == -1)
5409 {
5410 key = get_tv_string(&var1);
5411 if (*key == NUL)
5412 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005413 if (verbose)
5414 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415 clear_tv(&var1);
5416 return FAIL;
5417 }
5418 }
5419
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005420 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005421
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005422 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005423 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424 if (len == -1)
5425 clear_tv(&var1);
5426 if (item == NULL)
5427 return FAIL;
5428
5429 copy_tv(&item->di_tv, &var1);
5430 clear_tv(rettv);
5431 *rettv = var1;
5432 }
5433 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 }
5435 }
5436
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 return OK;
5438}
5439
5440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 * Get an option value.
5442 * "arg" points to the '&' or '+' before the option name.
5443 * "arg" is advanced to character after the option name.
5444 * Return OK or FAIL.
5445 */
5446 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005449 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 int evaluate;
5451{
5452 char_u *option_end;
5453 long numval;
5454 char_u *stringval;
5455 int opt_type;
5456 int c;
5457 int working = (**arg == '+'); /* has("+option") */
5458 int ret = OK;
5459 int opt_flags;
5460
5461 /*
5462 * Isolate the option name and find its value.
5463 */
5464 option_end = find_option_end(arg, &opt_flags);
5465 if (option_end == NULL)
5466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 EMSG2(_("E112: Option name missing: %s"), *arg);
5469 return FAIL;
5470 }
5471
5472 if (!evaluate)
5473 {
5474 *arg = option_end;
5475 return OK;
5476 }
5477
5478 c = *option_end;
5479 *option_end = NUL;
5480 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483 if (opt_type == -3) /* invalid name */
5484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 EMSG2(_("E113: Unknown option: %s"), *arg);
5487 ret = FAIL;
5488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005489 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 {
5491 if (opt_type == -2) /* hidden string option */
5492 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493 rettv->v_type = VAR_STRING;
5494 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 }
5496 else if (opt_type == -1) /* hidden number option */
5497 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005498 rettv->v_type = VAR_NUMBER;
5499 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 }
5501 else if (opt_type == 1) /* number option */
5502 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 rettv->v_type = VAR_NUMBER;
5504 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 }
5506 else /* string option */
5507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508 rettv->v_type = VAR_STRING;
5509 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 }
5511 }
5512 else if (working && (opt_type == -2 || opt_type == -1))
5513 ret = FAIL;
5514
5515 *option_end = c; /* put back for error messages */
5516 *arg = option_end;
5517
5518 return ret;
5519}
5520
5521/*
5522 * Allocate a variable for a string constant.
5523 * Return OK or FAIL.
5524 */
5525 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 int evaluate;
5530{
5531 char_u *p;
5532 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 int extra = 0;
5534
5535 /*
5536 * Find the end of the string, skipping backslashed characters.
5537 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005538 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
5540 if (*p == '\\' && p[1] != NUL)
5541 {
5542 ++p;
5543 /* A "\<x>" form occupies at least 4 characters, and produces up
5544 * to 6 characters: reserve space for 2 extra */
5545 if (*p == '<')
5546 extra += 2;
5547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
5549
5550 if (*p != '"')
5551 {
5552 EMSG2(_("E114: Missing quote: %s"), *arg);
5553 return FAIL;
5554 }
5555
5556 /* If only parsing, set *arg and return here */
5557 if (!evaluate)
5558 {
5559 *arg = p + 1;
5560 return OK;
5561 }
5562
5563 /*
5564 * Copy the string into allocated memory, handling backslashed
5565 * characters.
5566 */
5567 name = alloc((unsigned)(p - *arg + extra));
5568 if (name == NULL)
5569 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005570 rettv->v_type = VAR_STRING;
5571 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 {
5575 if (*p == '\\')
5576 {
5577 switch (*++p)
5578 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 case 'b': *name++ = BS; ++p; break;
5580 case 'e': *name++ = ESC; ++p; break;
5581 case 'f': *name++ = FF; ++p; break;
5582 case 'n': *name++ = NL; ++p; break;
5583 case 'r': *name++ = CAR; ++p; break;
5584 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
5586 case 'X': /* hex: "\x1", "\x12" */
5587 case 'x':
5588 case 'u': /* Unicode: "\u0023" */
5589 case 'U':
5590 if (vim_isxdigit(p[1]))
5591 {
5592 int n, nr;
5593 int c = toupper(*p);
5594
5595 if (c == 'X')
5596 n = 2;
5597 else
5598 n = 4;
5599 nr = 0;
5600 while (--n >= 0 && vim_isxdigit(p[1]))
5601 {
5602 ++p;
5603 nr = (nr << 4) + hex2nr(*p);
5604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606#ifdef FEAT_MBYTE
5607 /* For "\u" store the number according to
5608 * 'encoding'. */
5609 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 else
5612#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005613 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 break;
5616
5617 /* octal: "\1", "\12", "\123" */
5618 case '0':
5619 case '1':
5620 case '2':
5621 case '3':
5622 case '4':
5623 case '5':
5624 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005625 case '7': *name = *p++ - '0';
5626 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005628 *name = (*name << 3) + *p++ - '0';
5629 if (*p >= '0' && *p <= '7')
5630 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005632 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 break;
5634
5635 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 if (extra != 0)
5638 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005639 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 break;
5641 }
5642 /* FALLTHROUGH */
5643
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 break;
5646 }
5647 }
5648 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 *arg = p + 1;
5654
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 return OK;
5656}
5657
5658/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 * Return OK or FAIL.
5661 */
5662 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 int evaluate;
5667{
5668 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005669 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005670 int reduce = 0;
5671
5672 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005674 */
5675 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005680 break;
5681 ++reduce;
5682 ++p;
5683 }
5684 }
5685
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005688 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005689 return FAIL;
5690 }
5691
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005693 if (!evaluate)
5694 {
5695 *arg = p + 1;
5696 return OK;
5697 }
5698
5699 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005701 */
5702 str = alloc((unsigned)((p - *arg) - reduce));
5703 if (str == NULL)
5704 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 rettv->v_type = VAR_STRING;
5706 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005707
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005709 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005711 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005712 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005713 break;
5714 ++p;
5715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719 *arg = p + 1;
5720
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 return OK;
5722}
5723
5724/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725 * Allocate a variable for a List and fill it from "*arg".
5726 * Return OK or FAIL.
5727 */
5728 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005729get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005731 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732 int evaluate;
5733{
Bram Moolenaar33570922005-01-25 22:26:29 +00005734 list_T *l = NULL;
5735 typval_T tv;
5736 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737
5738 if (evaluate)
5739 {
5740 l = list_alloc();
5741 if (l == NULL)
5742 return FAIL;
5743 }
5744
5745 *arg = skipwhite(*arg + 1);
5746 while (**arg != ']' && **arg != NUL)
5747 {
5748 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5749 goto failret;
5750 if (evaluate)
5751 {
5752 item = listitem_alloc();
5753 if (item != NULL)
5754 {
5755 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005756 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005757 list_append(l, item);
5758 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005759 else
5760 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761 }
5762
5763 if (**arg == ']')
5764 break;
5765 if (**arg != ',')
5766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768 goto failret;
5769 }
5770 *arg = skipwhite(*arg + 1);
5771 }
5772
5773 if (**arg != ']')
5774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776failret:
5777 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005778 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 return FAIL;
5780 }
5781
5782 *arg = skipwhite(*arg + 1);
5783 if (evaluate)
5784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005785 rettv->v_type = VAR_LIST;
5786 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005787 ++l->lv_refcount;
5788 }
5789
5790 return OK;
5791}
5792
5793/*
5794 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005795 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005797 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798list_alloc()
5799{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005800 list_T *l;
5801
5802 l = (list_T *)alloc_clear(sizeof(list_T));
5803 if (l != NULL)
5804 {
5805 /* Prepend the list to the list of lists for garbage collection. */
5806 if (first_list != NULL)
5807 first_list->lv_used_prev = l;
5808 l->lv_used_prev = NULL;
5809 l->lv_used_next = first_list;
5810 first_list = l;
5811 }
5812 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813}
5814
5815/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005816 * Allocate an empty list for a return value.
5817 * Returns OK or FAIL.
5818 */
5819 static int
5820rettv_list_alloc(rettv)
5821 typval_T *rettv;
5822{
5823 list_T *l = list_alloc();
5824
5825 if (l == NULL)
5826 return FAIL;
5827
5828 rettv->vval.v_list = l;
5829 rettv->v_type = VAR_LIST;
5830 ++l->lv_refcount;
5831 return OK;
5832}
5833
5834/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 * Unreference a list: decrement the reference count and free it when it
5836 * becomes zero.
5837 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005838 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005839list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005840 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005842 if (l != NULL && --l->lv_refcount <= 0)
5843 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844}
5845
5846/*
5847 * Free a list, including all items it points to.
5848 * Ignores the reference count.
5849 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005850 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005851list_free(l, recurse)
5852 list_T *l;
5853 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854{
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005857 /* Remove the list from the list of lists for garbage collection. */
5858 if (l->lv_used_prev == NULL)
5859 first_list = l->lv_used_next;
5860 else
5861 l->lv_used_prev->lv_used_next = l->lv_used_next;
5862 if (l->lv_used_next != NULL)
5863 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5864
Bram Moolenaard9fba312005-06-26 22:34:35 +00005865 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005866 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005867 /* Remove the item before deleting it. */
5868 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005869 if (recurse || (item->li_tv.v_type != VAR_LIST
5870 && item->li_tv.v_type != VAR_DICT))
5871 clear_tv(&item->li_tv);
5872 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 }
5874 vim_free(l);
5875}
5876
5877/*
5878 * Allocate a list item.
5879 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005880 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881listitem_alloc()
5882{
Bram Moolenaar33570922005-01-25 22:26:29 +00005883 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884}
5885
5886/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005887 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 */
5889 static void
5890listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005891 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005893 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 vim_free(item);
5895}
5896
5897/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005898 * Remove a list item from a List and free it. Also clears the value.
5899 */
5900 static void
5901listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 list_T *l;
5903 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005904{
5905 list_remove(l, item, item);
5906 listitem_free(item);
5907}
5908
5909/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 * Get the number of items in a list.
5911 */
5912 static long
5913list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 if (l == NULL)
5917 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005918 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919}
5920
5921/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005922 * Return TRUE when two lists have exactly the same values.
5923 */
5924 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005925list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l1;
5927 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005929 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005930{
Bram Moolenaar33570922005-01-25 22:26:29 +00005931 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005932
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005933 if (l1 == NULL || l2 == NULL)
5934 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005935 if (l1 == l2)
5936 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005937 if (list_len(l1) != list_len(l2))
5938 return FALSE;
5939
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005940 for (item1 = l1->lv_first, item2 = l2->lv_first;
5941 item1 != NULL && item2 != NULL;
5942 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005943 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005944 return FALSE;
5945 return item1 == NULL && item2 == NULL;
5946}
5947
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005948#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5949 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005950/*
5951 * Return the dictitem that an entry in a hashtable points to.
5952 */
5953 dictitem_T *
5954dict_lookup(hi)
5955 hashitem_T *hi;
5956{
5957 return HI2DI(hi);
5958}
5959#endif
5960
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005961/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005962 * Return TRUE when two dictionaries have exactly the same key/values.
5963 */
5964 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005965dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 dict_T *d1;
5967 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005968 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005969 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005970{
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 hashitem_T *hi;
5972 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005973 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005974
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005975 if (d1 == NULL || d2 == NULL)
5976 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005977 if (d1 == d2)
5978 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005979 if (dict_len(d1) != dict_len(d2))
5980 return FALSE;
5981
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005982 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005984 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005985 if (!HASHITEM_EMPTY(hi))
5986 {
5987 item2 = dict_find(d2, hi->hi_key, -1);
5988 if (item2 == NULL)
5989 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005990 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005991 return FALSE;
5992 --todo;
5993 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005994 }
5995 return TRUE;
5996}
5997
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005998static int tv_equal_recurse_limit;
5999
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006000/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006001 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006002 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006003 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006004 */
6005 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006006tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 typval_T *tv1;
6008 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006009 int ic; /* ignore case */
6010 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011{
6012 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006013 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006014 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006015 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006017 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006018 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006019
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006020 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006021 * recursiveness to a limit. We guess they are equal then.
6022 * A fixed limit has the problem of still taking an awful long time.
6023 * Reduce the limit every time running into it. That should work fine for
6024 * deeply linked structures that are not recursively linked and catch
6025 * recursiveness quickly. */
6026 if (!recursive)
6027 tv_equal_recurse_limit = 1000;
6028 if (recursive_cnt >= tv_equal_recurse_limit)
6029 {
6030 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006031 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006033
6034 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006035 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006036 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006037 ++recursive_cnt;
6038 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6039 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006040 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006041
6042 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006043 ++recursive_cnt;
6044 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6045 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006046 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006047
6048 case VAR_FUNC:
6049 return (tv1->vval.v_string != NULL
6050 && tv2->vval.v_string != NULL
6051 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6052
6053 case VAR_NUMBER:
6054 return tv1->vval.v_number == tv2->vval.v_number;
6055
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006056#ifdef FEAT_FLOAT
6057 case VAR_FLOAT:
6058 return tv1->vval.v_float == tv2->vval.v_float;
6059#endif
6060
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006061 case VAR_STRING:
6062 s1 = get_tv_string_buf(tv1, buf1);
6063 s2 = get_tv_string_buf(tv2, buf2);
6064 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006065 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006066
6067 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006068 return TRUE;
6069}
6070
6071/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 * Locate item with index "n" in list "l" and return it.
6073 * A negative index is counted from the end; -1 is the last item.
6074 * Returns NULL when "n" is out of range.
6075 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079 long n;
6080{
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082 long idx;
6083
6084 if (l == NULL)
6085 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006086
6087 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006089 n = l->lv_len + n;
6090
6091 /* Check for index out of range. */
6092 if (n < 0 || n >= l->lv_len)
6093 return NULL;
6094
6095 /* When there is a cached index may start search from there. */
6096 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006098 if (n < l->lv_idx / 2)
6099 {
6100 /* closest to the start of the list */
6101 item = l->lv_first;
6102 idx = 0;
6103 }
6104 else if (n > (l->lv_idx + l->lv_len) / 2)
6105 {
6106 /* closest to the end of the list */
6107 item = l->lv_last;
6108 idx = l->lv_len - 1;
6109 }
6110 else
6111 {
6112 /* closest to the cached index */
6113 item = l->lv_idx_item;
6114 idx = l->lv_idx;
6115 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116 }
6117 else
6118 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006119 if (n < l->lv_len / 2)
6120 {
6121 /* closest to the start of the list */
6122 item = l->lv_first;
6123 idx = 0;
6124 }
6125 else
6126 {
6127 /* closest to the end of the list */
6128 item = l->lv_last;
6129 idx = l->lv_len - 1;
6130 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006131 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006132
6133 while (n > idx)
6134 {
6135 /* search forward */
6136 item = item->li_next;
6137 ++idx;
6138 }
6139 while (n < idx)
6140 {
6141 /* search backward */
6142 item = item->li_prev;
6143 --idx;
6144 }
6145
6146 /* cache the used index */
6147 l->lv_idx = idx;
6148 l->lv_idx_item = item;
6149
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 return item;
6151}
6152
6153/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006154 * Get list item "l[idx]" as a number.
6155 */
6156 static long
6157list_find_nr(l, idx, errorp)
6158 list_T *l;
6159 long idx;
6160 int *errorp; /* set to TRUE when something wrong */
6161{
6162 listitem_T *li;
6163
6164 li = list_find(l, idx);
6165 if (li == NULL)
6166 {
6167 if (errorp != NULL)
6168 *errorp = TRUE;
6169 return -1L;
6170 }
6171 return get_tv_number_chk(&li->li_tv, errorp);
6172}
6173
6174/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006175 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6176 */
6177 char_u *
6178list_find_str(l, idx)
6179 list_T *l;
6180 long idx;
6181{
6182 listitem_T *li;
6183
6184 li = list_find(l, idx - 1);
6185 if (li == NULL)
6186 {
6187 EMSGN(_(e_listidx), idx);
6188 return NULL;
6189 }
6190 return get_tv_string(&li->li_tv);
6191}
6192
6193/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006194 * Locate "item" list "l" and return its index.
6195 * Returns -1 when "item" is not in the list.
6196 */
6197 static long
6198list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006199 list_T *l;
6200 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006201{
6202 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006203 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006204
6205 if (l == NULL)
6206 return -1;
6207 idx = 0;
6208 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6209 ++idx;
6210 if (li == NULL)
6211 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006212 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006213}
6214
6215/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006216 * Append item "item" to the end of list "l".
6217 */
6218 static void
6219list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006220 list_T *l;
6221 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006222{
6223 if (l->lv_last == NULL)
6224 {
6225 /* empty list */
6226 l->lv_first = item;
6227 l->lv_last = item;
6228 item->li_prev = NULL;
6229 }
6230 else
6231 {
6232 l->lv_last->li_next = item;
6233 item->li_prev = l->lv_last;
6234 l->lv_last = item;
6235 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006236 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006237 item->li_next = NULL;
6238}
6239
6240/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006243 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006244 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006245list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006246 list_T *l;
6247 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006249 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006250
Bram Moolenaar05159a02005-02-26 23:04:13 +00006251 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006253 copy_tv(tv, &li->li_tv);
6254 list_append(l, li);
6255 return OK;
6256}
6257
6258/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006259 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006260 * Return FAIL when out of memory.
6261 */
6262 int
6263list_append_dict(list, dict)
6264 list_T *list;
6265 dict_T *dict;
6266{
6267 listitem_T *li = listitem_alloc();
6268
6269 if (li == NULL)
6270 return FAIL;
6271 li->li_tv.v_type = VAR_DICT;
6272 li->li_tv.v_lock = 0;
6273 li->li_tv.vval.v_dict = dict;
6274 list_append(list, li);
6275 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006276 return OK;
6277}
6278
6279/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006280 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006281 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006282 * Returns FAIL when out of memory.
6283 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006284 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006285list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006286 list_T *l;
6287 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006288 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006289{
6290 listitem_T *li = listitem_alloc();
6291
6292 if (li == NULL)
6293 return FAIL;
6294 list_append(l, li);
6295 li->li_tv.v_type = VAR_STRING;
6296 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006297 if (str == NULL)
6298 li->li_tv.vval.v_string = NULL;
6299 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006300 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006301 return FAIL;
6302 return OK;
6303}
6304
6305/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006306 * Append "n" to list "l".
6307 * Returns FAIL when out of memory.
6308 */
6309 static int
6310list_append_number(l, n)
6311 list_T *l;
6312 varnumber_T n;
6313{
6314 listitem_T *li;
6315
6316 li = listitem_alloc();
6317 if (li == NULL)
6318 return FAIL;
6319 li->li_tv.v_type = VAR_NUMBER;
6320 li->li_tv.v_lock = 0;
6321 li->li_tv.vval.v_number = n;
6322 list_append(l, li);
6323 return OK;
6324}
6325
6326/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328 * If "item" is NULL append at the end.
6329 * Return FAIL when out of memory.
6330 */
6331 static int
6332list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006333 list_T *l;
6334 typval_T *tv;
6335 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336{
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338
6339 if (ni == NULL)
6340 return FAIL;
6341 copy_tv(tv, &ni->li_tv);
6342 if (item == NULL)
6343 /* Append new item at end of list. */
6344 list_append(l, ni);
6345 else
6346 {
6347 /* Insert new item before existing item. */
6348 ni->li_prev = item->li_prev;
6349 ni->li_next = item;
6350 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006351 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006352 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006353 ++l->lv_idx;
6354 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006355 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006356 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006357 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006358 l->lv_idx_item = NULL;
6359 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006360 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006361 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006362 }
6363 return OK;
6364}
6365
6366/*
6367 * Extend "l1" with "l2".
6368 * If "bef" is NULL append at the end, otherwise insert before this item.
6369 * Returns FAIL when out of memory.
6370 */
6371 static int
6372list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 list_T *l1;
6374 list_T *l2;
6375 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006376{
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006378 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006379
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006380 /* We also quit the loop when we have inserted the original item count of
6381 * the list, avoid a hang when we extend a list with itself. */
6382 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006383 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6384 return FAIL;
6385 return OK;
6386}
6387
6388/*
6389 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6390 * Return FAIL when out of memory.
6391 */
6392 static int
6393list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 list_T *l1;
6395 list_T *l2;
6396 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006397{
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006400 if (l1 == NULL || l2 == NULL)
6401 return FAIL;
6402
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006404 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006405 if (l == NULL)
6406 return FAIL;
6407 tv->v_type = VAR_LIST;
6408 tv->vval.v_list = l;
6409
6410 /* append all items from the second list */
6411 return list_extend(l, l2, NULL);
6412}
6413
6414/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006415 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006417 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 * Returns NULL when out of memory.
6419 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006420 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006421list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006424 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425{
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 list_T *copy;
6427 listitem_T *item;
6428 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429
6430 if (orig == NULL)
6431 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432
6433 copy = list_alloc();
6434 if (copy != NULL)
6435 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006436 if (copyID != 0)
6437 {
6438 /* Do this before adding the items, because one of the items may
6439 * refer back to this list. */
6440 orig->lv_copyID = copyID;
6441 orig->lv_copylist = copy;
6442 }
6443 for (item = orig->lv_first; item != NULL && !got_int;
6444 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445 {
6446 ni = listitem_alloc();
6447 if (ni == NULL)
6448 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006449 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006450 {
6451 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6452 {
6453 vim_free(ni);
6454 break;
6455 }
6456 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006458 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459 list_append(copy, ni);
6460 }
6461 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006462 if (item != NULL)
6463 {
6464 list_unref(copy);
6465 copy = NULL;
6466 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006467 }
6468
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469 return copy;
6470}
6471
6472/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006473 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006474 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006475 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006477list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 list_T *l;
6479 listitem_T *item;
6480 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006481{
Bram Moolenaar33570922005-01-25 22:26:29 +00006482 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484 /* notify watchers */
6485 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006486 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006487 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488 list_fix_watch(l, ip);
6489 if (ip == item2)
6490 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492
6493 if (item2->li_next == NULL)
6494 l->lv_last = item->li_prev;
6495 else
6496 item2->li_next->li_prev = item->li_prev;
6497 if (item->li_prev == NULL)
6498 l->lv_first = item2->li_next;
6499 else
6500 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006501 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502}
6503
6504/*
6505 * Return an allocated string with the string representation of a list.
6506 * May return NULL.
6507 */
6508 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006509list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006510 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006511 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512{
6513 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514
6515 if (tv->vval.v_list == NULL)
6516 return NULL;
6517 ga_init2(&ga, (int)sizeof(char), 80);
6518 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006519 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006520 {
6521 vim_free(ga.ga_data);
6522 return NULL;
6523 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 ga_append(&ga, ']');
6525 ga_append(&ga, NUL);
6526 return (char_u *)ga.ga_data;
6527}
6528
6529/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006530 * Join list "l" into a string in "*gap", using separator "sep".
6531 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006532 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006533 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006534 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006535list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006536 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006537 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006538 char_u *sep;
6539 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006540 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006541{
6542 int first = TRUE;
6543 char_u *tofree;
6544 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006545 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006546 char_u *s;
6547
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006548 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006549 {
6550 if (first)
6551 first = FALSE;
6552 else
6553 ga_concat(gap, sep);
6554
6555 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006556 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006557 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006558 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006559 if (s != NULL)
6560 ga_concat(gap, s);
6561 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562 if (s == NULL)
6563 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006564 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006565 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006566 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006567}
6568
6569/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006570 * Garbage collection for lists and dictionaries.
6571 *
6572 * We use reference counts to be able to free most items right away when they
6573 * are no longer used. But for composite items it's possible that it becomes
6574 * unused while the reference count is > 0: When there is a recursive
6575 * reference. Example:
6576 * :let l = [1, 2, 3]
6577 * :let d = {9: l}
6578 * :let l[1] = d
6579 *
6580 * Since this is quite unusual we handle this with garbage collection: every
6581 * once in a while find out which lists and dicts are not referenced from any
6582 * variable.
6583 *
6584 * Here is a good reference text about garbage collection (refers to Python
6585 * but it applies to all reference-counting mechanisms):
6586 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006587 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006588
6589/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006590 * Do garbage collection for lists and dicts.
6591 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006592 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006593 int
6594garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006595{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006596 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006597 buf_T *buf;
6598 win_T *wp;
6599 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006600 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006601 int did_free;
6602 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006603#ifdef FEAT_WINDOWS
6604 tabpage_T *tp;
6605#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006606
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006607 /* Only do this once. */
6608 want_garbage_collect = FALSE;
6609 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006610 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006611
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006612 /* We advance by two because we add one for items referenced through
6613 * previous_funccal. */
6614 current_copyID += COPYID_INC;
6615 copyID = current_copyID;
6616
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006617 /*
6618 * 1. Go through all accessible variables and mark all lists and dicts
6619 * with copyID.
6620 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006621
6622 /* Don't free variables in the previous_funccal list unless they are only
6623 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006624 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006625 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6626 {
6627 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6628 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6629 }
6630
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006631 /* script-local variables */
6632 for (i = 1; i <= ga_scripts.ga_len; ++i)
6633 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6634
6635 /* buffer-local variables */
6636 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6637 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6638
6639 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006640 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006641 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6642
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006643#ifdef FEAT_WINDOWS
6644 /* tabpage-local variables */
6645 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6646 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6647#endif
6648
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006649 /* global variables */
6650 set_ref_in_ht(&globvarht, copyID);
6651
6652 /* function-local variables */
6653 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6654 {
6655 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6656 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6657 }
6658
Bram Moolenaard812df62008-11-09 12:46:09 +00006659 /* v: vars */
6660 set_ref_in_ht(&vimvarht, copyID);
6661
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006662 /*
6663 * 2. Free lists and dictionaries that are not referenced.
6664 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006665 did_free = free_unref_items(copyID);
6666
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006667 /*
6668 * 3. Check if any funccal can be freed now.
6669 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006670 for (pfc = &previous_funccal; *pfc != NULL; )
6671 {
6672 if (can_free_funccal(*pfc, copyID))
6673 {
6674 fc = *pfc;
6675 *pfc = fc->caller;
6676 free_funccal(fc, TRUE);
6677 did_free = TRUE;
6678 did_free_funccal = TRUE;
6679 }
6680 else
6681 pfc = &(*pfc)->caller;
6682 }
6683 if (did_free_funccal)
6684 /* When a funccal was freed some more items might be garbage
6685 * collected, so run again. */
6686 (void)garbage_collect();
6687
6688 return did_free;
6689}
6690
6691/*
6692 * Free lists and dictionaries that are no longer referenced.
6693 */
6694 static int
6695free_unref_items(copyID)
6696 int copyID;
6697{
6698 dict_T *dd;
6699 list_T *ll;
6700 int did_free = FALSE;
6701
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006702 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006703 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006704 */
6705 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006706 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006707 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006708 /* Free the Dictionary and ordinary items it contains, but don't
6709 * recurse into Lists and Dictionaries, they will be in the list
6710 * of dicts or list of lists. */
6711 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006712 did_free = TRUE;
6713
6714 /* restart, next dict may also have been freed */
6715 dd = first_dict;
6716 }
6717 else
6718 dd = dd->dv_used_next;
6719
6720 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006721 * Go through the list of lists and free items without the copyID.
6722 * But don't free a list that has a watcher (used in a for loop), these
6723 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006724 */
6725 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006726 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6727 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006728 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006729 /* Free the List and ordinary items it contains, but don't recurse
6730 * into Lists and Dictionaries, they will be in the list of dicts
6731 * or list of lists. */
6732 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006733 did_free = TRUE;
6734
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006735 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 ll = first_list;
6737 }
6738 else
6739 ll = ll->lv_used_next;
6740
6741 return did_free;
6742}
6743
6744/*
6745 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6746 */
6747 static void
6748set_ref_in_ht(ht, copyID)
6749 hashtab_T *ht;
6750 int copyID;
6751{
6752 int todo;
6753 hashitem_T *hi;
6754
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006755 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006756 for (hi = ht->ht_array; todo > 0; ++hi)
6757 if (!HASHITEM_EMPTY(hi))
6758 {
6759 --todo;
6760 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6761 }
6762}
6763
6764/*
6765 * Mark all lists and dicts referenced through list "l" with "copyID".
6766 */
6767 static void
6768set_ref_in_list(l, copyID)
6769 list_T *l;
6770 int copyID;
6771{
6772 listitem_T *li;
6773
6774 for (li = l->lv_first; li != NULL; li = li->li_next)
6775 set_ref_in_item(&li->li_tv, copyID);
6776}
6777
6778/*
6779 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6780 */
6781 static void
6782set_ref_in_item(tv, copyID)
6783 typval_T *tv;
6784 int copyID;
6785{
6786 dict_T *dd;
6787 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006788
6789 switch (tv->v_type)
6790 {
6791 case VAR_DICT:
6792 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006793 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006794 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006795 /* Didn't see this dict yet. */
6796 dd->dv_copyID = copyID;
6797 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006798 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006799 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006800
6801 case VAR_LIST:
6802 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006803 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006804 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006805 /* Didn't see this list yet. */
6806 ll->lv_copyID = copyID;
6807 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006808 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006809 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006810 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006811 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006812}
6813
6814/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006815 * Allocate an empty header for a dictionary.
6816 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006817 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006818dict_alloc()
6819{
Bram Moolenaar33570922005-01-25 22:26:29 +00006820 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006821
Bram Moolenaar33570922005-01-25 22:26:29 +00006822 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006823 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006824 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006825 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006826 if (first_dict != NULL)
6827 first_dict->dv_used_prev = d;
6828 d->dv_used_next = first_dict;
6829 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006830 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006831
Bram Moolenaar33570922005-01-25 22:26:29 +00006832 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006833 d->dv_lock = 0;
6834 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006835 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006836 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006837 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006838}
6839
6840/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006841 * Allocate an empty dict for a return value.
6842 * Returns OK or FAIL.
6843 */
6844 static int
6845rettv_dict_alloc(rettv)
6846 typval_T *rettv;
6847{
6848 dict_T *d = dict_alloc();
6849
6850 if (d == NULL)
6851 return FAIL;
6852
6853 rettv->vval.v_dict = d;
6854 rettv->v_type = VAR_DICT;
6855 ++d->dv_refcount;
6856 return OK;
6857}
6858
6859
6860/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006861 * Unreference a Dictionary: decrement the reference count and free it when it
6862 * becomes zero.
6863 */
6864 static void
6865dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006866 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006867{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006868 if (d != NULL && --d->dv_refcount <= 0)
6869 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006870}
6871
6872/*
6873 * Free a Dictionary, including all items it contains.
6874 * Ignores the reference count.
6875 */
6876 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006877dict_free(d, recurse)
6878 dict_T *d;
6879 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006880{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006881 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006882 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006883 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006884
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 /* Remove the dict from the list of dicts for garbage collection. */
6886 if (d->dv_used_prev == NULL)
6887 first_dict = d->dv_used_next;
6888 else
6889 d->dv_used_prev->dv_used_next = d->dv_used_next;
6890 if (d->dv_used_next != NULL)
6891 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6892
6893 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006894 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006895 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006896 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006897 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006898 if (!HASHITEM_EMPTY(hi))
6899 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006900 /* Remove the item before deleting it, just in case there is
6901 * something recursive causing trouble. */
6902 di = HI2DI(hi);
6903 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006904 if (recurse || (di->di_tv.v_type != VAR_LIST
6905 && di->di_tv.v_type != VAR_DICT))
6906 clear_tv(&di->di_tv);
6907 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006908 --todo;
6909 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006910 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006911 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006912 vim_free(d);
6913}
6914
6915/*
6916 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006917 * The "key" is copied to the new item.
6918 * Note that the value of the item "di_tv" still needs to be initialized!
6919 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006920 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006921 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006922dictitem_alloc(key)
6923 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006924{
Bram Moolenaar33570922005-01-25 22:26:29 +00006925 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006926
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006927 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006928 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006929 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006930 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006931 di->di_flags = 0;
6932 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006933 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006934}
6935
6936/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006937 * Make a copy of a Dictionary item.
6938 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006939 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006940dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006941 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006942{
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006944
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006945 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6946 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006947 if (di != NULL)
6948 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006949 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006950 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006951 copy_tv(&org->di_tv, &di->di_tv);
6952 }
6953 return di;
6954}
6955
6956/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006957 * Remove item "item" from Dictionary "dict" and free it.
6958 */
6959 static void
6960dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006961 dict_T *dict;
6962 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006963{
Bram Moolenaar33570922005-01-25 22:26:29 +00006964 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006965
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006967 if (HASHITEM_EMPTY(hi))
6968 EMSG2(_(e_intern2), "dictitem_remove()");
6969 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006970 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006971 dictitem_free(item);
6972}
6973
6974/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006975 * Free a dict item. Also clears the value.
6976 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006977 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006978dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006979 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006981 clear_tv(&item->di_tv);
6982 vim_free(item);
6983}
6984
6985/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006986 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6987 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006988 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006989 * Returns NULL when out of memory.
6990 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006992dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006993 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006994 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006995 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006996{
Bram Moolenaar33570922005-01-25 22:26:29 +00006997 dict_T *copy;
6998 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006999 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007000 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007001
7002 if (orig == NULL)
7003 return NULL;
7004
7005 copy = dict_alloc();
7006 if (copy != NULL)
7007 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007008 if (copyID != 0)
7009 {
7010 orig->dv_copyID = copyID;
7011 orig->dv_copydict = copy;
7012 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007013 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007014 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007015 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007016 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007017 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007018 --todo;
7019
7020 di = dictitem_alloc(hi->hi_key);
7021 if (di == NULL)
7022 break;
7023 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007024 {
7025 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7026 copyID) == FAIL)
7027 {
7028 vim_free(di);
7029 break;
7030 }
7031 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007032 else
7033 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7034 if (dict_add(copy, di) == FAIL)
7035 {
7036 dictitem_free(di);
7037 break;
7038 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007040 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007041
Bram Moolenaare9a41262005-01-15 22:18:47 +00007042 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007043 if (todo > 0)
7044 {
7045 dict_unref(copy);
7046 copy = NULL;
7047 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007048 }
7049
7050 return copy;
7051}
7052
7053/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007054 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007055 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007056 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007057 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007058dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007059 dict_T *d;
7060 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061{
Bram Moolenaar33570922005-01-25 22:26:29 +00007062 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063}
7064
Bram Moolenaar8c711452005-01-14 21:53:12 +00007065/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007066 * Add a number or string entry to dictionary "d".
7067 * When "str" is NULL use number "nr", otherwise use "str".
7068 * Returns FAIL when out of memory and when key already exists.
7069 */
7070 int
7071dict_add_nr_str(d, key, nr, str)
7072 dict_T *d;
7073 char *key;
7074 long nr;
7075 char_u *str;
7076{
7077 dictitem_T *item;
7078
7079 item = dictitem_alloc((char_u *)key);
7080 if (item == NULL)
7081 return FAIL;
7082 item->di_tv.v_lock = 0;
7083 if (str == NULL)
7084 {
7085 item->di_tv.v_type = VAR_NUMBER;
7086 item->di_tv.vval.v_number = nr;
7087 }
7088 else
7089 {
7090 item->di_tv.v_type = VAR_STRING;
7091 item->di_tv.vval.v_string = vim_strsave(str);
7092 }
7093 if (dict_add(d, item) == FAIL)
7094 {
7095 dictitem_free(item);
7096 return FAIL;
7097 }
7098 return OK;
7099}
7100
7101/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007102 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007103 * Returns FAIL when out of memory and when key already exists.
7104 */
7105 int
7106dict_add_list(d, key, list)
7107 dict_T *d;
7108 char *key;
7109 list_T *list;
7110{
7111 dictitem_T *item;
7112
7113 item = dictitem_alloc((char_u *)key);
7114 if (item == NULL)
7115 return FAIL;
7116 item->di_tv.v_lock = 0;
7117 item->di_tv.v_type = VAR_LIST;
7118 item->di_tv.vval.v_list = list;
7119 if (dict_add(d, item) == FAIL)
7120 {
7121 dictitem_free(item);
7122 return FAIL;
7123 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007124 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007125 return OK;
7126}
7127
7128/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007129 * Get the number of items in a Dictionary.
7130 */
7131 static long
7132dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007134{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007135 if (d == NULL)
7136 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007137 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138}
7139
7140/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141 * Find item "key[len]" in Dictionary "d".
7142 * If "len" is negative use strlen(key).
7143 * Returns NULL when not found.
7144 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007145 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007146dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007148 char_u *key;
7149 int len;
7150{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151#define AKEYLEN 200
7152 char_u buf[AKEYLEN];
7153 char_u *akey;
7154 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007156
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157 if (len < 0)
7158 akey = key;
7159 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007160 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161 tofree = akey = vim_strnsave(key, len);
7162 if (akey == NULL)
7163 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007164 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007165 else
7166 {
7167 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007168 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007169 akey = buf;
7170 }
7171
Bram Moolenaar33570922005-01-25 22:26:29 +00007172 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007173 vim_free(tofree);
7174 if (HASHITEM_EMPTY(hi))
7175 return NULL;
7176 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177}
7178
7179/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007180 * Get a string item from a dictionary.
7181 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007182 * Returns NULL if the entry doesn't exist or out of memory.
7183 */
7184 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007185get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007186 dict_T *d;
7187 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007188 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007189{
7190 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007191 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007192
7193 di = dict_find(d, key, -1);
7194 if (di == NULL)
7195 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007196 s = get_tv_string(&di->di_tv);
7197 if (save && s != NULL)
7198 s = vim_strsave(s);
7199 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007200}
7201
7202/*
7203 * Get a number item from a dictionary.
7204 * Returns 0 if the entry doesn't exist or out of memory.
7205 */
7206 long
7207get_dict_number(d, key)
7208 dict_T *d;
7209 char_u *key;
7210{
7211 dictitem_T *di;
7212
7213 di = dict_find(d, key, -1);
7214 if (di == NULL)
7215 return 0;
7216 return get_tv_number(&di->di_tv);
7217}
7218
7219/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 * Return an allocated string with the string representation of a Dictionary.
7221 * May return NULL.
7222 */
7223 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007224dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007226 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227{
7228 garray_T ga;
7229 int first = TRUE;
7230 char_u *tofree;
7231 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007232 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007235 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007237 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238 return NULL;
7239 ga_init2(&ga, (int)sizeof(char), 80);
7240 ga_append(&ga, '{');
7241
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007242 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007243 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007245 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 --todo;
7248
7249 if (first)
7250 first = FALSE;
7251 else
7252 ga_concat(&ga, (char_u *)", ");
7253
7254 tofree = string_quote(hi->hi_key, FALSE);
7255 if (tofree != NULL)
7256 {
7257 ga_concat(&ga, tofree);
7258 vim_free(tofree);
7259 }
7260 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007261 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007262 if (s != NULL)
7263 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007265 if (s == NULL)
7266 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007269 if (todo > 0)
7270 {
7271 vim_free(ga.ga_data);
7272 return NULL;
7273 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274
7275 ga_append(&ga, '}');
7276 ga_append(&ga, NUL);
7277 return (char_u *)ga.ga_data;
7278}
7279
7280/*
7281 * Allocate a variable for a Dictionary and fill it from "*arg".
7282 * Return OK or FAIL. Returns NOTDONE for {expr}.
7283 */
7284 static int
7285get_dict_tv(arg, rettv, evaluate)
7286 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 int evaluate;
7289{
Bram Moolenaar33570922005-01-25 22:26:29 +00007290 dict_T *d = NULL;
7291 typval_T tvkey;
7292 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007293 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007294 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297
7298 /*
7299 * First check if it's not a curly-braces thing: {expr}.
7300 * Must do this without evaluating, otherwise a function may be called
7301 * twice. Unfortunately this means we need to call eval1() twice for the
7302 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 if (*start != '}')
7306 {
7307 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7308 return FAIL;
7309 if (*start == '}')
7310 return NOTDONE;
7311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312
7313 if (evaluate)
7314 {
7315 d = dict_alloc();
7316 if (d == NULL)
7317 return FAIL;
7318 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 tvkey.v_type = VAR_UNKNOWN;
7320 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321
7322 *arg = skipwhite(*arg + 1);
7323 while (**arg != '}' && **arg != NUL)
7324 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 goto failret;
7327 if (**arg != ':')
7328 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007329 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 goto failret;
7332 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007333 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007334 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007335 key = get_tv_string_buf_chk(&tvkey, buf);
7336 if (key == NULL || *key == NUL)
7337 {
7338 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7339 if (key != NULL)
7340 EMSG(_(e_emptykey));
7341 clear_tv(&tvkey);
7342 goto failret;
7343 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345
7346 *arg = skipwhite(*arg + 1);
7347 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7348 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007349 if (evaluate)
7350 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351 goto failret;
7352 }
7353 if (evaluate)
7354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356 if (item != NULL)
7357 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007358 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360 clear_tv(&tv);
7361 goto failret;
7362 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 item = dictitem_alloc(key);
7364 clear_tv(&tvkey);
7365 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007368 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 if (dict_add(d, item) == FAIL)
7370 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007371 }
7372 }
7373
7374 if (**arg == '}')
7375 break;
7376 if (**arg != ',')
7377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007378 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379 goto failret;
7380 }
7381 *arg = skipwhite(*arg + 1);
7382 }
7383
7384 if (**arg != '}')
7385 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007386 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387failret:
7388 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007389 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 return FAIL;
7391 }
7392
7393 *arg = skipwhite(*arg + 1);
7394 if (evaluate)
7395 {
7396 rettv->v_type = VAR_DICT;
7397 rettv->vval.v_dict = d;
7398 ++d->dv_refcount;
7399 }
7400
7401 return OK;
7402}
7403
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007405 * Return a string with the string representation of a variable.
7406 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007407 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007408 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007409 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007410 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007411 */
7412 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007413echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007415 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007416 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007417 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007418{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007419 static int recurse = 0;
7420 char_u *r = NULL;
7421
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007424 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 *tofree = NULL;
7426 return NULL;
7427 }
7428 ++recurse;
7429
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007430 switch (tv->v_type)
7431 {
7432 case VAR_FUNC:
7433 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434 r = tv->vval.v_string;
7435 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007436
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007437 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007438 if (tv->vval.v_list == NULL)
7439 {
7440 *tofree = NULL;
7441 r = NULL;
7442 }
7443 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7444 {
7445 *tofree = NULL;
7446 r = (char_u *)"[...]";
7447 }
7448 else
7449 {
7450 tv->vval.v_list->lv_copyID = copyID;
7451 *tofree = list2string(tv, copyID);
7452 r = *tofree;
7453 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007454 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007455
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007457 if (tv->vval.v_dict == NULL)
7458 {
7459 *tofree = NULL;
7460 r = NULL;
7461 }
7462 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7463 {
7464 *tofree = NULL;
7465 r = (char_u *)"{...}";
7466 }
7467 else
7468 {
7469 tv->vval.v_dict->dv_copyID = copyID;
7470 *tofree = dict2string(tv, copyID);
7471 r = *tofree;
7472 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007473 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007475 case VAR_STRING:
7476 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 *tofree = NULL;
7478 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007479 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007480
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007481#ifdef FEAT_FLOAT
7482 case VAR_FLOAT:
7483 *tofree = NULL;
7484 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7485 r = numbuf;
7486 break;
7487#endif
7488
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007489 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007490 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007491 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007492 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493
7494 --recurse;
7495 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007496}
7497
7498/*
7499 * Return a string with the string representation of a variable.
7500 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7501 * "numbuf" is used for a number.
7502 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007503 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007504 */
7505 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007506tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007507 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007508 char_u **tofree;
7509 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007510 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007511{
7512 switch (tv->v_type)
7513 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007514 case VAR_FUNC:
7515 *tofree = string_quote(tv->vval.v_string, TRUE);
7516 return *tofree;
7517 case VAR_STRING:
7518 *tofree = string_quote(tv->vval.v_string, FALSE);
7519 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007520#ifdef FEAT_FLOAT
7521 case VAR_FLOAT:
7522 *tofree = NULL;
7523 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7524 return numbuf;
7525#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007526 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007527 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007529 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007530 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007531 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007532 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007533 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007534}
7535
7536/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007537 * Return string "str" in ' quotes, doubling ' characters.
7538 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007540 */
7541 static char_u *
7542string_quote(str, function)
7543 char_u *str;
7544 int function;
7545{
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007547 char_u *p, *r, *s;
7548
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 len = (function ? 13 : 3);
7550 if (str != NULL)
7551 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007552 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007553 for (p = str; *p != NUL; mb_ptr_adv(p))
7554 if (*p == '\'')
7555 ++len;
7556 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007557 s = r = alloc(len);
7558 if (r != NULL)
7559 {
7560 if (function)
7561 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007563 r += 10;
7564 }
7565 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 if (str != NULL)
7568 for (p = str; *p != NUL; )
7569 {
7570 if (*p == '\'')
7571 *r++ = '\'';
7572 MB_COPY_CHAR(p, r);
7573 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007575 if (function)
7576 *r++ = ')';
7577 *r++ = NUL;
7578 }
7579 return s;
7580}
7581
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007582#ifdef FEAT_FLOAT
7583/*
7584 * Convert the string "text" to a floating point number.
7585 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7586 * this always uses a decimal point.
7587 * Returns the length of the text that was consumed.
7588 */
7589 static int
7590string2float(text, value)
7591 char_u *text;
7592 float_T *value; /* result stored here */
7593{
7594 char *s = (char *)text;
7595 float_T f;
7596
7597 f = strtod(s, &s);
7598 *value = f;
7599 return (int)((char_u *)s - text);
7600}
7601#endif
7602
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 * Get the value of an environment variable.
7605 * "arg" is pointing to the '$'. It is advanced to after the name.
7606 * If the environment variable was not set, silently assume it is empty.
7607 * Always return OK.
7608 */
7609 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007610get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 int evaluate;
7614{
7615 char_u *string = NULL;
7616 int len;
7617 int cc;
7618 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007619 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620
7621 ++*arg;
7622 name = *arg;
7623 len = get_env_len(arg);
7624 if (evaluate)
7625 {
7626 if (len != 0)
7627 {
7628 cc = name[len];
7629 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007630 /* first try vim_getenv(), fast for normal environment vars */
7631 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007633 {
7634 if (!mustfree)
7635 string = vim_strsave(string);
7636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 else
7638 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007639 if (mustfree)
7640 vim_free(string);
7641
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 /* next try expanding things like $VIM and ${HOME} */
7643 string = expand_env_save(name - 1);
7644 if (string != NULL && *string == '$')
7645 {
7646 vim_free(string);
7647 string = NULL;
7648 }
7649 }
7650 name[len] = cc;
7651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652 rettv->v_type = VAR_STRING;
7653 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655
7656 return OK;
7657}
7658
7659/*
7660 * Array with names and number of arguments of all internal functions
7661 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7662 */
7663static struct fst
7664{
7665 char *f_name; /* function name */
7666 char f_min_argc; /* minimal number of arguments */
7667 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007669 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670} functions[] =
7671{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007672#ifdef FEAT_FLOAT
7673 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007674 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007675#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007676 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {"append", 2, 2, f_append},
7678 {"argc", 0, 0, f_argc},
7679 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007680 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007681#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007682 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007683 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007684 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007687 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 {"bufexists", 1, 1, f_bufexists},
7689 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7690 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7691 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7692 {"buflisted", 1, 1, f_buflisted},
7693 {"bufloaded", 1, 1, f_bufloaded},
7694 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007695 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 {"bufwinnr", 1, 1, f_bufwinnr},
7697 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007698 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007699 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007700#ifdef FEAT_FLOAT
7701 {"ceil", 1, 1, f_ceil},
7702#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007703 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 {"char2nr", 1, 1, f_char2nr},
7705 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007706 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007708#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007709 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007710 {"complete_add", 1, 1, f_complete_add},
7711 {"complete_check", 0, 0, f_complete_check},
7712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007714 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007715#ifdef FEAT_FLOAT
7716 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007717 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007718#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007719 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007721 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007722 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {"delete", 1, 1, f_delete},
7724 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007725 {"diff_filler", 1, 1, f_diff_filler},
7726 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007727 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {"eventhandler", 0, 0, f_eventhandler},
7731 {"executable", 1, 1, f_executable},
7732 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007733#ifdef FEAT_FLOAT
7734 {"exp", 1, 1, f_exp},
7735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007737 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007738 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7740 {"filereadable", 1, 1, f_filereadable},
7741 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007742 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007743 {"finddir", 1, 3, f_finddir},
7744 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007745#ifdef FEAT_FLOAT
7746 {"float2nr", 1, 1, f_float2nr},
7747 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007748 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007749#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007750 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {"fnamemodify", 2, 2, f_fnamemodify},
7752 {"foldclosed", 1, 1, f_foldclosed},
7753 {"foldclosedend", 1, 1, f_foldclosedend},
7754 {"foldlevel", 1, 1, f_foldlevel},
7755 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007756 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007758 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007759 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007760 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007761 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {"getbufvar", 2, 2, f_getbufvar},
7763 {"getchar", 0, 1, f_getchar},
7764 {"getcharmod", 0, 0, f_getcharmod},
7765 {"getcmdline", 0, 0, f_getcmdline},
7766 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007767 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007769 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007770 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 {"getfsize", 1, 1, f_getfsize},
7772 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007773 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007774 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007775 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007776 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007777 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007778 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007779 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007780 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007782 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007783 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 {"getwinposx", 0, 0, f_getwinposx},
7785 {"getwinposy", 0, 0, f_getwinposy},
7786 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007787 {"glob", 1, 2, f_glob},
7788 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007790 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007791 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007792 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7794 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7795 {"histadd", 2, 2, f_histadd},
7796 {"histdel", 1, 2, f_histdel},
7797 {"histget", 1, 2, f_histget},
7798 {"histnr", 1, 1, f_histnr},
7799 {"hlID", 1, 1, f_hlID},
7800 {"hlexists", 1, 1, f_hlexists},
7801 {"hostname", 0, 0, f_hostname},
7802 {"iconv", 3, 3, f_iconv},
7803 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007804 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007805 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007807 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 {"inputrestore", 0, 0, f_inputrestore},
7809 {"inputsave", 0, 0, f_inputsave},
7810 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007811 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007813 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007814 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007815 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007816 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007818 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 {"libcall", 3, 3, f_libcall},
7820 {"libcallnr", 3, 3, f_libcallnr},
7821 {"line", 1, 1, f_line},
7822 {"line2byte", 1, 1, f_line2byte},
7823 {"lispindent", 1, 1, f_lispindent},
7824 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007825#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007826 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007827 {"log10", 1, 1, f_log10},
7828#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007829 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007830 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007831 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007832 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007833 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007834 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007835 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007836 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007837 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007838 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007839 {"max", 1, 1, f_max},
7840 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007841#ifdef vim_mkdir
7842 {"mkdir", 1, 3, f_mkdir},
7843#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007844 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007845#ifdef FEAT_MZSCHEME
7846 {"mzeval", 1, 1, f_mzeval},
7847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {"nextnonblank", 1, 1, f_nextnonblank},
7849 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007850 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007851#ifdef FEAT_FLOAT
7852 {"pow", 2, 2, f_pow},
7853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007855 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007856 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007857 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007858 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007859 {"reltime", 0, 2, f_reltime},
7860 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {"remote_expr", 2, 3, f_remote_expr},
7862 {"remote_foreground", 1, 1, f_remote_foreground},
7863 {"remote_peek", 1, 2, f_remote_peek},
7864 {"remote_read", 1, 1, f_remote_read},
7865 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007866 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007868 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007870 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#ifdef FEAT_FLOAT
7872 {"round", 1, 1, f_round},
7873#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007874 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007875 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007876 {"searchpair", 3, 7, f_searchpair},
7877 {"searchpairpos", 3, 7, f_searchpairpos},
7878 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {"server2client", 2, 2, f_server2client},
7880 {"serverlist", 0, 0, f_serverlist},
7881 {"setbufvar", 3, 3, f_setbufvar},
7882 {"setcmdpos", 1, 1, f_setcmdpos},
7883 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007884 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007885 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007886 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007887 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007889 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007890 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007892 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007894#ifdef FEAT_FLOAT
7895 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007896 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007897#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007898 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007899 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007900 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007901 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007902 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007903#ifdef FEAT_FLOAT
7904 {"sqrt", 1, 1, f_sqrt},
7905 {"str2float", 1, 1, f_str2float},
7906#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007907 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007908 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007909 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910#ifdef HAVE_STRFTIME
7911 {"strftime", 1, 2, f_strftime},
7912#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007914 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"strlen", 1, 1, f_strlen},
7916 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007917 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007919 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {"submatch", 1, 1, f_submatch},
7921 {"substitute", 4, 4, f_substitute},
7922 {"synID", 3, 3, f_synID},
7923 {"synIDattr", 2, 3, f_synIDattr},
7924 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007925 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007926 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007927 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007928 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007929 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007930 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007931 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007932 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007933#ifdef FEAT_FLOAT
7934 {"tan", 1, 1, f_tan},
7935 {"tanh", 1, 1, f_tanh},
7936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007938 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"tolower", 1, 1, f_tolower},
7940 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007941 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007942#ifdef FEAT_FLOAT
7943 {"trunc", 1, 1, f_trunc},
7944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007946 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007947 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007948 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"virtcol", 1, 1, f_virtcol},
7950 {"visualmode", 0, 1, f_visualmode},
7951 {"winbufnr", 1, 1, f_winbufnr},
7952 {"wincol", 0, 0, f_wincol},
7953 {"winheight", 1, 1, f_winheight},
7954 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007955 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007957 {"winrestview", 1, 1, f_winrestview},
7958 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007960 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961};
7962
7963#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7964
7965/*
7966 * Function given to ExpandGeneric() to obtain the list of internal
7967 * or user defined function names.
7968 */
7969 char_u *
7970get_function_name(xp, idx)
7971 expand_T *xp;
7972 int idx;
7973{
7974 static int intidx = -1;
7975 char_u *name;
7976
7977 if (idx == 0)
7978 intidx = -1;
7979 if (intidx < 0)
7980 {
7981 name = get_user_func_name(xp, idx);
7982 if (name != NULL)
7983 return name;
7984 }
7985 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7986 {
7987 STRCPY(IObuff, functions[intidx].f_name);
7988 STRCAT(IObuff, "(");
7989 if (functions[intidx].f_max_argc == 0)
7990 STRCAT(IObuff, ")");
7991 return IObuff;
7992 }
7993
7994 return NULL;
7995}
7996
7997/*
7998 * Function given to ExpandGeneric() to obtain the list of internal or
7999 * user defined variable or function names.
8000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 char_u *
8002get_expr_name(xp, idx)
8003 expand_T *xp;
8004 int idx;
8005{
8006 static int intidx = -1;
8007 char_u *name;
8008
8009 if (idx == 0)
8010 intidx = -1;
8011 if (intidx < 0)
8012 {
8013 name = get_function_name(xp, idx);
8014 if (name != NULL)
8015 return name;
8016 }
8017 return get_user_var_name(xp, ++intidx);
8018}
8019
8020#endif /* FEAT_CMDL_COMPL */
8021
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008022#if defined(EBCDIC) || defined(PROTO)
8023/*
8024 * Compare struct fst by function name.
8025 */
8026 static int
8027compare_func_name(s1, s2)
8028 const void *s1;
8029 const void *s2;
8030{
8031 struct fst *p1 = (struct fst *)s1;
8032 struct fst *p2 = (struct fst *)s2;
8033
8034 return STRCMP(p1->f_name, p2->f_name);
8035}
8036
8037/*
8038 * Sort the function table by function name.
8039 * The sorting of the table above is ASCII dependant.
8040 * On machines using EBCDIC we have to sort it.
8041 */
8042 static void
8043sortFunctions()
8044{
8045 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8046
8047 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8048}
8049#endif
8050
8051
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052/*
8053 * Find internal function in table above.
8054 * Return index, or -1 if not found
8055 */
8056 static int
8057find_internal_func(name)
8058 char_u *name; /* name of the function */
8059{
8060 int first = 0;
8061 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8062 int cmp;
8063 int x;
8064
8065 /*
8066 * Find the function name in the table. Binary search.
8067 */
8068 while (first <= last)
8069 {
8070 x = first + ((unsigned)(last - first) >> 1);
8071 cmp = STRCMP(name, functions[x].f_name);
8072 if (cmp < 0)
8073 last = x - 1;
8074 else if (cmp > 0)
8075 first = x + 1;
8076 else
8077 return x;
8078 }
8079 return -1;
8080}
8081
8082/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008083 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8084 * name it contains, otherwise return "name".
8085 */
8086 static char_u *
8087deref_func_name(name, lenp)
8088 char_u *name;
8089 int *lenp;
8090{
Bram Moolenaar33570922005-01-25 22:26:29 +00008091 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008092 int cc;
8093
8094 cc = name[*lenp];
8095 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008096 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008098 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008099 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008101 {
8102 *lenp = 0;
8103 return (char_u *)""; /* just in case */
8104 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008105 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008106 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008107 }
8108
8109 return name;
8110}
8111
8112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 * Allocate a variable for the result of a function.
8114 * Return OK or FAIL.
8115 */
8116 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008117get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8118 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 char_u *name; /* name of the function */
8120 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 char_u **arg; /* argument, pointing to the '(' */
8123 linenr_T firstline; /* first line of range */
8124 linenr_T lastline; /* last line of range */
8125 int *doesrange; /* return: function handled range */
8126 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008127 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
8129 char_u *argp;
8130 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008131 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 int argcount = 0; /* number of arguments found */
8133
8134 /*
8135 * Get the arguments.
8136 */
8137 argp = *arg;
8138 while (argcount < MAX_FUNC_ARGS)
8139 {
8140 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8141 if (*argp == ')' || *argp == ',' || *argp == NUL)
8142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8144 {
8145 ret = FAIL;
8146 break;
8147 }
8148 ++argcount;
8149 if (*argp != ',')
8150 break;
8151 }
8152 if (*argp == ')')
8153 ++argp;
8154 else
8155 ret = FAIL;
8156
8157 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008158 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008159 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008161 {
8162 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008163 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008164 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008165 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167
8168 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008169 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170
8171 *arg = skipwhite(argp);
8172 return ret;
8173}
8174
8175
8176/*
8177 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008178 * Return OK when the function can't be called, FAIL otherwise.
8179 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 */
8181 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008182call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008183 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008184 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008186 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008188 typval_T *argvars; /* vars for arguments, must have "argcount"
8189 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 linenr_T firstline; /* first line of range */
8191 linenr_T lastline; /* last line of range */
8192 int *doesrange; /* return: function handled range */
8193 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008194 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195{
8196 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197#define ERROR_UNKNOWN 0
8198#define ERROR_TOOMANY 1
8199#define ERROR_TOOFEW 2
8200#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008201#define ERROR_DICT 4
8202#define ERROR_NONE 5
8203#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 int error = ERROR_NONE;
8205 int i;
8206 int llen;
8207 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208#define FLEN_FIXED 40
8209 char_u fname_buf[FLEN_FIXED + 1];
8210 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008211 char_u *name;
8212
8213 /* Make a copy of the name, if it comes from a funcref variable it could
8214 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008215 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008216 if (name == NULL)
8217 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218
8219 /*
8220 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8221 * Change <SNR>123_name() to K_SNR 123_name().
8222 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 llen = eval_fname_script(name);
8225 if (llen > 0)
8226 {
8227 fname_buf[0] = K_SPECIAL;
8228 fname_buf[1] = KS_EXTRA;
8229 fname_buf[2] = (int)KE_SNR;
8230 i = 3;
8231 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8232 {
8233 if (current_SID <= 0)
8234 error = ERROR_SCRIPT;
8235 else
8236 {
8237 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8238 i = (int)STRLEN(fname_buf);
8239 }
8240 }
8241 if (i + STRLEN(name + llen) < FLEN_FIXED)
8242 {
8243 STRCPY(fname_buf + i, name + llen);
8244 fname = fname_buf;
8245 }
8246 else
8247 {
8248 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8249 if (fname == NULL)
8250 error = ERROR_OTHER;
8251 else
8252 {
8253 mch_memmove(fname, fname_buf, (size_t)i);
8254 STRCPY(fname + i, name + llen);
8255 }
8256 }
8257 }
8258 else
8259 fname = name;
8260
8261 *doesrange = FALSE;
8262
8263
8264 /* execute the function if no errors detected and executing */
8265 if (evaluate && error == ERROR_NONE)
8266 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008267 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8268 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 error = ERROR_UNKNOWN;
8270
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008271 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {
8273 /*
8274 * User defined function.
8275 */
8276 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008277
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008279 /* Trigger FuncUndefined event, may load the function. */
8280 if (fp == NULL
8281 && apply_autocmds(EVENT_FUNCUNDEFINED,
8282 fname, fname, TRUE, NULL)
8283 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008285 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 fp = find_func(fname);
8287 }
8288#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008289 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008290 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008291 {
8292 /* loaded a package, search for the function again */
8293 fp = find_func(fname);
8294 }
8295
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 if (fp != NULL)
8297 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008298 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008300 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008302 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008304 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008305 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 else
8307 {
8308 /*
8309 * Call the user function.
8310 * Save and restore search patterns, script variables and
8311 * redo buffer.
8312 */
8313 save_search_patterns();
8314 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008315 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008317 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008318 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8319 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8320 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008321 /* Function was unreferenced while being used, free it
8322 * now. */
8323 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 restoreRedobuff();
8325 restore_search_patterns();
8326 error = ERROR_NONE;
8327 }
8328 }
8329 }
8330 else
8331 {
8332 /*
8333 * Find the function name in the table, call its implementation.
8334 */
8335 i = find_internal_func(fname);
8336 if (i >= 0)
8337 {
8338 if (argcount < functions[i].f_min_argc)
8339 error = ERROR_TOOFEW;
8340 else if (argcount > functions[i].f_max_argc)
8341 error = ERROR_TOOMANY;
8342 else
8343 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008344 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008345 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 error = ERROR_NONE;
8347 }
8348 }
8349 }
8350 /*
8351 * The function call (or "FuncUndefined" autocommand sequence) might
8352 * have been aborted by an error, an interrupt, or an explicitly thrown
8353 * exception that has not been caught so far. This situation can be
8354 * tested for by calling aborting(). For an error in an internal
8355 * function or for the "E132" error in call_user_func(), however, the
8356 * throw point at which the "force_abort" flag (temporarily reset by
8357 * emsg()) is normally updated has not been reached yet. We need to
8358 * update that flag first to make aborting() reliable.
8359 */
8360 update_force_abort();
8361 }
8362 if (error == ERROR_NONE)
8363 ret = OK;
8364
8365 /*
8366 * Report an error unless the argument evaluation or function call has been
8367 * cancelled due to an aborting error, an interrupt, or an exception.
8368 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008369 if (!aborting())
8370 {
8371 switch (error)
8372 {
8373 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008374 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008375 break;
8376 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008377 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008378 break;
8379 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008380 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008381 name);
8382 break;
8383 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008384 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008385 name);
8386 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008387 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008388 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008389 name);
8390 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008391 }
8392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 if (fname != name && fname != fname_buf)
8395 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008396 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397
8398 return ret;
8399}
8400
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008401/*
8402 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008403 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008404 */
8405 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008406emsg_funcname(ermsg, name)
8407 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008408 char_u *name;
8409{
8410 char_u *p;
8411
8412 if (*name == K_SPECIAL)
8413 p = concat_str((char_u *)"<SNR>", name + 3);
8414 else
8415 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008416 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008417 if (p != name)
8418 vim_free(p);
8419}
8420
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008421/*
8422 * Return TRUE for a non-zero Number and a non-empty String.
8423 */
8424 static int
8425non_zero_arg(argvars)
8426 typval_T *argvars;
8427{
8428 return ((argvars[0].v_type == VAR_NUMBER
8429 && argvars[0].vval.v_number != 0)
8430 || (argvars[0].v_type == VAR_STRING
8431 && argvars[0].vval.v_string != NULL
8432 && *argvars[0].vval.v_string != NUL));
8433}
8434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435/*********************************************
8436 * Implementation of the built-in functions
8437 */
8438
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008439#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008440static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8441
8442/*
8443 * Get the float value of "argvars[0]" into "f".
8444 * Returns FAIL when the argument is not a Number or Float.
8445 */
8446 static int
8447get_float_arg(argvars, f)
8448 typval_T *argvars;
8449 float_T *f;
8450{
8451 if (argvars[0].v_type == VAR_FLOAT)
8452 {
8453 *f = argvars[0].vval.v_float;
8454 return OK;
8455 }
8456 if (argvars[0].v_type == VAR_NUMBER)
8457 {
8458 *f = (float_T)argvars[0].vval.v_number;
8459 return OK;
8460 }
8461 EMSG(_("E808: Number or Float required"));
8462 return FAIL;
8463}
8464
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008465/*
8466 * "abs(expr)" function
8467 */
8468 static void
8469f_abs(argvars, rettv)
8470 typval_T *argvars;
8471 typval_T *rettv;
8472{
8473 if (argvars[0].v_type == VAR_FLOAT)
8474 {
8475 rettv->v_type = VAR_FLOAT;
8476 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8477 }
8478 else
8479 {
8480 varnumber_T n;
8481 int error = FALSE;
8482
8483 n = get_tv_number_chk(&argvars[0], &error);
8484 if (error)
8485 rettv->vval.v_number = -1;
8486 else if (n > 0)
8487 rettv->vval.v_number = n;
8488 else
8489 rettv->vval.v_number = -n;
8490 }
8491}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008492
8493/*
8494 * "acos()" function
8495 */
8496 static void
8497f_acos(argvars, rettv)
8498 typval_T *argvars;
8499 typval_T *rettv;
8500{
8501 float_T f;
8502
8503 rettv->v_type = VAR_FLOAT;
8504 if (get_float_arg(argvars, &f) == OK)
8505 rettv->vval.v_float = acos(f);
8506 else
8507 rettv->vval.v_float = 0.0;
8508}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008509#endif
8510
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008512 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 */
8514 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008515f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008516 typval_T *argvars;
8517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518{
Bram Moolenaar33570922005-01-25 22:26:29 +00008519 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008524 if ((l = argvars[0].vval.v_list) != NULL
8525 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8526 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008527 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008528 }
8529 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008530 EMSG(_(e_listreq));
8531}
8532
8533/*
8534 * "append(lnum, string/list)" function
8535 */
8536 static void
8537f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008538 typval_T *argvars;
8539 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008540{
8541 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008542 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 list_T *l = NULL;
8544 listitem_T *li = NULL;
8545 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008546 long added = 0;
8547
Bram Moolenaar0d660222005-01-07 21:51:51 +00008548 lnum = get_tv_lnum(argvars);
8549 if (lnum >= 0
8550 && lnum <= curbuf->b_ml.ml_line_count
8551 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008552 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008553 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008554 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008555 l = argvars[1].vval.v_list;
8556 if (l == NULL)
8557 return;
8558 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008559 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008560 for (;;)
8561 {
8562 if (l == NULL)
8563 tv = &argvars[1]; /* append a string */
8564 else if (li == NULL)
8565 break; /* end of list */
8566 else
8567 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008568 line = get_tv_string_chk(tv);
8569 if (line == NULL) /* type error */
8570 {
8571 rettv->vval.v_number = 1; /* Failed */
8572 break;
8573 }
8574 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008575 ++added;
8576 if (l == NULL)
8577 break;
8578 li = li->li_next;
8579 }
8580
8581 appended_lines_mark(lnum, added);
8582 if (curwin->w_cursor.lnum > lnum)
8583 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008585 else
8586 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587}
8588
8589/*
8590 * "argc()" function
8591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008593f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008594 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008597 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598}
8599
8600/*
8601 * "argidx()" function
8602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008605 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609}
8610
8611/*
8612 * "argv(nr)" function
8613 */
8614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008616 typval_T *argvars;
8617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618{
8619 int idx;
8620
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008621 if (argvars[0].v_type != VAR_UNKNOWN)
8622 {
8623 idx = get_tv_number_chk(&argvars[0], NULL);
8624 if (idx >= 0 && idx < ARGCOUNT)
8625 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8626 else
8627 rettv->vval.v_string = NULL;
8628 rettv->v_type = VAR_STRING;
8629 }
8630 else if (rettv_list_alloc(rettv) == OK)
8631 for (idx = 0; idx < ARGCOUNT; ++idx)
8632 list_append_string(rettv->vval.v_list,
8633 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634}
8635
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008636#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008637/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008638 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008639 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008640 static void
8641f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008642 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008643 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008644{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008645 float_T f;
8646
8647 rettv->v_type = VAR_FLOAT;
8648 if (get_float_arg(argvars, &f) == OK)
8649 rettv->vval.v_float = asin(f);
8650 else
8651 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008652}
8653
8654/*
8655 * "atan()" function
8656 */
8657 static void
8658f_atan(argvars, rettv)
8659 typval_T *argvars;
8660 typval_T *rettv;
8661{
8662 float_T f;
8663
8664 rettv->v_type = VAR_FLOAT;
8665 if (get_float_arg(argvars, &f) == OK)
8666 rettv->vval.v_float = atan(f);
8667 else
8668 rettv->vval.v_float = 0.0;
8669}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008670
8671/*
8672 * "atan2()" function
8673 */
8674 static void
8675f_atan2(argvars, rettv)
8676 typval_T *argvars;
8677 typval_T *rettv;
8678{
8679 float_T fx, fy;
8680
8681 rettv->v_type = VAR_FLOAT;
8682 if (get_float_arg(argvars, &fx) == OK
8683 && get_float_arg(&argvars[1], &fy) == OK)
8684 rettv->vval.v_float = atan2(fx, fy);
8685 else
8686 rettv->vval.v_float = 0.0;
8687}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008688#endif
8689
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690/*
8691 * "browse(save, title, initdir, default)" function
8692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008694f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008695 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697{
8698#ifdef FEAT_BROWSE
8699 int save;
8700 char_u *title;
8701 char_u *initdir;
8702 char_u *defname;
8703 char_u buf[NUMBUFLEN];
8704 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008705 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008707 save = get_tv_number_chk(&argvars[0], &error);
8708 title = get_tv_string_chk(&argvars[1]);
8709 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8710 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008712 if (error || title == NULL || initdir == NULL || defname == NULL)
8713 rettv->vval.v_string = NULL;
8714 else
8715 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008716 do_browse(save ? BROWSE_SAVE : 0,
8717 title, defname, NULL, initdir, NULL, curbuf);
8718#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008719 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008720#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008721 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008722}
8723
8724/*
8725 * "browsedir(title, initdir)" function
8726 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008728f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008729 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008731{
8732#ifdef FEAT_BROWSE
8733 char_u *title;
8734 char_u *initdir;
8735 char_u buf[NUMBUFLEN];
8736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008737 title = get_tv_string_chk(&argvars[0]);
8738 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008740 if (title == NULL || initdir == NULL)
8741 rettv->vval.v_string = NULL;
8742 else
8743 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008744 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008746 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008748 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749}
8750
Bram Moolenaar33570922005-01-25 22:26:29 +00008751static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008752
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753/*
8754 * Find a buffer by number or exact name.
8755 */
8756 static buf_T *
8757find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008758 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759{
8760 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008762 if (avar->v_type == VAR_NUMBER)
8763 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008764 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008766 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008767 if (buf == NULL)
8768 {
8769 /* No full path name match, try a match with a URL or a "nofile"
8770 * buffer, these don't use the full path. */
8771 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8772 if (buf->b_fname != NULL
8773 && (path_with_url(buf->b_fname)
8774#ifdef FEAT_QUICKFIX
8775 || bt_nofile(buf)
8776#endif
8777 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008778 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008779 break;
8780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 }
8782 return buf;
8783}
8784
8785/*
8786 * "bufexists(expr)" function
8787 */
8788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008789f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008790 typval_T *argvars;
8791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008793 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794}
8795
8796/*
8797 * "buflisted(expr)" function
8798 */
8799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008800f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008801 typval_T *argvars;
8802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803{
8804 buf_T *buf;
8805
8806 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008807 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808}
8809
8810/*
8811 * "bufloaded(expr)" function
8812 */
8813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008815 typval_T *argvars;
8816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817{
8818 buf_T *buf;
8819
8820 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822}
8823
Bram Moolenaar33570922005-01-25 22:26:29 +00008824static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826/*
8827 * Get buffer by number or pattern.
8828 */
8829 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008831 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 int save_magic;
8835 char_u *save_cpo;
8836 buf_T *buf;
8837
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838 if (tv->v_type == VAR_NUMBER)
8839 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008840 if (tv->v_type != VAR_STRING)
8841 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 if (name == NULL || *name == NUL)
8843 return curbuf;
8844 if (name[0] == '$' && name[1] == NUL)
8845 return lastbuf;
8846
8847 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8848 save_magic = p_magic;
8849 p_magic = TRUE;
8850 save_cpo = p_cpo;
8851 p_cpo = (char_u *)"";
8852
8853 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8854 TRUE, FALSE));
8855
8856 p_magic = save_magic;
8857 p_cpo = save_cpo;
8858
8859 /* If not found, try expanding the name, like done for bufexists(). */
8860 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862
8863 return buf;
8864}
8865
8866/*
8867 * "bufname(expr)" function
8868 */
8869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008871 typval_T *argvars;
8872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
8874 buf_T *buf;
8875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008876 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008878 buf = get_buf_tv(&argvars[0]);
8879 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008881 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008883 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 --emsg_off;
8885}
8886
8887/*
8888 * "bufnr(expr)" function
8889 */
8890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008892 typval_T *argvars;
8893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894{
8895 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008896 int error = FALSE;
8897 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008899 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008901 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008902 --emsg_off;
8903
8904 /* If the buffer isn't found and the second argument is not zero create a
8905 * new buffer. */
8906 if (buf == NULL
8907 && argvars[1].v_type != VAR_UNKNOWN
8908 && get_tv_number_chk(&argvars[1], &error) != 0
8909 && !error
8910 && (name = get_tv_string_chk(&argvars[0])) != NULL
8911 && !error)
8912 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8913
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008917 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918}
8919
8920/*
8921 * "bufwinnr(nr)" function
8922 */
8923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008925 typval_T *argvars;
8926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927{
8928#ifdef FEAT_WINDOWS
8929 win_T *wp;
8930 int winnr = 0;
8931#endif
8932 buf_T *buf;
8933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937#ifdef FEAT_WINDOWS
8938 for (wp = firstwin; wp; wp = wp->w_next)
8939 {
8940 ++winnr;
8941 if (wp->w_buffer == buf)
8942 break;
8943 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947#endif
8948 --emsg_off;
8949}
8950
8951/*
8952 * "byte2line(byte)" function
8953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008956 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
8959#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961#else
8962 long boff = 0;
8963
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008964 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 (linenr_T)0, &boff);
8970#endif
8971}
8972
8973/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008974 * "byteidx()" function
8975 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 typval_T *argvars;
8979 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008980{
8981#ifdef FEAT_MBYTE
8982 char_u *t;
8983#endif
8984 char_u *str;
8985 long idx;
8986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 str = get_tv_string_chk(&argvars[0]);
8988 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008991 return;
8992
8993#ifdef FEAT_MBYTE
8994 t = str;
8995 for ( ; idx > 0; idx--)
8996 {
8997 if (*t == NUL) /* EOL reached */
8998 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008999 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009000 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009001 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009002#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009003 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009005#endif
9006}
9007
9008/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009009 * "call(func, arglist)" function
9010 */
9011 static void
9012f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 typval_T *argvars;
9014 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009015{
9016 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009017 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009018 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009020 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009021 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009022
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009023 if (argvars[1].v_type != VAR_LIST)
9024 {
9025 EMSG(_(e_listreq));
9026 return;
9027 }
9028 if (argvars[1].vval.v_list == NULL)
9029 return;
9030
9031 if (argvars[0].v_type == VAR_FUNC)
9032 func = argvars[0].vval.v_string;
9033 else
9034 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009035 if (*func == NUL)
9036 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009037
Bram Moolenaare9a41262005-01-15 22:18:47 +00009038 if (argvars[2].v_type != VAR_UNKNOWN)
9039 {
9040 if (argvars[2].v_type != VAR_DICT)
9041 {
9042 EMSG(_(e_dictreq));
9043 return;
9044 }
9045 selfdict = argvars[2].vval.v_dict;
9046 }
9047
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009048 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9049 item = item->li_next)
9050 {
9051 if (argc == MAX_FUNC_ARGS)
9052 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009053 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009054 break;
9055 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009056 /* Make a copy of each argument. This is needed to be able to set
9057 * v_lock to VAR_FIXED in the copy without changing the original list.
9058 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009059 copy_tv(&item->li_tv, &argv[argc++]);
9060 }
9061
9062 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009063 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009064 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9065 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009066
9067 /* Free the arguments. */
9068 while (argc > 0)
9069 clear_tv(&argv[--argc]);
9070}
9071
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009072#ifdef FEAT_FLOAT
9073/*
9074 * "ceil({float})" function
9075 */
9076 static void
9077f_ceil(argvars, rettv)
9078 typval_T *argvars;
9079 typval_T *rettv;
9080{
9081 float_T f;
9082
9083 rettv->v_type = VAR_FLOAT;
9084 if (get_float_arg(argvars, &f) == OK)
9085 rettv->vval.v_float = ceil(f);
9086 else
9087 rettv->vval.v_float = 0.0;
9088}
9089#endif
9090
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009091/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009092 * "changenr()" function
9093 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009094 static void
9095f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009096 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009097 typval_T *rettv;
9098{
9099 rettv->vval.v_number = curbuf->b_u_seq_cur;
9100}
9101
9102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 * "char2nr(string)" function
9104 */
9105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 typval_T *argvars;
9108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
9110#ifdef FEAT_MBYTE
9111 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009112 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 else
9114#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116}
9117
9118/*
9119 * "cindent(lnum)" function
9120 */
9121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009123 typval_T *argvars;
9124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126#ifdef FEAT_CINDENT
9127 pos_T pos;
9128 linenr_T lnum;
9129
9130 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9133 {
9134 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 curwin->w_cursor = pos;
9137 }
9138 else
9139#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141}
9142
9143/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009144 * "clearmatches()" function
9145 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009146 static void
9147f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009148 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009149 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009150{
9151#ifdef FEAT_SEARCH_EXTRA
9152 clear_matches(curwin);
9153#endif
9154}
9155
9156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 * "col(string)" function
9158 */
9159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009161 typval_T *argvars;
9162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163{
9164 colnr_T col = 0;
9165 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009166 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009168 fp = var2fpos(&argvars[0], FALSE, &fnum);
9169 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 {
9171 if (fp->col == MAXCOL)
9172 {
9173 /* '> can be MAXCOL, get the length of the line then */
9174 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009175 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 else
9177 col = MAXCOL;
9178 }
9179 else
9180 {
9181 col = fp->col + 1;
9182#ifdef FEAT_VIRTUALEDIT
9183 /* col(".") when the cursor is on the NUL at the end of the line
9184 * because of "coladd" can be seen as an extra column. */
9185 if (virtual_active() && fp == &curwin->w_cursor)
9186 {
9187 char_u *p = ml_get_cursor();
9188
9189 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9190 curwin->w_virtcol - curwin->w_cursor.coladd))
9191 {
9192# ifdef FEAT_MBYTE
9193 int l;
9194
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009195 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 col += l;
9197# else
9198 if (*p != NUL && p[1] == NUL)
9199 ++col;
9200# endif
9201 }
9202 }
9203#endif
9204 }
9205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207}
9208
Bram Moolenaar572cb562005-08-05 21:35:02 +00009209#if defined(FEAT_INS_EXPAND)
9210/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009211 * "complete()" function
9212 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009213 static void
9214f_complete(argvars, rettv)
9215 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009216 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009217{
9218 int startcol;
9219
9220 if ((State & INSERT) == 0)
9221 {
9222 EMSG(_("E785: complete() can only be used in Insert mode"));
9223 return;
9224 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009225
9226 /* Check for undo allowed here, because if something was already inserted
9227 * the line was already saved for undo and this check isn't done. */
9228 if (!undo_allowed())
9229 return;
9230
Bram Moolenaarade00832006-03-10 21:46:58 +00009231 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9232 {
9233 EMSG(_(e_invarg));
9234 return;
9235 }
9236
9237 startcol = get_tv_number_chk(&argvars[0], NULL);
9238 if (startcol <= 0)
9239 return;
9240
9241 set_completion(startcol - 1, argvars[1].vval.v_list);
9242}
9243
9244/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009245 * "complete_add()" function
9246 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009247 static void
9248f_complete_add(argvars, rettv)
9249 typval_T *argvars;
9250 typval_T *rettv;
9251{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009252 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009253}
9254
9255/*
9256 * "complete_check()" function
9257 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009258 static void
9259f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009260 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009261 typval_T *rettv;
9262{
9263 int saved = RedrawingDisabled;
9264
9265 RedrawingDisabled = 0;
9266 ins_compl_check_keys(0);
9267 rettv->vval.v_number = compl_interrupted;
9268 RedrawingDisabled = saved;
9269}
9270#endif
9271
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272/*
9273 * "confirm(message, buttons[, default [, type]])" function
9274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009276f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009277 typval_T *argvars UNUSED;
9278 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279{
9280#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9281 char_u *message;
9282 char_u *buttons = NULL;
9283 char_u buf[NUMBUFLEN];
9284 char_u buf2[NUMBUFLEN];
9285 int def = 1;
9286 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009287 char_u *typestr;
9288 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009290 message = get_tv_string_chk(&argvars[0]);
9291 if (message == NULL)
9292 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009293 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009295 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9296 if (buttons == NULL)
9297 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009298 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009300 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009301 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009303 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9304 if (typestr == NULL)
9305 error = TRUE;
9306 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009308 switch (TOUPPER_ASC(*typestr))
9309 {
9310 case 'E': type = VIM_ERROR; break;
9311 case 'Q': type = VIM_QUESTION; break;
9312 case 'I': type = VIM_INFO; break;
9313 case 'W': type = VIM_WARNING; break;
9314 case 'G': type = VIM_GENERIC; break;
9315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 }
9317 }
9318 }
9319 }
9320
9321 if (buttons == NULL || *buttons == NUL)
9322 buttons = (char_u *)_("&Ok");
9323
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009324 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009325 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009326 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327#endif
9328}
9329
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009330/*
9331 * "copy()" function
9332 */
9333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 typval_T *argvars;
9336 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009337{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009338 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009339}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009341#ifdef FEAT_FLOAT
9342/*
9343 * "cos()" function
9344 */
9345 static void
9346f_cos(argvars, rettv)
9347 typval_T *argvars;
9348 typval_T *rettv;
9349{
9350 float_T f;
9351
9352 rettv->v_type = VAR_FLOAT;
9353 if (get_float_arg(argvars, &f) == OK)
9354 rettv->vval.v_float = cos(f);
9355 else
9356 rettv->vval.v_float = 0.0;
9357}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009358
9359/*
9360 * "cosh()" function
9361 */
9362 static void
9363f_cosh(argvars, rettv)
9364 typval_T *argvars;
9365 typval_T *rettv;
9366{
9367 float_T f;
9368
9369 rettv->v_type = VAR_FLOAT;
9370 if (get_float_arg(argvars, &f) == OK)
9371 rettv->vval.v_float = cosh(f);
9372 else
9373 rettv->vval.v_float = 0.0;
9374}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009375#endif
9376
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009378 * "count()" function
9379 */
9380 static void
9381f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009382 typval_T *argvars;
9383 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009384{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009385 long n = 0;
9386 int ic = FALSE;
9387
Bram Moolenaare9a41262005-01-15 22:18:47 +00009388 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009389 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009390 listitem_T *li;
9391 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009393
Bram Moolenaare9a41262005-01-15 22:18:47 +00009394 if ((l = argvars[0].vval.v_list) != NULL)
9395 {
9396 li = l->lv_first;
9397 if (argvars[2].v_type != VAR_UNKNOWN)
9398 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009399 int error = FALSE;
9400
9401 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009402 if (argvars[3].v_type != VAR_UNKNOWN)
9403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009404 idx = get_tv_number_chk(&argvars[3], &error);
9405 if (!error)
9406 {
9407 li = list_find(l, idx);
9408 if (li == NULL)
9409 EMSGN(_(e_listidx), idx);
9410 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009411 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412 if (error)
9413 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009414 }
9415
9416 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009417 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009418 ++n;
9419 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009421 else if (argvars[0].v_type == VAR_DICT)
9422 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009423 int todo;
9424 dict_T *d;
9425 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009427 if ((d = argvars[0].vval.v_dict) != NULL)
9428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009429 int error = FALSE;
9430
Bram Moolenaare9a41262005-01-15 22:18:47 +00009431 if (argvars[2].v_type != VAR_UNKNOWN)
9432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009433 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009434 if (argvars[3].v_type != VAR_UNKNOWN)
9435 EMSG(_(e_invarg));
9436 }
9437
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009438 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009440 {
9441 if (!HASHITEM_EMPTY(hi))
9442 {
9443 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009444 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009445 ++n;
9446 }
9447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009448 }
9449 }
9450 else
9451 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009452 rettv->vval.v_number = n;
9453}
9454
9455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9457 *
9458 * Checks the existence of a cscope connection.
9459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009462 typval_T *argvars UNUSED;
9463 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464{
9465#ifdef FEAT_CSCOPE
9466 int num = 0;
9467 char_u *dbpath = NULL;
9468 char_u *prepend = NULL;
9469 char_u buf[NUMBUFLEN];
9470
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009471 if (argvars[0].v_type != VAR_UNKNOWN
9472 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 num = (int)get_tv_number(&argvars[0]);
9475 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009476 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 }
9479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481#endif
9482}
9483
9484/*
9485 * "cursor(lnum, col)" function
9486 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009487 * Moves the cursor to the specified line and column.
9488 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009492 typval_T *argvars;
9493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
9495 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009496#ifdef FEAT_VIRTUALEDIT
9497 long coladd = 0;
9498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009500 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009501 if (argvars[1].v_type == VAR_UNKNOWN)
9502 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009503 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009504
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009505 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009506 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009507 line = pos.lnum;
9508 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009509#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009510 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009511#endif
9512 }
9513 else
9514 {
9515 line = get_tv_lnum(argvars);
9516 col = get_tv_number_chk(&argvars[1], NULL);
9517#ifdef FEAT_VIRTUALEDIT
9518 if (argvars[2].v_type != VAR_UNKNOWN)
9519 coladd = get_tv_number_chk(&argvars[2], NULL);
9520#endif
9521 }
9522 if (line < 0 || col < 0
9523#ifdef FEAT_VIRTUALEDIT
9524 || coladd < 0
9525#endif
9526 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009527 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 if (line > 0)
9529 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 if (col > 0)
9531 curwin->w_cursor.col = col - 1;
9532#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009533 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534#endif
9535
9536 /* Make sure the cursor is in a valid position. */
9537 check_cursor();
9538#ifdef FEAT_MBYTE
9539 /* Correct cursor for multi-byte character. */
9540 if (has_mbyte)
9541 mb_adjust_cursor();
9542#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009543
9544 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009545 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546}
9547
9548/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009549 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 */
9551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009553 typval_T *argvars;
9554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009556 int noref = 0;
9557
9558 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009559 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009560 if (noref < 0 || noref > 1)
9561 EMSG(_(e_invarg));
9562 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009563 {
9564 current_copyID += COPYID_INC;
9565 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567}
9568
9569/*
9570 * "delete()" function
9571 */
9572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009574 typval_T *argvars;
9575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576{
9577 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009580 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581}
9582
9583/*
9584 * "did_filetype()" function
9585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009587f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009588 typval_T *argvars UNUSED;
9589 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590{
9591#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593#endif
9594}
9595
9596/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009597 * "diff_filler()" function
9598 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009601 typval_T *argvars UNUSED;
9602 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009603{
9604#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009606#endif
9607}
9608
9609/*
9610 * "diff_hlID()" function
9611 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009613f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009614 typval_T *argvars UNUSED;
9615 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009616{
9617#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009619 static linenr_T prev_lnum = 0;
9620 static int changedtick = 0;
9621 static int fnum = 0;
9622 static int change_start = 0;
9623 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009624 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009625 int filler_lines;
9626 int col;
9627
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009628 if (lnum < 0) /* ignore type error in {lnum} arg */
9629 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009630 if (lnum != prev_lnum
9631 || changedtick != curbuf->b_changedtick
9632 || fnum != curbuf->b_fnum)
9633 {
9634 /* New line, buffer, change: need to get the values. */
9635 filler_lines = diff_check(curwin, lnum);
9636 if (filler_lines < 0)
9637 {
9638 if (filler_lines == -1)
9639 {
9640 change_start = MAXCOL;
9641 change_end = -1;
9642 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9643 hlID = HLF_ADD; /* added line */
9644 else
9645 hlID = HLF_CHD; /* changed line */
9646 }
9647 else
9648 hlID = HLF_ADD; /* added line */
9649 }
9650 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009651 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009652 prev_lnum = lnum;
9653 changedtick = curbuf->b_changedtick;
9654 fnum = curbuf->b_fnum;
9655 }
9656
9657 if (hlID == HLF_CHD || hlID == HLF_TXD)
9658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009659 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009660 if (col >= change_start && col <= change_end)
9661 hlID = HLF_TXD; /* changed text */
9662 else
9663 hlID = HLF_CHD; /* changed line */
9664 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009665 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009666#endif
9667}
9668
9669/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009670 * "empty({expr})" function
9671 */
9672 static void
9673f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009674 typval_T *argvars;
9675 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009676{
9677 int n;
9678
9679 switch (argvars[0].v_type)
9680 {
9681 case VAR_STRING:
9682 case VAR_FUNC:
9683 n = argvars[0].vval.v_string == NULL
9684 || *argvars[0].vval.v_string == NUL;
9685 break;
9686 case VAR_NUMBER:
9687 n = argvars[0].vval.v_number == 0;
9688 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009689#ifdef FEAT_FLOAT
9690 case VAR_FLOAT:
9691 n = argvars[0].vval.v_float == 0.0;
9692 break;
9693#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009694 case VAR_LIST:
9695 n = argvars[0].vval.v_list == NULL
9696 || argvars[0].vval.v_list->lv_first == NULL;
9697 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009698 case VAR_DICT:
9699 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009700 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009701 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009702 default:
9703 EMSG2(_(e_intern2), "f_empty()");
9704 n = 0;
9705 }
9706
9707 rettv->vval.v_number = n;
9708}
9709
9710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 * "escape({string}, {chars})" function
9712 */
9713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009715 typval_T *argvars;
9716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717{
9718 char_u buf[NUMBUFLEN];
9719
Bram Moolenaar758711c2005-02-02 23:11:38 +00009720 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9721 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723}
9724
9725/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009726 * "eval()" function
9727 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009728 static void
9729f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009730 typval_T *argvars;
9731 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009732{
9733 char_u *s;
9734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009735 s = get_tv_string_chk(&argvars[0]);
9736 if (s != NULL)
9737 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9740 {
9741 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009742 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009743 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009744 else if (*s != NUL)
9745 EMSG(_(e_trailing));
9746}
9747
9748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 * "eventhandler()" function
9750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009752f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009753 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757}
9758
9759/*
9760 * "executable()" function
9761 */
9762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009764 typval_T *argvars;
9765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768}
9769
9770/*
9771 * "exists()" function
9772 */
9773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009775 typval_T *argvars;
9776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
9778 char_u *p;
9779 char_u *name;
9780 int n = FALSE;
9781 int len = 0;
9782
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009783 no_autoload = TRUE;
9784
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 if (*p == '$') /* environment variable */
9787 {
9788 /* first try "normal" environment variables (fast) */
9789 if (mch_getenv(p + 1) != NULL)
9790 n = TRUE;
9791 else
9792 {
9793 /* try expanding things like $VIM and ${HOME} */
9794 p = expand_env_save(p);
9795 if (p != NULL && *p != '$')
9796 n = TRUE;
9797 vim_free(p);
9798 }
9799 }
9800 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009803 if (*skipwhite(p) != NUL)
9804 n = FALSE; /* trailing garbage */
9805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 else if (*p == '*') /* internal or user defined function */
9807 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009808 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 }
9810 else if (*p == ':')
9811 {
9812 n = cmd_exists(p + 1);
9813 }
9814 else if (*p == '#')
9815 {
9816#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009817 if (p[1] == '#')
9818 n = autocmd_supported(p + 2);
9819 else
9820 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821#endif
9822 }
9823 else /* internal variable */
9824 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009825 char_u *tofree;
9826 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009828 /* get_name_len() takes care of expanding curly braces */
9829 name = p;
9830 len = get_name_len(&p, &tofree, TRUE, FALSE);
9831 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009833 if (tofree != NULL)
9834 name = tofree;
9835 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9836 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009838 /* handle d.key, l[idx], f(expr) */
9839 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9840 if (n)
9841 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 }
9843 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009844 if (*p != NUL)
9845 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009847 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 }
9849
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009851
9852 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853}
9854
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009855#ifdef FEAT_FLOAT
9856/*
9857 * "exp()" function
9858 */
9859 static void
9860f_exp(argvars, rettv)
9861 typval_T *argvars;
9862 typval_T *rettv;
9863{
9864 float_T f;
9865
9866 rettv->v_type = VAR_FLOAT;
9867 if (get_float_arg(argvars, &f) == OK)
9868 rettv->vval.v_float = exp(f);
9869 else
9870 rettv->vval.v_float = 0.0;
9871}
9872#endif
9873
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874/*
9875 * "expand()" function
9876 */
9877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009878f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009879 typval_T *argvars;
9880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881{
9882 char_u *s;
9883 int len;
9884 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009885 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009887 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889 rettv->v_type = VAR_STRING;
9890 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 if (*s == '%' || *s == '#' || *s == '<')
9892 {
9893 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009894 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 --emsg_off;
9896 }
9897 else
9898 {
9899 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009900 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009901 if (argvars[1].v_type != VAR_UNKNOWN
9902 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009903 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 if (!error)
9905 {
9906 ExpandInit(&xpc);
9907 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009908 if (p_wic)
9909 options += WILD_ICASE;
9910 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009911 }
9912 else
9913 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 }
9915}
9916
9917/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009918 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009919 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009920 */
9921 static void
9922f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009923 typval_T *argvars;
9924 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009925{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009926 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009927 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009928 list_T *l1, *l2;
9929 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009930 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009931 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009932
Bram Moolenaare9a41262005-01-15 22:18:47 +00009933 l1 = argvars[0].vval.v_list;
9934 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009935 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9936 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009937 {
9938 if (argvars[2].v_type != VAR_UNKNOWN)
9939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009940 before = get_tv_number_chk(&argvars[2], &error);
9941 if (error)
9942 return; /* type error; errmsg already given */
9943
Bram Moolenaar758711c2005-02-02 23:11:38 +00009944 if (before == l1->lv_len)
9945 item = NULL;
9946 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009947 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009948 item = list_find(l1, before);
9949 if (item == NULL)
9950 {
9951 EMSGN(_(e_listidx), before);
9952 return;
9953 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009954 }
9955 }
9956 else
9957 item = NULL;
9958 list_extend(l1, l2, item);
9959
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960 copy_tv(&argvars[0], rettv);
9961 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009962 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009963 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9964 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009965 dict_T *d1, *d2;
9966 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009967 char_u *action;
9968 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009969 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009970 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009971
9972 d1 = argvars[0].vval.v_dict;
9973 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009974 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9975 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009976 {
9977 /* Check the third argument. */
9978 if (argvars[2].v_type != VAR_UNKNOWN)
9979 {
9980 static char *(av[]) = {"keep", "force", "error"};
9981
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009982 action = get_tv_string_chk(&argvars[2]);
9983 if (action == NULL)
9984 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009985 for (i = 0; i < 3; ++i)
9986 if (STRCMP(action, av[i]) == 0)
9987 break;
9988 if (i == 3)
9989 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009990 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009991 return;
9992 }
9993 }
9994 else
9995 action = (char_u *)"force";
9996
9997 /* Go over all entries in the second dict and add them to the
9998 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009999 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010000 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010001 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010002 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010003 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010004 --todo;
10005 di1 = dict_find(d1, hi2->hi_key, -1);
10006 if (di1 == NULL)
10007 {
10008 di1 = dictitem_copy(HI2DI(hi2));
10009 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10010 dictitem_free(di1);
10011 }
10012 else if (*action == 'e')
10013 {
10014 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10015 break;
10016 }
10017 else if (*action == 'f')
10018 {
10019 clear_tv(&di1->di_tv);
10020 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10021 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010022 }
10023 }
10024
Bram Moolenaare9a41262005-01-15 22:18:47 +000010025 copy_tv(&argvars[0], rettv);
10026 }
10027 }
10028 else
10029 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010030}
10031
10032/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010033 * "feedkeys()" function
10034 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010035 static void
10036f_feedkeys(argvars, rettv)
10037 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010038 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010039{
10040 int remap = TRUE;
10041 char_u *keys, *flags;
10042 char_u nbuf[NUMBUFLEN];
10043 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010044 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010045
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010046 /* This is not allowed in the sandbox. If the commands would still be
10047 * executed in the sandbox it would be OK, but it probably happens later,
10048 * when "sandbox" is no longer set. */
10049 if (check_secure())
10050 return;
10051
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010052 keys = get_tv_string(&argvars[0]);
10053 if (*keys != NUL)
10054 {
10055 if (argvars[1].v_type != VAR_UNKNOWN)
10056 {
10057 flags = get_tv_string_buf(&argvars[1], nbuf);
10058 for ( ; *flags != NUL; ++flags)
10059 {
10060 switch (*flags)
10061 {
10062 case 'n': remap = FALSE; break;
10063 case 'm': remap = TRUE; break;
10064 case 't': typed = TRUE; break;
10065 }
10066 }
10067 }
10068
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010069 /* Need to escape K_SPECIAL and CSI before putting the string in the
10070 * typeahead buffer. */
10071 keys_esc = vim_strsave_escape_csi(keys);
10072 if (keys_esc != NULL)
10073 {
10074 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010075 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010076 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010077 if (vgetc_busy)
10078 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010079 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010080 }
10081}
10082
10083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 * "filereadable()" function
10085 */
10086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010088 typval_T *argvars;
10089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010091 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 char_u *p;
10093 int n;
10094
Bram Moolenaarc236c162008-07-13 17:41:49 +000010095#ifndef O_NONBLOCK
10096# define O_NONBLOCK 0
10097#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010099 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10100 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 {
10102 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010103 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 }
10105 else
10106 n = FALSE;
10107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109}
10110
10111/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010112 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 * rights to write into.
10114 */
10115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010117 typval_T *argvars;
10118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010120 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121}
10122
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010123static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010124
10125 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010126findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010127 typval_T *argvars;
10128 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010129 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010130{
10131#ifdef FEAT_SEARCHPATH
10132 char_u *fname;
10133 char_u *fresult = NULL;
10134 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10135 char_u *p;
10136 char_u pathbuf[NUMBUFLEN];
10137 int count = 1;
10138 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010139 int error = FALSE;
10140#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010141
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010142 rettv->vval.v_string = NULL;
10143 rettv->v_type = VAR_STRING;
10144
10145#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010146 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010147
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010148 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10151 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010152 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 else
10154 {
10155 if (*p != NUL)
10156 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010158 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010159 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010161 }
10162
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010163 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10164 error = TRUE;
10165
10166 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010168 do
10169 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010170 if (rettv->v_type == VAR_STRING)
10171 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010172 fresult = find_file_in_path_option(first ? fname : NULL,
10173 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010174 0, first, path,
10175 find_what,
10176 curbuf->b_ffname,
10177 find_what == FINDFILE_DIR
10178 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010180
10181 if (fresult != NULL && rettv->v_type == VAR_LIST)
10182 list_append_string(rettv->vval.v_list, fresult, -1);
10183
10184 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010185 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010186
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010187 if (rettv->v_type == VAR_STRING)
10188 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010189#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010190}
10191
Bram Moolenaar33570922005-01-25 22:26:29 +000010192static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10193static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010194
10195/*
10196 * Implementation of map() and filter().
10197 */
10198 static void
10199filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010200 typval_T *argvars;
10201 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010202 int map;
10203{
10204 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010205 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010206 listitem_T *li, *nli;
10207 list_T *l = NULL;
10208 dictitem_T *di;
10209 hashtab_T *ht;
10210 hashitem_T *hi;
10211 dict_T *d = NULL;
10212 typval_T save_val;
10213 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010214 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010215 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010216 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010217 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010218 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010219
Bram Moolenaare9a41262005-01-15 22:18:47 +000010220 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010221 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010222 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010223 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010224 return;
10225 }
10226 else if (argvars[0].v_type == VAR_DICT)
10227 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010228 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010229 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010230 return;
10231 }
10232 else
10233 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010234 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010235 return;
10236 }
10237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010238 expr = get_tv_string_buf_chk(&argvars[1], buf);
10239 /* On type errors, the preceding call has already displayed an error
10240 * message. Avoid a misleading error message for an empty string that
10241 * was not passed as argument. */
10242 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010244 prepare_vimvar(VV_VAL, &save_val);
10245 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010246
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010247 /* We reset "did_emsg" to be able to detect whether an error
10248 * occurred during evaluation of the expression. */
10249 save_did_emsg = did_emsg;
10250 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010251
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010252 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010255 vimvars[VV_KEY].vv_type = VAR_STRING;
10256
10257 ht = &d->dv_hashtab;
10258 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010259 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010260 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010262 if (!HASHITEM_EMPTY(hi))
10263 {
10264 --todo;
10265 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010266 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 break;
10268 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010269 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010270 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010271 break;
10272 if (!map && rem)
10273 dictitem_remove(d, di);
10274 clear_tv(&vimvars[VV_KEY].vv_tv);
10275 }
10276 }
10277 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010278 }
10279 else
10280 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010281 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 for (li = l->lv_first; li != NULL; li = nli)
10284 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010285 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010286 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010287 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010288 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010289 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010290 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010291 break;
10292 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010294 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010295 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010296 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010297
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010298 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010299 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010300
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010301 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010302 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010303
10304 copy_tv(&argvars[0], rettv);
10305}
10306
10307 static int
10308filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010309 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010310 char_u *expr;
10311 int map;
10312 int *remp;
10313{
Bram Moolenaar33570922005-01-25 22:26:29 +000010314 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010316 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010317
Bram Moolenaar33570922005-01-25 22:26:29 +000010318 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010319 s = expr;
10320 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010321 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010322 if (*s != NUL) /* check for trailing chars after expr */
10323 {
10324 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010325 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010326 }
10327 if (map)
10328 {
10329 /* map(): replace the list item value */
10330 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010331 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010332 *tv = rettv;
10333 }
10334 else
10335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010336 int error = FALSE;
10337
Bram Moolenaare9a41262005-01-15 22:18:47 +000010338 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010339 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010340 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010341 /* On type error, nothing has been removed; return FAIL to stop the
10342 * loop. The error message was given by get_tv_number_chk(). */
10343 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010344 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010345 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010346 retval = OK;
10347theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010348 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010349 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010350}
10351
10352/*
10353 * "filter()" function
10354 */
10355 static void
10356f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010357 typval_T *argvars;
10358 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010359{
10360 filter_map(argvars, rettv, FALSE);
10361}
10362
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010363/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010364 * "finddir({fname}[, {path}[, {count}]])" function
10365 */
10366 static void
10367f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010368 typval_T *argvars;
10369 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010370{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010371 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010372}
10373
10374/*
10375 * "findfile({fname}[, {path}[, {count}]])" function
10376 */
10377 static void
10378f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010379 typval_T *argvars;
10380 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010381{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010382 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010383}
10384
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010385#ifdef FEAT_FLOAT
10386/*
10387 * "float2nr({float})" function
10388 */
10389 static void
10390f_float2nr(argvars, rettv)
10391 typval_T *argvars;
10392 typval_T *rettv;
10393{
10394 float_T f;
10395
10396 if (get_float_arg(argvars, &f) == OK)
10397 {
10398 if (f < -0x7fffffff)
10399 rettv->vval.v_number = -0x7fffffff;
10400 else if (f > 0x7fffffff)
10401 rettv->vval.v_number = 0x7fffffff;
10402 else
10403 rettv->vval.v_number = (varnumber_T)f;
10404 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010405}
10406
10407/*
10408 * "floor({float})" function
10409 */
10410 static void
10411f_floor(argvars, rettv)
10412 typval_T *argvars;
10413 typval_T *rettv;
10414{
10415 float_T f;
10416
10417 rettv->v_type = VAR_FLOAT;
10418 if (get_float_arg(argvars, &f) == OK)
10419 rettv->vval.v_float = floor(f);
10420 else
10421 rettv->vval.v_float = 0.0;
10422}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010423
10424/*
10425 * "fmod()" function
10426 */
10427 static void
10428f_fmod(argvars, rettv)
10429 typval_T *argvars;
10430 typval_T *rettv;
10431{
10432 float_T fx, fy;
10433
10434 rettv->v_type = VAR_FLOAT;
10435 if (get_float_arg(argvars, &fx) == OK
10436 && get_float_arg(&argvars[1], &fy) == OK)
10437 rettv->vval.v_float = fmod(fx, fy);
10438 else
10439 rettv->vval.v_float = 0.0;
10440}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010441#endif
10442
Bram Moolenaar0d660222005-01-07 21:51:51 +000010443/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010444 * "fnameescape({string})" function
10445 */
10446 static void
10447f_fnameescape(argvars, rettv)
10448 typval_T *argvars;
10449 typval_T *rettv;
10450{
10451 rettv->vval.v_string = vim_strsave_fnameescape(
10452 get_tv_string(&argvars[0]), FALSE);
10453 rettv->v_type = VAR_STRING;
10454}
10455
10456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 * "fnamemodify({fname}, {mods})" function
10458 */
10459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010460f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010461 typval_T *argvars;
10462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463{
10464 char_u *fname;
10465 char_u *mods;
10466 int usedlen = 0;
10467 int len;
10468 char_u *fbuf = NULL;
10469 char_u buf[NUMBUFLEN];
10470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471 fname = get_tv_string_chk(&argvars[0]);
10472 mods = get_tv_string_buf_chk(&argvars[1], buf);
10473 if (fname == NULL || mods == NULL)
10474 fname = NULL;
10475 else
10476 {
10477 len = (int)STRLEN(fname);
10478 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010483 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010485 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 vim_free(fbuf);
10487}
10488
Bram Moolenaar33570922005-01-25 22:26:29 +000010489static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490
10491/*
10492 * "foldclosed()" function
10493 */
10494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010495foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 typval_T *argvars;
10497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 int end;
10499{
10500#ifdef FEAT_FOLDING
10501 linenr_T lnum;
10502 linenr_T first, last;
10503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010504 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10506 {
10507 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10508 {
10509 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 return;
10514 }
10515 }
10516#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010517 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518}
10519
10520/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010521 * "foldclosed()" function
10522 */
10523 static void
10524f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010525 typval_T *argvars;
10526 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010527{
10528 foldclosed_both(argvars, rettv, FALSE);
10529}
10530
10531/*
10532 * "foldclosedend()" function
10533 */
10534 static void
10535f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010536 typval_T *argvars;
10537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010538{
10539 foldclosed_both(argvars, rettv, TRUE);
10540}
10541
10542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 * "foldlevel()" function
10544 */
10545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010547 typval_T *argvars;
10548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549{
10550#ifdef FEAT_FOLDING
10551 linenr_T lnum;
10552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557}
10558
10559/*
10560 * "foldtext()" function
10561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010563f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010564 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566{
10567#ifdef FEAT_FOLDING
10568 linenr_T lnum;
10569 char_u *s;
10570 char_u *r;
10571 int len;
10572 char *txt;
10573#endif
10574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 rettv->v_type = VAR_STRING;
10576 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10579 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10580 <= curbuf->b_ml.ml_line_count
10581 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 {
10583 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010584 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10585 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 {
10587 if (!linewhite(lnum))
10588 break;
10589 ++lnum;
10590 }
10591
10592 /* Find interesting text in this line. */
10593 s = skipwhite(ml_get(lnum));
10594 /* skip C comment-start */
10595 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010598 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010599 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010600 {
10601 s = skipwhite(ml_get(lnum + 1));
10602 if (*s == '*')
10603 s = skipwhite(s + 1);
10604 }
10605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 txt = _("+-%s%3ld lines: ");
10607 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010608 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 + 20 /* for %3ld */
10610 + STRLEN(s))); /* concatenated */
10611 if (r != NULL)
10612 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10614 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10615 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 len = (int)STRLEN(r);
10617 STRCAT(r, s);
10618 /* remove 'foldmarker' and 'commentstring' */
10619 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010620 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 }
10622 }
10623#endif
10624}
10625
10626/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010627 * "foldtextresult(lnum)" function
10628 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010630f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010631 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010632 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010633{
10634#ifdef FEAT_FOLDING
10635 linenr_T lnum;
10636 char_u *text;
10637 char_u buf[51];
10638 foldinfo_T foldinfo;
10639 int fold_count;
10640#endif
10641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010642 rettv->v_type = VAR_STRING;
10643 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010644#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010646 /* treat illegal types and illegal string values for {lnum} the same */
10647 if (lnum < 0)
10648 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010649 fold_count = foldedCount(curwin, lnum, &foldinfo);
10650 if (fold_count > 0)
10651 {
10652 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10653 &foldinfo, buf);
10654 if (text == buf)
10655 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010657 }
10658#endif
10659}
10660
10661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 * "foreground()" function
10663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010666 typval_T *argvars UNUSED;
10667 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669#ifdef FEAT_GUI
10670 if (gui.in_use)
10671 gui_mch_set_foreground();
10672#else
10673# ifdef WIN32
10674 win32_set_foreground();
10675# endif
10676#endif
10677}
10678
10679/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010680 * "function()" function
10681 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010683f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010684 typval_T *argvars;
10685 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010686{
10687 char_u *s;
10688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010690 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010691 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010692 /* Don't check an autoload name for existence here. */
10693 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010694 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010695 else
10696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697 rettv->vval.v_string = vim_strsave(s);
10698 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010699 }
10700}
10701
10702/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010703 * "garbagecollect()" function
10704 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010705 static void
10706f_garbagecollect(argvars, rettv)
10707 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010708 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010709{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010710 /* This is postponed until we are back at the toplevel, because we may be
10711 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10712 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010713
10714 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10715 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010716}
10717
10718/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010719 * "get()" function
10720 */
10721 static void
10722f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010723 typval_T *argvars;
10724 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010725{
Bram Moolenaar33570922005-01-25 22:26:29 +000010726 listitem_T *li;
10727 list_T *l;
10728 dictitem_T *di;
10729 dict_T *d;
10730 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010731
Bram Moolenaare9a41262005-01-15 22:18:47 +000010732 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010733 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010734 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010736 int error = FALSE;
10737
10738 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10739 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010740 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010741 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010742 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010743 else if (argvars[0].v_type == VAR_DICT)
10744 {
10745 if ((d = argvars[0].vval.v_dict) != NULL)
10746 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010747 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010748 if (di != NULL)
10749 tv = &di->di_tv;
10750 }
10751 }
10752 else
10753 EMSG2(_(e_listdictarg), "get()");
10754
10755 if (tv == NULL)
10756 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010757 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010758 copy_tv(&argvars[2], rettv);
10759 }
10760 else
10761 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010762}
10763
Bram Moolenaar342337a2005-07-21 21:11:17 +000010764static 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 +000010765
10766/*
10767 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010768 * Return a range (from start to end) of lines in rettv from the specified
10769 * buffer.
10770 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010771 */
10772 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010773get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010774 buf_T *buf;
10775 linenr_T start;
10776 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010777 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010778 typval_T *rettv;
10779{
10780 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010781
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010782 if (retlist && rettv_list_alloc(rettv) == FAIL)
10783 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010784
10785 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10786 return;
10787
10788 if (!retlist)
10789 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010790 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10791 p = ml_get_buf(buf, start, FALSE);
10792 else
10793 p = (char_u *)"";
10794
10795 rettv->v_type = VAR_STRING;
10796 rettv->vval.v_string = vim_strsave(p);
10797 }
10798 else
10799 {
10800 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010801 return;
10802
10803 if (start < 1)
10804 start = 1;
10805 if (end > buf->b_ml.ml_line_count)
10806 end = buf->b_ml.ml_line_count;
10807 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010808 if (list_append_string(rettv->vval.v_list,
10809 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010810 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010811 }
10812}
10813
10814/*
10815 * "getbufline()" function
10816 */
10817 static void
10818f_getbufline(argvars, rettv)
10819 typval_T *argvars;
10820 typval_T *rettv;
10821{
10822 linenr_T lnum;
10823 linenr_T end;
10824 buf_T *buf;
10825
10826 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10827 ++emsg_off;
10828 buf = get_buf_tv(&argvars[0]);
10829 --emsg_off;
10830
Bram Moolenaar661b1822005-07-28 22:36:45 +000010831 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010832 if (argvars[2].v_type == VAR_UNKNOWN)
10833 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010834 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010835 end = get_tv_lnum_buf(&argvars[2], buf);
10836
Bram Moolenaar342337a2005-07-21 21:11:17 +000010837 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010838}
10839
Bram Moolenaar0d660222005-01-07 21:51:51 +000010840/*
10841 * "getbufvar()" function
10842 */
10843 static void
10844f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010845 typval_T *argvars;
10846 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010847{
10848 buf_T *buf;
10849 buf_T *save_curbuf;
10850 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010851 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010853 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10854 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010855 ++emsg_off;
10856 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010857
10858 rettv->v_type = VAR_STRING;
10859 rettv->vval.v_string = NULL;
10860
10861 if (buf != NULL && varname != NULL)
10862 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010863 /* set curbuf to be our buf, temporarily */
10864 save_curbuf = curbuf;
10865 curbuf = buf;
10866
Bram Moolenaar0d660222005-01-07 21:51:51 +000010867 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010868 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010869 else
10870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010871 if (*varname == NUL)
10872 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10873 * scope prefix before the NUL byte is required by
10874 * find_var_in_ht(). */
10875 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010876 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010877 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010878 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010879 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010880 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010881
10882 /* restore previous notion of curbuf */
10883 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010884 }
10885
10886 --emsg_off;
10887}
10888
10889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 * "getchar()" function
10891 */
10892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010893f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010894 typval_T *argvars;
10895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896{
10897 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010898 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010900 /* Position the cursor. Needed after a message that ends in a space. */
10901 windgoto(msg_row, msg_col);
10902
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 ++no_mapping;
10904 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010905 for (;;)
10906 {
10907 if (argvars[0].v_type == VAR_UNKNOWN)
10908 /* getchar(): blocking wait. */
10909 n = safe_vgetc();
10910 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10911 /* getchar(1): only check if char avail */
10912 n = vpeekc();
10913 else if (error || vpeekc() == NUL)
10914 /* illegal argument or getchar(0) and no char avail: return zero */
10915 n = 0;
10916 else
10917 /* getchar(0) and char avail: return char */
10918 n = safe_vgetc();
10919 if (n == K_IGNORE)
10920 continue;
10921 break;
10922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 --no_mapping;
10924 --allow_keys;
10925
Bram Moolenaar219b8702006-11-01 14:32:36 +000010926 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10927 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10928 vimvars[VV_MOUSE_COL].vv_nr = 0;
10929
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010930 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 if (IS_SPECIAL(n) || mod_mask != 0)
10932 {
10933 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10934 int i = 0;
10935
10936 /* Turn a special key into three bytes, plus modifier. */
10937 if (mod_mask != 0)
10938 {
10939 temp[i++] = K_SPECIAL;
10940 temp[i++] = KS_MODIFIER;
10941 temp[i++] = mod_mask;
10942 }
10943 if (IS_SPECIAL(n))
10944 {
10945 temp[i++] = K_SPECIAL;
10946 temp[i++] = K_SECOND(n);
10947 temp[i++] = K_THIRD(n);
10948 }
10949#ifdef FEAT_MBYTE
10950 else if (has_mbyte)
10951 i += (*mb_char2bytes)(n, temp + i);
10952#endif
10953 else
10954 temp[i++] = n;
10955 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010956 rettv->v_type = VAR_STRING;
10957 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010958
10959#ifdef FEAT_MOUSE
10960 if (n == K_LEFTMOUSE
10961 || n == K_LEFTMOUSE_NM
10962 || n == K_LEFTDRAG
10963 || n == K_LEFTRELEASE
10964 || n == K_LEFTRELEASE_NM
10965 || n == K_MIDDLEMOUSE
10966 || n == K_MIDDLEDRAG
10967 || n == K_MIDDLERELEASE
10968 || n == K_RIGHTMOUSE
10969 || n == K_RIGHTDRAG
10970 || n == K_RIGHTRELEASE
10971 || n == K_X1MOUSE
10972 || n == K_X1DRAG
10973 || n == K_X1RELEASE
10974 || n == K_X2MOUSE
10975 || n == K_X2DRAG
10976 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020010977 || n == K_MOUSELEFT
10978 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000010979 || n == K_MOUSEDOWN
10980 || n == K_MOUSEUP)
10981 {
10982 int row = mouse_row;
10983 int col = mouse_col;
10984 win_T *win;
10985 linenr_T lnum;
10986# ifdef FEAT_WINDOWS
10987 win_T *wp;
10988# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010989 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010990
10991 if (row >= 0 && col >= 0)
10992 {
10993 /* Find the window at the mouse coordinates and compute the
10994 * text position. */
10995 win = mouse_find_win(&row, &col);
10996 (void)mouse_comp_pos(win, &row, &col, &lnum);
10997# ifdef FEAT_WINDOWS
10998 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010999 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011000# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011001 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011002 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11003 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11004 }
11005 }
11006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 }
11008}
11009
11010/*
11011 * "getcharmod()" function
11012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011015 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011018 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019}
11020
11021/*
11022 * "getcmdline()" function
11023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011026 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011029 rettv->v_type = VAR_STRING;
11030 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031}
11032
11033/*
11034 * "getcmdpos()" function
11035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011038 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042}
11043
11044/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011045 * "getcmdtype()" function
11046 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011047 static void
11048f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011049 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011050 typval_T *rettv;
11051{
11052 rettv->v_type = VAR_STRING;
11053 rettv->vval.v_string = alloc(2);
11054 if (rettv->vval.v_string != NULL)
11055 {
11056 rettv->vval.v_string[0] = get_cmdline_type();
11057 rettv->vval.v_string[1] = NUL;
11058 }
11059}
11060
11061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 * "getcwd()" function
11063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011066 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068{
11069 char_u cwd[MAXPATHL];
11070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 else
11075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011078 if (rettv->vval.v_string != NULL)
11079 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080#endif
11081 }
11082}
11083
11084/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011085 * "getfontname()" function
11086 */
11087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011089 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011090 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011091{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011092 rettv->v_type = VAR_STRING;
11093 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011094#ifdef FEAT_GUI
11095 if (gui.in_use)
11096 {
11097 GuiFont font;
11098 char_u *name = NULL;
11099
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011100 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011101 {
11102 /* Get the "Normal" font. Either the name saved by
11103 * hl_set_font_name() or from the font ID. */
11104 font = gui.norm_font;
11105 name = hl_get_font_name();
11106 }
11107 else
11108 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011110 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11111 return;
11112 font = gui_mch_get_font(name, FALSE);
11113 if (font == NOFONT)
11114 return; /* Invalid font name, return empty string. */
11115 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011117 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011118 gui_mch_free_font(font);
11119 }
11120#endif
11121}
11122
11123/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011124 * "getfperm({fname})" function
11125 */
11126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011128 typval_T *argvars;
11129 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011130{
11131 char_u *fname;
11132 struct stat st;
11133 char_u *perm = NULL;
11134 char_u flags[] = "rwx";
11135 int i;
11136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011139 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011140 if (mch_stat((char *)fname, &st) >= 0)
11141 {
11142 perm = vim_strsave((char_u *)"---------");
11143 if (perm != NULL)
11144 {
11145 for (i = 0; i < 9; i++)
11146 {
11147 if (st.st_mode & (1 << (8 - i)))
11148 perm[i] = flags[i % 3];
11149 }
11150 }
11151 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011153}
11154
11155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 * "getfsize({fname})" function
11157 */
11158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011159f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011160 typval_T *argvars;
11161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162{
11163 char_u *fname;
11164 struct stat st;
11165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011166 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011168 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169
11170 if (mch_stat((char *)fname, &st) >= 0)
11171 {
11172 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011176 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011177
11178 /* non-perfect check for overflow */
11179 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11180 rettv->vval.v_number = -2;
11181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 }
11183 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185}
11186
11187/*
11188 * "getftime({fname})" function
11189 */
11190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011191f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011192 typval_T *argvars;
11193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194{
11195 char_u *fname;
11196 struct stat st;
11197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011198 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199
11200 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011201 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011203 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204}
11205
11206/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011207 * "getftype({fname})" function
11208 */
11209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011211 typval_T *argvars;
11212 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011213{
11214 char_u *fname;
11215 struct stat st;
11216 char_u *type = NULL;
11217 char *t;
11218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011222 if (mch_lstat((char *)fname, &st) >= 0)
11223 {
11224#ifdef S_ISREG
11225 if (S_ISREG(st.st_mode))
11226 t = "file";
11227 else if (S_ISDIR(st.st_mode))
11228 t = "dir";
11229# ifdef S_ISLNK
11230 else if (S_ISLNK(st.st_mode))
11231 t = "link";
11232# endif
11233# ifdef S_ISBLK
11234 else if (S_ISBLK(st.st_mode))
11235 t = "bdev";
11236# endif
11237# ifdef S_ISCHR
11238 else if (S_ISCHR(st.st_mode))
11239 t = "cdev";
11240# endif
11241# ifdef S_ISFIFO
11242 else if (S_ISFIFO(st.st_mode))
11243 t = "fifo";
11244# endif
11245# ifdef S_ISSOCK
11246 else if (S_ISSOCK(st.st_mode))
11247 t = "fifo";
11248# endif
11249 else
11250 t = "other";
11251#else
11252# ifdef S_IFMT
11253 switch (st.st_mode & S_IFMT)
11254 {
11255 case S_IFREG: t = "file"; break;
11256 case S_IFDIR: t = "dir"; break;
11257# ifdef S_IFLNK
11258 case S_IFLNK: t = "link"; break;
11259# endif
11260# ifdef S_IFBLK
11261 case S_IFBLK: t = "bdev"; break;
11262# endif
11263# ifdef S_IFCHR
11264 case S_IFCHR: t = "cdev"; break;
11265# endif
11266# ifdef S_IFIFO
11267 case S_IFIFO: t = "fifo"; break;
11268# endif
11269# ifdef S_IFSOCK
11270 case S_IFSOCK: t = "socket"; break;
11271# endif
11272 default: t = "other";
11273 }
11274# else
11275 if (mch_isdir(fname))
11276 t = "dir";
11277 else
11278 t = "file";
11279# endif
11280#endif
11281 type = vim_strsave((char_u *)t);
11282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011284}
11285
11286/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011287 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011288 */
11289 static void
11290f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011291 typval_T *argvars;
11292 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011293{
11294 linenr_T lnum;
11295 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011296 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011297
11298 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011299 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011300 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011301 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011302 retlist = FALSE;
11303 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011304 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011305 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011306 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011307 retlist = TRUE;
11308 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011309
Bram Moolenaar342337a2005-07-21 21:11:17 +000011310 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011311}
11312
11313/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011314 * "getmatches()" function
11315 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011316 static void
11317f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011318 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011319 typval_T *rettv;
11320{
11321#ifdef FEAT_SEARCH_EXTRA
11322 dict_T *dict;
11323 matchitem_T *cur = curwin->w_match_head;
11324
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011325 if (rettv_list_alloc(rettv) == OK)
11326 {
11327 while (cur != NULL)
11328 {
11329 dict = dict_alloc();
11330 if (dict == NULL)
11331 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011332 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11333 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11334 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11335 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11336 list_append_dict(rettv->vval.v_list, dict);
11337 cur = cur->next;
11338 }
11339 }
11340#endif
11341}
11342
11343/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011344 * "getpid()" function
11345 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011346 static void
11347f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011348 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011349 typval_T *rettv;
11350{
11351 rettv->vval.v_number = mch_get_pid();
11352}
11353
11354/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011355 * "getpos(string)" function
11356 */
11357 static void
11358f_getpos(argvars, rettv)
11359 typval_T *argvars;
11360 typval_T *rettv;
11361{
11362 pos_T *fp;
11363 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011364 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011365
11366 if (rettv_list_alloc(rettv) == OK)
11367 {
11368 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011369 fp = var2fpos(&argvars[0], TRUE, &fnum);
11370 if (fnum != -1)
11371 list_append_number(l, (varnumber_T)fnum);
11372 else
11373 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011374 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11375 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011376 list_append_number(l, (fp != NULL)
11377 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011378 : (varnumber_T)0);
11379 list_append_number(l,
11380#ifdef FEAT_VIRTUALEDIT
11381 (fp != NULL) ? (varnumber_T)fp->coladd :
11382#endif
11383 (varnumber_T)0);
11384 }
11385 else
11386 rettv->vval.v_number = FALSE;
11387}
11388
11389/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011390 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011391 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011392 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011393f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011394 typval_T *argvars UNUSED;
11395 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011396{
11397#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011398 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011399#endif
11400
Bram Moolenaar2641f772005-03-25 21:58:17 +000011401#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011402 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011403 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011404 wp = NULL;
11405 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11406 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011407 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011408 if (wp == NULL)
11409 return;
11410 }
11411
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011412 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011413 }
11414#endif
11415}
11416
11417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 * "getreg()" function
11419 */
11420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011422 typval_T *argvars;
11423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424{
11425 char_u *strregname;
11426 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011427 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011428 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011430 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011432 strregname = get_tv_string_chk(&argvars[0]);
11433 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011434 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011438 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 regname = (strregname == NULL ? '"' : *strregname);
11440 if (regname == 0)
11441 regname = '"';
11442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011444 rettv->vval.v_string = error ? NULL :
11445 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446}
11447
11448/*
11449 * "getregtype()" function
11450 */
11451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011453 typval_T *argvars;
11454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455{
11456 char_u *strregname;
11457 int regname;
11458 char_u buf[NUMBUFLEN + 2];
11459 long reglen = 0;
11460
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011461 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011462 {
11463 strregname = get_tv_string_chk(&argvars[0]);
11464 if (strregname == NULL) /* type error; errmsg already given */
11465 {
11466 rettv->v_type = VAR_STRING;
11467 rettv->vval.v_string = NULL;
11468 return;
11469 }
11470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 else
11472 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011473 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474
11475 regname = (strregname == NULL ? '"' : *strregname);
11476 if (regname == 0)
11477 regname = '"';
11478
11479 buf[0] = NUL;
11480 buf[1] = NUL;
11481 switch (get_reg_type(regname, &reglen))
11482 {
11483 case MLINE: buf[0] = 'V'; break;
11484 case MCHAR: buf[0] = 'v'; break;
11485#ifdef FEAT_VISUAL
11486 case MBLOCK:
11487 buf[0] = Ctrl_V;
11488 sprintf((char *)buf + 1, "%ld", reglen + 1);
11489 break;
11490#endif
11491 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->v_type = VAR_STRING;
11493 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494}
11495
11496/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011497 * "gettabvar()" function
11498 */
11499 static void
11500f_gettabvar(argvars, rettv)
11501 typval_T *argvars;
11502 typval_T *rettv;
11503{
11504 tabpage_T *tp;
11505 dictitem_T *v;
11506 char_u *varname;
11507
11508 rettv->v_type = VAR_STRING;
11509 rettv->vval.v_string = NULL;
11510
11511 varname = get_tv_string_chk(&argvars[1]);
11512 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11513 if (tp != NULL && varname != NULL)
11514 {
11515 /* look up the variable */
11516 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11517 if (v != NULL)
11518 copy_tv(&v->di_tv, rettv);
11519 }
11520}
11521
11522/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011523 * "gettabwinvar()" function
11524 */
11525 static void
11526f_gettabwinvar(argvars, rettv)
11527 typval_T *argvars;
11528 typval_T *rettv;
11529{
11530 getwinvar(argvars, rettv, 1);
11531}
11532
11533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 * "getwinposx()" function
11535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011538 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542#ifdef FEAT_GUI
11543 if (gui.in_use)
11544 {
11545 int x, y;
11546
11547 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 }
11550#endif
11551}
11552
11553/*
11554 * "getwinposy()" function
11555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011557f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011558 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562#ifdef FEAT_GUI
11563 if (gui.in_use)
11564 {
11565 int x, y;
11566
11567 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011568 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 }
11570#endif
11571}
11572
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011573/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011574 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011575 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011576 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011577find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011578 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011579 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011580{
11581#ifdef FEAT_WINDOWS
11582 win_T *wp;
11583#endif
11584 int nr;
11585
11586 nr = get_tv_number_chk(vp, NULL);
11587
11588#ifdef FEAT_WINDOWS
11589 if (nr < 0)
11590 return NULL;
11591 if (nr == 0)
11592 return curwin;
11593
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011594 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11595 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011596 if (--nr <= 0)
11597 break;
11598 return wp;
11599#else
11600 if (nr == 0 || nr == 1)
11601 return curwin;
11602 return NULL;
11603#endif
11604}
11605
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606/*
11607 * "getwinvar()" function
11608 */
11609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011611 typval_T *argvars;
11612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011614 getwinvar(argvars, rettv, 0);
11615}
11616
11617/*
11618 * getwinvar() and gettabwinvar()
11619 */
11620 static void
11621getwinvar(argvars, rettv, off)
11622 typval_T *argvars;
11623 typval_T *rettv;
11624 int off; /* 1 for gettabwinvar() */
11625{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 win_T *win, *oldcurwin;
11627 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011628 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011629 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011631#ifdef FEAT_WINDOWS
11632 if (off == 1)
11633 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11634 else
11635 tp = curtab;
11636#endif
11637 win = find_win_by_nr(&argvars[off], tp);
11638 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011639 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011641 rettv->v_type = VAR_STRING;
11642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643
11644 if (win != NULL && varname != NULL)
11645 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011646 /* Set curwin to be our win, temporarily. Also set curbuf, so
11647 * that we can get buffer-local options. */
11648 oldcurwin = curwin;
11649 curwin = win;
11650 curbuf = win->w_buffer;
11651
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 else
11655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011656 if (*varname == NUL)
11657 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11658 * scope prefix before the NUL byte is required by
11659 * find_var_in_ht(). */
11660 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011662 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011664 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011666
11667 /* restore previous notion of curwin */
11668 curwin = oldcurwin;
11669 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 }
11671
11672 --emsg_off;
11673}
11674
11675/*
11676 * "glob()" function
11677 */
11678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011679f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011680 typval_T *argvars;
11681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011683 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011685 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011687 /* When the optional second argument is non-zero, don't remove matches
11688 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11689 if (argvars[1].v_type != VAR_UNKNOWN
11690 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011691 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011692 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011693 if (!error)
11694 {
11695 ExpandInit(&xpc);
11696 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011697 if (p_wic)
11698 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011699 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011700 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011701 }
11702 else
11703 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704}
11705
11706/*
11707 * "globpath()" function
11708 */
11709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011710f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011711 typval_T *argvars;
11712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011714 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011716 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011717 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011719 /* When the optional second argument is non-zero, don't remove matches
11720 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11721 if (argvars[2].v_type != VAR_UNKNOWN
11722 && get_tv_number_chk(&argvars[2], &error))
11723 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011724 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011725 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011726 rettv->vval.v_string = NULL;
11727 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011728 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11729 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730}
11731
11732/*
11733 * "has()" function
11734 */
11735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011736f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011737 typval_T *argvars;
11738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739{
11740 int i;
11741 char_u *name;
11742 int n = FALSE;
11743 static char *(has_list[]) =
11744 {
11745#ifdef AMIGA
11746 "amiga",
11747# ifdef FEAT_ARP
11748 "arp",
11749# endif
11750#endif
11751#ifdef __BEOS__
11752 "beos",
11753#endif
11754#ifdef MSDOS
11755# ifdef DJGPP
11756 "dos32",
11757# else
11758 "dos16",
11759# endif
11760#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011761#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 "mac",
11763#endif
11764#if defined(MACOS_X_UNIX)
11765 "macunix",
11766#endif
11767#ifdef OS2
11768 "os2",
11769#endif
11770#ifdef __QNX__
11771 "qnx",
11772#endif
11773#ifdef RISCOS
11774 "riscos",
11775#endif
11776#ifdef UNIX
11777 "unix",
11778#endif
11779#ifdef VMS
11780 "vms",
11781#endif
11782#ifdef WIN16
11783 "win16",
11784#endif
11785#ifdef WIN32
11786 "win32",
11787#endif
11788#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11789 "win32unix",
11790#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011791#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 "win64",
11793#endif
11794#ifdef EBCDIC
11795 "ebcdic",
11796#endif
11797#ifndef CASE_INSENSITIVE_FILENAME
11798 "fname_case",
11799#endif
11800#ifdef FEAT_ARABIC
11801 "arabic",
11802#endif
11803#ifdef FEAT_AUTOCMD
11804 "autocmd",
11805#endif
11806#ifdef FEAT_BEVAL
11807 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011808# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11809 "balloon_multiline",
11810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811#endif
11812#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11813 "builtin_terms",
11814# ifdef ALL_BUILTIN_TCAPS
11815 "all_builtin_terms",
11816# endif
11817#endif
11818#ifdef FEAT_BYTEOFF
11819 "byte_offset",
11820#endif
11821#ifdef FEAT_CINDENT
11822 "cindent",
11823#endif
11824#ifdef FEAT_CLIENTSERVER
11825 "clientserver",
11826#endif
11827#ifdef FEAT_CLIPBOARD
11828 "clipboard",
11829#endif
11830#ifdef FEAT_CMDL_COMPL
11831 "cmdline_compl",
11832#endif
11833#ifdef FEAT_CMDHIST
11834 "cmdline_hist",
11835#endif
11836#ifdef FEAT_COMMENTS
11837 "comments",
11838#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011839#ifdef FEAT_CONCEAL
11840 "conceal",
11841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842#ifdef FEAT_CRYPT
11843 "cryptv",
11844#endif
11845#ifdef FEAT_CSCOPE
11846 "cscope",
11847#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011848#ifdef FEAT_CURSORBIND
11849 "cursorbind",
11850#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011851#ifdef CURSOR_SHAPE
11852 "cursorshape",
11853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854#ifdef DEBUG
11855 "debug",
11856#endif
11857#ifdef FEAT_CON_DIALOG
11858 "dialog_con",
11859#endif
11860#ifdef FEAT_GUI_DIALOG
11861 "dialog_gui",
11862#endif
11863#ifdef FEAT_DIFF
11864 "diff",
11865#endif
11866#ifdef FEAT_DIGRAPHS
11867 "digraphs",
11868#endif
11869#ifdef FEAT_DND
11870 "dnd",
11871#endif
11872#ifdef FEAT_EMACS_TAGS
11873 "emacs_tags",
11874#endif
11875 "eval", /* always present, of course! */
11876#ifdef FEAT_EX_EXTRA
11877 "ex_extra",
11878#endif
11879#ifdef FEAT_SEARCH_EXTRA
11880 "extra_search",
11881#endif
11882#ifdef FEAT_FKMAP
11883 "farsi",
11884#endif
11885#ifdef FEAT_SEARCHPATH
11886 "file_in_path",
11887#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011888#if defined(UNIX) && !defined(USE_SYSTEM)
11889 "filterpipe",
11890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891#ifdef FEAT_FIND_ID
11892 "find_in_path",
11893#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011894#ifdef FEAT_FLOAT
11895 "float",
11896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897#ifdef FEAT_FOLDING
11898 "folding",
11899#endif
11900#ifdef FEAT_FOOTER
11901 "footer",
11902#endif
11903#if !defined(USE_SYSTEM) && defined(UNIX)
11904 "fork",
11905#endif
11906#ifdef FEAT_GETTEXT
11907 "gettext",
11908#endif
11909#ifdef FEAT_GUI
11910 "gui",
11911#endif
11912#ifdef FEAT_GUI_ATHENA
11913# ifdef FEAT_GUI_NEXTAW
11914 "gui_neXtaw",
11915# else
11916 "gui_athena",
11917# endif
11918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919#ifdef FEAT_GUI_GTK
11920 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011923#ifdef FEAT_GUI_GNOME
11924 "gui_gnome",
11925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926#ifdef FEAT_GUI_MAC
11927 "gui_mac",
11928#endif
11929#ifdef FEAT_GUI_MOTIF
11930 "gui_motif",
11931#endif
11932#ifdef FEAT_GUI_PHOTON
11933 "gui_photon",
11934#endif
11935#ifdef FEAT_GUI_W16
11936 "gui_win16",
11937#endif
11938#ifdef FEAT_GUI_W32
11939 "gui_win32",
11940#endif
11941#ifdef FEAT_HANGULIN
11942 "hangul_input",
11943#endif
11944#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11945 "iconv",
11946#endif
11947#ifdef FEAT_INS_EXPAND
11948 "insert_expand",
11949#endif
11950#ifdef FEAT_JUMPLIST
11951 "jumplist",
11952#endif
11953#ifdef FEAT_KEYMAP
11954 "keymap",
11955#endif
11956#ifdef FEAT_LANGMAP
11957 "langmap",
11958#endif
11959#ifdef FEAT_LIBCALL
11960 "libcall",
11961#endif
11962#ifdef FEAT_LINEBREAK
11963 "linebreak",
11964#endif
11965#ifdef FEAT_LISP
11966 "lispindent",
11967#endif
11968#ifdef FEAT_LISTCMDS
11969 "listcmds",
11970#endif
11971#ifdef FEAT_LOCALMAP
11972 "localmap",
11973#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011974#ifdef FEAT_LUA
11975# ifndef DYNAMIC_LUA
11976 "lua",
11977# endif
11978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979#ifdef FEAT_MENU
11980 "menu",
11981#endif
11982#ifdef FEAT_SESSION
11983 "mksession",
11984#endif
11985#ifdef FEAT_MODIFY_FNAME
11986 "modify_fname",
11987#endif
11988#ifdef FEAT_MOUSE
11989 "mouse",
11990#endif
11991#ifdef FEAT_MOUSESHAPE
11992 "mouseshape",
11993#endif
11994#if defined(UNIX) || defined(VMS)
11995# ifdef FEAT_MOUSE_DEC
11996 "mouse_dec",
11997# endif
11998# ifdef FEAT_MOUSE_GPM
11999 "mouse_gpm",
12000# endif
12001# ifdef FEAT_MOUSE_JSB
12002 "mouse_jsbterm",
12003# endif
12004# ifdef FEAT_MOUSE_NET
12005 "mouse_netterm",
12006# endif
12007# ifdef FEAT_MOUSE_PTERM
12008 "mouse_pterm",
12009# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012010# ifdef FEAT_SYSMOUSE
12011 "mouse_sysmouse",
12012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013# ifdef FEAT_MOUSE_XTERM
12014 "mouse_xterm",
12015# endif
12016#endif
12017#ifdef FEAT_MBYTE
12018 "multi_byte",
12019#endif
12020#ifdef FEAT_MBYTE_IME
12021 "multi_byte_ime",
12022#endif
12023#ifdef FEAT_MULTI_LANG
12024 "multi_lang",
12025#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012026#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012027#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012028 "mzscheme",
12029#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031#ifdef FEAT_OLE
12032 "ole",
12033#endif
12034#ifdef FEAT_OSFILETYPE
12035 "osfiletype",
12036#endif
12037#ifdef FEAT_PATH_EXTRA
12038 "path_extra",
12039#endif
12040#ifdef FEAT_PERL
12041#ifndef DYNAMIC_PERL
12042 "perl",
12043#endif
12044#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012045#ifdef FEAT_PERSISTENT_UNDO
12046 "persistent_undo",
12047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048#ifdef FEAT_PYTHON
12049#ifndef DYNAMIC_PYTHON
12050 "python",
12051#endif
12052#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012053#ifdef FEAT_PYTHON3
12054#ifndef DYNAMIC_PYTHON3
12055 "python3",
12056#endif
12057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058#ifdef FEAT_POSTSCRIPT
12059 "postscript",
12060#endif
12061#ifdef FEAT_PRINTER
12062 "printer",
12063#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012064#ifdef FEAT_PROFILE
12065 "profile",
12066#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012067#ifdef FEAT_RELTIME
12068 "reltime",
12069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070#ifdef FEAT_QUICKFIX
12071 "quickfix",
12072#endif
12073#ifdef FEAT_RIGHTLEFT
12074 "rightleft",
12075#endif
12076#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12077 "ruby",
12078#endif
12079#ifdef FEAT_SCROLLBIND
12080 "scrollbind",
12081#endif
12082#ifdef FEAT_CMDL_INFO
12083 "showcmd",
12084 "cmdline_info",
12085#endif
12086#ifdef FEAT_SIGNS
12087 "signs",
12088#endif
12089#ifdef FEAT_SMARTINDENT
12090 "smartindent",
12091#endif
12092#ifdef FEAT_SNIFF
12093 "sniff",
12094#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012095#ifdef STARTUPTIME
12096 "startuptime",
12097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098#ifdef FEAT_STL_OPT
12099 "statusline",
12100#endif
12101#ifdef FEAT_SUN_WORKSHOP
12102 "sun_workshop",
12103#endif
12104#ifdef FEAT_NETBEANS_INTG
12105 "netbeans_intg",
12106#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012107#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012108 "spell",
12109#endif
12110#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111 "syntax",
12112#endif
12113#if defined(USE_SYSTEM) || !defined(UNIX)
12114 "system",
12115#endif
12116#ifdef FEAT_TAG_BINS
12117 "tag_binary",
12118#endif
12119#ifdef FEAT_TAG_OLDSTATIC
12120 "tag_old_static",
12121#endif
12122#ifdef FEAT_TAG_ANYWHITE
12123 "tag_any_white",
12124#endif
12125#ifdef FEAT_TCL
12126# ifndef DYNAMIC_TCL
12127 "tcl",
12128# endif
12129#endif
12130#ifdef TERMINFO
12131 "terminfo",
12132#endif
12133#ifdef FEAT_TERMRESPONSE
12134 "termresponse",
12135#endif
12136#ifdef FEAT_TEXTOBJ
12137 "textobjects",
12138#endif
12139#ifdef HAVE_TGETENT
12140 "tgetent",
12141#endif
12142#ifdef FEAT_TITLE
12143 "title",
12144#endif
12145#ifdef FEAT_TOOLBAR
12146 "toolbar",
12147#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012148#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12149 "unnamedplus",
12150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151#ifdef FEAT_USR_CMDS
12152 "user-commands", /* was accidentally included in 5.4 */
12153 "user_commands",
12154#endif
12155#ifdef FEAT_VIMINFO
12156 "viminfo",
12157#endif
12158#ifdef FEAT_VERTSPLIT
12159 "vertsplit",
12160#endif
12161#ifdef FEAT_VIRTUALEDIT
12162 "virtualedit",
12163#endif
12164#ifdef FEAT_VISUAL
12165 "visual",
12166#endif
12167#ifdef FEAT_VISUALEXTRA
12168 "visualextra",
12169#endif
12170#ifdef FEAT_VREPLACE
12171 "vreplace",
12172#endif
12173#ifdef FEAT_WILDIGN
12174 "wildignore",
12175#endif
12176#ifdef FEAT_WILDMENU
12177 "wildmenu",
12178#endif
12179#ifdef FEAT_WINDOWS
12180 "windows",
12181#endif
12182#ifdef FEAT_WAK
12183 "winaltkeys",
12184#endif
12185#ifdef FEAT_WRITEBACKUP
12186 "writebackup",
12187#endif
12188#ifdef FEAT_XIM
12189 "xim",
12190#endif
12191#ifdef FEAT_XFONTSET
12192 "xfontset",
12193#endif
12194#ifdef USE_XSMP
12195 "xsmp",
12196#endif
12197#ifdef USE_XSMP_INTERACT
12198 "xsmp_interact",
12199#endif
12200#ifdef FEAT_XCLIPBOARD
12201 "xterm_clipboard",
12202#endif
12203#ifdef FEAT_XTERM_SAVE
12204 "xterm_save",
12205#endif
12206#if defined(UNIX) && defined(FEAT_X11)
12207 "X11",
12208#endif
12209 NULL
12210 };
12211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012212 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 for (i = 0; has_list[i] != NULL; ++i)
12214 if (STRICMP(name, has_list[i]) == 0)
12215 {
12216 n = TRUE;
12217 break;
12218 }
12219
12220 if (n == FALSE)
12221 {
12222 if (STRNICMP(name, "patch", 5) == 0)
12223 n = has_patch(atoi((char *)name + 5));
12224 else if (STRICMP(name, "vim_starting") == 0)
12225 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012226#ifdef FEAT_MBYTE
12227 else if (STRICMP(name, "multi_byte_encoding") == 0)
12228 n = has_mbyte;
12229#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012230#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12231 else if (STRICMP(name, "balloon_multiline") == 0)
12232 n = multiline_balloon_available();
12233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234#ifdef DYNAMIC_TCL
12235 else if (STRICMP(name, "tcl") == 0)
12236 n = tcl_enabled(FALSE);
12237#endif
12238#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12239 else if (STRICMP(name, "iconv") == 0)
12240 n = iconv_enabled(FALSE);
12241#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012242#ifdef DYNAMIC_LUA
12243 else if (STRICMP(name, "lua") == 0)
12244 n = lua_enabled(FALSE);
12245#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012246#ifdef DYNAMIC_MZSCHEME
12247 else if (STRICMP(name, "mzscheme") == 0)
12248 n = mzscheme_enabled(FALSE);
12249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250#ifdef DYNAMIC_RUBY
12251 else if (STRICMP(name, "ruby") == 0)
12252 n = ruby_enabled(FALSE);
12253#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012254#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255#ifdef DYNAMIC_PYTHON
12256 else if (STRICMP(name, "python") == 0)
12257 n = python_enabled(FALSE);
12258#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012259#endif
12260#ifdef FEAT_PYTHON3
12261#ifdef DYNAMIC_PYTHON3
12262 else if (STRICMP(name, "python3") == 0)
12263 n = python3_enabled(FALSE);
12264#endif
12265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266#ifdef DYNAMIC_PERL
12267 else if (STRICMP(name, "perl") == 0)
12268 n = perl_enabled(FALSE);
12269#endif
12270#ifdef FEAT_GUI
12271 else if (STRICMP(name, "gui_running") == 0)
12272 n = (gui.in_use || gui.starting);
12273# ifdef FEAT_GUI_W32
12274 else if (STRICMP(name, "gui_win32s") == 0)
12275 n = gui_is_win32s();
12276# endif
12277# ifdef FEAT_BROWSE
12278 else if (STRICMP(name, "browse") == 0)
12279 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12280# endif
12281#endif
12282#ifdef FEAT_SYN_HL
12283 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012284 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285#endif
12286#if defined(WIN3264)
12287 else if (STRICMP(name, "win95") == 0)
12288 n = mch_windows95();
12289#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012290#ifdef FEAT_NETBEANS_INTG
12291 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012292 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 }
12295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012296 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297}
12298
12299/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012300 * "has_key()" function
12301 */
12302 static void
12303f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012304 typval_T *argvars;
12305 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012306{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012307 if (argvars[0].v_type != VAR_DICT)
12308 {
12309 EMSG(_(e_dictreq));
12310 return;
12311 }
12312 if (argvars[0].vval.v_dict == NULL)
12313 return;
12314
12315 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012316 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012317}
12318
12319/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012320 * "haslocaldir()" function
12321 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012322 static void
12323f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012324 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012325 typval_T *rettv;
12326{
12327 rettv->vval.v_number = (curwin->w_localdir != NULL);
12328}
12329
12330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 * "hasmapto()" function
12332 */
12333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012334f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012335 typval_T *argvars;
12336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337{
12338 char_u *name;
12339 char_u *mode;
12340 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012341 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012344 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 mode = (char_u *)"nvo";
12346 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012349 if (argvars[2].v_type != VAR_UNKNOWN)
12350 abbr = get_tv_number(&argvars[2]);
12351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352
Bram Moolenaar2c932302006-03-18 21:42:09 +000012353 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012354 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012356 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357}
12358
12359/*
12360 * "histadd()" function
12361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012363f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012364 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366{
12367#ifdef FEAT_CMDHIST
12368 int histype;
12369 char_u *str;
12370 char_u buf[NUMBUFLEN];
12371#endif
12372
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012373 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 if (check_restricted() || check_secure())
12375 return;
12376#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012377 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12378 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 if (histype >= 0)
12380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012381 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 if (*str != NUL)
12383 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012384 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012386 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387 return;
12388 }
12389 }
12390#endif
12391}
12392
12393/*
12394 * "histdel()" function
12395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012397f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012398 typval_T *argvars UNUSED;
12399 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400{
12401#ifdef FEAT_CMDHIST
12402 int n;
12403 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012404 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012406 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12407 if (str == NULL)
12408 n = 0;
12409 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012411 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012412 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012414 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416 else
12417 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012418 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012419 get_tv_string_buf(&argvars[1], buf));
12420 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421#endif
12422}
12423
12424/*
12425 * "histget()" function
12426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012429 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431{
12432#ifdef FEAT_CMDHIST
12433 int type;
12434 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012435 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012437 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12438 if (str == NULL)
12439 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012441 {
12442 type = get_histtype(str);
12443 if (argvars[1].v_type == VAR_UNKNOWN)
12444 idx = get_history_idx(type);
12445 else
12446 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12447 /* -1 on type error */
12448 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012451 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012453 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454}
12455
12456/*
12457 * "histnr()" function
12458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012460f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012461 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463{
12464 int i;
12465
12466#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012467 char_u *history = get_tv_string_chk(&argvars[0]);
12468
12469 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470 if (i >= HIST_CMD && i < HIST_COUNT)
12471 i = get_history_idx(i);
12472 else
12473#endif
12474 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476}
12477
12478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479 * "highlightID(name)" function
12480 */
12481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012482f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012483 typval_T *argvars;
12484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487}
12488
12489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012490 * "highlight_exists()" function
12491 */
12492 static void
12493f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012494 typval_T *argvars;
12495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012496{
12497 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12498}
12499
12500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 * "hostname()" function
12502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012504f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012505 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507{
12508 char_u hostname[256];
12509
12510 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012511 rettv->v_type = VAR_STRING;
12512 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513}
12514
12515/*
12516 * iconv() function
12517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012519f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012520 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522{
12523#ifdef FEAT_MBYTE
12524 char_u buf1[NUMBUFLEN];
12525 char_u buf2[NUMBUFLEN];
12526 char_u *from, *to, *str;
12527 vimconv_T vimconv;
12528#endif
12529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012530 rettv->v_type = VAR_STRING;
12531 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532
12533#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534 str = get_tv_string(&argvars[0]);
12535 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12536 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 vimconv.vc_type = CONV_NONE;
12538 convert_setup(&vimconv, from, to);
12539
12540 /* If the encodings are equal, no conversion needed. */
12541 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012542 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012544 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545
12546 convert_setup(&vimconv, NULL, NULL);
12547 vim_free(from);
12548 vim_free(to);
12549#endif
12550}
12551
12552/*
12553 * "indent()" function
12554 */
12555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012556f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012557 typval_T *argvars;
12558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559{
12560 linenr_T lnum;
12561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012562 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012566 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567}
12568
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012569/*
12570 * "index()" function
12571 */
12572 static void
12573f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012574 typval_T *argvars;
12575 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012576{
Bram Moolenaar33570922005-01-25 22:26:29 +000012577 list_T *l;
12578 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012579 long idx = 0;
12580 int ic = FALSE;
12581
12582 rettv->vval.v_number = -1;
12583 if (argvars[0].v_type != VAR_LIST)
12584 {
12585 EMSG(_(e_listreq));
12586 return;
12587 }
12588 l = argvars[0].vval.v_list;
12589 if (l != NULL)
12590 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012591 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012592 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012594 int error = FALSE;
12595
Bram Moolenaar758711c2005-02-02 23:11:38 +000012596 /* Start at specified item. Use the cached index that list_find()
12597 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012598 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012599 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012600 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012601 ic = get_tv_number_chk(&argvars[3], &error);
12602 if (error)
12603 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012604 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012605
Bram Moolenaar758711c2005-02-02 23:11:38 +000012606 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012607 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012608 {
12609 rettv->vval.v_number = idx;
12610 break;
12611 }
12612 }
12613}
12614
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615static int inputsecret_flag = 0;
12616
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012617static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12618
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012620 * This function is used by f_input() and f_inputdialog() functions. The third
12621 * argument to f_input() specifies the type of completion to use at the
12622 * prompt. The third argument to f_inputdialog() specifies the value to return
12623 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624 */
12625 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012626get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012627 typval_T *argvars;
12628 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012629 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012631 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632 char_u *p = NULL;
12633 int c;
12634 char_u buf[NUMBUFLEN];
12635 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012636 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012637 int xp_type = EXPAND_NOTHING;
12638 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012640 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012641 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642
12643#ifdef NO_CONSOLE_INPUT
12644 /* While starting up, there is no place to enter text. */
12645 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647#endif
12648
12649 cmd_silent = FALSE; /* Want to see the prompt. */
12650 if (prompt != NULL)
12651 {
12652 /* Only the part of the message after the last NL is considered as
12653 * prompt for the command line */
12654 p = vim_strrchr(prompt, '\n');
12655 if (p == NULL)
12656 p = prompt;
12657 else
12658 {
12659 ++p;
12660 c = *p;
12661 *p = NUL;
12662 msg_start();
12663 msg_clr_eos();
12664 msg_puts_attr(prompt, echo_attr);
12665 msg_didout = FALSE;
12666 msg_starthere();
12667 *p = c;
12668 }
12669 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012671 if (argvars[1].v_type != VAR_UNKNOWN)
12672 {
12673 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12674 if (defstr != NULL)
12675 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012677 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012678 {
12679 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012680 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012681 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012682
Bram Moolenaar4463f292005-09-25 22:20:24 +000012683 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012684
Bram Moolenaar4463f292005-09-25 22:20:24 +000012685 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12686 if (xp_name == NULL)
12687 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012688
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012689 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012690
Bram Moolenaar4463f292005-09-25 22:20:24 +000012691 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12692 &xp_arg) == FAIL)
12693 return;
12694 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012695 }
12696
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012697 if (defstr != NULL)
12698 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012699 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12700 xp_type, xp_arg);
12701
12702 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012704 /* since the user typed this, no need to wait for return */
12705 need_wait_return = FALSE;
12706 msg_didout = FALSE;
12707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 cmd_silent = cmd_silent_save;
12709}
12710
12711/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012712 * "input()" function
12713 * Also handles inputsecret() when inputsecret is set.
12714 */
12715 static void
12716f_input(argvars, rettv)
12717 typval_T *argvars;
12718 typval_T *rettv;
12719{
12720 get_user_input(argvars, rettv, FALSE);
12721}
12722
12723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 * "inputdialog()" function
12725 */
12726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012727f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012728 typval_T *argvars;
12729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730{
12731#if defined(FEAT_GUI_TEXTDIALOG)
12732 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12733 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12734 {
12735 char_u *message;
12736 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012737 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012739 message = get_tv_string_chk(&argvars[0]);
12740 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012741 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012742 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 else
12744 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012745 if (message != NULL && defstr != NULL
12746 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012747 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749 else
12750 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012751 if (message != NULL && defstr != NULL
12752 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012753 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754 rettv->vval.v_string = vim_strsave(
12755 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012757 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 }
12761 else
12762#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012763 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764}
12765
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012766/*
12767 * "inputlist()" function
12768 */
12769 static void
12770f_inputlist(argvars, rettv)
12771 typval_T *argvars;
12772 typval_T *rettv;
12773{
12774 listitem_T *li;
12775 int selected;
12776 int mouse_used;
12777
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012778#ifdef NO_CONSOLE_INPUT
12779 /* While starting up, there is no place to enter text. */
12780 if (no_console_input())
12781 return;
12782#endif
12783 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12784 {
12785 EMSG2(_(e_listarg), "inputlist()");
12786 return;
12787 }
12788
12789 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012790 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012791 lines_left = Rows; /* avoid more prompt */
12792 msg_scroll = TRUE;
12793 msg_clr_eos();
12794
12795 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12796 {
12797 msg_puts(get_tv_string(&li->li_tv));
12798 msg_putchar('\n');
12799 }
12800
12801 /* Ask for choice. */
12802 selected = prompt_for_number(&mouse_used);
12803 if (mouse_used)
12804 selected -= lines_left;
12805
12806 rettv->vval.v_number = selected;
12807}
12808
12809
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12811
12812/*
12813 * "inputrestore()" function
12814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012817 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819{
12820 if (ga_userinput.ga_len > 0)
12821 {
12822 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12824 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012825 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 }
12827 else if (p_verbose > 1)
12828 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012829 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 }
12832}
12833
12834/*
12835 * "inputsave()" function
12836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012838f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012839 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012842 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 if (ga_grow(&ga_userinput, 1) == OK)
12844 {
12845 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12846 + ga_userinput.ga_len);
12847 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012848 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849 }
12850 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852}
12853
12854/*
12855 * "inputsecret()" function
12856 */
12857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012858f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012859 typval_T *argvars;
12860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861{
12862 ++cmdline_star;
12863 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 --cmdline_star;
12866 --inputsecret_flag;
12867}
12868
12869/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012870 * "insert()" function
12871 */
12872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012874 typval_T *argvars;
12875 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012876{
12877 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012878 listitem_T *item;
12879 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012880 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012881
12882 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012883 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012884 else if ((l = argvars[0].vval.v_list) != NULL
12885 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012886 {
12887 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012888 before = get_tv_number_chk(&argvars[2], &error);
12889 if (error)
12890 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012891
Bram Moolenaar758711c2005-02-02 23:11:38 +000012892 if (before == l->lv_len)
12893 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012894 else
12895 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012896 item = list_find(l, before);
12897 if (item == NULL)
12898 {
12899 EMSGN(_(e_listidx), before);
12900 l = NULL;
12901 }
12902 }
12903 if (l != NULL)
12904 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012905 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012906 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012907 }
12908 }
12909}
12910
12911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912 * "isdirectory()" function
12913 */
12914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012916 typval_T *argvars;
12917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920}
12921
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012922/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012923 * "islocked()" function
12924 */
12925 static void
12926f_islocked(argvars, rettv)
12927 typval_T *argvars;
12928 typval_T *rettv;
12929{
12930 lval_T lv;
12931 char_u *end;
12932 dictitem_T *di;
12933
12934 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012935 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12936 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012937 if (end != NULL && lv.ll_name != NULL)
12938 {
12939 if (*end != NUL)
12940 EMSG(_(e_trailing));
12941 else
12942 {
12943 if (lv.ll_tv == NULL)
12944 {
12945 if (check_changedtick(lv.ll_name))
12946 rettv->vval.v_number = 1; /* always locked */
12947 else
12948 {
12949 di = find_var(lv.ll_name, NULL);
12950 if (di != NULL)
12951 {
12952 /* Consider a variable locked when:
12953 * 1. the variable itself is locked
12954 * 2. the value of the variable is locked.
12955 * 3. the List or Dict value is locked.
12956 */
12957 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12958 || tv_islocked(&di->di_tv));
12959 }
12960 }
12961 }
12962 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012963 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012964 else if (lv.ll_newkey != NULL)
12965 EMSG2(_(e_dictkey), lv.ll_newkey);
12966 else if (lv.ll_list != NULL)
12967 /* List item. */
12968 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12969 else
12970 /* Dictionary item. */
12971 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12972 }
12973 }
12974
12975 clear_lval(&lv);
12976}
12977
Bram Moolenaar33570922005-01-25 22:26:29 +000012978static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012979
12980/*
12981 * Turn a dict into a list:
12982 * "what" == 0: list of keys
12983 * "what" == 1: list of values
12984 * "what" == 2: list of items
12985 */
12986 static void
12987dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012988 typval_T *argvars;
12989 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012990 int what;
12991{
Bram Moolenaar33570922005-01-25 22:26:29 +000012992 list_T *l2;
12993 dictitem_T *di;
12994 hashitem_T *hi;
12995 listitem_T *li;
12996 listitem_T *li2;
12997 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012998 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012999
Bram Moolenaar8c711452005-01-14 21:53:12 +000013000 if (argvars[0].v_type != VAR_DICT)
13001 {
13002 EMSG(_(e_dictreq));
13003 return;
13004 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013005 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013006 return;
13007
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013008 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013009 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013010
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013011 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013012 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013013 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013014 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013015 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013016 --todo;
13017 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013018
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013019 li = listitem_alloc();
13020 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013021 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013022 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013023
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013024 if (what == 0)
13025 {
13026 /* keys() */
13027 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013028 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013029 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13030 }
13031 else if (what == 1)
13032 {
13033 /* values() */
13034 copy_tv(&di->di_tv, &li->li_tv);
13035 }
13036 else
13037 {
13038 /* items() */
13039 l2 = list_alloc();
13040 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013041 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013042 li->li_tv.vval.v_list = l2;
13043 if (l2 == NULL)
13044 break;
13045 ++l2->lv_refcount;
13046
13047 li2 = listitem_alloc();
13048 if (li2 == NULL)
13049 break;
13050 list_append(l2, li2);
13051 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013052 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013053 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13054
13055 li2 = listitem_alloc();
13056 if (li2 == NULL)
13057 break;
13058 list_append(l2, li2);
13059 copy_tv(&di->di_tv, &li2->li_tv);
13060 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013061 }
13062 }
13063}
13064
13065/*
13066 * "items(dict)" function
13067 */
13068 static void
13069f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013070 typval_T *argvars;
13071 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013072{
13073 dict_list(argvars, rettv, 2);
13074}
13075
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013077 * "join()" function
13078 */
13079 static void
13080f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013081 typval_T *argvars;
13082 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013083{
13084 garray_T ga;
13085 char_u *sep;
13086
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013087 if (argvars[0].v_type != VAR_LIST)
13088 {
13089 EMSG(_(e_listreq));
13090 return;
13091 }
13092 if (argvars[0].vval.v_list == NULL)
13093 return;
13094 if (argvars[1].v_type == VAR_UNKNOWN)
13095 sep = (char_u *)" ";
13096 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013097 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013098
13099 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013100
13101 if (sep != NULL)
13102 {
13103 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013104 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013105 ga_append(&ga, NUL);
13106 rettv->vval.v_string = (char_u *)ga.ga_data;
13107 }
13108 else
13109 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013110}
13111
13112/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013113 * "keys()" function
13114 */
13115 static void
13116f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013117 typval_T *argvars;
13118 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013119{
13120 dict_list(argvars, rettv, 0);
13121}
13122
13123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 * "last_buffer_nr()" function.
13125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013127f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013128 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130{
13131 int n = 0;
13132 buf_T *buf;
13133
13134 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13135 if (n < buf->b_fnum)
13136 n = buf->b_fnum;
13137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013138 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013139}
13140
13141/*
13142 * "len()" function
13143 */
13144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013145f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013146 typval_T *argvars;
13147 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013148{
13149 switch (argvars[0].v_type)
13150 {
13151 case VAR_STRING:
13152 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013153 rettv->vval.v_number = (varnumber_T)STRLEN(
13154 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013155 break;
13156 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013157 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013158 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013159 case VAR_DICT:
13160 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13161 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013162 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013163 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013164 break;
13165 }
13166}
13167
Bram Moolenaar33570922005-01-25 22:26:29 +000013168static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013169
13170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013171libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013172 typval_T *argvars;
13173 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013174 int type;
13175{
13176#ifdef FEAT_LIBCALL
13177 char_u *string_in;
13178 char_u **string_result;
13179 int nr_result;
13180#endif
13181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013182 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013183 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013184 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013185
13186 if (check_restricted() || check_secure())
13187 return;
13188
13189#ifdef FEAT_LIBCALL
13190 /* The first two args must be strings, otherwise its meaningless */
13191 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13192 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013193 string_in = NULL;
13194 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013195 string_in = argvars[2].vval.v_string;
13196 if (type == VAR_NUMBER)
13197 string_result = NULL;
13198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013200 if (mch_libcall(argvars[0].vval.v_string,
13201 argvars[1].vval.v_string,
13202 string_in,
13203 argvars[2].vval.v_number,
13204 string_result,
13205 &nr_result) == OK
13206 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013208 }
13209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210}
13211
13212/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013213 * "libcall()" function
13214 */
13215 static void
13216f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013217 typval_T *argvars;
13218 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013219{
13220 libcall_common(argvars, rettv, VAR_STRING);
13221}
13222
13223/*
13224 * "libcallnr()" function
13225 */
13226 static void
13227f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013228 typval_T *argvars;
13229 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013230{
13231 libcall_common(argvars, rettv, VAR_NUMBER);
13232}
13233
13234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 * "line(string)" function
13236 */
13237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013239 typval_T *argvars;
13240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241{
13242 linenr_T lnum = 0;
13243 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013244 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013246 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 if (fp != NULL)
13248 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250}
13251
13252/*
13253 * "line2byte(lnum)" function
13254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013256f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013257 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259{
13260#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013261 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262#else
13263 linenr_T lnum;
13264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013265 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013267 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013269 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13270 if (rettv->vval.v_number >= 0)
13271 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272#endif
13273}
13274
13275/*
13276 * "lispindent(lnum)" function
13277 */
13278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013279f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013280 typval_T *argvars;
13281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282{
13283#ifdef FEAT_LISP
13284 pos_T pos;
13285 linenr_T lnum;
13286
13287 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013288 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13290 {
13291 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013292 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 curwin->w_cursor = pos;
13294 }
13295 else
13296#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013297 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298}
13299
13300/*
13301 * "localtime()" function
13302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013304f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013305 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013308 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309}
13310
Bram Moolenaar33570922005-01-25 22:26:29 +000013311static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312
13313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013314get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013315 typval_T *argvars;
13316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 int exact;
13318{
13319 char_u *keys;
13320 char_u *which;
13321 char_u buf[NUMBUFLEN];
13322 char_u *keys_buf = NULL;
13323 char_u *rhs;
13324 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013325 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013326 int get_dict = FALSE;
13327 mapblock_T *mp;
13328 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329
13330 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013331 rettv->v_type = VAR_STRING;
13332 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 if (*keys == NUL)
13336 return;
13337
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013338 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013340 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013341 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013342 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013343 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013344 if (argvars[3].v_type != VAR_UNKNOWN)
13345 get_dict = get_tv_number(&argvars[3]);
13346 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348 else
13349 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013350 if (which == NULL)
13351 return;
13352
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353 mode = get_map_mode(&which, 0);
13354
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013355 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013356 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013358
13359 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013361 /* Return a string. */
13362 if (rhs != NULL)
13363 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364
Bram Moolenaarbd743252010-10-20 21:23:33 +020013365 }
13366 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13367 {
13368 /* Return a dictionary. */
13369 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13370 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13371 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372
Bram Moolenaarbd743252010-10-20 21:23:33 +020013373 dict_add_nr_str(dict, "lhs", 0L, lhs);
13374 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13375 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13376 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13377 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13378 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13379 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13380 dict_add_nr_str(dict, "mode", 0L, mapmode);
13381
13382 vim_free(lhs);
13383 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 }
13385}
13386
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013387#ifdef FEAT_FLOAT
13388/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013389 * "log()" function
13390 */
13391 static void
13392f_log(argvars, rettv)
13393 typval_T *argvars;
13394 typval_T *rettv;
13395{
13396 float_T f;
13397
13398 rettv->v_type = VAR_FLOAT;
13399 if (get_float_arg(argvars, &f) == OK)
13400 rettv->vval.v_float = log(f);
13401 else
13402 rettv->vval.v_float = 0.0;
13403}
13404
13405/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013406 * "log10()" function
13407 */
13408 static void
13409f_log10(argvars, rettv)
13410 typval_T *argvars;
13411 typval_T *rettv;
13412{
13413 float_T f;
13414
13415 rettv->v_type = VAR_FLOAT;
13416 if (get_float_arg(argvars, &f) == OK)
13417 rettv->vval.v_float = log10(f);
13418 else
13419 rettv->vval.v_float = 0.0;
13420}
13421#endif
13422
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013424 * "map()" function
13425 */
13426 static void
13427f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013428 typval_T *argvars;
13429 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013430{
13431 filter_map(argvars, rettv, TRUE);
13432}
13433
13434/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013435 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 */
13437 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013438f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013439 typval_T *argvars;
13440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013442 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443}
13444
13445/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013446 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447 */
13448 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013449f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013450 typval_T *argvars;
13451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013453 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454}
13455
Bram Moolenaar33570922005-01-25 22:26:29 +000013456static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457
13458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013459find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013460 typval_T *argvars;
13461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 int type;
13463{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013464 char_u *str = NULL;
13465 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 char_u *pat;
13467 regmatch_T regmatch;
13468 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013469 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 char_u *save_cpo;
13471 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013472 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013473 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013474 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013475 list_T *l = NULL;
13476 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013477 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013478 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479
13480 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13481 save_cpo = p_cpo;
13482 p_cpo = (char_u *)"";
13483
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013484 rettv->vval.v_number = -1;
13485 if (type == 3)
13486 {
13487 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013488 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013489 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013490 }
13491 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013493 rettv->v_type = VAR_STRING;
13494 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013497 if (argvars[0].v_type == VAR_LIST)
13498 {
13499 if ((l = argvars[0].vval.v_list) == NULL)
13500 goto theend;
13501 li = l->lv_first;
13502 }
13503 else
13504 expr = str = get_tv_string(&argvars[0]);
13505
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013506 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13507 if (pat == NULL)
13508 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013509
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013510 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013512 int error = FALSE;
13513
13514 start = get_tv_number_chk(&argvars[2], &error);
13515 if (error)
13516 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013517 if (l != NULL)
13518 {
13519 li = list_find(l, start);
13520 if (li == NULL)
13521 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013522 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013523 }
13524 else
13525 {
13526 if (start < 0)
13527 start = 0;
13528 if (start > (long)STRLEN(str))
13529 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013530 /* When "count" argument is there ignore matches before "start",
13531 * otherwise skip part of the string. Differs when pattern is "^"
13532 * or "\<". */
13533 if (argvars[3].v_type != VAR_UNKNOWN)
13534 startcol = start;
13535 else
13536 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013537 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013538
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013539 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013540 nth = get_tv_number_chk(&argvars[3], &error);
13541 if (error)
13542 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 }
13544
13545 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13546 if (regmatch.regprog != NULL)
13547 {
13548 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013549
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013550 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013551 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013552 if (l != NULL)
13553 {
13554 if (li == NULL)
13555 {
13556 match = FALSE;
13557 break;
13558 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013559 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013560 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013561 if (str == NULL)
13562 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013563 }
13564
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013565 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013566
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013567 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013568 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013569 if (l == NULL && !match)
13570 break;
13571
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013572 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013573 if (l != NULL)
13574 {
13575 li = li->li_next;
13576 ++idx;
13577 }
13578 else
13579 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013580#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013581 startcol = (colnr_T)(regmatch.startp[0]
13582 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013583#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013584 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013585#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013586 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013587 }
13588
13589 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013591 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013592 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013593 int i;
13594
13595 /* return list with matched string and submatches */
13596 for (i = 0; i < NSUBEXP; ++i)
13597 {
13598 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013599 {
13600 if (list_append_string(rettv->vval.v_list,
13601 (char_u *)"", 0) == FAIL)
13602 break;
13603 }
13604 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013605 regmatch.startp[i],
13606 (int)(regmatch.endp[i] - regmatch.startp[i]))
13607 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013608 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013609 }
13610 }
13611 else if (type == 2)
13612 {
13613 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013614 if (l != NULL)
13615 copy_tv(&li->li_tv, rettv);
13616 else
13617 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013619 }
13620 else if (l != NULL)
13621 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622 else
13623 {
13624 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 (varnumber_T)(regmatch.startp[0] - str);
13627 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013630 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 }
13632 }
13633 vim_free(regmatch.regprog);
13634 }
13635
13636theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013637 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 p_cpo = save_cpo;
13639}
13640
13641/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013642 * "match()" function
13643 */
13644 static void
13645f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013646 typval_T *argvars;
13647 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013648{
13649 find_some_match(argvars, rettv, 1);
13650}
13651
13652/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013653 * "matchadd()" function
13654 */
13655 static void
13656f_matchadd(argvars, rettv)
13657 typval_T *argvars;
13658 typval_T *rettv;
13659{
13660#ifdef FEAT_SEARCH_EXTRA
13661 char_u buf[NUMBUFLEN];
13662 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13663 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13664 int prio = 10; /* default priority */
13665 int id = -1;
13666 int error = FALSE;
13667
13668 rettv->vval.v_number = -1;
13669
13670 if (grp == NULL || pat == NULL)
13671 return;
13672 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013673 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013674 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013675 if (argvars[3].v_type != VAR_UNKNOWN)
13676 id = get_tv_number_chk(&argvars[3], &error);
13677 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013678 if (error == TRUE)
13679 return;
13680 if (id >= 1 && id <= 3)
13681 {
13682 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13683 return;
13684 }
13685
13686 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13687#endif
13688}
13689
13690/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013691 * "matcharg()" function
13692 */
13693 static void
13694f_matcharg(argvars, rettv)
13695 typval_T *argvars;
13696 typval_T *rettv;
13697{
13698 if (rettv_list_alloc(rettv) == OK)
13699 {
13700#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013701 int id = get_tv_number(&argvars[0]);
13702 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013703
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013704 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013705 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013706 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13707 {
13708 list_append_string(rettv->vval.v_list,
13709 syn_id2name(m->hlg_id), -1);
13710 list_append_string(rettv->vval.v_list, m->pattern, -1);
13711 }
13712 else
13713 {
13714 list_append_string(rettv->vval.v_list, NUL, -1);
13715 list_append_string(rettv->vval.v_list, NUL, -1);
13716 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013717 }
13718#endif
13719 }
13720}
13721
13722/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013723 * "matchdelete()" function
13724 */
13725 static void
13726f_matchdelete(argvars, rettv)
13727 typval_T *argvars;
13728 typval_T *rettv;
13729{
13730#ifdef FEAT_SEARCH_EXTRA
13731 rettv->vval.v_number = match_delete(curwin,
13732 (int)get_tv_number(&argvars[0]), TRUE);
13733#endif
13734}
13735
13736/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013737 * "matchend()" function
13738 */
13739 static void
13740f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013741 typval_T *argvars;
13742 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013743{
13744 find_some_match(argvars, rettv, 0);
13745}
13746
13747/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013748 * "matchlist()" function
13749 */
13750 static void
13751f_matchlist(argvars, rettv)
13752 typval_T *argvars;
13753 typval_T *rettv;
13754{
13755 find_some_match(argvars, rettv, 3);
13756}
13757
13758/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013759 * "matchstr()" function
13760 */
13761 static void
13762f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013763 typval_T *argvars;
13764 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013765{
13766 find_some_match(argvars, rettv, 2);
13767}
13768
Bram Moolenaar33570922005-01-25 22:26:29 +000013769static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013770
13771 static void
13772max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 typval_T *argvars;
13774 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013775 int domax;
13776{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013777 long n = 0;
13778 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013779 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013780
13781 if (argvars[0].v_type == VAR_LIST)
13782 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013783 list_T *l;
13784 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013785
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013786 l = argvars[0].vval.v_list;
13787 if (l != NULL)
13788 {
13789 li = l->lv_first;
13790 if (li != NULL)
13791 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013792 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013793 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013794 {
13795 li = li->li_next;
13796 if (li == NULL)
13797 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013798 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013799 if (domax ? i > n : i < n)
13800 n = i;
13801 }
13802 }
13803 }
13804 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013805 else if (argvars[0].v_type == VAR_DICT)
13806 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013808 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013809 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013810 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013811
13812 d = argvars[0].vval.v_dict;
13813 if (d != NULL)
13814 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013815 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013816 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013817 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013818 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013819 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013820 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013821 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013822 if (first)
13823 {
13824 n = i;
13825 first = FALSE;
13826 }
13827 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013828 n = i;
13829 }
13830 }
13831 }
13832 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013833 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013834 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013836}
13837
13838/*
13839 * "max()" function
13840 */
13841 static void
13842f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013843 typval_T *argvars;
13844 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013845{
13846 max_min(argvars, rettv, TRUE);
13847}
13848
13849/*
13850 * "min()" function
13851 */
13852 static void
13853f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013854 typval_T *argvars;
13855 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013856{
13857 max_min(argvars, rettv, FALSE);
13858}
13859
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013860static int mkdir_recurse __ARGS((char_u *dir, int prot));
13861
13862/*
13863 * Create the directory in which "dir" is located, and higher levels when
13864 * needed.
13865 */
13866 static int
13867mkdir_recurse(dir, prot)
13868 char_u *dir;
13869 int prot;
13870{
13871 char_u *p;
13872 char_u *updir;
13873 int r = FAIL;
13874
13875 /* Get end of directory name in "dir".
13876 * We're done when it's "/" or "c:/". */
13877 p = gettail_sep(dir);
13878 if (p <= get_past_head(dir))
13879 return OK;
13880
13881 /* If the directory exists we're done. Otherwise: create it.*/
13882 updir = vim_strnsave(dir, (int)(p - dir));
13883 if (updir == NULL)
13884 return FAIL;
13885 if (mch_isdir(updir))
13886 r = OK;
13887 else if (mkdir_recurse(updir, prot) == OK)
13888 r = vim_mkdir_emsg(updir, prot);
13889 vim_free(updir);
13890 return r;
13891}
13892
13893#ifdef vim_mkdir
13894/*
13895 * "mkdir()" function
13896 */
13897 static void
13898f_mkdir(argvars, rettv)
13899 typval_T *argvars;
13900 typval_T *rettv;
13901{
13902 char_u *dir;
13903 char_u buf[NUMBUFLEN];
13904 int prot = 0755;
13905
13906 rettv->vval.v_number = FAIL;
13907 if (check_restricted() || check_secure())
13908 return;
13909
13910 dir = get_tv_string_buf(&argvars[0], buf);
13911 if (argvars[1].v_type != VAR_UNKNOWN)
13912 {
13913 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013914 prot = get_tv_number_chk(&argvars[2], NULL);
13915 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013916 mkdir_recurse(dir, prot);
13917 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013918 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013919}
13920#endif
13921
Bram Moolenaar0d660222005-01-07 21:51:51 +000013922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923 * "mode()" function
13924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013926f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013927 typval_T *argvars;
13928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013930 char_u buf[3];
13931
13932 buf[1] = NUL;
13933 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934
13935#ifdef FEAT_VISUAL
13936 if (VIsual_active)
13937 {
13938 if (VIsual_select)
13939 buf[0] = VIsual_mode + 's' - 'v';
13940 else
13941 buf[0] = VIsual_mode;
13942 }
13943 else
13944#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013945 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13946 || State == CONFIRM)
13947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013949 if (State == ASKMORE)
13950 buf[1] = 'm';
13951 else if (State == CONFIRM)
13952 buf[1] = '?';
13953 }
13954 else if (State == EXTERNCMD)
13955 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 else if (State & INSERT)
13957 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013958#ifdef FEAT_VREPLACE
13959 if (State & VREPLACE_FLAG)
13960 {
13961 buf[0] = 'R';
13962 buf[1] = 'v';
13963 }
13964 else
13965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966 if (State & REPLACE_FLAG)
13967 buf[0] = 'R';
13968 else
13969 buf[0] = 'i';
13970 }
13971 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013974 if (exmode_active)
13975 buf[1] = 'v';
13976 }
13977 else if (exmode_active)
13978 {
13979 buf[0] = 'c';
13980 buf[1] = 'e';
13981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013985 if (finish_op)
13986 buf[1] = 'o';
13987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013989 /* Clear out the minor mode when the argument is not a non-zero number or
13990 * non-empty string. */
13991 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013992 buf[1] = NUL;
13993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013994 rettv->vval.v_string = vim_strsave(buf);
13995 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996}
13997
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013998#ifdef FEAT_MZSCHEME
13999/*
14000 * "mzeval()" function
14001 */
14002 static void
14003f_mzeval(argvars, rettv)
14004 typval_T *argvars;
14005 typval_T *rettv;
14006{
14007 char_u *str;
14008 char_u buf[NUMBUFLEN];
14009
14010 str = get_tv_string_buf(&argvars[0], buf);
14011 do_mzeval(str, rettv);
14012}
14013#endif
14014
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014016 * "nextnonblank()" function
14017 */
14018 static void
14019f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014020 typval_T *argvars;
14021 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014022{
14023 linenr_T lnum;
14024
14025 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014027 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014028 {
14029 lnum = 0;
14030 break;
14031 }
14032 if (*skipwhite(ml_get(lnum)) != NUL)
14033 break;
14034 }
14035 rettv->vval.v_number = lnum;
14036}
14037
14038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039 * "nr2char()" function
14040 */
14041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014042f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014043 typval_T *argvars;
14044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045{
14046 char_u buf[NUMBUFLEN];
14047
14048#ifdef FEAT_MBYTE
14049 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014050 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051 else
14052#endif
14053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014054 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 buf[1] = NUL;
14056 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014057 rettv->v_type = VAR_STRING;
14058 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014059}
14060
14061/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014062 * "pathshorten()" function
14063 */
14064 static void
14065f_pathshorten(argvars, rettv)
14066 typval_T *argvars;
14067 typval_T *rettv;
14068{
14069 char_u *p;
14070
14071 rettv->v_type = VAR_STRING;
14072 p = get_tv_string_chk(&argvars[0]);
14073 if (p == NULL)
14074 rettv->vval.v_string = NULL;
14075 else
14076 {
14077 p = vim_strsave(p);
14078 rettv->vval.v_string = p;
14079 if (p != NULL)
14080 shorten_dir(p);
14081 }
14082}
14083
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014084#ifdef FEAT_FLOAT
14085/*
14086 * "pow()" function
14087 */
14088 static void
14089f_pow(argvars, rettv)
14090 typval_T *argvars;
14091 typval_T *rettv;
14092{
14093 float_T fx, fy;
14094
14095 rettv->v_type = VAR_FLOAT;
14096 if (get_float_arg(argvars, &fx) == OK
14097 && get_float_arg(&argvars[1], &fy) == OK)
14098 rettv->vval.v_float = pow(fx, fy);
14099 else
14100 rettv->vval.v_float = 0.0;
14101}
14102#endif
14103
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014104/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014105 * "prevnonblank()" function
14106 */
14107 static void
14108f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014109 typval_T *argvars;
14110 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014111{
14112 linenr_T lnum;
14113
14114 lnum = get_tv_lnum(argvars);
14115 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14116 lnum = 0;
14117 else
14118 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14119 --lnum;
14120 rettv->vval.v_number = lnum;
14121}
14122
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014123#ifdef HAVE_STDARG_H
14124/* This dummy va_list is here because:
14125 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14126 * - locally in the function results in a "used before set" warning
14127 * - using va_start() to initialize it gives "function with fixed args" error */
14128static va_list ap;
14129#endif
14130
Bram Moolenaar8c711452005-01-14 21:53:12 +000014131/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014132 * "printf()" function
14133 */
14134 static void
14135f_printf(argvars, rettv)
14136 typval_T *argvars;
14137 typval_T *rettv;
14138{
14139 rettv->v_type = VAR_STRING;
14140 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014141#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014142 {
14143 char_u buf[NUMBUFLEN];
14144 int len;
14145 char_u *s;
14146 int saved_did_emsg = did_emsg;
14147 char *fmt;
14148
14149 /* Get the required length, allocate the buffer and do it for real. */
14150 did_emsg = FALSE;
14151 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014152 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014153 if (!did_emsg)
14154 {
14155 s = alloc(len + 1);
14156 if (s != NULL)
14157 {
14158 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014159 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014160 }
14161 }
14162 did_emsg |= saved_did_emsg;
14163 }
14164#endif
14165}
14166
14167/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014168 * "pumvisible()" function
14169 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014170 static void
14171f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014172 typval_T *argvars UNUSED;
14173 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014174{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014175#ifdef FEAT_INS_EXPAND
14176 if (pum_visible())
14177 rettv->vval.v_number = 1;
14178#endif
14179}
14180
14181/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014182 * "range()" function
14183 */
14184 static void
14185f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014186 typval_T *argvars;
14187 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014188{
14189 long start;
14190 long end;
14191 long stride = 1;
14192 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014196 if (argvars[1].v_type == VAR_UNKNOWN)
14197 {
14198 end = start - 1;
14199 start = 0;
14200 }
14201 else
14202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014203 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014204 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014205 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014206 }
14207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 if (error)
14209 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014210 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014211 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014212 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014213 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014214 else
14215 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014216 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014217 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014218 if (list_append_number(rettv->vval.v_list,
14219 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014220 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014221 }
14222}
14223
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014224/*
14225 * "readfile()" function
14226 */
14227 static void
14228f_readfile(argvars, rettv)
14229 typval_T *argvars;
14230 typval_T *rettv;
14231{
14232 int binary = FALSE;
14233 char_u *fname;
14234 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014235 listitem_T *li;
14236#define FREAD_SIZE 200 /* optimized for text lines */
14237 char_u buf[FREAD_SIZE];
14238 int readlen; /* size of last fread() */
14239 int buflen; /* nr of valid chars in buf[] */
14240 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14241 int tolist; /* first byte in buf[] still to be put in list */
14242 int chop; /* how many CR to chop off */
14243 char_u *prev = NULL; /* previously read bytes, if any */
14244 int prevlen = 0; /* length of "prev" if not NULL */
14245 char_u *s;
14246 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014247 long maxline = MAXLNUM;
14248 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014249
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014250 if (argvars[1].v_type != VAR_UNKNOWN)
14251 {
14252 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14253 binary = TRUE;
14254 if (argvars[2].v_type != VAR_UNKNOWN)
14255 maxline = get_tv_number(&argvars[2]);
14256 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014257
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014258 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014259 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014260
14261 /* Always open the file in binary mode, library functions have a mind of
14262 * their own about CR-LF conversion. */
14263 fname = get_tv_string(&argvars[0]);
14264 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14265 {
14266 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14267 return;
14268 }
14269
14270 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014271 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014272 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014273 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014274 buflen = filtd + readlen;
14275 tolist = 0;
14276 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14277 {
14278 if (buf[filtd] == '\n' || readlen <= 0)
14279 {
14280 /* Only when in binary mode add an empty list item when the
14281 * last line ends in a '\n'. */
14282 if (!binary && readlen == 0 && filtd == 0)
14283 break;
14284
14285 /* Found end-of-line or end-of-file: add a text line to the
14286 * list. */
14287 chop = 0;
14288 if (!binary)
14289 while (filtd - chop - 1 >= tolist
14290 && buf[filtd - chop - 1] == '\r')
14291 ++chop;
14292 len = filtd - tolist - chop;
14293 if (prev == NULL)
14294 s = vim_strnsave(buf + tolist, len);
14295 else
14296 {
14297 s = alloc((unsigned)(prevlen + len + 1));
14298 if (s != NULL)
14299 {
14300 mch_memmove(s, prev, prevlen);
14301 vim_free(prev);
14302 prev = NULL;
14303 mch_memmove(s + prevlen, buf + tolist, len);
14304 s[prevlen + len] = NUL;
14305 }
14306 }
14307 tolist = filtd + 1;
14308
14309 li = listitem_alloc();
14310 if (li == NULL)
14311 {
14312 vim_free(s);
14313 break;
14314 }
14315 li->li_tv.v_type = VAR_STRING;
14316 li->li_tv.v_lock = 0;
14317 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014318 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014319
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014320 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014321 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014322 if (readlen <= 0)
14323 break;
14324 }
14325 else if (buf[filtd] == NUL)
14326 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014327#ifdef FEAT_MBYTE
14328 else if (buf[filtd] == 0xef
14329 && enc_utf8
14330 && filtd + 2 < buflen
14331 && !binary
14332 && buf[filtd + 1] == 0xbb
14333 && buf[filtd + 2] == 0xbf)
14334 {
14335 /* remove utf-8 byte order mark */
14336 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14337 --filtd;
14338 buflen -= 3;
14339 }
14340#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014341 }
14342 if (readlen <= 0)
14343 break;
14344
14345 if (tolist == 0)
14346 {
14347 /* "buf" is full, need to move text to an allocated buffer */
14348 if (prev == NULL)
14349 {
14350 prev = vim_strnsave(buf, buflen);
14351 prevlen = buflen;
14352 }
14353 else
14354 {
14355 s = alloc((unsigned)(prevlen + buflen));
14356 if (s != NULL)
14357 {
14358 mch_memmove(s, prev, prevlen);
14359 mch_memmove(s + prevlen, buf, buflen);
14360 vim_free(prev);
14361 prev = s;
14362 prevlen += buflen;
14363 }
14364 }
14365 filtd = 0;
14366 }
14367 else
14368 {
14369 mch_memmove(buf, buf + tolist, buflen - tolist);
14370 filtd -= tolist;
14371 }
14372 }
14373
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014374 /*
14375 * For a negative line count use only the lines at the end of the file,
14376 * free the rest.
14377 */
14378 if (maxline < 0)
14379 while (cnt > -maxline)
14380 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014381 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014382 --cnt;
14383 }
14384
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014385 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014386 fclose(fd);
14387}
14388
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014389#if defined(FEAT_RELTIME)
14390static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14391
14392/*
14393 * Convert a List to proftime_T.
14394 * Return FAIL when there is something wrong.
14395 */
14396 static int
14397list2proftime(arg, tm)
14398 typval_T *arg;
14399 proftime_T *tm;
14400{
14401 long n1, n2;
14402 int error = FALSE;
14403
14404 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14405 || arg->vval.v_list->lv_len != 2)
14406 return FAIL;
14407 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14408 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14409# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014410 tm->HighPart = n1;
14411 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014412# else
14413 tm->tv_sec = n1;
14414 tm->tv_usec = n2;
14415# endif
14416 return error ? FAIL : OK;
14417}
14418#endif /* FEAT_RELTIME */
14419
14420/*
14421 * "reltime()" function
14422 */
14423 static void
14424f_reltime(argvars, rettv)
14425 typval_T *argvars;
14426 typval_T *rettv;
14427{
14428#ifdef FEAT_RELTIME
14429 proftime_T res;
14430 proftime_T start;
14431
14432 if (argvars[0].v_type == VAR_UNKNOWN)
14433 {
14434 /* No arguments: get current time. */
14435 profile_start(&res);
14436 }
14437 else if (argvars[1].v_type == VAR_UNKNOWN)
14438 {
14439 if (list2proftime(&argvars[0], &res) == FAIL)
14440 return;
14441 profile_end(&res);
14442 }
14443 else
14444 {
14445 /* Two arguments: compute the difference. */
14446 if (list2proftime(&argvars[0], &start) == FAIL
14447 || list2proftime(&argvars[1], &res) == FAIL)
14448 return;
14449 profile_sub(&res, &start);
14450 }
14451
14452 if (rettv_list_alloc(rettv) == OK)
14453 {
14454 long n1, n2;
14455
14456# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014457 n1 = res.HighPart;
14458 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014459# else
14460 n1 = res.tv_sec;
14461 n2 = res.tv_usec;
14462# endif
14463 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14464 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14465 }
14466#endif
14467}
14468
14469/*
14470 * "reltimestr()" function
14471 */
14472 static void
14473f_reltimestr(argvars, rettv)
14474 typval_T *argvars;
14475 typval_T *rettv;
14476{
14477#ifdef FEAT_RELTIME
14478 proftime_T tm;
14479#endif
14480
14481 rettv->v_type = VAR_STRING;
14482 rettv->vval.v_string = NULL;
14483#ifdef FEAT_RELTIME
14484 if (list2proftime(&argvars[0], &tm) == OK)
14485 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14486#endif
14487}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014488
Bram Moolenaar0d660222005-01-07 21:51:51 +000014489#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14490static void make_connection __ARGS((void));
14491static int check_connection __ARGS((void));
14492
14493 static void
14494make_connection()
14495{
14496 if (X_DISPLAY == NULL
14497# ifdef FEAT_GUI
14498 && !gui.in_use
14499# endif
14500 )
14501 {
14502 x_force_connect = TRUE;
14503 setup_term_clip();
14504 x_force_connect = FALSE;
14505 }
14506}
14507
14508 static int
14509check_connection()
14510{
14511 make_connection();
14512 if (X_DISPLAY == NULL)
14513 {
14514 EMSG(_("E240: No connection to Vim server"));
14515 return FAIL;
14516 }
14517 return OK;
14518}
14519#endif
14520
14521#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014522static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014523
14524 static void
14525remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014526 typval_T *argvars;
14527 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014528 int expr;
14529{
14530 char_u *server_name;
14531 char_u *keys;
14532 char_u *r = NULL;
14533 char_u buf[NUMBUFLEN];
14534# ifdef WIN32
14535 HWND w;
14536# else
14537 Window w;
14538# endif
14539
14540 if (check_restricted() || check_secure())
14541 return;
14542
14543# ifdef FEAT_X11
14544 if (check_connection() == FAIL)
14545 return;
14546# endif
14547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548 server_name = get_tv_string_chk(&argvars[0]);
14549 if (server_name == NULL)
14550 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014551 keys = get_tv_string_buf(&argvars[1], buf);
14552# ifdef WIN32
14553 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14554# else
14555 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14556 < 0)
14557# endif
14558 {
14559 if (r != NULL)
14560 EMSG(r); /* sending worked but evaluation failed */
14561 else
14562 EMSG2(_("E241: Unable to send to %s"), server_name);
14563 return;
14564 }
14565
14566 rettv->vval.v_string = r;
14567
14568 if (argvars[2].v_type != VAR_UNKNOWN)
14569 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014570 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014571 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014572 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014573
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014574 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014575 v.di_tv.v_type = VAR_STRING;
14576 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014577 idvar = get_tv_string_chk(&argvars[2]);
14578 if (idvar != NULL)
14579 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014580 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581 }
14582}
14583#endif
14584
14585/*
14586 * "remote_expr()" function
14587 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014588 static void
14589f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014590 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014591 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014592{
14593 rettv->v_type = VAR_STRING;
14594 rettv->vval.v_string = NULL;
14595#ifdef FEAT_CLIENTSERVER
14596 remote_common(argvars, rettv, TRUE);
14597#endif
14598}
14599
14600/*
14601 * "remote_foreground()" function
14602 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014603 static void
14604f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014605 typval_T *argvars UNUSED;
14606 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014607{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014608#ifdef FEAT_CLIENTSERVER
14609# ifdef WIN32
14610 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 {
14612 char_u *server_name = get_tv_string_chk(&argvars[0]);
14613
14614 if (server_name != NULL)
14615 serverForeground(server_name);
14616 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014617# else
14618 /* Send a foreground() expression to the server. */
14619 argvars[1].v_type = VAR_STRING;
14620 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14621 argvars[2].v_type = VAR_UNKNOWN;
14622 remote_common(argvars, rettv, TRUE);
14623 vim_free(argvars[1].vval.v_string);
14624# endif
14625#endif
14626}
14627
Bram Moolenaar0d660222005-01-07 21:51:51 +000014628 static void
14629f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014630 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014631 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014632{
14633#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014634 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014635 char_u *s = NULL;
14636# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014637 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014639 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014640
14641 if (check_restricted() || check_secure())
14642 {
14643 rettv->vval.v_number = -1;
14644 return;
14645 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014646 serverid = get_tv_string_chk(&argvars[0]);
14647 if (serverid == NULL)
14648 {
14649 rettv->vval.v_number = -1;
14650 return; /* type error; errmsg already given */
14651 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014652# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014653 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014654 if (n == 0)
14655 rettv->vval.v_number = -1;
14656 else
14657 {
14658 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14659 rettv->vval.v_number = (s != NULL);
14660 }
14661# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014662 if (check_connection() == FAIL)
14663 return;
14664
14665 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014666 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014667# endif
14668
14669 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014671 char_u *retvar;
14672
Bram Moolenaar33570922005-01-25 22:26:29 +000014673 v.di_tv.v_type = VAR_STRING;
14674 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014675 retvar = get_tv_string_chk(&argvars[1]);
14676 if (retvar != NULL)
14677 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014678 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014679 }
14680#else
14681 rettv->vval.v_number = -1;
14682#endif
14683}
14684
Bram Moolenaar0d660222005-01-07 21:51:51 +000014685 static void
14686f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014687 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014688 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014689{
14690 char_u *r = NULL;
14691
14692#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014693 char_u *serverid = get_tv_string_chk(&argvars[0]);
14694
14695 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014696 {
14697# ifdef WIN32
14698 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014699 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014700
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014701 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014702 if (n != 0)
14703 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14704 if (r == NULL)
14705# else
14706 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014707 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014708# endif
14709 EMSG(_("E277: Unable to read a server reply"));
14710 }
14711#endif
14712 rettv->v_type = VAR_STRING;
14713 rettv->vval.v_string = r;
14714}
14715
14716/*
14717 * "remote_send()" function
14718 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014719 static void
14720f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014721 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014722 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014723{
14724 rettv->v_type = VAR_STRING;
14725 rettv->vval.v_string = NULL;
14726#ifdef FEAT_CLIENTSERVER
14727 remote_common(argvars, rettv, FALSE);
14728#endif
14729}
14730
14731/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014732 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014733 */
14734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014735f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014736 typval_T *argvars;
14737 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014738{
Bram Moolenaar33570922005-01-25 22:26:29 +000014739 list_T *l;
14740 listitem_T *item, *item2;
14741 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014742 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014743 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014744 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014745 dict_T *d;
14746 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014747
Bram Moolenaar8c711452005-01-14 21:53:12 +000014748 if (argvars[0].v_type == VAR_DICT)
14749 {
14750 if (argvars[2].v_type != VAR_UNKNOWN)
14751 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014752 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014753 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014755 key = get_tv_string_chk(&argvars[1]);
14756 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014758 di = dict_find(d, key, -1);
14759 if (di == NULL)
14760 EMSG2(_(e_dictkey), key);
14761 else
14762 {
14763 *rettv = di->di_tv;
14764 init_tv(&di->di_tv);
14765 dictitem_remove(d, di);
14766 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014767 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014768 }
14769 }
14770 else if (argvars[0].v_type != VAR_LIST)
14771 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014772 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014773 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014775 int error = FALSE;
14776
14777 idx = get_tv_number_chk(&argvars[1], &error);
14778 if (error)
14779 ; /* type error: do nothing, errmsg already given */
14780 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014781 EMSGN(_(e_listidx), idx);
14782 else
14783 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014784 if (argvars[2].v_type == VAR_UNKNOWN)
14785 {
14786 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014787 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014788 *rettv = item->li_tv;
14789 vim_free(item);
14790 }
14791 else
14792 {
14793 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014794 end = get_tv_number_chk(&argvars[2], &error);
14795 if (error)
14796 ; /* type error: do nothing */
14797 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014798 EMSGN(_(e_listidx), end);
14799 else
14800 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014801 int cnt = 0;
14802
14803 for (li = item; li != NULL; li = li->li_next)
14804 {
14805 ++cnt;
14806 if (li == item2)
14807 break;
14808 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014809 if (li == NULL) /* didn't find "item2" after "item" */
14810 EMSG(_(e_invrange));
14811 else
14812 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014813 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014814 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014815 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014816 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014817 l->lv_first = item;
14818 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014819 item->li_prev = NULL;
14820 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014821 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014822 }
14823 }
14824 }
14825 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014826 }
14827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828}
14829
14830/*
14831 * "rename({from}, {to})" function
14832 */
14833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014834f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014835 typval_T *argvars;
14836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837{
14838 char_u buf[NUMBUFLEN];
14839
14840 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014841 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014843 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14844 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845}
14846
14847/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014848 * "repeat()" function
14849 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014850 static void
14851f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014852 typval_T *argvars;
14853 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014854{
14855 char_u *p;
14856 int n;
14857 int slen;
14858 int len;
14859 char_u *r;
14860 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014861
14862 n = get_tv_number(&argvars[1]);
14863 if (argvars[0].v_type == VAR_LIST)
14864 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014865 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014866 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014867 if (list_extend(rettv->vval.v_list,
14868 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014869 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014870 }
14871 else
14872 {
14873 p = get_tv_string(&argvars[0]);
14874 rettv->v_type = VAR_STRING;
14875 rettv->vval.v_string = NULL;
14876
14877 slen = (int)STRLEN(p);
14878 len = slen * n;
14879 if (len <= 0)
14880 return;
14881
14882 r = alloc(len + 1);
14883 if (r != NULL)
14884 {
14885 for (i = 0; i < n; i++)
14886 mch_memmove(r + i * slen, p, (size_t)slen);
14887 r[len] = NUL;
14888 }
14889
14890 rettv->vval.v_string = r;
14891 }
14892}
14893
14894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895 * "resolve()" function
14896 */
14897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014898f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014899 typval_T *argvars;
14900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901{
14902 char_u *p;
14903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014904 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905#ifdef FEAT_SHORTCUT
14906 {
14907 char_u *v = NULL;
14908
14909 v = mch_resolve_shortcut(p);
14910 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014911 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914 }
14915#else
14916# ifdef HAVE_READLINK
14917 {
14918 char_u buf[MAXPATHL + 1];
14919 char_u *cpy;
14920 int len;
14921 char_u *remain = NULL;
14922 char_u *q;
14923 int is_relative_to_current = FALSE;
14924 int has_trailing_pathsep = FALSE;
14925 int limit = 100;
14926
14927 p = vim_strsave(p);
14928
14929 if (p[0] == '.' && (vim_ispathsep(p[1])
14930 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14931 is_relative_to_current = TRUE;
14932
14933 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014934 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935 has_trailing_pathsep = TRUE;
14936
14937 q = getnextcomp(p);
14938 if (*q != NUL)
14939 {
14940 /* Separate the first path component in "p", and keep the
14941 * remainder (beginning with the path separator). */
14942 remain = vim_strsave(q - 1);
14943 q[-1] = NUL;
14944 }
14945
14946 for (;;)
14947 {
14948 for (;;)
14949 {
14950 len = readlink((char *)p, (char *)buf, MAXPATHL);
14951 if (len <= 0)
14952 break;
14953 buf[len] = NUL;
14954
14955 if (limit-- == 0)
14956 {
14957 vim_free(p);
14958 vim_free(remain);
14959 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014960 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 goto fail;
14962 }
14963
14964 /* Ensure that the result will have a trailing path separator
14965 * if the argument has one. */
14966 if (remain == NULL && has_trailing_pathsep)
14967 add_pathsep(buf);
14968
14969 /* Separate the first path component in the link value and
14970 * concatenate the remainders. */
14971 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14972 if (*q != NUL)
14973 {
14974 if (remain == NULL)
14975 remain = vim_strsave(q - 1);
14976 else
14977 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014978 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 if (cpy != NULL)
14980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981 vim_free(remain);
14982 remain = cpy;
14983 }
14984 }
14985 q[-1] = NUL;
14986 }
14987
14988 q = gettail(p);
14989 if (q > p && *q == NUL)
14990 {
14991 /* Ignore trailing path separator. */
14992 q[-1] = NUL;
14993 q = gettail(p);
14994 }
14995 if (q > p && !mch_isFullName(buf))
14996 {
14997 /* symlink is relative to directory of argument */
14998 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14999 if (cpy != NULL)
15000 {
15001 STRCPY(cpy, p);
15002 STRCPY(gettail(cpy), buf);
15003 vim_free(p);
15004 p = cpy;
15005 }
15006 }
15007 else
15008 {
15009 vim_free(p);
15010 p = vim_strsave(buf);
15011 }
15012 }
15013
15014 if (remain == NULL)
15015 break;
15016
15017 /* Append the first path component of "remain" to "p". */
15018 q = getnextcomp(remain + 1);
15019 len = q - remain - (*q != NUL);
15020 cpy = vim_strnsave(p, STRLEN(p) + len);
15021 if (cpy != NULL)
15022 {
15023 STRNCAT(cpy, remain, len);
15024 vim_free(p);
15025 p = cpy;
15026 }
15027 /* Shorten "remain". */
15028 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015029 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030 else
15031 {
15032 vim_free(remain);
15033 remain = NULL;
15034 }
15035 }
15036
15037 /* If the result is a relative path name, make it explicitly relative to
15038 * the current directory if and only if the argument had this form. */
15039 if (!vim_ispathsep(*p))
15040 {
15041 if (is_relative_to_current
15042 && *p != NUL
15043 && !(p[0] == '.'
15044 && (p[1] == NUL
15045 || vim_ispathsep(p[1])
15046 || (p[1] == '.'
15047 && (p[2] == NUL
15048 || vim_ispathsep(p[2]))))))
15049 {
15050 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015051 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 if (cpy != NULL)
15053 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 vim_free(p);
15055 p = cpy;
15056 }
15057 }
15058 else if (!is_relative_to_current)
15059 {
15060 /* Strip leading "./". */
15061 q = p;
15062 while (q[0] == '.' && vim_ispathsep(q[1]))
15063 q += 2;
15064 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015065 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066 }
15067 }
15068
15069 /* Ensure that the result will have no trailing path separator
15070 * if the argument had none. But keep "/" or "//". */
15071 if (!has_trailing_pathsep)
15072 {
15073 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015074 if (after_pathsep(p, q))
15075 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076 }
15077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015078 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079 }
15080# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015081 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082# endif
15083#endif
15084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015085 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086
15087#ifdef HAVE_READLINK
15088fail:
15089#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015090 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091}
15092
15093/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015094 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095 */
15096 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015098 typval_T *argvars;
15099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100{
Bram Moolenaar33570922005-01-25 22:26:29 +000015101 list_T *l;
15102 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104 if (argvars[0].v_type != VAR_LIST)
15105 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015106 else if ((l = argvars[0].vval.v_list) != NULL
15107 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015108 {
15109 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015110 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015111 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112 while (li != NULL)
15113 {
15114 ni = li->li_prev;
15115 list_append(l, li);
15116 li = ni;
15117 }
15118 rettv->vval.v_list = l;
15119 rettv->v_type = VAR_LIST;
15120 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015121 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123}
15124
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015125#define SP_NOMOVE 0x01 /* don't move cursor */
15126#define SP_REPEAT 0x02 /* repeat to find outer pair */
15127#define SP_RETCOUNT 0x04 /* return matchcount */
15128#define SP_SETPCMARK 0x08 /* set previous context mark */
15129#define SP_START 0x10 /* accept match at start position */
15130#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15131#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015132
Bram Moolenaar33570922005-01-25 22:26:29 +000015133static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015134
15135/*
15136 * Get flags for a search function.
15137 * Possibly sets "p_ws".
15138 * Returns BACKWARD, FORWARD or zero (for an error).
15139 */
15140 static int
15141get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015142 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015143 int *flagsp;
15144{
15145 int dir = FORWARD;
15146 char_u *flags;
15147 char_u nbuf[NUMBUFLEN];
15148 int mask;
15149
15150 if (varp->v_type != VAR_UNKNOWN)
15151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015152 flags = get_tv_string_buf_chk(varp, nbuf);
15153 if (flags == NULL)
15154 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015155 while (*flags != NUL)
15156 {
15157 switch (*flags)
15158 {
15159 case 'b': dir = BACKWARD; break;
15160 case 'w': p_ws = TRUE; break;
15161 case 'W': p_ws = FALSE; break;
15162 default: mask = 0;
15163 if (flagsp != NULL)
15164 switch (*flags)
15165 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015166 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015167 case 'e': mask = SP_END; break;
15168 case 'm': mask = SP_RETCOUNT; break;
15169 case 'n': mask = SP_NOMOVE; break;
15170 case 'p': mask = SP_SUBPAT; break;
15171 case 'r': mask = SP_REPEAT; break;
15172 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173 }
15174 if (mask == 0)
15175 {
15176 EMSG2(_(e_invarg2), flags);
15177 dir = 0;
15178 }
15179 else
15180 *flagsp |= mask;
15181 }
15182 if (dir == 0)
15183 break;
15184 ++flags;
15185 }
15186 }
15187 return dir;
15188}
15189
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015191 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015193 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015194search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015195 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015196 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015197 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015199 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200 char_u *pat;
15201 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015202 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203 int save_p_ws = p_ws;
15204 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015205 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015206 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015207 proftime_T tm;
15208#ifdef FEAT_RELTIME
15209 long time_limit = 0;
15210#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015211 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015212 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015214 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015215 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015216 if (dir == 0)
15217 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015218 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015219 if (flags & SP_START)
15220 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015221 if (flags & SP_END)
15222 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015223
Bram Moolenaar76929292008-01-06 19:07:36 +000015224 /* Optional arguments: line number to stop searching and timeout. */
15225 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015226 {
15227 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15228 if (lnum_stop < 0)
15229 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015230#ifdef FEAT_RELTIME
15231 if (argvars[3].v_type != VAR_UNKNOWN)
15232 {
15233 time_limit = get_tv_number_chk(&argvars[3], NULL);
15234 if (time_limit < 0)
15235 goto theend;
15236 }
15237#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015238 }
15239
Bram Moolenaar76929292008-01-06 19:07:36 +000015240#ifdef FEAT_RELTIME
15241 /* Set the time limit, if there is one. */
15242 profile_setlimit(time_limit, &tm);
15243#endif
15244
Bram Moolenaar231334e2005-07-25 20:46:57 +000015245 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015246 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015247 * Check to make sure only those flags are set.
15248 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15249 * flags cannot be set. Check for that condition also.
15250 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015251 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015252 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015254 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015255 goto theend;
15256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015258 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015259 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015260 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015261 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015263 if (flags & SP_SUBPAT)
15264 retval = subpatnum;
15265 else
15266 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015267 if (flags & SP_SETPCMARK)
15268 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015270 if (match_pos != NULL)
15271 {
15272 /* Store the match cursor position */
15273 match_pos->lnum = pos.lnum;
15274 match_pos->col = pos.col + 1;
15275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276 /* "/$" will put the cursor after the end of the line, may need to
15277 * correct that here */
15278 check_cursor();
15279 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015280
15281 /* If 'n' flag is used: restore cursor position. */
15282 if (flags & SP_NOMOVE)
15283 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015284 else
15285 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015286theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015288
15289 return retval;
15290}
15291
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015292#ifdef FEAT_FLOAT
15293/*
15294 * "round({float})" function
15295 */
15296 static void
15297f_round(argvars, rettv)
15298 typval_T *argvars;
15299 typval_T *rettv;
15300{
15301 float_T f;
15302
15303 rettv->v_type = VAR_FLOAT;
15304 if (get_float_arg(argvars, &f) == OK)
15305 /* round() is not in C90, use ceil() or floor() instead. */
15306 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15307 else
15308 rettv->vval.v_float = 0.0;
15309}
15310#endif
15311
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015312/*
15313 * "search()" function
15314 */
15315 static void
15316f_search(argvars, rettv)
15317 typval_T *argvars;
15318 typval_T *rettv;
15319{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015320 int flags = 0;
15321
15322 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323}
15324
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015326 * "searchdecl()" function
15327 */
15328 static void
15329f_searchdecl(argvars, rettv)
15330 typval_T *argvars;
15331 typval_T *rettv;
15332{
15333 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015334 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015335 int error = FALSE;
15336 char_u *name;
15337
15338 rettv->vval.v_number = 1; /* default: FAIL */
15339
15340 name = get_tv_string_chk(&argvars[0]);
15341 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015342 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015343 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015344 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15345 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15346 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015347 if (!error && name != NULL)
15348 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015349 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015350}
15351
15352/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015353 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015355 static int
15356searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015357 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015358 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359{
15360 char_u *spat, *mpat, *epat;
15361 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363 int dir;
15364 int flags = 0;
15365 char_u nbuf1[NUMBUFLEN];
15366 char_u nbuf2[NUMBUFLEN];
15367 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015368 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015369 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015370 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015373 spat = get_tv_string_chk(&argvars[0]);
15374 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15375 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15376 if (spat == NULL || mpat == NULL || epat == NULL)
15377 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 /* Handle the optional fourth argument: flags */
15380 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015381 if (dir == 0)
15382 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015383
15384 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015385 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15386 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015387 if ((flags & (SP_END | SP_SUBPAT)) != 0
15388 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015389 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015390 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015391 goto theend;
15392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015394 /* Using 'r' implies 'W', otherwise it doesn't work. */
15395 if (flags & SP_REPEAT)
15396 p_ws = FALSE;
15397
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015398 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015399 if (argvars[3].v_type == VAR_UNKNOWN
15400 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 skip = (char_u *)"";
15402 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015404 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015405 if (argvars[5].v_type != VAR_UNKNOWN)
15406 {
15407 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15408 if (lnum_stop < 0)
15409 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015410#ifdef FEAT_RELTIME
15411 if (argvars[6].v_type != VAR_UNKNOWN)
15412 {
15413 time_limit = get_tv_number_chk(&argvars[6], NULL);
15414 if (time_limit < 0)
15415 goto theend;
15416 }
15417#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015418 }
15419 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015420 if (skip == NULL)
15421 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015423 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015424 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015425
15426theend:
15427 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015428
15429 return retval;
15430}
15431
15432/*
15433 * "searchpair()" function
15434 */
15435 static void
15436f_searchpair(argvars, rettv)
15437 typval_T *argvars;
15438 typval_T *rettv;
15439{
15440 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15441}
15442
15443/*
15444 * "searchpairpos()" function
15445 */
15446 static void
15447f_searchpairpos(argvars, rettv)
15448 typval_T *argvars;
15449 typval_T *rettv;
15450{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015451 pos_T match_pos;
15452 int lnum = 0;
15453 int col = 0;
15454
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015455 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015456 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015457
15458 if (searchpair_cmn(argvars, &match_pos) > 0)
15459 {
15460 lnum = match_pos.lnum;
15461 col = match_pos.col;
15462 }
15463
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015464 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15465 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015466}
15467
15468/*
15469 * Search for a start/middle/end thing.
15470 * Used by searchpair(), see its documentation for the details.
15471 * Returns 0 or -1 for no match,
15472 */
15473 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015474do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15475 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015476 char_u *spat; /* start pattern */
15477 char_u *mpat; /* middle pattern */
15478 char_u *epat; /* end pattern */
15479 int dir; /* BACKWARD or FORWARD */
15480 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015481 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015482 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015483 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015484 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015485{
15486 char_u *save_cpo;
15487 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15488 long retval = 0;
15489 pos_T pos;
15490 pos_T firstpos;
15491 pos_T foundpos;
15492 pos_T save_cursor;
15493 pos_T save_pos;
15494 int n;
15495 int r;
15496 int nest = 1;
15497 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015498 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015499 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015500
15501 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15502 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015503 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015504
Bram Moolenaar76929292008-01-06 19:07:36 +000015505#ifdef FEAT_RELTIME
15506 /* Set the time limit, if there is one. */
15507 profile_setlimit(time_limit, &tm);
15508#endif
15509
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015510 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15511 * start/middle/end (pat3, for the top pair). */
15512 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15513 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15514 if (pat2 == NULL || pat3 == NULL)
15515 goto theend;
15516 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15517 if (*mpat == NUL)
15518 STRCPY(pat3, pat2);
15519 else
15520 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15521 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015522 if (flags & SP_START)
15523 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015524
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 save_cursor = curwin->w_cursor;
15526 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015527 clearpos(&firstpos);
15528 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 pat = pat3;
15530 for (;;)
15531 {
15532 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015533 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15535 /* didn't find it or found the first match again: FAIL */
15536 break;
15537
15538 if (firstpos.lnum == 0)
15539 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015540 if (equalpos(pos, foundpos))
15541 {
15542 /* Found the same position again. Can happen with a pattern that
15543 * has "\zs" at the end and searching backwards. Advance one
15544 * character and try again. */
15545 if (dir == BACKWARD)
15546 decl(&pos);
15547 else
15548 incl(&pos);
15549 }
15550 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015552 /* clear the start flag to avoid getting stuck here */
15553 options &= ~SEARCH_START;
15554
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 /* If the skip pattern matches, ignore this match. */
15556 if (*skip != NUL)
15557 {
15558 save_pos = curwin->w_cursor;
15559 curwin->w_cursor = pos;
15560 r = eval_to_bool(skip, &err, NULL, FALSE);
15561 curwin->w_cursor = save_pos;
15562 if (err)
15563 {
15564 /* Evaluating {skip} caused an error, break here. */
15565 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015566 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 break;
15568 }
15569 if (r)
15570 continue;
15571 }
15572
15573 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15574 {
15575 /* Found end when searching backwards or start when searching
15576 * forward: nested pair. */
15577 ++nest;
15578 pat = pat2; /* nested, don't search for middle */
15579 }
15580 else
15581 {
15582 /* Found end when searching forward or start when searching
15583 * backward: end of (nested) pair; or found middle in outer pair. */
15584 if (--nest == 1)
15585 pat = pat3; /* outer level, search for middle */
15586 }
15587
15588 if (nest == 0)
15589 {
15590 /* Found the match: return matchcount or line number. */
15591 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015592 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015594 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015595 if (flags & SP_SETPCMARK)
15596 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597 curwin->w_cursor = pos;
15598 if (!(flags & SP_REPEAT))
15599 break;
15600 nest = 1; /* search for next unmatched */
15601 }
15602 }
15603
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015604 if (match_pos != NULL)
15605 {
15606 /* Store the match cursor position */
15607 match_pos->lnum = curwin->w_cursor.lnum;
15608 match_pos->col = curwin->w_cursor.col + 1;
15609 }
15610
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015612 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613 curwin->w_cursor = save_cursor;
15614
15615theend:
15616 vim_free(pat2);
15617 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015618 if (p_cpo == empty_option)
15619 p_cpo = save_cpo;
15620 else
15621 /* Darn, evaluating the {skip} expression changed the value. */
15622 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015623
15624 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625}
15626
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015627/*
15628 * "searchpos()" function
15629 */
15630 static void
15631f_searchpos(argvars, rettv)
15632 typval_T *argvars;
15633 typval_T *rettv;
15634{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015635 pos_T match_pos;
15636 int lnum = 0;
15637 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015638 int n;
15639 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015640
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015641 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015642 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015643
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015644 n = search_cmn(argvars, &match_pos, &flags);
15645 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015646 {
15647 lnum = match_pos.lnum;
15648 col = match_pos.col;
15649 }
15650
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015651 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15652 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015653 if (flags & SP_SUBPAT)
15654 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015655}
15656
15657
Bram Moolenaar0d660222005-01-07 21:51:51 +000015658 static void
15659f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015660 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015663#ifdef FEAT_CLIENTSERVER
15664 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015665 char_u *server = get_tv_string_chk(&argvars[0]);
15666 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667
Bram Moolenaar0d660222005-01-07 21:51:51 +000015668 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015669 if (server == NULL || reply == NULL)
15670 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015671 if (check_restricted() || check_secure())
15672 return;
15673# ifdef FEAT_X11
15674 if (check_connection() == FAIL)
15675 return;
15676# endif
15677
15678 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015680 EMSG(_("E258: Unable to send to client"));
15681 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015683 rettv->vval.v_number = 0;
15684#else
15685 rettv->vval.v_number = -1;
15686#endif
15687}
15688
Bram Moolenaar0d660222005-01-07 21:51:51 +000015689 static void
15690f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015691 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015692 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015693{
15694 char_u *r = NULL;
15695
15696#ifdef FEAT_CLIENTSERVER
15697# ifdef WIN32
15698 r = serverGetVimNames();
15699# else
15700 make_connection();
15701 if (X_DISPLAY != NULL)
15702 r = serverGetVimNames(X_DISPLAY);
15703# endif
15704#endif
15705 rettv->v_type = VAR_STRING;
15706 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707}
15708
15709/*
15710 * "setbufvar()" function
15711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015713f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015714 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015715 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716{
15717 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015720 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721 char_u nbuf[NUMBUFLEN];
15722
15723 if (check_restricted() || check_secure())
15724 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015725 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15726 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015727 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 varp = &argvars[2];
15729
15730 if (buf != NULL && varname != NULL && varp != NULL)
15731 {
15732 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734
15735 if (*varname == '&')
15736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015737 long numval;
15738 char_u *strval;
15739 int error = FALSE;
15740
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015742 numval = get_tv_number_chk(varp, &error);
15743 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015744 if (!error && strval != NULL)
15745 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 }
15747 else
15748 {
15749 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15750 if (bufvarname != NULL)
15751 {
15752 STRCPY(bufvarname, "b:");
15753 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015754 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 vim_free(bufvarname);
15756 }
15757 }
15758
15759 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762}
15763
15764/*
15765 * "setcmdpos()" function
15766 */
15767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015768f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015769 typval_T *argvars;
15770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015772 int pos = (int)get_tv_number(&argvars[0]) - 1;
15773
15774 if (pos >= 0)
15775 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776}
15777
15778/*
15779 * "setline()" function
15780 */
15781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015782f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015783 typval_T *argvars;
15784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785{
15786 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015787 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015788 list_T *l = NULL;
15789 listitem_T *li = NULL;
15790 long added = 0;
15791 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015793 lnum = get_tv_lnum(&argvars[0]);
15794 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015796 l = argvars[1].vval.v_list;
15797 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015799 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015800 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015801
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015802 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015803 for (;;)
15804 {
15805 if (l != NULL)
15806 {
15807 /* list argument, get next string */
15808 if (li == NULL)
15809 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015810 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015811 li = li->li_next;
15812 }
15813
15814 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015815 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015816 break;
15817 if (lnum <= curbuf->b_ml.ml_line_count)
15818 {
15819 /* existing line, replace it */
15820 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15821 {
15822 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015823 if (lnum == curwin->w_cursor.lnum)
15824 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015825 rettv->vval.v_number = 0; /* OK */
15826 }
15827 }
15828 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15829 {
15830 /* lnum is one past the last line, append the line */
15831 ++added;
15832 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15833 rettv->vval.v_number = 0; /* OK */
15834 }
15835
15836 if (l == NULL) /* only one string argument */
15837 break;
15838 ++lnum;
15839 }
15840
15841 if (added > 0)
15842 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843}
15844
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015845static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15846
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015848 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015849 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015850 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015851set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015852 win_T *wp UNUSED;
15853 typval_T *list_arg UNUSED;
15854 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015855 typval_T *rettv;
15856{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015857#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015858 char_u *act;
15859 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015860#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015861
Bram Moolenaar2641f772005-03-25 21:58:17 +000015862 rettv->vval.v_number = -1;
15863
15864#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015865 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015866 EMSG(_(e_listreq));
15867 else
15868 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015869 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015870
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015871 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015872 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015873 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015874 if (act == NULL)
15875 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015876 if (*act == 'a' || *act == 'r')
15877 action = *act;
15878 }
15879
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015880 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015881 rettv->vval.v_number = 0;
15882 }
15883#endif
15884}
15885
15886/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015887 * "setloclist()" function
15888 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015889 static void
15890f_setloclist(argvars, rettv)
15891 typval_T *argvars;
15892 typval_T *rettv;
15893{
15894 win_T *win;
15895
15896 rettv->vval.v_number = -1;
15897
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015898 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015899 if (win != NULL)
15900 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15901}
15902
15903/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015904 * "setmatches()" function
15905 */
15906 static void
15907f_setmatches(argvars, rettv)
15908 typval_T *argvars;
15909 typval_T *rettv;
15910{
15911#ifdef FEAT_SEARCH_EXTRA
15912 list_T *l;
15913 listitem_T *li;
15914 dict_T *d;
15915
15916 rettv->vval.v_number = -1;
15917 if (argvars[0].v_type != VAR_LIST)
15918 {
15919 EMSG(_(e_listreq));
15920 return;
15921 }
15922 if ((l = argvars[0].vval.v_list) != NULL)
15923 {
15924
15925 /* To some extent make sure that we are dealing with a list from
15926 * "getmatches()". */
15927 li = l->lv_first;
15928 while (li != NULL)
15929 {
15930 if (li->li_tv.v_type != VAR_DICT
15931 || (d = li->li_tv.vval.v_dict) == NULL)
15932 {
15933 EMSG(_(e_invarg));
15934 return;
15935 }
15936 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15937 && dict_find(d, (char_u *)"pattern", -1) != NULL
15938 && dict_find(d, (char_u *)"priority", -1) != NULL
15939 && dict_find(d, (char_u *)"id", -1) != NULL))
15940 {
15941 EMSG(_(e_invarg));
15942 return;
15943 }
15944 li = li->li_next;
15945 }
15946
15947 clear_matches(curwin);
15948 li = l->lv_first;
15949 while (li != NULL)
15950 {
15951 d = li->li_tv.vval.v_dict;
15952 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15953 get_dict_string(d, (char_u *)"pattern", FALSE),
15954 (int)get_dict_number(d, (char_u *)"priority"),
15955 (int)get_dict_number(d, (char_u *)"id"));
15956 li = li->li_next;
15957 }
15958 rettv->vval.v_number = 0;
15959 }
15960#endif
15961}
15962
15963/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015964 * "setpos()" function
15965 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015966 static void
15967f_setpos(argvars, rettv)
15968 typval_T *argvars;
15969 typval_T *rettv;
15970{
15971 pos_T pos;
15972 int fnum;
15973 char_u *name;
15974
Bram Moolenaar08250432008-02-13 11:42:46 +000015975 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015976 name = get_tv_string_chk(argvars);
15977 if (name != NULL)
15978 {
15979 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15980 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015981 if (--pos.col < 0)
15982 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015983 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015984 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015985 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015986 if (fnum == curbuf->b_fnum)
15987 {
15988 curwin->w_cursor = pos;
15989 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015990 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015991 }
15992 else
15993 EMSG(_(e_invarg));
15994 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015995 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15996 {
15997 /* set mark */
15998 if (setmark_pos(name[1], &pos, fnum) == OK)
15999 rettv->vval.v_number = 0;
16000 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016001 else
16002 EMSG(_(e_invarg));
16003 }
16004 }
16005}
16006
16007/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016008 * "setqflist()" function
16009 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016010 static void
16011f_setqflist(argvars, rettv)
16012 typval_T *argvars;
16013 typval_T *rettv;
16014{
16015 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16016}
16017
16018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019 * "setreg()" function
16020 */
16021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016022f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016023 typval_T *argvars;
16024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025{
16026 int regname;
16027 char_u *strregname;
16028 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016029 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030 int append;
16031 char_u yank_type;
16032 long block_len;
16033
16034 block_len = -1;
16035 yank_type = MAUTO;
16036 append = FALSE;
16037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016038 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016039 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016041 if (strregname == NULL)
16042 return; /* type error; errmsg already given */
16043 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016044 if (regname == 0 || regname == '@')
16045 regname = '"';
16046 else if (regname == '=')
16047 return;
16048
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016049 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016051 stropt = get_tv_string_chk(&argvars[2]);
16052 if (stropt == NULL)
16053 return; /* type error */
16054 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 switch (*stropt)
16056 {
16057 case 'a': case 'A': /* append */
16058 append = TRUE;
16059 break;
16060 case 'v': case 'c': /* character-wise selection */
16061 yank_type = MCHAR;
16062 break;
16063 case 'V': case 'l': /* line-wise selection */
16064 yank_type = MLINE;
16065 break;
16066#ifdef FEAT_VISUAL
16067 case 'b': case Ctrl_V: /* block-wise selection */
16068 yank_type = MBLOCK;
16069 if (VIM_ISDIGIT(stropt[1]))
16070 {
16071 ++stropt;
16072 block_len = getdigits(&stropt) - 1;
16073 --stropt;
16074 }
16075 break;
16076#endif
16077 }
16078 }
16079
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016080 strval = get_tv_string_chk(&argvars[1]);
16081 if (strval != NULL)
16082 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016084 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085}
16086
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016087/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016088 * "settabvar()" function
16089 */
16090 static void
16091f_settabvar(argvars, rettv)
16092 typval_T *argvars;
16093 typval_T *rettv;
16094{
16095 tabpage_T *save_curtab;
16096 char_u *varname, *tabvarname;
16097 typval_T *varp;
16098 tabpage_T *tp;
16099
16100 rettv->vval.v_number = 0;
16101
16102 if (check_restricted() || check_secure())
16103 return;
16104
16105 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16106 varname = get_tv_string_chk(&argvars[1]);
16107 varp = &argvars[2];
16108
16109 if (tp != NULL && varname != NULL && varp != NULL)
16110 {
16111 save_curtab = curtab;
16112 goto_tabpage_tp(tp);
16113
16114 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16115 if (tabvarname != NULL)
16116 {
16117 STRCPY(tabvarname, "t:");
16118 STRCPY(tabvarname + 2, varname);
16119 set_var(tabvarname, varp, TRUE);
16120 vim_free(tabvarname);
16121 }
16122
16123 /* Restore current tabpage */
16124 if (valid_tabpage(save_curtab))
16125 goto_tabpage_tp(save_curtab);
16126 }
16127}
16128
16129/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016130 * "settabwinvar()" function
16131 */
16132 static void
16133f_settabwinvar(argvars, rettv)
16134 typval_T *argvars;
16135 typval_T *rettv;
16136{
16137 setwinvar(argvars, rettv, 1);
16138}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139
16140/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016141 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016144f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016145 typval_T *argvars;
16146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016148 setwinvar(argvars, rettv, 0);
16149}
16150
16151/*
16152 * "setwinvar()" and "settabwinvar()" functions
16153 */
16154 static void
16155setwinvar(argvars, rettv, off)
16156 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016157 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016158 int off;
16159{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 win_T *win;
16161#ifdef FEAT_WINDOWS
16162 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016163 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164#endif
16165 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016166 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016168 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169
16170 if (check_restricted() || check_secure())
16171 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016172
16173#ifdef FEAT_WINDOWS
16174 if (off == 1)
16175 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16176 else
16177 tp = curtab;
16178#endif
16179 win = find_win_by_nr(&argvars[off], tp);
16180 varname = get_tv_string_chk(&argvars[off + 1]);
16181 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182
16183 if (win != NULL && varname != NULL && varp != NULL)
16184 {
16185#ifdef FEAT_WINDOWS
16186 /* set curwin to be our win, temporarily */
16187 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016188 save_curtab = curtab;
16189 goto_tabpage_tp(tp);
16190 if (!win_valid(win))
16191 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192 curwin = win;
16193 curbuf = curwin->w_buffer;
16194#endif
16195
16196 if (*varname == '&')
16197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016198 long numval;
16199 char_u *strval;
16200 int error = FALSE;
16201
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016203 numval = get_tv_number_chk(varp, &error);
16204 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016205 if (!error && strval != NULL)
16206 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 }
16208 else
16209 {
16210 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16211 if (winvarname != NULL)
16212 {
16213 STRCPY(winvarname, "w:");
16214 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016215 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216 vim_free(winvarname);
16217 }
16218 }
16219
16220#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016221 /* Restore current tabpage and window, if still valid (autocomands can
16222 * make them invalid). */
16223 if (valid_tabpage(save_curtab))
16224 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225 if (win_valid(save_curwin))
16226 {
16227 curwin = save_curwin;
16228 curbuf = curwin->w_buffer;
16229 }
16230#endif
16231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232}
16233
16234/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016235 * "shellescape({string})" function
16236 */
16237 static void
16238f_shellescape(argvars, rettv)
16239 typval_T *argvars;
16240 typval_T *rettv;
16241{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016242 rettv->vval.v_string = vim_strsave_shellescape(
16243 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016244 rettv->v_type = VAR_STRING;
16245}
16246
16247/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016248 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249 */
16250 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016251f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016252 typval_T *argvars;
16253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016255 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256
Bram Moolenaar0d660222005-01-07 21:51:51 +000016257 p = get_tv_string(&argvars[0]);
16258 rettv->vval.v_string = vim_strsave(p);
16259 simplify_filename(rettv->vval.v_string); /* simplify in place */
16260 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261}
16262
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016263#ifdef FEAT_FLOAT
16264/*
16265 * "sin()" function
16266 */
16267 static void
16268f_sin(argvars, rettv)
16269 typval_T *argvars;
16270 typval_T *rettv;
16271{
16272 float_T f;
16273
16274 rettv->v_type = VAR_FLOAT;
16275 if (get_float_arg(argvars, &f) == OK)
16276 rettv->vval.v_float = sin(f);
16277 else
16278 rettv->vval.v_float = 0.0;
16279}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016280
16281/*
16282 * "sinh()" function
16283 */
16284 static void
16285f_sinh(argvars, rettv)
16286 typval_T *argvars;
16287 typval_T *rettv;
16288{
16289 float_T f;
16290
16291 rettv->v_type = VAR_FLOAT;
16292 if (get_float_arg(argvars, &f) == OK)
16293 rettv->vval.v_float = sinh(f);
16294 else
16295 rettv->vval.v_float = 0.0;
16296}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016297#endif
16298
Bram Moolenaar0d660222005-01-07 21:51:51 +000016299static int
16300#ifdef __BORLANDC__
16301 _RTLENTRYF
16302#endif
16303 item_compare __ARGS((const void *s1, const void *s2));
16304static int
16305#ifdef __BORLANDC__
16306 _RTLENTRYF
16307#endif
16308 item_compare2 __ARGS((const void *s1, const void *s2));
16309
16310static int item_compare_ic;
16311static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016312static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016313#define ITEM_COMPARE_FAIL 999
16314
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016318 static int
16319#ifdef __BORLANDC__
16320_RTLENTRYF
16321#endif
16322item_compare(s1, s2)
16323 const void *s1;
16324 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016326 char_u *p1, *p2;
16327 char_u *tofree1, *tofree2;
16328 int res;
16329 char_u numbuf1[NUMBUFLEN];
16330 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016332 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16333 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016334 if (p1 == NULL)
16335 p1 = (char_u *)"";
16336 if (p2 == NULL)
16337 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016338 if (item_compare_ic)
16339 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016341 res = STRCMP(p1, p2);
16342 vim_free(tofree1);
16343 vim_free(tofree2);
16344 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345}
16346
16347 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016348#ifdef __BORLANDC__
16349_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016351item_compare2(s1, s2)
16352 const void *s1;
16353 const void *s2;
16354{
16355 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016356 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016357 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016358 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016360 /* shortcut after failure in previous call; compare all items equal */
16361 if (item_compare_func_err)
16362 return 0;
16363
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016364 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16365 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016366 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16367 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016368
16369 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016370 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016371 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016372 clear_tv(&argv[0]);
16373 clear_tv(&argv[1]);
16374
16375 if (res == FAIL)
16376 res = ITEM_COMPARE_FAIL;
16377 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016378 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16379 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016380 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016381 clear_tv(&rettv);
16382 return res;
16383}
16384
16385/*
16386 * "sort({list})" function
16387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016389f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016390 typval_T *argvars;
16391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392{
Bram Moolenaar33570922005-01-25 22:26:29 +000016393 list_T *l;
16394 listitem_T *li;
16395 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016396 long len;
16397 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399 if (argvars[0].v_type != VAR_LIST)
16400 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401 else
16402 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016403 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016404 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016405 return;
16406 rettv->vval.v_list = l;
16407 rettv->v_type = VAR_LIST;
16408 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410 len = list_len(l);
16411 if (len <= 1)
16412 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413
Bram Moolenaar0d660222005-01-07 21:51:51 +000016414 item_compare_ic = FALSE;
16415 item_compare_func = NULL;
16416 if (argvars[1].v_type != VAR_UNKNOWN)
16417 {
16418 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016419 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016420 else
16421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016422 int error = FALSE;
16423
16424 i = get_tv_number_chk(&argvars[1], &error);
16425 if (error)
16426 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427 if (i == 1)
16428 item_compare_ic = TRUE;
16429 else
16430 item_compare_func = get_tv_string(&argvars[1]);
16431 }
16432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433
Bram Moolenaar0d660222005-01-07 21:51:51 +000016434 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016435 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436 if (ptrs == NULL)
16437 return;
16438 i = 0;
16439 for (li = l->lv_first; li != NULL; li = li->li_next)
16440 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016442 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016443 /* test the compare function */
16444 if (item_compare_func != NULL
16445 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16446 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016447 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016449 {
16450 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016451 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016452 item_compare_func == NULL ? item_compare : item_compare2);
16453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016454 if (!item_compare_func_err)
16455 {
16456 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016457 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016458 l->lv_len = 0;
16459 for (i = 0; i < len; ++i)
16460 list_append(l, ptrs[i]);
16461 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462 }
16463
16464 vim_free(ptrs);
16465 }
16466}
16467
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016468/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016469 * "soundfold({word})" function
16470 */
16471 static void
16472f_soundfold(argvars, rettv)
16473 typval_T *argvars;
16474 typval_T *rettv;
16475{
16476 char_u *s;
16477
16478 rettv->v_type = VAR_STRING;
16479 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016480#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016481 rettv->vval.v_string = eval_soundfold(s);
16482#else
16483 rettv->vval.v_string = vim_strsave(s);
16484#endif
16485}
16486
16487/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016488 * "spellbadword()" function
16489 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016490 static void
16491f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016492 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016493 typval_T *rettv;
16494{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016495 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016496 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016497 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016498
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016499 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016500 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016501
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016502#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016503 if (argvars[0].v_type == VAR_UNKNOWN)
16504 {
16505 /* Find the start and length of the badly spelled word. */
16506 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16507 if (len != 0)
16508 word = ml_get_cursor();
16509 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016510 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016511 {
16512 char_u *str = get_tv_string_chk(&argvars[0]);
16513 int capcol = -1;
16514
16515 if (str != NULL)
16516 {
16517 /* Check the argument for spelling. */
16518 while (*str != NUL)
16519 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016520 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016521 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016522 {
16523 word = str;
16524 break;
16525 }
16526 str += len;
16527 }
16528 }
16529 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016530#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016531
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016532 list_append_string(rettv->vval.v_list, word, len);
16533 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016534 attr == HLF_SPB ? "bad" :
16535 attr == HLF_SPR ? "rare" :
16536 attr == HLF_SPL ? "local" :
16537 attr == HLF_SPC ? "caps" :
16538 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016539}
16540
16541/*
16542 * "spellsuggest()" function
16543 */
16544 static void
16545f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016546 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016547 typval_T *rettv;
16548{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016549#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016550 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016551 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016552 int maxcount;
16553 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016554 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016555 listitem_T *li;
16556 int need_capital = FALSE;
16557#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016558
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016559 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016560 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016561
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016562#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016563 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016564 {
16565 str = get_tv_string(&argvars[0]);
16566 if (argvars[1].v_type != VAR_UNKNOWN)
16567 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016568 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016569 if (maxcount <= 0)
16570 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016571 if (argvars[2].v_type != VAR_UNKNOWN)
16572 {
16573 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16574 if (typeerr)
16575 return;
16576 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016577 }
16578 else
16579 maxcount = 25;
16580
Bram Moolenaar4770d092006-01-12 23:22:24 +000016581 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016582
16583 for (i = 0; i < ga.ga_len; ++i)
16584 {
16585 str = ((char_u **)ga.ga_data)[i];
16586
16587 li = listitem_alloc();
16588 if (li == NULL)
16589 vim_free(str);
16590 else
16591 {
16592 li->li_tv.v_type = VAR_STRING;
16593 li->li_tv.v_lock = 0;
16594 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016595 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016596 }
16597 }
16598 ga_clear(&ga);
16599 }
16600#endif
16601}
16602
Bram Moolenaar0d660222005-01-07 21:51:51 +000016603 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016604f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016605 typval_T *argvars;
16606 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016607{
16608 char_u *str;
16609 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016610 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016611 regmatch_T regmatch;
16612 char_u patbuf[NUMBUFLEN];
16613 char_u *save_cpo;
16614 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016615 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016616 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016617 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016618
16619 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16620 save_cpo = p_cpo;
16621 p_cpo = (char_u *)"";
16622
16623 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016624 if (argvars[1].v_type != VAR_UNKNOWN)
16625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016626 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16627 if (pat == NULL)
16628 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016629 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016630 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016631 }
16632 if (pat == NULL || *pat == NUL)
16633 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016634
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016635 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016637 if (typeerr)
16638 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639
Bram Moolenaar0d660222005-01-07 21:51:51 +000016640 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16641 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016643 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016644 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016645 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016646 if (*str == NUL)
16647 match = FALSE; /* empty item at the end */
16648 else
16649 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016650 if (match)
16651 end = regmatch.startp[0];
16652 else
16653 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016654 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16655 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016656 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016657 if (list_append_string(rettv->vval.v_list, str,
16658 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016659 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016660 }
16661 if (!match)
16662 break;
16663 /* Advance to just after the match. */
16664 if (regmatch.endp[0] > str)
16665 col = 0;
16666 else
16667 {
16668 /* Don't get stuck at the same match. */
16669#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016670 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016671#else
16672 col = 1;
16673#endif
16674 }
16675 str = regmatch.endp[0];
16676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677
Bram Moolenaar0d660222005-01-07 21:51:51 +000016678 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680
Bram Moolenaar0d660222005-01-07 21:51:51 +000016681 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682}
16683
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016684#ifdef FEAT_FLOAT
16685/*
16686 * "sqrt()" function
16687 */
16688 static void
16689f_sqrt(argvars, rettv)
16690 typval_T *argvars;
16691 typval_T *rettv;
16692{
16693 float_T f;
16694
16695 rettv->v_type = VAR_FLOAT;
16696 if (get_float_arg(argvars, &f) == OK)
16697 rettv->vval.v_float = sqrt(f);
16698 else
16699 rettv->vval.v_float = 0.0;
16700}
16701
16702/*
16703 * "str2float()" function
16704 */
16705 static void
16706f_str2float(argvars, rettv)
16707 typval_T *argvars;
16708 typval_T *rettv;
16709{
16710 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16711
16712 if (*p == '+')
16713 p = skipwhite(p + 1);
16714 (void)string2float(p, &rettv->vval.v_float);
16715 rettv->v_type = VAR_FLOAT;
16716}
16717#endif
16718
Bram Moolenaar2c932302006-03-18 21:42:09 +000016719/*
16720 * "str2nr()" function
16721 */
16722 static void
16723f_str2nr(argvars, rettv)
16724 typval_T *argvars;
16725 typval_T *rettv;
16726{
16727 int base = 10;
16728 char_u *p;
16729 long n;
16730
16731 if (argvars[1].v_type != VAR_UNKNOWN)
16732 {
16733 base = get_tv_number(&argvars[1]);
16734 if (base != 8 && base != 10 && base != 16)
16735 {
16736 EMSG(_(e_invarg));
16737 return;
16738 }
16739 }
16740
16741 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016742 if (*p == '+')
16743 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016744 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16745 rettv->vval.v_number = n;
16746}
16747
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748#ifdef HAVE_STRFTIME
16749/*
16750 * "strftime({format}[, {time}])" function
16751 */
16752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016753f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016754 typval_T *argvars;
16755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756{
16757 char_u result_buf[256];
16758 struct tm *curtime;
16759 time_t seconds;
16760 char_u *p;
16761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016762 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016764 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016765 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 seconds = time(NULL);
16767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016768 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 curtime = localtime(&seconds);
16770 /* MSVC returns NULL for an invalid value of seconds. */
16771 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016772 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 else
16774 {
16775# ifdef FEAT_MBYTE
16776 vimconv_T conv;
16777 char_u *enc;
16778
16779 conv.vc_type = CONV_NONE;
16780 enc = enc_locale();
16781 convert_setup(&conv, p_enc, enc);
16782 if (conv.vc_type != CONV_NONE)
16783 p = string_convert(&conv, p, NULL);
16784# endif
16785 if (p != NULL)
16786 (void)strftime((char *)result_buf, sizeof(result_buf),
16787 (char *)p, curtime);
16788 else
16789 result_buf[0] = NUL;
16790
16791# ifdef FEAT_MBYTE
16792 if (conv.vc_type != CONV_NONE)
16793 vim_free(p);
16794 convert_setup(&conv, enc, p_enc);
16795 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016796 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 else
16798# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016799 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800
16801# ifdef FEAT_MBYTE
16802 /* Release conversion descriptors */
16803 convert_setup(&conv, NULL, NULL);
16804 vim_free(enc);
16805# endif
16806 }
16807}
16808#endif
16809
16810/*
16811 * "stridx()" function
16812 */
16813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016814f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016815 typval_T *argvars;
16816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817{
16818 char_u buf[NUMBUFLEN];
16819 char_u *needle;
16820 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016821 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016823 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016825 needle = get_tv_string_chk(&argvars[1]);
16826 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016827 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016828 if (needle == NULL || haystack == NULL)
16829 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830
Bram Moolenaar33570922005-01-25 22:26:29 +000016831 if (argvars[2].v_type != VAR_UNKNOWN)
16832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016833 int error = FALSE;
16834
16835 start_idx = get_tv_number_chk(&argvars[2], &error);
16836 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016837 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016838 if (start_idx >= 0)
16839 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016840 }
16841
16842 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16843 if (pos != NULL)
16844 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845}
16846
16847/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016848 * "string()" function
16849 */
16850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016851f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016852 typval_T *argvars;
16853 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016854{
16855 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016856 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016858 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016859 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016860 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016861 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016862 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863}
16864
16865/*
16866 * "strlen()" function
16867 */
16868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016869f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016870 typval_T *argvars;
16871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016873 rettv->vval.v_number = (varnumber_T)(STRLEN(
16874 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875}
16876
16877/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016878 * "strchars()" function
16879 */
16880 static void
16881f_strchars(argvars, rettv)
16882 typval_T *argvars;
16883 typval_T *rettv;
16884{
16885 char_u *s = get_tv_string(&argvars[0]);
16886#ifdef FEAT_MBYTE
16887 varnumber_T len = 0;
16888
16889 while (*s != NUL)
16890 {
16891 mb_cptr2char_adv(&s);
16892 ++len;
16893 }
16894 rettv->vval.v_number = len;
16895#else
16896 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16897#endif
16898}
16899
16900/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016901 * "strdisplaywidth()" function
16902 */
16903 static void
16904f_strdisplaywidth(argvars, rettv)
16905 typval_T *argvars;
16906 typval_T *rettv;
16907{
16908 char_u *s = get_tv_string(&argvars[0]);
16909 int col = 0;
16910
16911 if (argvars[1].v_type != VAR_UNKNOWN)
16912 col = get_tv_number(&argvars[1]);
16913
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016914 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016915}
16916
16917/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016918 * "strwidth()" function
16919 */
16920 static void
16921f_strwidth(argvars, rettv)
16922 typval_T *argvars;
16923 typval_T *rettv;
16924{
16925 char_u *s = get_tv_string(&argvars[0]);
16926
16927 rettv->vval.v_number = (varnumber_T)(
16928#ifdef FEAT_MBYTE
16929 mb_string2cells(s, -1)
16930#else
16931 STRLEN(s)
16932#endif
16933 );
16934}
16935
16936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 * "strpart()" function
16938 */
16939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016940f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016941 typval_T *argvars;
16942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943{
16944 char_u *p;
16945 int n;
16946 int len;
16947 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016948 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016950 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951 slen = (int)STRLEN(p);
16952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016953 n = get_tv_number_chk(&argvars[1], &error);
16954 if (error)
16955 len = 0;
16956 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016957 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 else
16959 len = slen - n; /* default len: all bytes that are available. */
16960
16961 /*
16962 * Only return the overlap between the specified part and the actual
16963 * string.
16964 */
16965 if (n < 0)
16966 {
16967 len += n;
16968 n = 0;
16969 }
16970 else if (n > slen)
16971 n = slen;
16972 if (len < 0)
16973 len = 0;
16974 else if (n + len > slen)
16975 len = slen - n;
16976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016977 rettv->v_type = VAR_STRING;
16978 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979}
16980
16981/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 * "strridx()" function
16983 */
16984 static void
16985f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016986 typval_T *argvars;
16987 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016988{
16989 char_u buf[NUMBUFLEN];
16990 char_u *needle;
16991 char_u *haystack;
16992 char_u *rest;
16993 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016994 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016996 needle = get_tv_string_chk(&argvars[1]);
16997 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016998
16999 rettv->vval.v_number = -1;
17000 if (needle == NULL || haystack == NULL)
17001 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017002
17003 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017004 if (argvars[2].v_type != VAR_UNKNOWN)
17005 {
17006 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017007 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017008 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017010 }
17011 else
17012 end_idx = haystack_len;
17013
Bram Moolenaar0d660222005-01-07 21:51:51 +000017014 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017015 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017016 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017017 lastmatch = haystack + end_idx;
17018 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017020 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017021 for (rest = haystack; *rest != '\0'; ++rest)
17022 {
17023 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017024 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017025 break;
17026 lastmatch = rest;
17027 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017028 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017029
17030 if (lastmatch == NULL)
17031 rettv->vval.v_number = -1;
17032 else
17033 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17034}
17035
17036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 * "strtrans()" function
17038 */
17039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017040f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017041 typval_T *argvars;
17042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017044 rettv->v_type = VAR_STRING;
17045 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046}
17047
17048/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017049 * "submatch()" function
17050 */
17051 static void
17052f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017053 typval_T *argvars;
17054 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017055{
17056 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017057 rettv->vval.v_string =
17058 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017059}
17060
17061/*
17062 * "substitute()" function
17063 */
17064 static void
17065f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017066 typval_T *argvars;
17067 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017068{
17069 char_u patbuf[NUMBUFLEN];
17070 char_u subbuf[NUMBUFLEN];
17071 char_u flagsbuf[NUMBUFLEN];
17072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017073 char_u *str = get_tv_string_chk(&argvars[0]);
17074 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17075 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17076 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17077
Bram Moolenaar0d660222005-01-07 21:51:51 +000017078 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017079 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17080 rettv->vval.v_string = NULL;
17081 else
17082 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017083}
17084
17085/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017086 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017089f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017090 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092{
17093 int id = 0;
17094#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017095 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 long col;
17097 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017098 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017100 lnum = get_tv_lnum(argvars); /* -1 on type error */
17101 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17102 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017104 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017105 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017106 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107#endif
17108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017109 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110}
17111
17112/*
17113 * "synIDattr(id, what [, mode])" function
17114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017116f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017117 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119{
17120 char_u *p = NULL;
17121#ifdef FEAT_SYN_HL
17122 int id;
17123 char_u *what;
17124 char_u *mode;
17125 char_u modebuf[NUMBUFLEN];
17126 int modec;
17127
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017128 id = get_tv_number(&argvars[0]);
17129 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017130 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017132 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017134 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135 modec = 0; /* replace invalid with current */
17136 }
17137 else
17138 {
17139#ifdef FEAT_GUI
17140 if (gui.in_use)
17141 modec = 'g';
17142 else
17143#endif
17144 if (t_colors > 1)
17145 modec = 'c';
17146 else
17147 modec = 't';
17148 }
17149
17150
17151 switch (TOLOWER_ASC(what[0]))
17152 {
17153 case 'b':
17154 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17155 p = highlight_color(id, what, modec);
17156 else /* bold */
17157 p = highlight_has_attr(id, HL_BOLD, modec);
17158 break;
17159
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017160 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161 p = highlight_color(id, what, modec);
17162 break;
17163
17164 case 'i':
17165 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17166 p = highlight_has_attr(id, HL_INVERSE, modec);
17167 else /* italic */
17168 p = highlight_has_attr(id, HL_ITALIC, modec);
17169 break;
17170
17171 case 'n': /* name */
17172 p = get_highlight_name(NULL, id - 1);
17173 break;
17174
17175 case 'r': /* reverse */
17176 p = highlight_has_attr(id, HL_INVERSE, modec);
17177 break;
17178
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017179 case 's':
17180 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17181 p = highlight_color(id, what, modec);
17182 else /* standout */
17183 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 break;
17185
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017186 case 'u':
17187 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17188 /* underline */
17189 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17190 else
17191 /* undercurl */
17192 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 break;
17194 }
17195
17196 if (p != NULL)
17197 p = vim_strsave(p);
17198#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017199 rettv->v_type = VAR_STRING;
17200 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017201}
17202
17203/*
17204 * "synIDtrans(id)" function
17205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017207f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017208 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210{
17211 int id;
17212
17213#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017214 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215
17216 if (id > 0)
17217 id = syn_get_final_id(id);
17218 else
17219#endif
17220 id = 0;
17221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017222 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223}
17224
17225/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017226 * "synconcealed(lnum, col)" function
17227 */
17228 static void
17229f_synconcealed(argvars, rettv)
17230 typval_T *argvars UNUSED;
17231 typval_T *rettv;
17232{
17233#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17234 long lnum;
17235 long col;
17236 int syntax_flags = 0;
17237 int cchar;
17238 int matchid = 0;
17239 char_u str[NUMBUFLEN];
17240#endif
17241
17242 rettv->v_type = VAR_LIST;
17243 rettv->vval.v_list = NULL;
17244
17245#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17246 lnum = get_tv_lnum(argvars); /* -1 on type error */
17247 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17248
17249 vim_memset(str, NUL, sizeof(str));
17250
17251 if (rettv_list_alloc(rettv) != FAIL)
17252 {
17253 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17254 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17255 && curwin->w_p_cole > 0)
17256 {
17257 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17258 syntax_flags = get_syntax_info(&matchid);
17259
17260 /* get the conceal character */
17261 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17262 {
17263 cchar = syn_get_sub_char();
17264 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17265 cchar = lcs_conceal;
17266 if (cchar != NUL)
17267 {
17268# ifdef FEAT_MBYTE
17269 if (has_mbyte)
17270 (*mb_char2bytes)(cchar, str);
17271 else
17272# endif
17273 str[0] = cchar;
17274 }
17275 }
17276 }
17277
17278 list_append_number(rettv->vval.v_list,
17279 (syntax_flags & HL_CONCEAL) != 0);
17280 /* -1 to auto-determine strlen */
17281 list_append_string(rettv->vval.v_list, str, -1);
17282 list_append_number(rettv->vval.v_list, matchid);
17283 }
17284#endif
17285}
17286
17287/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017288 * "synstack(lnum, col)" function
17289 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017290 static void
17291f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017292 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017293 typval_T *rettv;
17294{
17295#ifdef FEAT_SYN_HL
17296 long lnum;
17297 long col;
17298 int i;
17299 int id;
17300#endif
17301
17302 rettv->v_type = VAR_LIST;
17303 rettv->vval.v_list = NULL;
17304
17305#ifdef FEAT_SYN_HL
17306 lnum = get_tv_lnum(argvars); /* -1 on type error */
17307 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17308
17309 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017310 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017311 && rettv_list_alloc(rettv) != FAIL)
17312 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017313 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017314 for (i = 0; ; ++i)
17315 {
17316 id = syn_get_stack_item(i);
17317 if (id < 0)
17318 break;
17319 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17320 break;
17321 }
17322 }
17323#endif
17324}
17325
17326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 * "system()" function
17328 */
17329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017330f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017331 typval_T *argvars;
17332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017334 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017336 char_u *infile = NULL;
17337 char_u buf[NUMBUFLEN];
17338 int err = FALSE;
17339 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017341 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017342 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017343
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017344 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017345 {
17346 /*
17347 * Write the string to a temp file, to be used for input of the shell
17348 * command.
17349 */
17350 if ((infile = vim_tempname('i')) == NULL)
17351 {
17352 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017353 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017354 }
17355
17356 fd = mch_fopen((char *)infile, WRITEBIN);
17357 if (fd == NULL)
17358 {
17359 EMSG2(_(e_notopen), infile);
17360 goto done;
17361 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017362 p = get_tv_string_buf_chk(&argvars[1], buf);
17363 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017364 {
17365 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017366 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017367 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017368 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17369 err = TRUE;
17370 if (fclose(fd) != 0)
17371 err = TRUE;
17372 if (err)
17373 {
17374 EMSG(_("E677: Error writing temp file"));
17375 goto done;
17376 }
17377 }
17378
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017379 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17380 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017381
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382#ifdef USE_CR
17383 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017384 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 {
17386 char_u *s;
17387
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017388 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 {
17390 if (*s == CAR)
17391 *s = NL;
17392 }
17393 }
17394#else
17395# ifdef USE_CRNL
17396 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017397 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398 {
17399 char_u *s, *d;
17400
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017401 d = res;
17402 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 {
17404 if (s[0] == CAR && s[1] == NL)
17405 ++s;
17406 *d++ = *s;
17407 }
17408 *d = NUL;
17409 }
17410# endif
17411#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017412
17413done:
17414 if (infile != NULL)
17415 {
17416 mch_remove(infile);
17417 vim_free(infile);
17418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017419 rettv->v_type = VAR_STRING;
17420 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421}
17422
17423/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017424 * "tabpagebuflist()" function
17425 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017426 static void
17427f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017428 typval_T *argvars UNUSED;
17429 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017430{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017431#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017432 tabpage_T *tp;
17433 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017434
17435 if (argvars[0].v_type == VAR_UNKNOWN)
17436 wp = firstwin;
17437 else
17438 {
17439 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17440 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017441 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017442 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017443 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017444 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017445 for (; wp != NULL; wp = wp->w_next)
17446 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017447 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017448 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017449 }
17450#endif
17451}
17452
17453
17454/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017455 * "tabpagenr()" function
17456 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017457 static void
17458f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017459 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017460 typval_T *rettv;
17461{
17462 int nr = 1;
17463#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017464 char_u *arg;
17465
17466 if (argvars[0].v_type != VAR_UNKNOWN)
17467 {
17468 arg = get_tv_string_chk(&argvars[0]);
17469 nr = 0;
17470 if (arg != NULL)
17471 {
17472 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017473 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017474 else
17475 EMSG2(_(e_invexpr2), arg);
17476 }
17477 }
17478 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017479 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017480#endif
17481 rettv->vval.v_number = nr;
17482}
17483
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017484
17485#ifdef FEAT_WINDOWS
17486static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17487
17488/*
17489 * Common code for tabpagewinnr() and winnr().
17490 */
17491 static int
17492get_winnr(tp, argvar)
17493 tabpage_T *tp;
17494 typval_T *argvar;
17495{
17496 win_T *twin;
17497 int nr = 1;
17498 win_T *wp;
17499 char_u *arg;
17500
17501 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17502 if (argvar->v_type != VAR_UNKNOWN)
17503 {
17504 arg = get_tv_string_chk(argvar);
17505 if (arg == NULL)
17506 nr = 0; /* type error; errmsg already given */
17507 else if (STRCMP(arg, "$") == 0)
17508 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17509 else if (STRCMP(arg, "#") == 0)
17510 {
17511 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17512 if (twin == NULL)
17513 nr = 0;
17514 }
17515 else
17516 {
17517 EMSG2(_(e_invexpr2), arg);
17518 nr = 0;
17519 }
17520 }
17521
17522 if (nr > 0)
17523 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17524 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017525 {
17526 if (wp == NULL)
17527 {
17528 /* didn't find it in this tabpage */
17529 nr = 0;
17530 break;
17531 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017532 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017533 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017534 return nr;
17535}
17536#endif
17537
17538/*
17539 * "tabpagewinnr()" function
17540 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017541 static void
17542f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017543 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017544 typval_T *rettv;
17545{
17546 int nr = 1;
17547#ifdef FEAT_WINDOWS
17548 tabpage_T *tp;
17549
17550 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17551 if (tp == NULL)
17552 nr = 0;
17553 else
17554 nr = get_winnr(tp, &argvars[1]);
17555#endif
17556 rettv->vval.v_number = nr;
17557}
17558
17559
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017560/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017561 * "tagfiles()" function
17562 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017563 static void
17564f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017565 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017566 typval_T *rettv;
17567{
17568 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017569 tagname_T tn;
17570 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017571
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017572 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017573 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017574
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017575 for (first = TRUE; ; first = FALSE)
17576 if (get_tagfname(&tn, first, fname) == FAIL
17577 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017578 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017579 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017580}
17581
17582/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017583 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017584 */
17585 static void
17586f_taglist(argvars, rettv)
17587 typval_T *argvars;
17588 typval_T *rettv;
17589{
17590 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017591
17592 tag_pattern = get_tv_string(&argvars[0]);
17593
17594 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017595 if (*tag_pattern == NUL)
17596 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017597
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017598 if (rettv_list_alloc(rettv) == OK)
17599 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017600}
17601
17602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 * "tempname()" function
17604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017606f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017607 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609{
17610 static int x = 'A';
17611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017612 rettv->v_type = VAR_STRING;
17613 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614
17615 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17616 * names. Skip 'I' and 'O', they are used for shell redirection. */
17617 do
17618 {
17619 if (x == 'Z')
17620 x = '0';
17621 else if (x == '9')
17622 x = 'A';
17623 else
17624 {
17625#ifdef EBCDIC
17626 if (x == 'I')
17627 x = 'J';
17628 else if (x == 'R')
17629 x = 'S';
17630 else
17631#endif
17632 ++x;
17633 }
17634 } while (x == 'I' || x == 'O');
17635}
17636
17637/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017638 * "test(list)" function: Just checking the walls...
17639 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017640 static void
17641f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017642 typval_T *argvars UNUSED;
17643 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017644{
17645 /* Used for unit testing. Change the code below to your liking. */
17646#if 0
17647 listitem_T *li;
17648 list_T *l;
17649 char_u *bad, *good;
17650
17651 if (argvars[0].v_type != VAR_LIST)
17652 return;
17653 l = argvars[0].vval.v_list;
17654 if (l == NULL)
17655 return;
17656 li = l->lv_first;
17657 if (li == NULL)
17658 return;
17659 bad = get_tv_string(&li->li_tv);
17660 li = li->li_next;
17661 if (li == NULL)
17662 return;
17663 good = get_tv_string(&li->li_tv);
17664 rettv->vval.v_number = test_edit_score(bad, good);
17665#endif
17666}
17667
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017668#ifdef FEAT_FLOAT
17669/*
17670 * "tan()" function
17671 */
17672 static void
17673f_tan(argvars, rettv)
17674 typval_T *argvars;
17675 typval_T *rettv;
17676{
17677 float_T f;
17678
17679 rettv->v_type = VAR_FLOAT;
17680 if (get_float_arg(argvars, &f) == OK)
17681 rettv->vval.v_float = tan(f);
17682 else
17683 rettv->vval.v_float = 0.0;
17684}
17685
17686/*
17687 * "tanh()" function
17688 */
17689 static void
17690f_tanh(argvars, rettv)
17691 typval_T *argvars;
17692 typval_T *rettv;
17693{
17694 float_T f;
17695
17696 rettv->v_type = VAR_FLOAT;
17697 if (get_float_arg(argvars, &f) == OK)
17698 rettv->vval.v_float = tanh(f);
17699 else
17700 rettv->vval.v_float = 0.0;
17701}
17702#endif
17703
Bram Moolenaard52d9742005-08-21 22:20:28 +000017704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 * "tolower(string)" function
17706 */
17707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017708f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017709 typval_T *argvars;
17710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711{
17712 char_u *p;
17713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017714 p = vim_strsave(get_tv_string(&argvars[0]));
17715 rettv->v_type = VAR_STRING;
17716 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717
17718 if (p != NULL)
17719 while (*p != NUL)
17720 {
17721#ifdef FEAT_MBYTE
17722 int l;
17723
17724 if (enc_utf8)
17725 {
17726 int c, lc;
17727
17728 c = utf_ptr2char(p);
17729 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017730 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731 /* TODO: reallocate string when byte count changes. */
17732 if (utf_char2len(lc) == l)
17733 utf_char2bytes(lc, p);
17734 p += l;
17735 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017736 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 p += l; /* skip multi-byte character */
17738 else
17739#endif
17740 {
17741 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17742 ++p;
17743 }
17744 }
17745}
17746
17747/*
17748 * "toupper(string)" function
17749 */
17750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017751f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017752 typval_T *argvars;
17753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017755 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017756 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757}
17758
17759/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017760 * "tr(string, fromstr, tostr)" function
17761 */
17762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017763f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017764 typval_T *argvars;
17765 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017766{
17767 char_u *instr;
17768 char_u *fromstr;
17769 char_u *tostr;
17770 char_u *p;
17771#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017772 int inlen;
17773 int fromlen;
17774 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017775 int idx;
17776 char_u *cpstr;
17777 int cplen;
17778 int first = TRUE;
17779#endif
17780 char_u buf[NUMBUFLEN];
17781 char_u buf2[NUMBUFLEN];
17782 garray_T ga;
17783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017784 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017785 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17786 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017787
17788 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017789 rettv->v_type = VAR_STRING;
17790 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017791 if (fromstr == NULL || tostr == NULL)
17792 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017793 ga_init2(&ga, (int)sizeof(char), 80);
17794
17795#ifdef FEAT_MBYTE
17796 if (!has_mbyte)
17797#endif
17798 /* not multi-byte: fromstr and tostr must be the same length */
17799 if (STRLEN(fromstr) != STRLEN(tostr))
17800 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017801#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017802error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017803#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017804 EMSG2(_(e_invarg2), fromstr);
17805 ga_clear(&ga);
17806 return;
17807 }
17808
17809 /* fromstr and tostr have to contain the same number of chars */
17810 while (*instr != NUL)
17811 {
17812#ifdef FEAT_MBYTE
17813 if (has_mbyte)
17814 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017815 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017816 cpstr = instr;
17817 cplen = inlen;
17818 idx = 0;
17819 for (p = fromstr; *p != NUL; p += fromlen)
17820 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017821 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017822 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17823 {
17824 for (p = tostr; *p != NUL; p += tolen)
17825 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017826 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017827 if (idx-- == 0)
17828 {
17829 cplen = tolen;
17830 cpstr = p;
17831 break;
17832 }
17833 }
17834 if (*p == NUL) /* tostr is shorter than fromstr */
17835 goto error;
17836 break;
17837 }
17838 ++idx;
17839 }
17840
17841 if (first && cpstr == instr)
17842 {
17843 /* Check that fromstr and tostr have the same number of
17844 * (multi-byte) characters. Done only once when a character
17845 * of instr doesn't appear in fromstr. */
17846 first = FALSE;
17847 for (p = tostr; *p != NUL; p += tolen)
17848 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017849 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017850 --idx;
17851 }
17852 if (idx != 0)
17853 goto error;
17854 }
17855
17856 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017857 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017858 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017859
17860 instr += inlen;
17861 }
17862 else
17863#endif
17864 {
17865 /* When not using multi-byte chars we can do it faster. */
17866 p = vim_strchr(fromstr, *instr);
17867 if (p != NULL)
17868 ga_append(&ga, tostr[p - fromstr]);
17869 else
17870 ga_append(&ga, *instr);
17871 ++instr;
17872 }
17873 }
17874
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017875 /* add a terminating NUL */
17876 ga_grow(&ga, 1);
17877 ga_append(&ga, NUL);
17878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017879 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017880}
17881
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017882#ifdef FEAT_FLOAT
17883/*
17884 * "trunc({float})" function
17885 */
17886 static void
17887f_trunc(argvars, rettv)
17888 typval_T *argvars;
17889 typval_T *rettv;
17890{
17891 float_T f;
17892
17893 rettv->v_type = VAR_FLOAT;
17894 if (get_float_arg(argvars, &f) == OK)
17895 /* trunc() is not in C90, use floor() or ceil() instead. */
17896 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17897 else
17898 rettv->vval.v_float = 0.0;
17899}
17900#endif
17901
Bram Moolenaar8299df92004-07-10 09:47:34 +000017902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 * "type(expr)" function
17904 */
17905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017906f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017907 typval_T *argvars;
17908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017910 int n;
17911
17912 switch (argvars[0].v_type)
17913 {
17914 case VAR_NUMBER: n = 0; break;
17915 case VAR_STRING: n = 1; break;
17916 case VAR_FUNC: n = 2; break;
17917 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017918 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017919#ifdef FEAT_FLOAT
17920 case VAR_FLOAT: n = 5; break;
17921#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017922 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17923 }
17924 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925}
17926
17927/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017928 * "undofile(name)" function
17929 */
17930 static void
17931f_undofile(argvars, rettv)
17932 typval_T *argvars;
17933 typval_T *rettv;
17934{
17935 rettv->v_type = VAR_STRING;
17936#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017937 {
17938 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17939
17940 if (ffname != NULL)
17941 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17942 vim_free(ffname);
17943 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017944#else
17945 rettv->vval.v_string = NULL;
17946#endif
17947}
17948
17949/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017950 * "undotree()" function
17951 */
17952 static void
17953f_undotree(argvars, rettv)
17954 typval_T *argvars UNUSED;
17955 typval_T *rettv;
17956{
17957 if (rettv_dict_alloc(rettv) == OK)
17958 {
17959 dict_T *dict = rettv->vval.v_dict;
17960 list_T *list;
17961
Bram Moolenaar730cde92010-06-27 05:18:54 +020017962 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017963 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017964 dict_add_nr_str(dict, "save_last",
17965 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017966 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17967 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017968 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017969
17970 list = list_alloc();
17971 if (list != NULL)
17972 {
17973 u_eval_tree(curbuf->b_u_oldhead, list);
17974 dict_add_list(dict, "entries", list);
17975 }
17976 }
17977}
17978
17979/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017980 * "values(dict)" function
17981 */
17982 static void
17983f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017984 typval_T *argvars;
17985 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017986{
17987 dict_list(argvars, rettv, 1);
17988}
17989
17990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 * "virtcol(string)" function
17992 */
17993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017994f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017995 typval_T *argvars;
17996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997{
17998 colnr_T vcol = 0;
17999 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018000 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018002 fp = var2fpos(&argvars[0], FALSE, &fnum);
18003 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18004 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 {
18006 getvvcol(curwin, fp, NULL, NULL, &vcol);
18007 ++vcol;
18008 }
18009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018010 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011}
18012
18013/*
18014 * "visualmode()" function
18015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018017f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018018 typval_T *argvars UNUSED;
18019 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020{
18021#ifdef FEAT_VISUAL
18022 char_u str[2];
18023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018024 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025 str[0] = curbuf->b_visual_mode_eval;
18026 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018027 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028
18029 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018030 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032#endif
18033}
18034
18035/*
18036 * "winbufnr(nr)" function
18037 */
18038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018039f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018040 typval_T *argvars;
18041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042{
18043 win_T *wp;
18044
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018045 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018047 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018049 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050}
18051
18052/*
18053 * "wincol()" function
18054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018056f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018057 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059{
18060 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018061 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062}
18063
18064/*
18065 * "winheight(nr)" function
18066 */
18067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018068f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018069 typval_T *argvars;
18070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071{
18072 win_T *wp;
18073
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018074 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018076 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018078 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079}
18080
18081/*
18082 * "winline()" function
18083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018085f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018086 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088{
18089 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018090 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091}
18092
18093/*
18094 * "winnr()" function
18095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018097f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018098 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100{
18101 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018102
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018104 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018106 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107}
18108
18109/*
18110 * "winrestcmd()" function
18111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018113f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018114 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116{
18117#ifdef FEAT_WINDOWS
18118 win_T *wp;
18119 int winnr = 1;
18120 garray_T ga;
18121 char_u buf[50];
18122
18123 ga_init2(&ga, (int)sizeof(char), 70);
18124 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18125 {
18126 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18127 ga_concat(&ga, buf);
18128# ifdef FEAT_VERTSPLIT
18129 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18130 ga_concat(&ga, buf);
18131# endif
18132 ++winnr;
18133 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018134 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018136 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018138 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018140 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141}
18142
18143/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018144 * "winrestview()" function
18145 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018146 static void
18147f_winrestview(argvars, rettv)
18148 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018149 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018150{
18151 dict_T *dict;
18152
18153 if (argvars[0].v_type != VAR_DICT
18154 || (dict = argvars[0].vval.v_dict) == NULL)
18155 EMSG(_(e_invarg));
18156 else
18157 {
18158 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18159 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18160#ifdef FEAT_VIRTUALEDIT
18161 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18162#endif
18163 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018164 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018165
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018166 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018167#ifdef FEAT_DIFF
18168 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18169#endif
18170 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18171 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18172
18173 check_cursor();
18174 changed_cline_bef_curs();
18175 invalidate_botline();
18176 redraw_later(VALID);
18177
18178 if (curwin->w_topline == 0)
18179 curwin->w_topline = 1;
18180 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18181 curwin->w_topline = curbuf->b_ml.ml_line_count;
18182#ifdef FEAT_DIFF
18183 check_topfill(curwin, TRUE);
18184#endif
18185 }
18186}
18187
18188/*
18189 * "winsaveview()" function
18190 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018191 static void
18192f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018193 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018194 typval_T *rettv;
18195{
18196 dict_T *dict;
18197
Bram Moolenaara800b422010-06-27 01:15:55 +020018198 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018199 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018200 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018201
18202 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18203 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18204#ifdef FEAT_VIRTUALEDIT
18205 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18206#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018207 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018208 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18209
18210 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18211#ifdef FEAT_DIFF
18212 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18213#endif
18214 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18215 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18216}
18217
18218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 * "winwidth(nr)" function
18220 */
18221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018222f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018223 typval_T *argvars;
18224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225{
18226 win_T *wp;
18227
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018228 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018230 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 else
18232#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018233 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018235 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236#endif
18237}
18238
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018240 * "writefile()" function
18241 */
18242 static void
18243f_writefile(argvars, rettv)
18244 typval_T *argvars;
18245 typval_T *rettv;
18246{
18247 int binary = FALSE;
18248 char_u *fname;
18249 FILE *fd;
18250 listitem_T *li;
18251 char_u *s;
18252 int ret = 0;
18253 int c;
18254
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018255 if (check_restricted() || check_secure())
18256 return;
18257
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018258 if (argvars[0].v_type != VAR_LIST)
18259 {
18260 EMSG2(_(e_listarg), "writefile()");
18261 return;
18262 }
18263 if (argvars[0].vval.v_list == NULL)
18264 return;
18265
18266 if (argvars[2].v_type != VAR_UNKNOWN
18267 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18268 binary = TRUE;
18269
18270 /* Always open the file in binary mode, library functions have a mind of
18271 * their own about CR-LF conversion. */
18272 fname = get_tv_string(&argvars[1]);
18273 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18274 {
18275 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18276 ret = -1;
18277 }
18278 else
18279 {
18280 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18281 li = li->li_next)
18282 {
18283 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18284 {
18285 if (*s == '\n')
18286 c = putc(NUL, fd);
18287 else
18288 c = putc(*s, fd);
18289 if (c == EOF)
18290 {
18291 ret = -1;
18292 break;
18293 }
18294 }
18295 if (!binary || li->li_next != NULL)
18296 if (putc('\n', fd) == EOF)
18297 {
18298 ret = -1;
18299 break;
18300 }
18301 if (ret < 0)
18302 {
18303 EMSG(_(e_write));
18304 break;
18305 }
18306 }
18307 fclose(fd);
18308 }
18309
18310 rettv->vval.v_number = ret;
18311}
18312
18313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018315 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 */
18317 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018318var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018319 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018320 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018321 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018323 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018325 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326
Bram Moolenaara5525202006-03-02 22:52:09 +000018327 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018328 if (varp->v_type == VAR_LIST)
18329 {
18330 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018331 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018332 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018333 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018334
18335 l = varp->vval.v_list;
18336 if (l == NULL)
18337 return NULL;
18338
18339 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018340 pos.lnum = list_find_nr(l, 0L, &error);
18341 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018342 return NULL; /* invalid line number */
18343
18344 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018345 pos.col = list_find_nr(l, 1L, &error);
18346 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018347 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018348 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018349
18350 /* We accept "$" for the column number: last column. */
18351 li = list_find(l, 1L);
18352 if (li != NULL && li->li_tv.v_type == VAR_STRING
18353 && li->li_tv.vval.v_string != NULL
18354 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18355 pos.col = len + 1;
18356
Bram Moolenaara5525202006-03-02 22:52:09 +000018357 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018358 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018359 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018360 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018361
Bram Moolenaara5525202006-03-02 22:52:09 +000018362#ifdef FEAT_VIRTUALEDIT
18363 /* Get the virtual offset. Defaults to zero. */
18364 pos.coladd = list_find_nr(l, 2L, &error);
18365 if (error)
18366 pos.coladd = 0;
18367#endif
18368
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018369 return &pos;
18370 }
18371
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018372 name = get_tv_string_chk(varp);
18373 if (name == NULL)
18374 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018375 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018377#ifdef FEAT_VISUAL
18378 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18379 {
18380 if (VIsual_active)
18381 return &VIsual;
18382 return &curwin->w_cursor;
18383 }
18384#endif
18385 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018387 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18389 return NULL;
18390 return pp;
18391 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018392
18393#ifdef FEAT_VIRTUALEDIT
18394 pos.coladd = 0;
18395#endif
18396
Bram Moolenaar477933c2007-07-17 14:32:23 +000018397 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018398 {
18399 pos.col = 0;
18400 if (name[1] == '0') /* "w0": first visible line */
18401 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018402 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018403 pos.lnum = curwin->w_topline;
18404 return &pos;
18405 }
18406 else if (name[1] == '$') /* "w$": last visible line */
18407 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018408 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018409 pos.lnum = curwin->w_botline - 1;
18410 return &pos;
18411 }
18412 }
18413 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018415 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416 {
18417 pos.lnum = curbuf->b_ml.ml_line_count;
18418 pos.col = 0;
18419 }
18420 else
18421 {
18422 pos.lnum = curwin->w_cursor.lnum;
18423 pos.col = (colnr_T)STRLEN(ml_get_curline());
18424 }
18425 return &pos;
18426 }
18427 return NULL;
18428}
18429
18430/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018431 * Convert list in "arg" into a position and optional file number.
18432 * When "fnump" is NULL there is no file number, only 3 items.
18433 * Note that the column is passed on as-is, the caller may want to decrement
18434 * it to use 1 for the first column.
18435 * Return FAIL when conversion is not possible, doesn't check the position for
18436 * validity.
18437 */
18438 static int
18439list2fpos(arg, posp, fnump)
18440 typval_T *arg;
18441 pos_T *posp;
18442 int *fnump;
18443{
18444 list_T *l = arg->vval.v_list;
18445 long i = 0;
18446 long n;
18447
Bram Moolenaarbde35262006-07-23 20:12:24 +000018448 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18449 * when "fnump" isn't NULL and "coladd" is optional. */
18450 if (arg->v_type != VAR_LIST
18451 || l == NULL
18452 || l->lv_len < (fnump == NULL ? 2 : 3)
18453 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018454 return FAIL;
18455
18456 if (fnump != NULL)
18457 {
18458 n = list_find_nr(l, i++, NULL); /* fnum */
18459 if (n < 0)
18460 return FAIL;
18461 if (n == 0)
18462 n = curbuf->b_fnum; /* current buffer */
18463 *fnump = n;
18464 }
18465
18466 n = list_find_nr(l, i++, NULL); /* lnum */
18467 if (n < 0)
18468 return FAIL;
18469 posp->lnum = n;
18470
18471 n = list_find_nr(l, i++, NULL); /* col */
18472 if (n < 0)
18473 return FAIL;
18474 posp->col = n;
18475
18476#ifdef FEAT_VIRTUALEDIT
18477 n = list_find_nr(l, i, NULL);
18478 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018479 posp->coladd = 0;
18480 else
18481 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018482#endif
18483
18484 return OK;
18485}
18486
18487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 * Get the length of an environment variable name.
18489 * Advance "arg" to the first character after the name.
18490 * Return 0 for error.
18491 */
18492 static int
18493get_env_len(arg)
18494 char_u **arg;
18495{
18496 char_u *p;
18497 int len;
18498
18499 for (p = *arg; vim_isIDc(*p); ++p)
18500 ;
18501 if (p == *arg) /* no name found */
18502 return 0;
18503
18504 len = (int)(p - *arg);
18505 *arg = p;
18506 return len;
18507}
18508
18509/*
18510 * Get the length of the name of a function or internal variable.
18511 * "arg" is advanced to the first non-white character after the name.
18512 * Return 0 if something is wrong.
18513 */
18514 static int
18515get_id_len(arg)
18516 char_u **arg;
18517{
18518 char_u *p;
18519 int len;
18520
18521 /* Find the end of the name. */
18522 for (p = *arg; eval_isnamec(*p); ++p)
18523 ;
18524 if (p == *arg) /* no name found */
18525 return 0;
18526
18527 len = (int)(p - *arg);
18528 *arg = skipwhite(p);
18529
18530 return len;
18531}
18532
18533/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018534 * Get the length of the name of a variable or function.
18535 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018537 * Return -1 if curly braces expansion failed.
18538 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 * If the name contains 'magic' {}'s, expand them and return the
18540 * expanded name in an allocated string via 'alias' - caller must free.
18541 */
18542 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018543get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 char_u **arg;
18545 char_u **alias;
18546 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018547 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548{
18549 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 char_u *p;
18551 char_u *expr_start;
18552 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553
18554 *alias = NULL; /* default to no alias */
18555
18556 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18557 && (*arg)[2] == (int)KE_SNR)
18558 {
18559 /* hard coded <SNR>, already translated */
18560 *arg += 3;
18561 return get_id_len(arg) + 3;
18562 }
18563 len = eval_fname_script(*arg);
18564 if (len > 0)
18565 {
18566 /* literal "<SID>", "s:" or "<SNR>" */
18567 *arg += len;
18568 }
18569
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018571 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018573 p = find_name_end(*arg, &expr_start, &expr_end,
18574 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 if (expr_start != NULL)
18576 {
18577 char_u *temp_string;
18578
18579 if (!evaluate)
18580 {
18581 len += (int)(p - *arg);
18582 *arg = skipwhite(p);
18583 return len;
18584 }
18585
18586 /*
18587 * Include any <SID> etc in the expanded string:
18588 * Thus the -len here.
18589 */
18590 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18591 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018592 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 *alias = temp_string;
18594 *arg = skipwhite(p);
18595 return (int)STRLEN(temp_string);
18596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597
18598 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018599 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600 EMSG2(_(e_invexpr2), *arg);
18601
18602 return len;
18603}
18604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018605/*
18606 * Find the end of a variable or function name, taking care of magic braces.
18607 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18608 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018609 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018610 * Return a pointer to just after the name. Equal to "arg" if there is no
18611 * valid name.
18612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018614find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 char_u *arg;
18616 char_u **expr_start;
18617 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018618 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018620 int mb_nest = 0;
18621 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622 char_u *p;
18623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018624 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018626 *expr_start = NULL;
18627 *expr_end = NULL;
18628 }
18629
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018630 /* Quick check for valid starting character. */
18631 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18632 return arg;
18633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018634 for (p = arg; *p != NUL
18635 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018636 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018637 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018638 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018639 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018640 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018641 if (*p == '\'')
18642 {
18643 /* skip over 'string' to avoid counting [ and ] inside it. */
18644 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18645 ;
18646 if (*p == NUL)
18647 break;
18648 }
18649 else if (*p == '"')
18650 {
18651 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18652 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18653 if (*p == '\\' && p[1] != NUL)
18654 ++p;
18655 if (*p == NUL)
18656 break;
18657 }
18658
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018659 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018661 if (*p == '[')
18662 ++br_nest;
18663 else if (*p == ']')
18664 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018666
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018667 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018669 if (*p == '{')
18670 {
18671 mb_nest++;
18672 if (expr_start != NULL && *expr_start == NULL)
18673 *expr_start = p;
18674 }
18675 else if (*p == '}')
18676 {
18677 mb_nest--;
18678 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18679 *expr_end = p;
18680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682 }
18683
18684 return p;
18685}
18686
18687/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018688 * Expands out the 'magic' {}'s in a variable/function name.
18689 * Note that this can call itself recursively, to deal with
18690 * constructs like foo{bar}{baz}{bam}
18691 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18692 * "in_start" ^
18693 * "expr_start" ^
18694 * "expr_end" ^
18695 * "in_end" ^
18696 *
18697 * Returns a new allocated string, which the caller must free.
18698 * Returns NULL for failure.
18699 */
18700 static char_u *
18701make_expanded_name(in_start, expr_start, expr_end, in_end)
18702 char_u *in_start;
18703 char_u *expr_start;
18704 char_u *expr_end;
18705 char_u *in_end;
18706{
18707 char_u c1;
18708 char_u *retval = NULL;
18709 char_u *temp_result;
18710 char_u *nextcmd = NULL;
18711
18712 if (expr_end == NULL || in_end == NULL)
18713 return NULL;
18714 *expr_start = NUL;
18715 *expr_end = NUL;
18716 c1 = *in_end;
18717 *in_end = NUL;
18718
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018719 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018720 if (temp_result != NULL && nextcmd == NULL)
18721 {
18722 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18723 + (in_end - expr_end) + 1));
18724 if (retval != NULL)
18725 {
18726 STRCPY(retval, in_start);
18727 STRCAT(retval, temp_result);
18728 STRCAT(retval, expr_end + 1);
18729 }
18730 }
18731 vim_free(temp_result);
18732
18733 *in_end = c1; /* put char back for error messages */
18734 *expr_start = '{';
18735 *expr_end = '}';
18736
18737 if (retval != NULL)
18738 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018739 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018740 if (expr_start != NULL)
18741 {
18742 /* Further expansion! */
18743 temp_result = make_expanded_name(retval, expr_start,
18744 expr_end, temp_result);
18745 vim_free(retval);
18746 retval = temp_result;
18747 }
18748 }
18749
18750 return retval;
18751}
18752
18753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018755 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 */
18757 static int
18758eval_isnamec(c)
18759 int c;
18760{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018761 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18762}
18763
18764/*
18765 * Return TRUE if character "c" can be used as the first character in a
18766 * variable or function name (excluding '{' and '}').
18767 */
18768 static int
18769eval_isnamec1(c)
18770 int c;
18771{
18772 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773}
18774
18775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776 * Set number v: variable to "val".
18777 */
18778 void
18779set_vim_var_nr(idx, val)
18780 int idx;
18781 long val;
18782{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018783 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784}
18785
18786/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018787 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 */
18789 long
18790get_vim_var_nr(idx)
18791 int idx;
18792{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018793 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794}
18795
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018796/*
18797 * Get string v: variable value. Uses a static buffer, can only be used once.
18798 */
18799 char_u *
18800get_vim_var_str(idx)
18801 int idx;
18802{
18803 return get_tv_string(&vimvars[idx].vv_tv);
18804}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018805
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018807 * Get List v: variable value. Caller must take care of reference count when
18808 * needed.
18809 */
18810 list_T *
18811get_vim_var_list(idx)
18812 int idx;
18813{
18814 return vimvars[idx].vv_list;
18815}
18816
18817/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018818 * Set v:char to character "c".
18819 */
18820 void
18821set_vim_var_char(c)
18822 int c;
18823{
18824#ifdef FEAT_MBYTE
18825 char_u buf[MB_MAXBYTES];
18826#else
18827 char_u buf[2];
18828#endif
18829
18830#ifdef FEAT_MBYTE
18831 if (has_mbyte)
18832 buf[(*mb_char2bytes)(c, buf)] = NUL;
18833 else
18834#endif
18835 {
18836 buf[0] = c;
18837 buf[1] = NUL;
18838 }
18839 set_vim_var_string(VV_CHAR, buf, -1);
18840}
18841
18842/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018843 * Set v:count to "count" and v:count1 to "count1".
18844 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 */
18846 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018847set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848 long count;
18849 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018850 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018852 if (set_prevcount)
18853 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018854 vimvars[VV_COUNT].vv_nr = count;
18855 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856}
18857
18858/*
18859 * Set string v: variable to a copy of "val".
18860 */
18861 void
18862set_vim_var_string(idx, val, len)
18863 int idx;
18864 char_u *val;
18865 int len; /* length of "val" to use or -1 (whole string) */
18866{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018867 /* Need to do this (at least) once, since we can't initialize a union.
18868 * Will always be invoked when "v:progname" is set. */
18869 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18870
Bram Moolenaare9a41262005-01-15 22:18:47 +000018871 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018873 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018875 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018877 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878}
18879
18880/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018881 * Set List v: variable to "val".
18882 */
18883 void
18884set_vim_var_list(idx, val)
18885 int idx;
18886 list_T *val;
18887{
18888 list_unref(vimvars[idx].vv_list);
18889 vimvars[idx].vv_list = val;
18890 if (val != NULL)
18891 ++val->lv_refcount;
18892}
18893
18894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 * Set v:register if needed.
18896 */
18897 void
18898set_reg_var(c)
18899 int c;
18900{
18901 char_u regname;
18902
18903 if (c == 0 || c == ' ')
18904 regname = '"';
18905 else
18906 regname = c;
18907 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018908 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 set_vim_var_string(VV_REG, &regname, 1);
18910}
18911
18912/*
18913 * Get or set v:exception. If "oldval" == NULL, return the current value.
18914 * Otherwise, restore the value to "oldval" and return NULL.
18915 * Must always be called in pairs to save and restore v:exception! Does not
18916 * take care of memory allocations.
18917 */
18918 char_u *
18919v_exception(oldval)
18920 char_u *oldval;
18921{
18922 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018923 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924
Bram Moolenaare9a41262005-01-15 22:18:47 +000018925 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 return NULL;
18927}
18928
18929/*
18930 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18931 * Otherwise, restore the value to "oldval" and return NULL.
18932 * Must always be called in pairs to save and restore v:throwpoint! Does not
18933 * take care of memory allocations.
18934 */
18935 char_u *
18936v_throwpoint(oldval)
18937 char_u *oldval;
18938{
18939 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018940 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941
Bram Moolenaare9a41262005-01-15 22:18:47 +000018942 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943 return NULL;
18944}
18945
18946#if defined(FEAT_AUTOCMD) || defined(PROTO)
18947/*
18948 * Set v:cmdarg.
18949 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18950 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18951 * Must always be called in pairs!
18952 */
18953 char_u *
18954set_cmdarg(eap, oldarg)
18955 exarg_T *eap;
18956 char_u *oldarg;
18957{
18958 char_u *oldval;
18959 char_u *newval;
18960 unsigned len;
18961
Bram Moolenaare9a41262005-01-15 22:18:47 +000018962 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018963 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018965 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018966 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018967 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 }
18969
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018970 if (eap->force_bin == FORCE_BIN)
18971 len = 6;
18972 else if (eap->force_bin == FORCE_NOBIN)
18973 len = 8;
18974 else
18975 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018976
18977 if (eap->read_edit)
18978 len += 7;
18979
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018980 if (eap->force_ff != 0)
18981 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18982# ifdef FEAT_MBYTE
18983 if (eap->force_enc != 0)
18984 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018985 if (eap->bad_char != 0)
18986 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018987# endif
18988
18989 newval = alloc(len + 1);
18990 if (newval == NULL)
18991 return NULL;
18992
18993 if (eap->force_bin == FORCE_BIN)
18994 sprintf((char *)newval, " ++bin");
18995 else if (eap->force_bin == FORCE_NOBIN)
18996 sprintf((char *)newval, " ++nobin");
18997 else
18998 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018999
19000 if (eap->read_edit)
19001 STRCAT(newval, " ++edit");
19002
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019003 if (eap->force_ff != 0)
19004 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19005 eap->cmd + eap->force_ff);
19006# ifdef FEAT_MBYTE
19007 if (eap->force_enc != 0)
19008 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19009 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019010 if (eap->bad_char == BAD_KEEP)
19011 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19012 else if (eap->bad_char == BAD_DROP)
19013 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19014 else if (eap->bad_char != 0)
19015 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019016# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019017 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019018 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019}
19020#endif
19021
19022/*
19023 * Get the value of internal variable "name".
19024 * Return OK or FAIL.
19025 */
19026 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019027get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 char_u *name;
19029 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019030 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019031 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032{
19033 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019034 typval_T *tv = NULL;
19035 typval_T atv;
19036 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038
19039 /* truncate the name, so that we can use strcmp() */
19040 cc = name[len];
19041 name[len] = NUL;
19042
19043 /*
19044 * Check for "b:changedtick".
19045 */
19046 if (STRCMP(name, "b:changedtick") == 0)
19047 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019048 atv.v_type = VAR_NUMBER;
19049 atv.vval.v_number = curbuf->b_changedtick;
19050 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 }
19052
19053 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 * Check for user-defined variables.
19055 */
19056 else
19057 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019058 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019060 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 }
19062
Bram Moolenaare9a41262005-01-15 22:18:47 +000019063 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019065 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019066 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067 ret = FAIL;
19068 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019069 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019070 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071
19072 name[len] = cc;
19073
19074 return ret;
19075}
19076
19077/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019078 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19079 * Also handle function call with Funcref variable: func(expr)
19080 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19081 */
19082 static int
19083handle_subscript(arg, rettv, evaluate, verbose)
19084 char_u **arg;
19085 typval_T *rettv;
19086 int evaluate; /* do more than finding the end */
19087 int verbose; /* give error messages */
19088{
19089 int ret = OK;
19090 dict_T *selfdict = NULL;
19091 char_u *s;
19092 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019093 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019094
19095 while (ret == OK
19096 && (**arg == '['
19097 || (**arg == '.' && rettv->v_type == VAR_DICT)
19098 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19099 && !vim_iswhite(*(*arg - 1)))
19100 {
19101 if (**arg == '(')
19102 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019103 /* need to copy the funcref so that we can clear rettv */
19104 functv = *rettv;
19105 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019106
19107 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019108 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019109 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019110 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19111 &len, evaluate, selfdict);
19112
19113 /* Clear the funcref afterwards, so that deleting it while
19114 * evaluating the arguments is possible (see test55). */
19115 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019116
19117 /* Stop the expression evaluation when immediately aborting on
19118 * error, or when an interrupt occurred or an exception was thrown
19119 * but not caught. */
19120 if (aborting())
19121 {
19122 if (ret == OK)
19123 clear_tv(rettv);
19124 ret = FAIL;
19125 }
19126 dict_unref(selfdict);
19127 selfdict = NULL;
19128 }
19129 else /* **arg == '[' || **arg == '.' */
19130 {
19131 dict_unref(selfdict);
19132 if (rettv->v_type == VAR_DICT)
19133 {
19134 selfdict = rettv->vval.v_dict;
19135 if (selfdict != NULL)
19136 ++selfdict->dv_refcount;
19137 }
19138 else
19139 selfdict = NULL;
19140 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19141 {
19142 clear_tv(rettv);
19143 ret = FAIL;
19144 }
19145 }
19146 }
19147 dict_unref(selfdict);
19148 return ret;
19149}
19150
19151/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019152 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019153 * value).
19154 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019155 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019156alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019157{
Bram Moolenaar33570922005-01-25 22:26:29 +000019158 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019159}
19160
19161/*
19162 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 * The string "s" must have been allocated, it is consumed.
19164 * Return NULL for out of memory, the variable otherwise.
19165 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019166 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019167alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 char_u *s;
19169{
Bram Moolenaar33570922005-01-25 22:26:29 +000019170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019172 rettv = alloc_tv();
19173 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019175 rettv->v_type = VAR_STRING;
19176 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 }
19178 else
19179 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019180 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181}
19182
19183/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019184 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019186 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019187free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019188 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189{
19190 if (varp != NULL)
19191 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019192 switch (varp->v_type)
19193 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019194 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019195 func_unref(varp->vval.v_string);
19196 /*FALLTHROUGH*/
19197 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019198 vim_free(varp->vval.v_string);
19199 break;
19200 case VAR_LIST:
19201 list_unref(varp->vval.v_list);
19202 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019203 case VAR_DICT:
19204 dict_unref(varp->vval.v_dict);
19205 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019206 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019207#ifdef FEAT_FLOAT
19208 case VAR_FLOAT:
19209#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019210 case VAR_UNKNOWN:
19211 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019212 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019213 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019214 break;
19215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216 vim_free(varp);
19217 }
19218}
19219
19220/*
19221 * Free the memory for a variable value and set the value to NULL or 0.
19222 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019223 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019224clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019225 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226{
19227 if (varp != NULL)
19228 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019229 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019231 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019232 func_unref(varp->vval.v_string);
19233 /*FALLTHROUGH*/
19234 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019235 vim_free(varp->vval.v_string);
19236 varp->vval.v_string = NULL;
19237 break;
19238 case VAR_LIST:
19239 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019240 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019241 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019242 case VAR_DICT:
19243 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019244 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019245 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019246 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019247 varp->vval.v_number = 0;
19248 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019249#ifdef FEAT_FLOAT
19250 case VAR_FLOAT:
19251 varp->vval.v_float = 0.0;
19252 break;
19253#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019254 case VAR_UNKNOWN:
19255 break;
19256 default:
19257 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019259 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260 }
19261}
19262
19263/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019264 * Set the value of a variable to NULL without freeing items.
19265 */
19266 static void
19267init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019268 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019269{
19270 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019271 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019272}
19273
19274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275 * Get the number value of a variable.
19276 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019277 * For incompatible types, return 0.
19278 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19279 * caller of incompatible types: it sets *denote to TRUE if "denote"
19280 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281 */
19282 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019283get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019284 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019286 int error = FALSE;
19287
19288 return get_tv_number_chk(varp, &error); /* return 0L on error */
19289}
19290
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019291 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019292get_tv_number_chk(varp, denote)
19293 typval_T *varp;
19294 int *denote;
19295{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019296 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019297
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019298 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019300 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019301 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019302#ifdef FEAT_FLOAT
19303 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019304 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019305 break;
19306#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019307 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019308 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019309 break;
19310 case VAR_STRING:
19311 if (varp->vval.v_string != NULL)
19312 vim_str2nr(varp->vval.v_string, NULL, NULL,
19313 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019314 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019315 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019316 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019317 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019318 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019319 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019320 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019321 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019322 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019323 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019325 if (denote == NULL) /* useful for values that must be unsigned */
19326 n = -1;
19327 else
19328 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019329 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330}
19331
19332/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019333 * Get the lnum from the first argument.
19334 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019335 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 */
19337 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019338get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019339 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340{
Bram Moolenaar33570922005-01-25 22:26:29 +000019341 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 linenr_T lnum;
19343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019344 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 if (lnum == 0) /* no valid number, try using line() */
19346 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019347 rettv.v_type = VAR_NUMBER;
19348 f_line(argvars, &rettv);
19349 lnum = rettv.vval.v_number;
19350 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351 }
19352 return lnum;
19353}
19354
19355/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019356 * Get the lnum from the first argument.
19357 * Also accepts "$", then "buf" is used.
19358 * Returns 0 on error.
19359 */
19360 static linenr_T
19361get_tv_lnum_buf(argvars, buf)
19362 typval_T *argvars;
19363 buf_T *buf;
19364{
19365 if (argvars[0].v_type == VAR_STRING
19366 && argvars[0].vval.v_string != NULL
19367 && argvars[0].vval.v_string[0] == '$'
19368 && buf != NULL)
19369 return buf->b_ml.ml_line_count;
19370 return get_tv_number_chk(&argvars[0], NULL);
19371}
19372
19373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374 * Get the string value of a variable.
19375 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019376 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19377 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 * If the String variable has never been set, return an empty string.
19379 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019380 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19381 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 */
19383 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019384get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019385 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386{
19387 static char_u mybuf[NUMBUFLEN];
19388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019389 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390}
19391
19392 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019393get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019394 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019395 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019397 char_u *res = get_tv_string_buf_chk(varp, buf);
19398
19399 return res != NULL ? res : (char_u *)"";
19400}
19401
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019402 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019403get_tv_string_chk(varp)
19404 typval_T *varp;
19405{
19406 static char_u mybuf[NUMBUFLEN];
19407
19408 return get_tv_string_buf_chk(varp, mybuf);
19409}
19410
19411 static char_u *
19412get_tv_string_buf_chk(varp, buf)
19413 typval_T *varp;
19414 char_u *buf;
19415{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019416 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019418 case VAR_NUMBER:
19419 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19420 return buf;
19421 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019422 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019423 break;
19424 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019425 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019426 break;
19427 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019428 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019429 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019430#ifdef FEAT_FLOAT
19431 case VAR_FLOAT:
19432 EMSG(_("E806: using Float as a String"));
19433 break;
19434#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019435 case VAR_STRING:
19436 if (varp->vval.v_string != NULL)
19437 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019438 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019439 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019440 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019443 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444}
19445
19446/*
19447 * Find variable "name" in the list of variables.
19448 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019449 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019450 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019451 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019453 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019454find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019456 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019459 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460
Bram Moolenaara7043832005-01-21 11:56:39 +000019461 ht = find_var_ht(name, &varname);
19462 if (htp != NULL)
19463 *htp = ht;
19464 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019466 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467}
19468
19469/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019470 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019471 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019473 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019474find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019475 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019476 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019477 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019478{
Bram Moolenaar33570922005-01-25 22:26:29 +000019479 hashitem_T *hi;
19480
19481 if (*varname == NUL)
19482 {
19483 /* Must be something like "s:", otherwise "ht" would be NULL. */
19484 switch (varname[-2])
19485 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019486 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019487 case 'g': return &globvars_var;
19488 case 'v': return &vimvars_var;
19489 case 'b': return &curbuf->b_bufvar;
19490 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019491#ifdef FEAT_WINDOWS
19492 case 't': return &curtab->tp_winvar;
19493#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019494 case 'l': return current_funccal == NULL
19495 ? NULL : &current_funccal->l_vars_var;
19496 case 'a': return current_funccal == NULL
19497 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019498 }
19499 return NULL;
19500 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019501
19502 hi = hash_find(ht, varname);
19503 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019504 {
19505 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019506 * worked find the variable again. Don't auto-load a script if it was
19507 * loaded already, otherwise it would be loaded every time when
19508 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019509 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019510 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019511 hi = hash_find(ht, varname);
19512 if (HASHITEM_EMPTY(hi))
19513 return NULL;
19514 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019515 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019516}
19517
19518/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019519 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019520 * Set "varname" to the start of name without ':'.
19521 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019522 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019523find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 char_u *name;
19525 char_u **varname;
19526{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019527 hashitem_T *hi;
19528
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 if (name[1] != ':')
19530 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019531 /* The name must not start with a colon or #. */
19532 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 return NULL;
19534 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019535
19536 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019537 hi = hash_find(&compat_hashtab, name);
19538 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019539 return &compat_hashtab;
19540
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019542 return &globvarht; /* global variable */
19543 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 }
19545 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019546 if (*name == 'g') /* global variable */
19547 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019548 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19549 */
19550 if (vim_strchr(name + 2, ':') != NULL
19551 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019552 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019554 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019556 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019557#ifdef FEAT_WINDOWS
19558 if (*name == 't') /* tab page variable */
19559 return &curtab->tp_vars.dv_hashtab;
19560#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019561 if (*name == 'v') /* v: variable */
19562 return &vimvarht;
19563 if (*name == 'a' && current_funccal != NULL) /* function argument */
19564 return &current_funccal->l_avars.dv_hashtab;
19565 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19566 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 if (*name == 's' /* script variable */
19568 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19569 return &SCRIPT_VARS(current_SID);
19570 return NULL;
19571}
19572
19573/*
19574 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019575 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 * Returns NULL when it doesn't exist.
19577 */
19578 char_u *
19579get_var_value(name)
19580 char_u *name;
19581{
Bram Moolenaar33570922005-01-25 22:26:29 +000019582 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583
Bram Moolenaara7043832005-01-21 11:56:39 +000019584 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585 if (v == NULL)
19586 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019587 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588}
19589
19590/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019591 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592 * sourcing this script and when executing functions defined in the script.
19593 */
19594 void
19595new_script_vars(id)
19596 scid_T id;
19597{
Bram Moolenaara7043832005-01-21 11:56:39 +000019598 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019599 hashtab_T *ht;
19600 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019601
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19603 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019604 /* Re-allocating ga_data means that an ht_array pointing to
19605 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019606 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019607 for (i = 1; i <= ga_scripts.ga_len; ++i)
19608 {
19609 ht = &SCRIPT_VARS(i);
19610 if (ht->ht_mask == HT_INIT_SIZE - 1)
19611 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019612 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019613 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019614 }
19615
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 while (ga_scripts.ga_len < id)
19617 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019618 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019619 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019620 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 }
19623 }
19624}
19625
19626/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019627 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19628 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 */
19630 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019631init_var_dict(dict, dict_var)
19632 dict_T *dict;
19633 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634{
Bram Moolenaar33570922005-01-25 22:26:29 +000019635 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019636 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019637 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019638 dict_var->di_tv.vval.v_dict = dict;
19639 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019640 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019641 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19642 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643}
19644
19645/*
19646 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 * Frees all allocated variables and the value they contain.
19648 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 */
19650 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019651vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 hashtab_T *ht;
19653{
19654 vars_clear_ext(ht, TRUE);
19655}
19656
19657/*
19658 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19659 */
19660 static void
19661vars_clear_ext(ht, free_val)
19662 hashtab_T *ht;
19663 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664{
Bram Moolenaara7043832005-01-21 11:56:39 +000019665 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019666 hashitem_T *hi;
19667 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668
Bram Moolenaar33570922005-01-25 22:26:29 +000019669 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019670 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019671 for (hi = ht->ht_array; todo > 0; ++hi)
19672 {
19673 if (!HASHITEM_EMPTY(hi))
19674 {
19675 --todo;
19676
Bram Moolenaar33570922005-01-25 22:26:29 +000019677 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019678 * ht_array might change then. hash_clear() takes care of it
19679 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019680 v = HI2DI(hi);
19681 if (free_val)
19682 clear_tv(&v->di_tv);
19683 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19684 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019685 }
19686 }
19687 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019688 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689}
19690
Bram Moolenaara7043832005-01-21 11:56:39 +000019691/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019692 * Delete a variable from hashtab "ht" at item "hi".
19693 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019696delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019697 hashtab_T *ht;
19698 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699{
Bram Moolenaar33570922005-01-25 22:26:29 +000019700 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019701
19702 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019703 clear_tv(&di->di_tv);
19704 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705}
19706
19707/*
19708 * List the value of one internal variable.
19709 */
19710 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019711list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019712 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019714 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019716 char_u *tofree;
19717 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019718 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019719
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019720 current_copyID += COPYID_INC;
19721 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019722 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019723 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019724 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725}
19726
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019728list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729 char_u *prefix;
19730 char_u *name;
19731 int type;
19732 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019733 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734{
Bram Moolenaar31859182007-08-14 20:41:13 +000019735 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19736 msg_start();
19737 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738 if (name != NULL) /* "a:" vars don't have a name stored */
19739 msg_puts(name);
19740 msg_putchar(' ');
19741 msg_advance(22);
19742 if (type == VAR_NUMBER)
19743 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019744 else if (type == VAR_FUNC)
19745 msg_putchar('*');
19746 else if (type == VAR_LIST)
19747 {
19748 msg_putchar('[');
19749 if (*string == '[')
19750 ++string;
19751 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019752 else if (type == VAR_DICT)
19753 {
19754 msg_putchar('{');
19755 if (*string == '{')
19756 ++string;
19757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 else
19759 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019760
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019762
19763 if (type == VAR_FUNC)
19764 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019765 if (*first)
19766 {
19767 msg_clr_eos();
19768 *first = FALSE;
19769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770}
19771
19772/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019773 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 * If the variable already exists, the value is updated.
19775 * Otherwise the variable is created.
19776 */
19777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019778set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019780 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019781 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782{
Bram Moolenaar33570922005-01-25 22:26:29 +000019783 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019785 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019786 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019788 ht = find_var_ht(name, &varname);
19789 if (ht == NULL || *varname == NUL)
19790 {
19791 EMSG2(_(e_illvar), name);
19792 return;
19793 }
19794 v = find_var_in_ht(ht, varname, TRUE);
19795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019796 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019797 {
19798 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19799 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19800 ? name[2] : name[0]))
19801 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019802 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019803 return;
19804 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019805 /* Don't allow hiding a function. When "v" is not NULL we migth be
19806 * assigning another function to the same var, the type is checked
19807 * below. */
19808 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019809 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019810 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019811 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019812 return;
19813 }
19814 }
19815
Bram Moolenaar33570922005-01-25 22:26:29 +000019816 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019818 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019819 if (var_check_ro(v->di_flags, name)
19820 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019821 return;
19822 if (v->di_tv.v_type != tv->v_type
19823 && !((v->di_tv.v_type == VAR_STRING
19824 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019825 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019826 || tv->v_type == VAR_NUMBER))
19827#ifdef FEAT_FLOAT
19828 && !((v->di_tv.v_type == VAR_NUMBER
19829 || v->di_tv.v_type == VAR_FLOAT)
19830 && (tv->v_type == VAR_NUMBER
19831 || tv->v_type == VAR_FLOAT))
19832#endif
19833 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019834 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019835 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019836 return;
19837 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019838
19839 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019840 * Handle setting internal v: variables separately: we don't change
19841 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019842 */
19843 if (ht == &vimvarht)
19844 {
19845 if (v->di_tv.v_type == VAR_STRING)
19846 {
19847 vim_free(v->di_tv.vval.v_string);
19848 if (copy || tv->v_type != VAR_STRING)
19849 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19850 else
19851 {
19852 /* Take over the string to avoid an extra alloc/free. */
19853 v->di_tv.vval.v_string = tv->vval.v_string;
19854 tv->vval.v_string = NULL;
19855 }
19856 }
19857 else if (v->di_tv.v_type != VAR_NUMBER)
19858 EMSG2(_(e_intern2), "set_var()");
19859 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019860 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019861 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019862 if (STRCMP(varname, "searchforward") == 0)
19863 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19864 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019865 return;
19866 }
19867
19868 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869 }
19870 else /* add a new variable */
19871 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019872 /* Can't add "v:" variable. */
19873 if (ht == &vimvarht)
19874 {
19875 EMSG2(_(e_illvar), name);
19876 return;
19877 }
19878
Bram Moolenaar92124a32005-06-17 22:03:40 +000019879 /* Make sure the variable name is valid. */
19880 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019881 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19882 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019883 {
19884 EMSG2(_(e_illvar), varname);
19885 return;
19886 }
19887
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019888 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19889 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019890 if (v == NULL)
19891 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019892 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019893 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019895 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896 return;
19897 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019898 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019900
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019901 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019902 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019903 else
19904 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019905 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019906 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019907 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909}
19910
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019911/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019912 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019913 * Also give an error message.
19914 */
19915 static int
19916var_check_ro(flags, name)
19917 int flags;
19918 char_u *name;
19919{
19920 if (flags & DI_FLAGS_RO)
19921 {
19922 EMSG2(_(e_readonlyvar), name);
19923 return TRUE;
19924 }
19925 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19926 {
19927 EMSG2(_(e_readonlysbx), name);
19928 return TRUE;
19929 }
19930 return FALSE;
19931}
19932
19933/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019934 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19935 * Also give an error message.
19936 */
19937 static int
19938var_check_fixed(flags, name)
19939 int flags;
19940 char_u *name;
19941{
19942 if (flags & DI_FLAGS_FIX)
19943 {
19944 EMSG2(_("E795: Cannot delete variable %s"), name);
19945 return TRUE;
19946 }
19947 return FALSE;
19948}
19949
19950/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019951 * Return TRUE if typeval "tv" is set to be locked (immutable).
19952 * Also give an error message, using "name".
19953 */
19954 static int
19955tv_check_lock(lock, name)
19956 int lock;
19957 char_u *name;
19958{
19959 if (lock & VAR_LOCKED)
19960 {
19961 EMSG2(_("E741: Value is locked: %s"),
19962 name == NULL ? (char_u *)_("Unknown") : name);
19963 return TRUE;
19964 }
19965 if (lock & VAR_FIXED)
19966 {
19967 EMSG2(_("E742: Cannot change value of %s"),
19968 name == NULL ? (char_u *)_("Unknown") : name);
19969 return TRUE;
19970 }
19971 return FALSE;
19972}
19973
19974/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019975 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019976 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019977 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019978 * It is OK for "from" and "to" to point to the same item. This is used to
19979 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019980 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019981 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019982copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019983 typval_T *from;
19984 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019986 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019987 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019988 switch (from->v_type)
19989 {
19990 case VAR_NUMBER:
19991 to->vval.v_number = from->vval.v_number;
19992 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019993#ifdef FEAT_FLOAT
19994 case VAR_FLOAT:
19995 to->vval.v_float = from->vval.v_float;
19996 break;
19997#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019998 case VAR_STRING:
19999 case VAR_FUNC:
20000 if (from->vval.v_string == NULL)
20001 to->vval.v_string = NULL;
20002 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020003 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020004 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020005 if (from->v_type == VAR_FUNC)
20006 func_ref(to->vval.v_string);
20007 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020008 break;
20009 case VAR_LIST:
20010 if (from->vval.v_list == NULL)
20011 to->vval.v_list = NULL;
20012 else
20013 {
20014 to->vval.v_list = from->vval.v_list;
20015 ++to->vval.v_list->lv_refcount;
20016 }
20017 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020018 case VAR_DICT:
20019 if (from->vval.v_dict == NULL)
20020 to->vval.v_dict = NULL;
20021 else
20022 {
20023 to->vval.v_dict = from->vval.v_dict;
20024 ++to->vval.v_dict->dv_refcount;
20025 }
20026 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020027 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020028 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020029 break;
20030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031}
20032
20033/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020034 * Make a copy of an item.
20035 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020036 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20037 * reference to an already copied list/dict can be used.
20038 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020039 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020040 static int
20041item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020042 typval_T *from;
20043 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020044 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020045 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020046{
20047 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020048 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020049
Bram Moolenaar33570922005-01-25 22:26:29 +000020050 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020051 {
20052 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020053 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020054 }
20055 ++recurse;
20056
20057 switch (from->v_type)
20058 {
20059 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020060#ifdef FEAT_FLOAT
20061 case VAR_FLOAT:
20062#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020063 case VAR_STRING:
20064 case VAR_FUNC:
20065 copy_tv(from, to);
20066 break;
20067 case VAR_LIST:
20068 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020069 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020070 if (from->vval.v_list == NULL)
20071 to->vval.v_list = NULL;
20072 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20073 {
20074 /* use the copy made earlier */
20075 to->vval.v_list = from->vval.v_list->lv_copylist;
20076 ++to->vval.v_list->lv_refcount;
20077 }
20078 else
20079 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20080 if (to->vval.v_list == NULL)
20081 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020082 break;
20083 case VAR_DICT:
20084 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020085 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020086 if (from->vval.v_dict == NULL)
20087 to->vval.v_dict = NULL;
20088 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20089 {
20090 /* use the copy made earlier */
20091 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20092 ++to->vval.v_dict->dv_refcount;
20093 }
20094 else
20095 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20096 if (to->vval.v_dict == NULL)
20097 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020098 break;
20099 default:
20100 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020101 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020102 }
20103 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020104 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020105}
20106
20107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 * ":echo expr1 ..." print each argument separated with a space, add a
20109 * newline at the end.
20110 * ":echon expr1 ..." print each argument plain.
20111 */
20112 void
20113ex_echo(eap)
20114 exarg_T *eap;
20115{
20116 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020117 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020118 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 char_u *p;
20120 int needclr = TRUE;
20121 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020122 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123
20124 if (eap->skip)
20125 ++emsg_skip;
20126 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20127 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020128 /* If eval1() causes an error message the text from the command may
20129 * still need to be cleared. E.g., "echo 22,44". */
20130 need_clr_eos = needclr;
20131
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020133 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 {
20135 /*
20136 * Report the invalid expression unless the expression evaluation
20137 * has been cancelled due to an aborting error, an interrupt, or an
20138 * exception.
20139 */
20140 if (!aborting())
20141 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020142 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 break;
20144 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020145 need_clr_eos = FALSE;
20146
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 if (!eap->skip)
20148 {
20149 if (atstart)
20150 {
20151 atstart = FALSE;
20152 /* Call msg_start() after eval1(), evaluating the expression
20153 * may cause a message to appear. */
20154 if (eap->cmdidx == CMD_echo)
20155 msg_start();
20156 }
20157 else if (eap->cmdidx == CMD_echo)
20158 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020159 current_copyID += COPYID_INC;
20160 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020161 if (p != NULL)
20162 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020164 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020166 if (*p != TAB && needclr)
20167 {
20168 /* remove any text still there from the command */
20169 msg_clr_eos();
20170 needclr = FALSE;
20171 }
20172 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173 }
20174 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020175 {
20176#ifdef FEAT_MBYTE
20177 if (has_mbyte)
20178 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020179 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020180
20181 (void)msg_outtrans_len_attr(p, i, echo_attr);
20182 p += i - 1;
20183 }
20184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020186 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020189 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020191 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192 arg = skipwhite(arg);
20193 }
20194 eap->nextcmd = check_nextcmd(arg);
20195
20196 if (eap->skip)
20197 --emsg_skip;
20198 else
20199 {
20200 /* remove text that may still be there from the command */
20201 if (needclr)
20202 msg_clr_eos();
20203 if (eap->cmdidx == CMD_echo)
20204 msg_end();
20205 }
20206}
20207
20208/*
20209 * ":echohl {name}".
20210 */
20211 void
20212ex_echohl(eap)
20213 exarg_T *eap;
20214{
20215 int id;
20216
20217 id = syn_name2id(eap->arg);
20218 if (id == 0)
20219 echo_attr = 0;
20220 else
20221 echo_attr = syn_id2attr(id);
20222}
20223
20224/*
20225 * ":execute expr1 ..." execute the result of an expression.
20226 * ":echomsg expr1 ..." Print a message
20227 * ":echoerr expr1 ..." Print an error
20228 * Each gets spaces around each argument and a newline at the end for
20229 * echo commands
20230 */
20231 void
20232ex_execute(eap)
20233 exarg_T *eap;
20234{
20235 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020236 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 int ret = OK;
20238 char_u *p;
20239 garray_T ga;
20240 int len;
20241 int save_did_emsg;
20242
20243 ga_init2(&ga, 1, 80);
20244
20245 if (eap->skip)
20246 ++emsg_skip;
20247 while (*arg != NUL && *arg != '|' && *arg != '\n')
20248 {
20249 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020250 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 {
20252 /*
20253 * Report the invalid expression unless the expression evaluation
20254 * has been cancelled due to an aborting error, an interrupt, or an
20255 * exception.
20256 */
20257 if (!aborting())
20258 EMSG2(_(e_invexpr2), p);
20259 ret = FAIL;
20260 break;
20261 }
20262
20263 if (!eap->skip)
20264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020265 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 len = (int)STRLEN(p);
20267 if (ga_grow(&ga, len + 2) == FAIL)
20268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020269 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270 ret = FAIL;
20271 break;
20272 }
20273 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 ga.ga_len += len;
20277 }
20278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020279 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 arg = skipwhite(arg);
20281 }
20282
20283 if (ret != FAIL && ga.ga_data != NULL)
20284 {
20285 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020288 out_flush();
20289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 else if (eap->cmdidx == CMD_echoerr)
20291 {
20292 /* We don't want to abort following commands, restore did_emsg. */
20293 save_did_emsg = did_emsg;
20294 EMSG((char_u *)ga.ga_data);
20295 if (!force_abort)
20296 did_emsg = save_did_emsg;
20297 }
20298 else if (eap->cmdidx == CMD_execute)
20299 do_cmdline((char_u *)ga.ga_data,
20300 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20301 }
20302
20303 ga_clear(&ga);
20304
20305 if (eap->skip)
20306 --emsg_skip;
20307
20308 eap->nextcmd = check_nextcmd(arg);
20309}
20310
20311/*
20312 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20313 * "arg" points to the "&" or '+' when called, to "option" when returning.
20314 * Returns NULL when no option name found. Otherwise pointer to the char
20315 * after the option name.
20316 */
20317 static char_u *
20318find_option_end(arg, opt_flags)
20319 char_u **arg;
20320 int *opt_flags;
20321{
20322 char_u *p = *arg;
20323
20324 ++p;
20325 if (*p == 'g' && p[1] == ':')
20326 {
20327 *opt_flags = OPT_GLOBAL;
20328 p += 2;
20329 }
20330 else if (*p == 'l' && p[1] == ':')
20331 {
20332 *opt_flags = OPT_LOCAL;
20333 p += 2;
20334 }
20335 else
20336 *opt_flags = 0;
20337
20338 if (!ASCII_ISALPHA(*p))
20339 return NULL;
20340 *arg = p;
20341
20342 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20343 p += 4; /* termcap option */
20344 else
20345 while (ASCII_ISALPHA(*p))
20346 ++p;
20347 return p;
20348}
20349
20350/*
20351 * ":function"
20352 */
20353 void
20354ex_function(eap)
20355 exarg_T *eap;
20356{
20357 char_u *theline;
20358 int j;
20359 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 char_u *name = NULL;
20362 char_u *p;
20363 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020364 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 garray_T newargs;
20366 garray_T newlines;
20367 int varargs = FALSE;
20368 int mustend = FALSE;
20369 int flags = 0;
20370 ufunc_T *fp;
20371 int indent;
20372 int nesting;
20373 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020374 dictitem_T *v;
20375 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020376 static int func_nr = 0; /* number for nameless function */
20377 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020378 hashtab_T *ht;
20379 int todo;
20380 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020381 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382
20383 /*
20384 * ":function" without argument: list functions.
20385 */
20386 if (ends_excmd(*eap->arg))
20387 {
20388 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020389 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020390 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020391 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020392 {
20393 if (!HASHITEM_EMPTY(hi))
20394 {
20395 --todo;
20396 fp = HI2UF(hi);
20397 if (!isdigit(*fp->uf_name))
20398 list_func_head(fp, FALSE);
20399 }
20400 }
20401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402 eap->nextcmd = check_nextcmd(eap->arg);
20403 return;
20404 }
20405
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020406 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020407 * ":function /pat": list functions matching pattern.
20408 */
20409 if (*eap->arg == '/')
20410 {
20411 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20412 if (!eap->skip)
20413 {
20414 regmatch_T regmatch;
20415
20416 c = *p;
20417 *p = NUL;
20418 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20419 *p = c;
20420 if (regmatch.regprog != NULL)
20421 {
20422 regmatch.rm_ic = p_ic;
20423
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020424 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020425 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20426 {
20427 if (!HASHITEM_EMPTY(hi))
20428 {
20429 --todo;
20430 fp = HI2UF(hi);
20431 if (!isdigit(*fp->uf_name)
20432 && vim_regexec(&regmatch, fp->uf_name, 0))
20433 list_func_head(fp, FALSE);
20434 }
20435 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020436 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020437 }
20438 }
20439 if (*p == '/')
20440 ++p;
20441 eap->nextcmd = check_nextcmd(p);
20442 return;
20443 }
20444
20445 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020446 * Get the function name. There are these situations:
20447 * func normal function name
20448 * "name" == func, "fudi.fd_dict" == NULL
20449 * dict.func new dictionary entry
20450 * "name" == NULL, "fudi.fd_dict" set,
20451 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20452 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020453 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020454 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20455 * dict.func existing dict entry that's not a Funcref
20456 * "name" == NULL, "fudi.fd_dict" set,
20457 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020460 name = trans_function_name(&p, eap->skip, 0, &fudi);
20461 paren = (vim_strchr(p, '(') != NULL);
20462 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 {
20464 /*
20465 * Return on an invalid expression in braces, unless the expression
20466 * evaluation has been cancelled due to an aborting error, an
20467 * interrupt, or an exception.
20468 */
20469 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020470 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020471 if (!eap->skip && fudi.fd_newkey != NULL)
20472 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020473 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 else
20477 eap->skip = TRUE;
20478 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020479
Bram Moolenaar071d4272004-06-13 20:20:40 +000020480 /* An error in a function call during evaluation of an expression in magic
20481 * braces should not cause the function not to be defined. */
20482 saved_did_emsg = did_emsg;
20483 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484
20485 /*
20486 * ":function func" with only function name: list function.
20487 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020488 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 {
20490 if (!ends_excmd(*skipwhite(p)))
20491 {
20492 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020493 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 }
20495 eap->nextcmd = check_nextcmd(p);
20496 if (eap->nextcmd != NULL)
20497 *p = NUL;
20498 if (!eap->skip && !got_int)
20499 {
20500 fp = find_func(name);
20501 if (fp != NULL)
20502 {
20503 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020504 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020506 if (FUNCLINE(fp, j) == NULL)
20507 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 msg_putchar('\n');
20509 msg_outnum((long)(j + 1));
20510 if (j < 9)
20511 msg_putchar(' ');
20512 if (j < 99)
20513 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020514 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 out_flush(); /* show a line at a time */
20516 ui_breakcheck();
20517 }
20518 if (!got_int)
20519 {
20520 msg_putchar('\n');
20521 msg_puts((char_u *)" endfunction");
20522 }
20523 }
20524 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020525 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020527 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 }
20529
20530 /*
20531 * ":function name(arg1, arg2)" Define function.
20532 */
20533 p = skipwhite(p);
20534 if (*p != '(')
20535 {
20536 if (!eap->skip)
20537 {
20538 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020539 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540 }
20541 /* attempt to continue by skipping some text */
20542 if (vim_strchr(p, '(') != NULL)
20543 p = vim_strchr(p, '(');
20544 }
20545 p = skipwhite(p + 1);
20546
20547 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20548 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20549
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020550 if (!eap->skip)
20551 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020552 /* Check the name of the function. Unless it's a dictionary function
20553 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020554 if (name != NULL)
20555 arg = name;
20556 else
20557 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020558 if (arg != NULL && (fudi.fd_di == NULL
20559 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020560 {
20561 if (*arg == K_SPECIAL)
20562 j = 3;
20563 else
20564 j = 0;
20565 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20566 : eval_isnamec(arg[j])))
20567 ++j;
20568 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020569 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020570 }
20571 }
20572
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 /*
20574 * Isolate the arguments: "arg1, arg2, ...)"
20575 */
20576 while (*p != ')')
20577 {
20578 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20579 {
20580 varargs = TRUE;
20581 p += 3;
20582 mustend = TRUE;
20583 }
20584 else
20585 {
20586 arg = p;
20587 while (ASCII_ISALNUM(*p) || *p == '_')
20588 ++p;
20589 if (arg == p || isdigit(*arg)
20590 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20591 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20592 {
20593 if (!eap->skip)
20594 EMSG2(_("E125: Illegal argument: %s"), arg);
20595 break;
20596 }
20597 if (ga_grow(&newargs, 1) == FAIL)
20598 goto erret;
20599 c = *p;
20600 *p = NUL;
20601 arg = vim_strsave(arg);
20602 if (arg == NULL)
20603 goto erret;
20604 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20605 *p = c;
20606 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607 if (*p == ',')
20608 ++p;
20609 else
20610 mustend = TRUE;
20611 }
20612 p = skipwhite(p);
20613 if (mustend && *p != ')')
20614 {
20615 if (!eap->skip)
20616 EMSG2(_(e_invarg2), eap->arg);
20617 break;
20618 }
20619 }
20620 ++p; /* skip the ')' */
20621
Bram Moolenaare9a41262005-01-15 22:18:47 +000020622 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623 for (;;)
20624 {
20625 p = skipwhite(p);
20626 if (STRNCMP(p, "range", 5) == 0)
20627 {
20628 flags |= FC_RANGE;
20629 p += 5;
20630 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020631 else if (STRNCMP(p, "dict", 4) == 0)
20632 {
20633 flags |= FC_DICT;
20634 p += 4;
20635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 else if (STRNCMP(p, "abort", 5) == 0)
20637 {
20638 flags |= FC_ABORT;
20639 p += 5;
20640 }
20641 else
20642 break;
20643 }
20644
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020645 /* When there is a line break use what follows for the function body.
20646 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20647 if (*p == '\n')
20648 line_arg = p + 1;
20649 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 EMSG(_(e_trailing));
20651
20652 /*
20653 * Read the body of the function, until ":endfunction" is found.
20654 */
20655 if (KeyTyped)
20656 {
20657 /* Check if the function already exists, don't let the user type the
20658 * whole function before telling him it doesn't work! For a script we
20659 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020660 if (!eap->skip && !eap->forceit)
20661 {
20662 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20663 EMSG(_(e_funcdict));
20664 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020665 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020667
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020668 if (!eap->skip && did_emsg)
20669 goto erret;
20670
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671 msg_putchar('\n'); /* don't overwrite the function name */
20672 cmdline_row = msg_row;
20673 }
20674
20675 indent = 2;
20676 nesting = 0;
20677 for (;;)
20678 {
20679 msg_scroll = TRUE;
20680 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020681 sourcing_lnum_off = sourcing_lnum;
20682
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020683 if (line_arg != NULL)
20684 {
20685 /* Use eap->arg, split up in parts by line breaks. */
20686 theline = line_arg;
20687 p = vim_strchr(theline, '\n');
20688 if (p == NULL)
20689 line_arg += STRLEN(line_arg);
20690 else
20691 {
20692 *p = NUL;
20693 line_arg = p + 1;
20694 }
20695 }
20696 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 theline = getcmdline(':', 0L, indent);
20698 else
20699 theline = eap->getline(':', eap->cookie, indent);
20700 if (KeyTyped)
20701 lines_left = Rows - 1;
20702 if (theline == NULL)
20703 {
20704 EMSG(_("E126: Missing :endfunction"));
20705 goto erret;
20706 }
20707
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020708 /* Detect line continuation: sourcing_lnum increased more than one. */
20709 if (sourcing_lnum > sourcing_lnum_off + 1)
20710 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20711 else
20712 sourcing_lnum_off = 0;
20713
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 if (skip_until != NULL)
20715 {
20716 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20717 * don't check for ":endfunc". */
20718 if (STRCMP(theline, skip_until) == 0)
20719 {
20720 vim_free(skip_until);
20721 skip_until = NULL;
20722 }
20723 }
20724 else
20725 {
20726 /* skip ':' and blanks*/
20727 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20728 ;
20729
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020730 /* Check for "endfunction". */
20731 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020733 if (line_arg == NULL)
20734 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 break;
20736 }
20737
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020738 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739 * at "end". */
20740 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20741 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020742 else if (STRNCMP(p, "if", 2) == 0
20743 || STRNCMP(p, "wh", 2) == 0
20744 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 || STRNCMP(p, "try", 3) == 0)
20746 indent += 2;
20747
20748 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020749 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020751 if (*p == '!')
20752 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753 p += eval_fname_script(p);
20754 if (ASCII_ISALPHA(*p))
20755 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020756 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757 if (*skipwhite(p) == '(')
20758 {
20759 ++nesting;
20760 indent += 2;
20761 }
20762 }
20763 }
20764
20765 /* Check for ":append" or ":insert". */
20766 p = skip_range(p, NULL);
20767 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20768 || (p[0] == 'i'
20769 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20770 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20771 skip_until = vim_strsave((char_u *)".");
20772
20773 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20774 arg = skipwhite(skiptowhite(p));
20775 if (arg[0] == '<' && arg[1] =='<'
20776 && ((p[0] == 'p' && p[1] == 'y'
20777 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20778 || (p[0] == 'p' && p[1] == 'e'
20779 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20780 || (p[0] == 't' && p[1] == 'c'
20781 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20782 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20783 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020784 || (p[0] == 'm' && p[1] == 'z'
20785 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 ))
20787 {
20788 /* ":python <<" continues until a dot, like ":append" */
20789 p = skipwhite(arg + 2);
20790 if (*p == NUL)
20791 skip_until = vim_strsave((char_u *)".");
20792 else
20793 skip_until = vim_strsave(p);
20794 }
20795 }
20796
20797 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020798 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020799 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020800 if (line_arg == NULL)
20801 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020803 }
20804
20805 /* Copy the line to newly allocated memory. get_one_sourceline()
20806 * allocates 250 bytes per line, this saves 80% on average. The cost
20807 * is an extra alloc/free. */
20808 p = vim_strsave(theline);
20809 if (p != NULL)
20810 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020811 if (line_arg == NULL)
20812 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020813 theline = p;
20814 }
20815
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020816 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20817
20818 /* Add NULL lines for continuation lines, so that the line count is
20819 * equal to the index in the growarray. */
20820 while (sourcing_lnum_off-- > 0)
20821 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020822
20823 /* Check for end of eap->arg. */
20824 if (line_arg != NULL && *line_arg == NUL)
20825 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 }
20827
20828 /* Don't define the function when skipping commands or when an error was
20829 * detected. */
20830 if (eap->skip || did_emsg)
20831 goto erret;
20832
20833 /*
20834 * If there are no errors, add the function
20835 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020836 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020837 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020838 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020839 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020840 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020841 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020842 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020843 goto erret;
20844 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020845
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020846 fp = find_func(name);
20847 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020849 if (!eap->forceit)
20850 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020851 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020852 goto erret;
20853 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020854 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020855 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020856 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020857 name);
20858 goto erret;
20859 }
20860 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020861 ga_clear_strings(&(fp->uf_args));
20862 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020863 vim_free(name);
20864 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866 }
20867 else
20868 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020869 char numbuf[20];
20870
20871 fp = NULL;
20872 if (fudi.fd_newkey == NULL && !eap->forceit)
20873 {
20874 EMSG(_(e_funcdict));
20875 goto erret;
20876 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020877 if (fudi.fd_di == NULL)
20878 {
20879 /* Can't add a function to a locked dictionary */
20880 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20881 goto erret;
20882 }
20883 /* Can't change an existing function if it is locked */
20884 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20885 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020886
20887 /* Give the function a sequential number. Can only be used with a
20888 * Funcref! */
20889 vim_free(name);
20890 sprintf(numbuf, "%d", ++func_nr);
20891 name = vim_strsave((char_u *)numbuf);
20892 if (name == NULL)
20893 goto erret;
20894 }
20895
20896 if (fp == NULL)
20897 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020898 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020899 {
20900 int slen, plen;
20901 char_u *scriptname;
20902
20903 /* Check that the autoload name matches the script name. */
20904 j = FAIL;
20905 if (sourcing_name != NULL)
20906 {
20907 scriptname = autoload_name(name);
20908 if (scriptname != NULL)
20909 {
20910 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020911 plen = (int)STRLEN(p);
20912 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020913 if (slen > plen && fnamecmp(p,
20914 sourcing_name + slen - plen) == 0)
20915 j = OK;
20916 vim_free(scriptname);
20917 }
20918 }
20919 if (j == FAIL)
20920 {
20921 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20922 goto erret;
20923 }
20924 }
20925
20926 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927 if (fp == NULL)
20928 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020929
20930 if (fudi.fd_dict != NULL)
20931 {
20932 if (fudi.fd_di == NULL)
20933 {
20934 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020935 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020936 if (fudi.fd_di == NULL)
20937 {
20938 vim_free(fp);
20939 goto erret;
20940 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020941 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20942 {
20943 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020944 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020945 goto erret;
20946 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020947 }
20948 else
20949 /* overwrite existing dict entry */
20950 clear_tv(&fudi.fd_di->di_tv);
20951 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020952 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020953 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020954 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020955
20956 /* behave like "dict" was used */
20957 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020958 }
20959
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020961 STRCPY(fp->uf_name, name);
20962 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020964 fp->uf_args = newargs;
20965 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020966#ifdef FEAT_PROFILE
20967 fp->uf_tml_count = NULL;
20968 fp->uf_tml_total = NULL;
20969 fp->uf_tml_self = NULL;
20970 fp->uf_profiling = FALSE;
20971 if (prof_def_func())
20972 func_do_profile(fp);
20973#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020974 fp->uf_varargs = varargs;
20975 fp->uf_flags = flags;
20976 fp->uf_calls = 0;
20977 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020978 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979
20980erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981 ga_clear_strings(&newargs);
20982 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020983ret_free:
20984 vim_free(skip_until);
20985 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988}
20989
20990/*
20991 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020992 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020994 * flags:
20995 * TFN_INT: internal function name OK
20996 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 * Advances "pp" to just after the function name (if no error).
20998 */
20999 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021000trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 char_u **pp;
21002 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021003 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021004 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021006 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 char_u *start;
21008 char_u *end;
21009 int lead;
21010 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021012 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021013
21014 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021015 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021017
21018 /* Check for hard coded <SNR>: already translated function ID (from a user
21019 * command). */
21020 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21021 && (*pp)[2] == (int)KE_SNR)
21022 {
21023 *pp += 3;
21024 len = get_id_len(pp) + 3;
21025 return vim_strnsave(start, len);
21026 }
21027
21028 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21029 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021031 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021033
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021034 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21035 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021036 if (end == start)
21037 {
21038 if (!skip)
21039 EMSG(_("E129: Function name required"));
21040 goto theend;
21041 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021042 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021043 {
21044 /*
21045 * Report an invalid expression in braces, unless the expression
21046 * evaluation has been cancelled due to an aborting error, an
21047 * interrupt, or an exception.
21048 */
21049 if (!aborting())
21050 {
21051 if (end != NULL)
21052 EMSG2(_(e_invarg2), start);
21053 }
21054 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021055 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021056 goto theend;
21057 }
21058
21059 if (lv.ll_tv != NULL)
21060 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021061 if (fdp != NULL)
21062 {
21063 fdp->fd_dict = lv.ll_dict;
21064 fdp->fd_newkey = lv.ll_newkey;
21065 lv.ll_newkey = NULL;
21066 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021067 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021068 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21069 {
21070 name = vim_strsave(lv.ll_tv->vval.v_string);
21071 *pp = end;
21072 }
21073 else
21074 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021075 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21076 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021077 EMSG(_(e_funcref));
21078 else
21079 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021080 name = NULL;
21081 }
21082 goto theend;
21083 }
21084
21085 if (lv.ll_name == NULL)
21086 {
21087 /* Error found, but continue after the function name. */
21088 *pp = end;
21089 goto theend;
21090 }
21091
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021092 /* Check if the name is a Funcref. If so, use the value. */
21093 if (lv.ll_exp_name != NULL)
21094 {
21095 len = (int)STRLEN(lv.ll_exp_name);
21096 name = deref_func_name(lv.ll_exp_name, &len);
21097 if (name == lv.ll_exp_name)
21098 name = NULL;
21099 }
21100 else
21101 {
21102 len = (int)(end - *pp);
21103 name = deref_func_name(*pp, &len);
21104 if (name == *pp)
21105 name = NULL;
21106 }
21107 if (name != NULL)
21108 {
21109 name = vim_strsave(name);
21110 *pp = end;
21111 goto theend;
21112 }
21113
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021114 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021115 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021116 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021117 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21118 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21119 {
21120 /* When there was "s:" already or the name expanded to get a
21121 * leading "s:" then remove it. */
21122 lv.ll_name += 2;
21123 len -= 2;
21124 lead = 2;
21125 }
21126 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021127 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021128 {
21129 if (lead == 2) /* skip over "s:" */
21130 lv.ll_name += 2;
21131 len = (int)(end - lv.ll_name);
21132 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021133
21134 /*
21135 * Copy the function name to allocated memory.
21136 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21137 * Accept <SNR>123_name() outside a script.
21138 */
21139 if (skip)
21140 lead = 0; /* do nothing */
21141 else if (lead > 0)
21142 {
21143 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021144 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21145 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021146 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021147 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021148 if (current_SID <= 0)
21149 {
21150 EMSG(_(e_usingsid));
21151 goto theend;
21152 }
21153 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21154 lead += (int)STRLEN(sid_buf);
21155 }
21156 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021157 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021158 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021159 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021160 goto theend;
21161 }
21162 name = alloc((unsigned)(len + lead + 1));
21163 if (name != NULL)
21164 {
21165 if (lead > 0)
21166 {
21167 name[0] = K_SPECIAL;
21168 name[1] = KS_EXTRA;
21169 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021170 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021171 STRCPY(name + 3, sid_buf);
21172 }
21173 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21174 name[len + lead] = NUL;
21175 }
21176 *pp = end;
21177
21178theend:
21179 clear_lval(&lv);
21180 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181}
21182
21183/*
21184 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21185 * Return 2 if "p" starts with "s:".
21186 * Return 0 otherwise.
21187 */
21188 static int
21189eval_fname_script(p)
21190 char_u *p;
21191{
21192 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21193 || STRNICMP(p + 1, "SNR>", 4) == 0))
21194 return 5;
21195 if (p[0] == 's' && p[1] == ':')
21196 return 2;
21197 return 0;
21198}
21199
21200/*
21201 * Return TRUE if "p" starts with "<SID>" or "s:".
21202 * Only works if eval_fname_script() returned non-zero for "p"!
21203 */
21204 static int
21205eval_fname_sid(p)
21206 char_u *p;
21207{
21208 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21209}
21210
21211/*
21212 * List the head of the function: "name(arg1, arg2)".
21213 */
21214 static void
21215list_func_head(fp, indent)
21216 ufunc_T *fp;
21217 int indent;
21218{
21219 int j;
21220
21221 msg_start();
21222 if (indent)
21223 MSG_PUTS(" ");
21224 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021225 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 {
21227 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021228 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229 }
21230 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021231 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021233 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 {
21235 if (j)
21236 MSG_PUTS(", ");
21237 msg_puts(FUNCARG(fp, j));
21238 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021239 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 {
21241 if (j)
21242 MSG_PUTS(", ");
21243 MSG_PUTS("...");
21244 }
21245 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021246 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021247 if (p_verbose > 0)
21248 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249}
21250
21251/*
21252 * Find a function by name, return pointer to it in ufuncs.
21253 * Return NULL for unknown function.
21254 */
21255 static ufunc_T *
21256find_func(name)
21257 char_u *name;
21258{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021259 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021261 hi = hash_find(&func_hashtab, name);
21262 if (!HASHITEM_EMPTY(hi))
21263 return HI2UF(hi);
21264 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265}
21266
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021267#if defined(EXITFREE) || defined(PROTO)
21268 void
21269free_all_functions()
21270{
21271 hashitem_T *hi;
21272
21273 /* Need to start all over every time, because func_free() may change the
21274 * hash table. */
21275 while (func_hashtab.ht_used > 0)
21276 for (hi = func_hashtab.ht_array; ; ++hi)
21277 if (!HASHITEM_EMPTY(hi))
21278 {
21279 func_free(HI2UF(hi));
21280 break;
21281 }
21282}
21283#endif
21284
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021285/*
21286 * Return TRUE if a function "name" exists.
21287 */
21288 static int
21289function_exists(name)
21290 char_u *name;
21291{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021292 char_u *nm = name;
21293 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021294 int n = FALSE;
21295
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021296 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021297 nm = skipwhite(nm);
21298
21299 /* Only accept "funcname", "funcname ", "funcname (..." and
21300 * "funcname(...", not "funcname!...". */
21301 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021302 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021303 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021304 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021305 else
21306 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021307 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021308 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021309 return n;
21310}
21311
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021312/*
21313 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021314 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021315 */
21316 static int
21317builtin_function(name)
21318 char_u *name;
21319{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021320 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21321 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021322}
21323
Bram Moolenaar05159a02005-02-26 23:04:13 +000021324#if defined(FEAT_PROFILE) || defined(PROTO)
21325/*
21326 * Start profiling function "fp".
21327 */
21328 static void
21329func_do_profile(fp)
21330 ufunc_T *fp;
21331{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021332 int len = fp->uf_lines.ga_len;
21333
21334 if (len == 0)
21335 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021336 fp->uf_tm_count = 0;
21337 profile_zero(&fp->uf_tm_self);
21338 profile_zero(&fp->uf_tm_total);
21339 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021340 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021341 if (fp->uf_tml_total == NULL)
21342 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021343 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021344 if (fp->uf_tml_self == NULL)
21345 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021346 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021347 fp->uf_tml_idx = -1;
21348 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21349 || fp->uf_tml_self == NULL)
21350 return; /* out of memory */
21351
21352 fp->uf_profiling = TRUE;
21353}
21354
21355/*
21356 * Dump the profiling results for all functions in file "fd".
21357 */
21358 void
21359func_dump_profile(fd)
21360 FILE *fd;
21361{
21362 hashitem_T *hi;
21363 int todo;
21364 ufunc_T *fp;
21365 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021366 ufunc_T **sorttab;
21367 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021368
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021369 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021370 if (todo == 0)
21371 return; /* nothing to dump */
21372
Bram Moolenaar73830342005-02-28 22:48:19 +000021373 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21374
Bram Moolenaar05159a02005-02-26 23:04:13 +000021375 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21376 {
21377 if (!HASHITEM_EMPTY(hi))
21378 {
21379 --todo;
21380 fp = HI2UF(hi);
21381 if (fp->uf_profiling)
21382 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021383 if (sorttab != NULL)
21384 sorttab[st_len++] = fp;
21385
Bram Moolenaar05159a02005-02-26 23:04:13 +000021386 if (fp->uf_name[0] == K_SPECIAL)
21387 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21388 else
21389 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21390 if (fp->uf_tm_count == 1)
21391 fprintf(fd, "Called 1 time\n");
21392 else
21393 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21394 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21395 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21396 fprintf(fd, "\n");
21397 fprintf(fd, "count total (s) self (s)\n");
21398
21399 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21400 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021401 if (FUNCLINE(fp, i) == NULL)
21402 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021403 prof_func_line(fd, fp->uf_tml_count[i],
21404 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021405 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21406 }
21407 fprintf(fd, "\n");
21408 }
21409 }
21410 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021411
21412 if (sorttab != NULL && st_len > 0)
21413 {
21414 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21415 prof_total_cmp);
21416 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21417 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21418 prof_self_cmp);
21419 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21420 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021421
21422 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021423}
Bram Moolenaar73830342005-02-28 22:48:19 +000021424
21425 static void
21426prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21427 FILE *fd;
21428 ufunc_T **sorttab;
21429 int st_len;
21430 char *title;
21431 int prefer_self; /* when equal print only self time */
21432{
21433 int i;
21434 ufunc_T *fp;
21435
21436 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21437 fprintf(fd, "count total (s) self (s) function\n");
21438 for (i = 0; i < 20 && i < st_len; ++i)
21439 {
21440 fp = sorttab[i];
21441 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21442 prefer_self);
21443 if (fp->uf_name[0] == K_SPECIAL)
21444 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21445 else
21446 fprintf(fd, " %s()\n", fp->uf_name);
21447 }
21448 fprintf(fd, "\n");
21449}
21450
21451/*
21452 * Print the count and times for one function or function line.
21453 */
21454 static void
21455prof_func_line(fd, count, total, self, prefer_self)
21456 FILE *fd;
21457 int count;
21458 proftime_T *total;
21459 proftime_T *self;
21460 int prefer_self; /* when equal print only self time */
21461{
21462 if (count > 0)
21463 {
21464 fprintf(fd, "%5d ", count);
21465 if (prefer_self && profile_equal(total, self))
21466 fprintf(fd, " ");
21467 else
21468 fprintf(fd, "%s ", profile_msg(total));
21469 if (!prefer_self && profile_equal(total, self))
21470 fprintf(fd, " ");
21471 else
21472 fprintf(fd, "%s ", profile_msg(self));
21473 }
21474 else
21475 fprintf(fd, " ");
21476}
21477
21478/*
21479 * Compare function for total time sorting.
21480 */
21481 static int
21482#ifdef __BORLANDC__
21483_RTLENTRYF
21484#endif
21485prof_total_cmp(s1, s2)
21486 const void *s1;
21487 const void *s2;
21488{
21489 ufunc_T *p1, *p2;
21490
21491 p1 = *(ufunc_T **)s1;
21492 p2 = *(ufunc_T **)s2;
21493 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21494}
21495
21496/*
21497 * Compare function for self time sorting.
21498 */
21499 static int
21500#ifdef __BORLANDC__
21501_RTLENTRYF
21502#endif
21503prof_self_cmp(s1, s2)
21504 const void *s1;
21505 const void *s2;
21506{
21507 ufunc_T *p1, *p2;
21508
21509 p1 = *(ufunc_T **)s1;
21510 p2 = *(ufunc_T **)s2;
21511 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21512}
21513
Bram Moolenaar05159a02005-02-26 23:04:13 +000021514#endif
21515
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021516/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021517 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021518 * Return TRUE if a package was loaded.
21519 */
21520 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021521script_autoload(name, reload)
21522 char_u *name;
21523 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021524{
21525 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021526 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021527 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021528 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021529
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021530 /* Return quickly when autoload disabled. */
21531 if (no_autoload)
21532 return FALSE;
21533
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021534 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021535 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021536 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021537 return FALSE;
21538
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021539 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021540
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021541 /* Find the name in the list of previously loaded package names. Skip
21542 * "autoload/", it's always the same. */
21543 for (i = 0; i < ga_loaded.ga_len; ++i)
21544 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21545 break;
21546 if (!reload && i < ga_loaded.ga_len)
21547 ret = FALSE; /* was loaded already */
21548 else
21549 {
21550 /* Remember the name if it wasn't loaded already. */
21551 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21552 {
21553 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21554 tofree = NULL;
21555 }
21556
21557 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021558 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021559 ret = TRUE;
21560 }
21561
21562 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021563 return ret;
21564}
21565
21566/*
21567 * Return the autoload script name for a function or variable name.
21568 * Returns NULL when out of memory.
21569 */
21570 static char_u *
21571autoload_name(name)
21572 char_u *name;
21573{
21574 char_u *p;
21575 char_u *scriptname;
21576
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021577 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021578 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21579 if (scriptname == NULL)
21580 return FALSE;
21581 STRCPY(scriptname, "autoload/");
21582 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021583 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021584 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021585 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021586 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021587 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021588}
21589
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21591
21592/*
21593 * Function given to ExpandGeneric() to obtain the list of user defined
21594 * function names.
21595 */
21596 char_u *
21597get_user_func_name(xp, idx)
21598 expand_T *xp;
21599 int idx;
21600{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021601 static long_u done;
21602 static hashitem_T *hi;
21603 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604
21605 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021607 done = 0;
21608 hi = func_hashtab.ht_array;
21609 }
21610 if (done < func_hashtab.ht_used)
21611 {
21612 if (done++ > 0)
21613 ++hi;
21614 while (HASHITEM_EMPTY(hi))
21615 ++hi;
21616 fp = HI2UF(hi);
21617
21618 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21619 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620
21621 cat_func_name(IObuff, fp);
21622 if (xp->xp_context != EXPAND_USER_FUNC)
21623 {
21624 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021625 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 STRCAT(IObuff, ")");
21627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628 return IObuff;
21629 }
21630 return NULL;
21631}
21632
21633#endif /* FEAT_CMDL_COMPL */
21634
21635/*
21636 * Copy the function name of "fp" to buffer "buf".
21637 * "buf" must be able to hold the function name plus three bytes.
21638 * Takes care of script-local function names.
21639 */
21640 static void
21641cat_func_name(buf, fp)
21642 char_u *buf;
21643 ufunc_T *fp;
21644{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021645 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 {
21647 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021648 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 }
21650 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021651 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652}
21653
21654/*
21655 * ":delfunction {name}"
21656 */
21657 void
21658ex_delfunction(eap)
21659 exarg_T *eap;
21660{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021661 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 char_u *p;
21663 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021664 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665
21666 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021667 name = trans_function_name(&p, eap->skip, 0, &fudi);
21668 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021670 {
21671 if (fudi.fd_dict != NULL && !eap->skip)
21672 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021673 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 if (!ends_excmd(*skipwhite(p)))
21676 {
21677 vim_free(name);
21678 EMSG(_(e_trailing));
21679 return;
21680 }
21681 eap->nextcmd = check_nextcmd(p);
21682 if (eap->nextcmd != NULL)
21683 *p = NUL;
21684
21685 if (!eap->skip)
21686 fp = find_func(name);
21687 vim_free(name);
21688
21689 if (!eap->skip)
21690 {
21691 if (fp == NULL)
21692 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021693 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 return;
21695 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021696 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 {
21698 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21699 return;
21700 }
21701
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021702 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021704 /* Delete the dict item that refers to the function, it will
21705 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021706 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021708 else
21709 func_free(fp);
21710 }
21711}
21712
21713/*
21714 * Free a function and remove it from the list of functions.
21715 */
21716 static void
21717func_free(fp)
21718 ufunc_T *fp;
21719{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021720 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021721
21722 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021723 ga_clear_strings(&(fp->uf_args));
21724 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021725#ifdef FEAT_PROFILE
21726 vim_free(fp->uf_tml_count);
21727 vim_free(fp->uf_tml_total);
21728 vim_free(fp->uf_tml_self);
21729#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021730
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021731 /* remove the function from the function hashtable */
21732 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21733 if (HASHITEM_EMPTY(hi))
21734 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021735 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021736 hash_remove(&func_hashtab, hi);
21737
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021738 vim_free(fp);
21739}
21740
21741/*
21742 * Unreference a Function: decrement the reference count and free it when it
21743 * becomes zero. Only for numbered functions.
21744 */
21745 static void
21746func_unref(name)
21747 char_u *name;
21748{
21749 ufunc_T *fp;
21750
21751 if (name != NULL && isdigit(*name))
21752 {
21753 fp = find_func(name);
21754 if (fp == NULL)
21755 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021756 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021757 {
21758 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021759 * when "uf_calls" becomes zero. */
21760 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021761 func_free(fp);
21762 }
21763 }
21764}
21765
21766/*
21767 * Count a reference to a Function.
21768 */
21769 static void
21770func_ref(name)
21771 char_u *name;
21772{
21773 ufunc_T *fp;
21774
21775 if (name != NULL && isdigit(*name))
21776 {
21777 fp = find_func(name);
21778 if (fp == NULL)
21779 EMSG2(_(e_intern2), "func_ref()");
21780 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021781 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 }
21783}
21784
21785/*
21786 * Call a user function.
21787 */
21788 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021789call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 ufunc_T *fp; /* pointer to function */
21791 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021792 typval_T *argvars; /* arguments */
21793 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 linenr_T firstline; /* first line of range */
21795 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021796 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797{
Bram Moolenaar33570922005-01-25 22:26:29 +000021798 char_u *save_sourcing_name;
21799 linenr_T save_sourcing_lnum;
21800 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021801 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021802 int save_did_emsg;
21803 static int depth = 0;
21804 dictitem_T *v;
21805 int fixvar_idx = 0; /* index in fixvar[] */
21806 int i;
21807 int ai;
21808 char_u numbuf[NUMBUFLEN];
21809 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021810#ifdef FEAT_PROFILE
21811 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021812 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814
21815 /* If depth of calling is getting too high, don't execute the function */
21816 if (depth >= p_mfd)
21817 {
21818 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021819 rettv->v_type = VAR_NUMBER;
21820 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021821 return;
21822 }
21823 ++depth;
21824
21825 line_breakcheck(); /* check for CTRL-C hit */
21826
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021827 fc = (funccall_T *)alloc(sizeof(funccall_T));
21828 fc->caller = current_funccal;
21829 current_funccal = fc;
21830 fc->func = fp;
21831 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021832 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021833 fc->linenr = 0;
21834 fc->returned = FALSE;
21835 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021837 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21838 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839
Bram Moolenaar33570922005-01-25 22:26:29 +000021840 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021841 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021842 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21843 * each argument variable and saves a lot of time.
21844 */
21845 /*
21846 * Init l: variables.
21847 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021848 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021849 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021850 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021851 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21852 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021853 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021854 name = v->di_key;
21855 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021856 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021857 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021858 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021859 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021860 v->di_tv.vval.v_dict = selfdict;
21861 ++selfdict->dv_refcount;
21862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021863
Bram Moolenaar33570922005-01-25 22:26:29 +000021864 /*
21865 * Init a: variables.
21866 * Set a:0 to "argcount".
21867 * Set a:000 to a list with room for the "..." arguments.
21868 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021869 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21870 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021871 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021872 /* Use "name" to avoid a warning from some compiler that checks the
21873 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021874 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021875 name = v->di_key;
21876 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021877 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021878 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021879 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021880 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021881 v->di_tv.vval.v_list = &fc->l_varlist;
21882 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21883 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21884 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021885
21886 /*
21887 * Set a:firstline to "firstline" and a:lastline to "lastline".
21888 * Set a:name to named arguments.
21889 * Set a:N to the "..." arguments.
21890 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021891 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021892 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021893 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021894 (varnumber_T)lastline);
21895 for (i = 0; i < argcount; ++i)
21896 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021897 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021898 if (ai < 0)
21899 /* named argument a:name */
21900 name = FUNCARG(fp, i);
21901 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021902 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021903 /* "..." argument a:1, a:2, etc. */
21904 sprintf((char *)numbuf, "%d", ai + 1);
21905 name = numbuf;
21906 }
21907 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21908 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021909 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021910 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21911 }
21912 else
21913 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021914 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21915 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021916 if (v == NULL)
21917 break;
21918 v->di_flags = DI_FLAGS_RO;
21919 }
21920 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021921 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021922
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021923 /* Note: the values are copied directly to avoid alloc/free.
21924 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021925 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021926 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021927
21928 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21929 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021930 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21931 fc->l_listitems[ai].li_tv = argvars[i];
21932 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021933 }
21934 }
21935
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 /* Don't redraw while executing the function. */
21937 ++RedrawingDisabled;
21938 save_sourcing_name = sourcing_name;
21939 save_sourcing_lnum = sourcing_lnum;
21940 sourcing_lnum = 1;
21941 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021942 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 if (sourcing_name != NULL)
21944 {
21945 if (save_sourcing_name != NULL
21946 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21947 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21948 else
21949 STRCPY(sourcing_name, "function ");
21950 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21951
21952 if (p_verbose >= 12)
21953 {
21954 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021955 verbose_enter_scroll();
21956
Bram Moolenaar555b2802005-05-19 21:08:39 +000021957 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 if (p_verbose >= 14)
21959 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021961 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021962 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021963 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964
21965 msg_puts((char_u *)"(");
21966 for (i = 0; i < argcount; ++i)
21967 {
21968 if (i > 0)
21969 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021970 if (argvars[i].v_type == VAR_NUMBER)
21971 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 else
21973 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021974 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21975 if (s != NULL)
21976 {
21977 trunc_string(s, buf, MSG_BUF_CLEN);
21978 msg_puts(buf);
21979 vim_free(tofree);
21980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 }
21982 }
21983 msg_puts((char_u *)")");
21984 }
21985 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021986
21987 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 --no_wait_return;
21989 }
21990 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021991#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021992 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021993 {
21994 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21995 func_do_profile(fp);
21996 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021997 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021998 {
21999 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022000 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022001 profile_zero(&fp->uf_tm_children);
22002 }
22003 script_prof_save(&wait_start);
22004 }
22005#endif
22006
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022008 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 save_did_emsg = did_emsg;
22010 did_emsg = FALSE;
22011
22012 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022013 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22015
22016 --RedrawingDisabled;
22017
22018 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022019 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022021 clear_tv(rettv);
22022 rettv->v_type = VAR_NUMBER;
22023 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 }
22025
Bram Moolenaar05159a02005-02-26 23:04:13 +000022026#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022027 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022028 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022029 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022030 profile_end(&call_start);
22031 profile_sub_wait(&wait_start, &call_start);
22032 profile_add(&fp->uf_tm_total, &call_start);
22033 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022034 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022035 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022036 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22037 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022038 }
22039 }
22040#endif
22041
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042 /* when being verbose, mention the return value */
22043 if (p_verbose >= 12)
22044 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022046 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022049 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022050 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022051 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022052 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022053 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022055 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022056 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022057 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022058 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022059
Bram Moolenaar555b2802005-05-19 21:08:39 +000022060 /* The value may be very long. Skip the middle part, so that we
22061 * have some idea how it starts and ends. smsg() would always
22062 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022063 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022064 if (s != NULL)
22065 {
22066 trunc_string(s, buf, MSG_BUF_CLEN);
22067 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22068 vim_free(tofree);
22069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 }
22071 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022072
22073 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 --no_wait_return;
22075 }
22076
22077 vim_free(sourcing_name);
22078 sourcing_name = save_sourcing_name;
22079 sourcing_lnum = save_sourcing_lnum;
22080 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022081#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022082 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022083 script_prof_restore(&wait_start);
22084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085
22086 if (p_verbose >= 12 && sourcing_name != NULL)
22087 {
22088 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022089 verbose_enter_scroll();
22090
Bram Moolenaar555b2802005-05-19 21:08:39 +000022091 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022093
22094 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 --no_wait_return;
22096 }
22097
22098 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022099 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022101
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022102 /* If the a:000 list and the l: and a: dicts are not referenced we can
22103 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022104 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22105 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22106 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22107 {
22108 free_funccal(fc, FALSE);
22109 }
22110 else
22111 {
22112 hashitem_T *hi;
22113 listitem_T *li;
22114 int todo;
22115
22116 /* "fc" is still in use. This can happen when returning "a:000" or
22117 * assigning "l:" to a global variable.
22118 * Link "fc" in the list for garbage collection later. */
22119 fc->caller = previous_funccal;
22120 previous_funccal = fc;
22121
22122 /* Make a copy of the a: variables, since we didn't do that above. */
22123 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22124 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22125 {
22126 if (!HASHITEM_EMPTY(hi))
22127 {
22128 --todo;
22129 v = HI2DI(hi);
22130 copy_tv(&v->di_tv, &v->di_tv);
22131 }
22132 }
22133
22134 /* Make a copy of the a:000 items, since we didn't do that above. */
22135 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22136 copy_tv(&li->li_tv, &li->li_tv);
22137 }
22138}
22139
22140/*
22141 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022142 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022143 */
22144 static int
22145can_free_funccal(fc, copyID)
22146 funccall_T *fc;
22147 int copyID;
22148{
22149 return (fc->l_varlist.lv_copyID != copyID
22150 && fc->l_vars.dv_copyID != copyID
22151 && fc->l_avars.dv_copyID != copyID);
22152}
22153
22154/*
22155 * Free "fc" and what it contains.
22156 */
22157 static void
22158free_funccal(fc, free_val)
22159 funccall_T *fc;
22160 int free_val; /* a: vars were allocated */
22161{
22162 listitem_T *li;
22163
22164 /* The a: variables typevals may not have been allocated, only free the
22165 * allocated variables. */
22166 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22167
22168 /* free all l: variables */
22169 vars_clear(&fc->l_vars.dv_hashtab);
22170
22171 /* Free the a:000 variables if they were allocated. */
22172 if (free_val)
22173 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22174 clear_tv(&li->li_tv);
22175
22176 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177}
22178
22179/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022180 * Add a number variable "name" to dict "dp" with value "nr".
22181 */
22182 static void
22183add_nr_var(dp, v, name, nr)
22184 dict_T *dp;
22185 dictitem_T *v;
22186 char *name;
22187 varnumber_T nr;
22188{
22189 STRCPY(v->di_key, name);
22190 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22191 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22192 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022193 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022194 v->di_tv.vval.v_number = nr;
22195}
22196
22197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 * ":return [expr]"
22199 */
22200 void
22201ex_return(eap)
22202 exarg_T *eap;
22203{
22204 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022205 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 int returning = FALSE;
22207
22208 if (current_funccal == NULL)
22209 {
22210 EMSG(_("E133: :return not inside a function"));
22211 return;
22212 }
22213
22214 if (eap->skip)
22215 ++emsg_skip;
22216
22217 eap->nextcmd = NULL;
22218 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022219 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 {
22221 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022222 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022224 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225 }
22226 /* It's safer to return also on error. */
22227 else if (!eap->skip)
22228 {
22229 /*
22230 * Return unless the expression evaluation has been cancelled due to an
22231 * aborting error, an interrupt, or an exception.
22232 */
22233 if (!aborting())
22234 returning = do_return(eap, FALSE, TRUE, NULL);
22235 }
22236
22237 /* When skipping or the return gets pending, advance to the next command
22238 * in this line (!returning). Otherwise, ignore the rest of the line.
22239 * Following lines will be ignored by get_func_line(). */
22240 if (returning)
22241 eap->nextcmd = NULL;
22242 else if (eap->nextcmd == NULL) /* no argument */
22243 eap->nextcmd = check_nextcmd(arg);
22244
22245 if (eap->skip)
22246 --emsg_skip;
22247}
22248
22249/*
22250 * Return from a function. Possibly makes the return pending. Also called
22251 * for a pending return at the ":endtry" or after returning from an extra
22252 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022253 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022254 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 * FALSE when the return gets pending.
22256 */
22257 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022258do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259 exarg_T *eap;
22260 int reanimate;
22261 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022262 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263{
22264 int idx;
22265 struct condstack *cstack = eap->cstack;
22266
22267 if (reanimate)
22268 /* Undo the return. */
22269 current_funccal->returned = FALSE;
22270
22271 /*
22272 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22273 * not in its finally clause (which then is to be executed next) is found.
22274 * In this case, make the ":return" pending for execution at the ":endtry".
22275 * Otherwise, return normally.
22276 */
22277 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22278 if (idx >= 0)
22279 {
22280 cstack->cs_pending[idx] = CSTP_RETURN;
22281
22282 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022283 /* A pending return again gets pending. "rettv" points to an
22284 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022286 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 else
22288 {
22289 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022290 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022292 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022294 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 {
22296 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022297 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022298 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299 else
22300 EMSG(_(e_outofmem));
22301 }
22302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022303 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304
22305 if (reanimate)
22306 {
22307 /* The pending return value could be overwritten by a ":return"
22308 * without argument in a finally clause; reset the default
22309 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022310 current_funccal->rettv->v_type = VAR_NUMBER;
22311 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022312 }
22313 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022314 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315 }
22316 else
22317 {
22318 current_funccal->returned = TRUE;
22319
22320 /* If the return is carried out now, store the return value. For
22321 * a return immediately after reanimation, the value is already
22322 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022323 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022325 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022326 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022328 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329 }
22330 }
22331
22332 return idx < 0;
22333}
22334
22335/*
22336 * Free the variable with a pending return value.
22337 */
22338 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022339discard_pending_return(rettv)
22340 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341{
Bram Moolenaar33570922005-01-25 22:26:29 +000022342 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343}
22344
22345/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022346 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347 * is an allocated string. Used by report_pending() for verbose messages.
22348 */
22349 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022350get_return_cmd(rettv)
22351 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022353 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022354 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022355 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022357 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022358 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022359 if (s == NULL)
22360 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022361
22362 STRCPY(IObuff, ":return ");
22363 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22364 if (STRLEN(s) + 8 >= IOSIZE)
22365 STRCPY(IObuff + IOSIZE - 4, "...");
22366 vim_free(tofree);
22367 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368}
22369
22370/*
22371 * Get next function line.
22372 * Called by do_cmdline() to get the next line.
22373 * Returns allocated string, or NULL for end of function.
22374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375 char_u *
22376get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022377 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022379 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380{
Bram Moolenaar33570922005-01-25 22:26:29 +000022381 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022382 ufunc_T *fp = fcp->func;
22383 char_u *retval;
22384 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385
22386 /* If breakpoints have been added/deleted need to check for it. */
22387 if (fcp->dbg_tick != debug_tick)
22388 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022389 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 sourcing_lnum);
22391 fcp->dbg_tick = debug_tick;
22392 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022393#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022394 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022395 func_line_end(cookie);
22396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397
Bram Moolenaar05159a02005-02-26 23:04:13 +000022398 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022399 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22400 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 retval = NULL;
22402 else
22403 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022404 /* Skip NULL lines (continuation lines). */
22405 while (fcp->linenr < gap->ga_len
22406 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22407 ++fcp->linenr;
22408 if (fcp->linenr >= gap->ga_len)
22409 retval = NULL;
22410 else
22411 {
22412 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22413 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022414#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022415 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022416 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022417#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 }
22420
22421 /* Did we encounter a breakpoint? */
22422 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22423 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022424 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022426 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 sourcing_lnum);
22428 fcp->dbg_tick = debug_tick;
22429 }
22430
22431 return retval;
22432}
22433
Bram Moolenaar05159a02005-02-26 23:04:13 +000022434#if defined(FEAT_PROFILE) || defined(PROTO)
22435/*
22436 * Called when starting to read a function line.
22437 * "sourcing_lnum" must be correct!
22438 * When skipping lines it may not actually be executed, but we won't find out
22439 * until later and we need to store the time now.
22440 */
22441 void
22442func_line_start(cookie)
22443 void *cookie;
22444{
22445 funccall_T *fcp = (funccall_T *)cookie;
22446 ufunc_T *fp = fcp->func;
22447
22448 if (fp->uf_profiling && sourcing_lnum >= 1
22449 && sourcing_lnum <= fp->uf_lines.ga_len)
22450 {
22451 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022452 /* Skip continuation lines. */
22453 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22454 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022455 fp->uf_tml_execed = FALSE;
22456 profile_start(&fp->uf_tml_start);
22457 profile_zero(&fp->uf_tml_children);
22458 profile_get_wait(&fp->uf_tml_wait);
22459 }
22460}
22461
22462/*
22463 * Called when actually executing a function line.
22464 */
22465 void
22466func_line_exec(cookie)
22467 void *cookie;
22468{
22469 funccall_T *fcp = (funccall_T *)cookie;
22470 ufunc_T *fp = fcp->func;
22471
22472 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22473 fp->uf_tml_execed = TRUE;
22474}
22475
22476/*
22477 * Called when done with a function line.
22478 */
22479 void
22480func_line_end(cookie)
22481 void *cookie;
22482{
22483 funccall_T *fcp = (funccall_T *)cookie;
22484 ufunc_T *fp = fcp->func;
22485
22486 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22487 {
22488 if (fp->uf_tml_execed)
22489 {
22490 ++fp->uf_tml_count[fp->uf_tml_idx];
22491 profile_end(&fp->uf_tml_start);
22492 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022493 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022494 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22495 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022496 }
22497 fp->uf_tml_idx = -1;
22498 }
22499}
22500#endif
22501
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502/*
22503 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022504 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505 */
22506 int
22507func_has_ended(cookie)
22508 void *cookie;
22509{
Bram Moolenaar33570922005-01-25 22:26:29 +000022510 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022511
22512 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22513 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022514 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515 || fcp->returned);
22516}
22517
22518/*
22519 * return TRUE if cookie indicates a function which "abort"s on errors.
22520 */
22521 int
22522func_has_abort(cookie)
22523 void *cookie;
22524{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022525 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526}
22527
22528#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22529typedef enum
22530{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022531 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22532 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22533 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534} var_flavour_T;
22535
22536static var_flavour_T var_flavour __ARGS((char_u *varname));
22537
22538 static var_flavour_T
22539var_flavour(varname)
22540 char_u *varname;
22541{
22542 char_u *p = varname;
22543
22544 if (ASCII_ISUPPER(*p))
22545 {
22546 while (*(++p))
22547 if (ASCII_ISLOWER(*p))
22548 return VAR_FLAVOUR_SESSION;
22549 return VAR_FLAVOUR_VIMINFO;
22550 }
22551 else
22552 return VAR_FLAVOUR_DEFAULT;
22553}
22554#endif
22555
22556#if defined(FEAT_VIMINFO) || defined(PROTO)
22557/*
22558 * Restore global vars that start with a capital from the viminfo file
22559 */
22560 int
22561read_viminfo_varlist(virp, writing)
22562 vir_T *virp;
22563 int writing;
22564{
22565 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022566 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022567 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568
22569 if (!writing && (find_viminfo_parameter('!') != NULL))
22570 {
22571 tab = vim_strchr(virp->vir_line + 1, '\t');
22572 if (tab != NULL)
22573 {
22574 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022575 switch (*tab)
22576 {
22577 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022578#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022579 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022580#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022581 case 'D': type = VAR_DICT; break;
22582 case 'L': type = VAR_LIST; break;
22583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584
22585 tab = vim_strchr(tab, '\t');
22586 if (tab != NULL)
22587 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022588 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022589 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022590 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022592#ifdef FEAT_FLOAT
22593 else if (type == VAR_FLOAT)
22594 (void)string2float(tab + 1, &tv.vval.v_float);
22595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022597 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022598 if (type == VAR_DICT || type == VAR_LIST)
22599 {
22600 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22601
22602 if (etv == NULL)
22603 /* Failed to parse back the dict or list, use it as a
22604 * string. */
22605 tv.v_type = VAR_STRING;
22606 else
22607 {
22608 vim_free(tv.vval.v_string);
22609 tv = *etv;
22610 }
22611 }
22612
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022613 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022614
22615 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022616 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022617 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22618 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619 }
22620 }
22621 }
22622
22623 return viminfo_readline(virp);
22624}
22625
22626/*
22627 * Write global vars that start with a capital to the viminfo file
22628 */
22629 void
22630write_viminfo_varlist(fp)
22631 FILE *fp;
22632{
Bram Moolenaar33570922005-01-25 22:26:29 +000022633 hashitem_T *hi;
22634 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022635 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022636 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022637 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022638 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022639 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640
22641 if (find_viminfo_parameter('!') == NULL)
22642 return;
22643
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022644 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022645
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022646 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022647 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022648 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022649 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022651 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022652 this_var = HI2DI(hi);
22653 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022654 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022655 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022656 {
22657 case VAR_STRING: s = "STR"; break;
22658 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022659#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022660 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022661#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022662 case VAR_DICT: s = "DIC"; break;
22663 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022664 default: continue;
22665 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022666 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022667 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022668 if (p != NULL)
22669 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022670 vim_free(tofree);
22671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672 }
22673 }
22674}
22675#endif
22676
22677#if defined(FEAT_SESSION) || defined(PROTO)
22678 int
22679store_session_globals(fd)
22680 FILE *fd;
22681{
Bram Moolenaar33570922005-01-25 22:26:29 +000022682 hashitem_T *hi;
22683 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022684 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 char_u *p, *t;
22686
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022687 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022688 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022690 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022692 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022693 this_var = HI2DI(hi);
22694 if ((this_var->di_tv.v_type == VAR_NUMBER
22695 || this_var->di_tv.v_type == VAR_STRING)
22696 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022697 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022698 /* Escape special characters with a backslash. Turn a LF and
22699 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022700 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022701 (char_u *)"\\\"\n\r");
22702 if (p == NULL) /* out of memory */
22703 break;
22704 for (t = p; *t != NUL; ++t)
22705 if (*t == '\n')
22706 *t = 'n';
22707 else if (*t == '\r')
22708 *t = 'r';
22709 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022710 this_var->di_key,
22711 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22712 : ' ',
22713 p,
22714 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22715 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022716 || put_eol(fd) == FAIL)
22717 {
22718 vim_free(p);
22719 return FAIL;
22720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 vim_free(p);
22722 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022723#ifdef FEAT_FLOAT
22724 else if (this_var->di_tv.v_type == VAR_FLOAT
22725 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22726 {
22727 float_T f = this_var->di_tv.vval.v_float;
22728 int sign = ' ';
22729
22730 if (f < 0)
22731 {
22732 f = -f;
22733 sign = '-';
22734 }
22735 if ((fprintf(fd, "let %s = %c&%f",
22736 this_var->di_key, sign, f) < 0)
22737 || put_eol(fd) == FAIL)
22738 return FAIL;
22739 }
22740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741 }
22742 }
22743 return OK;
22744}
22745#endif
22746
Bram Moolenaar661b1822005-07-28 22:36:45 +000022747/*
22748 * Display script name where an item was last set.
22749 * Should only be invoked when 'verbose' is non-zero.
22750 */
22751 void
22752last_set_msg(scriptID)
22753 scid_T scriptID;
22754{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022755 char_u *p;
22756
Bram Moolenaar661b1822005-07-28 22:36:45 +000022757 if (scriptID != 0)
22758 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022759 p = home_replace_save(NULL, get_scriptname(scriptID));
22760 if (p != NULL)
22761 {
22762 verbose_enter();
22763 MSG_PUTS(_("\n\tLast set from "));
22764 MSG_PUTS(p);
22765 vim_free(p);
22766 verbose_leave();
22767 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022768 }
22769}
22770
Bram Moolenaard812df62008-11-09 12:46:09 +000022771/*
22772 * List v:oldfiles in a nice way.
22773 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022774 void
22775ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022776 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022777{
22778 list_T *l = vimvars[VV_OLDFILES].vv_list;
22779 listitem_T *li;
22780 int nr = 0;
22781
22782 if (l == NULL)
22783 msg((char_u *)_("No old files"));
22784 else
22785 {
22786 msg_start();
22787 msg_scroll = TRUE;
22788 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22789 {
22790 msg_outnum((long)++nr);
22791 MSG_PUTS(": ");
22792 msg_outtrans(get_tv_string(&li->li_tv));
22793 msg_putchar('\n');
22794 out_flush(); /* output one line at a time */
22795 ui_breakcheck();
22796 }
22797 /* Assume "got_int" was set to truncate the listing. */
22798 got_int = FALSE;
22799
22800#ifdef FEAT_BROWSE_CMD
22801 if (cmdmod.browse)
22802 {
22803 quit_more = FALSE;
22804 nr = prompt_for_number(FALSE);
22805 msg_starthere();
22806 if (nr > 0)
22807 {
22808 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22809 (long)nr);
22810
22811 if (p != NULL)
22812 {
22813 p = expand_env_save(p);
22814 eap->arg = p;
22815 eap->cmdidx = CMD_edit;
22816 cmdmod.browse = FALSE;
22817 do_exedit(eap, NULL);
22818 vim_free(p);
22819 }
22820 }
22821 }
22822#endif
22823 }
22824}
22825
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826#endif /* FEAT_EVAL */
22827
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022829#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830
22831#ifdef WIN3264
22832/*
22833 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22834 */
22835static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22836static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22837static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22838
22839/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022840 * Get the short path (8.3) for the filename in "fnamep".
22841 * Only works for a valid file name.
22842 * When the path gets longer "fnamep" is changed and the allocated buffer
22843 * is put in "bufp".
22844 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22845 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 */
22847 static int
22848get_short_pathname(fnamep, bufp, fnamelen)
22849 char_u **fnamep;
22850 char_u **bufp;
22851 int *fnamelen;
22852{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022853 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 char_u *newbuf;
22855
22856 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 l = GetShortPathName(*fnamep, *fnamep, len);
22858 if (l > len - 1)
22859 {
22860 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022861 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 newbuf = vim_strnsave(*fnamep, l);
22863 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022864 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865
22866 vim_free(*bufp);
22867 *fnamep = *bufp = newbuf;
22868
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022869 /* Really should always succeed, as the buffer is big enough. */
22870 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 }
22872
22873 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022874 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875}
22876
22877/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022878 * Get the short path (8.3) for the filename in "fname". The converted
22879 * path is returned in "bufp".
22880 *
22881 * Some of the directories specified in "fname" may not exist. This function
22882 * will shorten the existing directories at the beginning of the path and then
22883 * append the remaining non-existing path.
22884 *
22885 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022886 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022887 * bufp - Pointer to an allocated buffer for the filename.
22888 * fnamelen - Length of the filename pointed to by fname
22889 *
22890 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 */
22892 static int
22893shortpath_for_invalid_fname(fname, bufp, fnamelen)
22894 char_u **fname;
22895 char_u **bufp;
22896 int *fnamelen;
22897{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022898 char_u *short_fname, *save_fname, *pbuf_unused;
22899 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022901 int old_len, len;
22902 int new_len, sfx_len;
22903 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904
22905 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022906 old_len = *fnamelen;
22907 save_fname = vim_strnsave(*fname, old_len);
22908 pbuf_unused = NULL;
22909 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022911 endp = save_fname + old_len - 1; /* Find the end of the copy */
22912 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022914 /*
22915 * Try shortening the supplied path till it succeeds by removing one
22916 * directory at a time from the tail of the path.
22917 */
22918 len = 0;
22919 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022921 /* go back one path-separator */
22922 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22923 --endp;
22924 if (endp <= save_fname)
22925 break; /* processed the complete path */
22926
22927 /*
22928 * Replace the path separator with a NUL and try to shorten the
22929 * resulting path.
22930 */
22931 ch = *endp;
22932 *endp = 0;
22933 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022934 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022935 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22936 {
22937 retval = FAIL;
22938 goto theend;
22939 }
22940 *endp = ch; /* preserve the string */
22941
22942 if (len > 0)
22943 break; /* successfully shortened the path */
22944
22945 /* failed to shorten the path. Skip the path separator */
22946 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 }
22948
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022949 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022951 /*
22952 * Succeeded in shortening the path. Now concatenate the shortened
22953 * path with the remaining path at the tail.
22954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022956 /* Compute the length of the new path. */
22957 sfx_len = (int)(save_endp - endp) + 1;
22958 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022960 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022962 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022964 /* There is not enough space in the currently allocated string,
22965 * copy it to a buffer big enough. */
22966 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022968 {
22969 retval = FAIL;
22970 goto theend;
22971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 }
22973 else
22974 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022975 /* Transfer short_fname to the main buffer (it's big enough),
22976 * unless get_short_pathname() did its work in-place. */
22977 *fname = *bufp = save_fname;
22978 if (short_fname != save_fname)
22979 vim_strncpy(save_fname, short_fname, len);
22980 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022982
22983 /* concat the not-shortened part of the path */
22984 vim_strncpy(*fname + len, endp, sfx_len);
22985 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022987
22988theend:
22989 vim_free(pbuf_unused);
22990 vim_free(save_fname);
22991
22992 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993}
22994
22995/*
22996 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022997 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 */
22999 static int
23000shortpath_for_partial(fnamep, bufp, fnamelen)
23001 char_u **fnamep;
23002 char_u **bufp;
23003 int *fnamelen;
23004{
23005 int sepcount, len, tflen;
23006 char_u *p;
23007 char_u *pbuf, *tfname;
23008 int hasTilde;
23009
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023010 /* Count up the path separators from the RHS.. so we know which part
23011 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023013 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 if (vim_ispathsep(*p))
23015 ++sepcount;
23016
23017 /* Need full path first (use expand_env() to remove a "~/") */
23018 hasTilde = (**fnamep == '~');
23019 if (hasTilde)
23020 pbuf = tfname = expand_env_save(*fnamep);
23021 else
23022 pbuf = tfname = FullName_save(*fnamep, FALSE);
23023
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023024 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023026 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23027 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028
23029 if (len == 0)
23030 {
23031 /* Don't have a valid filename, so shorten the rest of the
23032 * path if we can. This CAN give us invalid 8.3 filenames, but
23033 * there's not a lot of point in guessing what it might be.
23034 */
23035 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023036 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23037 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 }
23039
23040 /* Count the paths backward to find the beginning of the desired string. */
23041 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023042 {
23043#ifdef FEAT_MBYTE
23044 if (has_mbyte)
23045 p -= mb_head_off(tfname, p);
23046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 if (vim_ispathsep(*p))
23048 {
23049 if (sepcount == 0 || (hasTilde && sepcount == 1))
23050 break;
23051 else
23052 sepcount --;
23053 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 if (hasTilde)
23056 {
23057 --p;
23058 if (p >= tfname)
23059 *p = '~';
23060 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023061 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062 }
23063 else
23064 ++p;
23065
23066 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23067 vim_free(*bufp);
23068 *fnamelen = (int)STRLEN(p);
23069 *bufp = pbuf;
23070 *fnamep = p;
23071
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023072 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073}
23074#endif /* WIN3264 */
23075
23076/*
23077 * Adjust a filename, according to a string of modifiers.
23078 * *fnamep must be NUL terminated when called. When returning, the length is
23079 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023080 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 * When there is an error, *fnamep is set to NULL.
23082 */
23083 int
23084modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23085 char_u *src; /* string with modifiers */
23086 int *usedlen; /* characters after src that are used */
23087 char_u **fnamep; /* file name so far */
23088 char_u **bufp; /* buffer for allocated file name or NULL */
23089 int *fnamelen; /* length of fnamep */
23090{
23091 int valid = 0;
23092 char_u *tail;
23093 char_u *s, *p, *pbuf;
23094 char_u dirname[MAXPATHL];
23095 int c;
23096 int has_fullname = 0;
23097#ifdef WIN3264
23098 int has_shortname = 0;
23099#endif
23100
23101repeat:
23102 /* ":p" - full path/file_name */
23103 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23104 {
23105 has_fullname = 1;
23106
23107 valid |= VALID_PATH;
23108 *usedlen += 2;
23109
23110 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23111 if ((*fnamep)[0] == '~'
23112#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23113 && ((*fnamep)[1] == '/'
23114# ifdef BACKSLASH_IN_FILENAME
23115 || (*fnamep)[1] == '\\'
23116# endif
23117 || (*fnamep)[1] == NUL)
23118
23119#endif
23120 )
23121 {
23122 *fnamep = expand_env_save(*fnamep);
23123 vim_free(*bufp); /* free any allocated file name */
23124 *bufp = *fnamep;
23125 if (*fnamep == NULL)
23126 return -1;
23127 }
23128
23129 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023130 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131 {
23132 if (vim_ispathsep(*p)
23133 && p[1] == '.'
23134 && (p[2] == NUL
23135 || vim_ispathsep(p[2])
23136 || (p[2] == '.'
23137 && (p[3] == NUL || vim_ispathsep(p[3])))))
23138 break;
23139 }
23140
23141 /* FullName_save() is slow, don't use it when not needed. */
23142 if (*p != NUL || !vim_isAbsName(*fnamep))
23143 {
23144 *fnamep = FullName_save(*fnamep, *p != NUL);
23145 vim_free(*bufp); /* free any allocated file name */
23146 *bufp = *fnamep;
23147 if (*fnamep == NULL)
23148 return -1;
23149 }
23150
23151 /* Append a path separator to a directory. */
23152 if (mch_isdir(*fnamep))
23153 {
23154 /* Make room for one or two extra characters. */
23155 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23156 vim_free(*bufp); /* free any allocated file name */
23157 *bufp = *fnamep;
23158 if (*fnamep == NULL)
23159 return -1;
23160 add_pathsep(*fnamep);
23161 }
23162 }
23163
23164 /* ":." - path relative to the current directory */
23165 /* ":~" - path relative to the home directory */
23166 /* ":8" - shortname path - postponed till after */
23167 while (src[*usedlen] == ':'
23168 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23169 {
23170 *usedlen += 2;
23171 if (c == '8')
23172 {
23173#ifdef WIN3264
23174 has_shortname = 1; /* Postpone this. */
23175#endif
23176 continue;
23177 }
23178 pbuf = NULL;
23179 /* Need full path first (use expand_env() to remove a "~/") */
23180 if (!has_fullname)
23181 {
23182 if (c == '.' && **fnamep == '~')
23183 p = pbuf = expand_env_save(*fnamep);
23184 else
23185 p = pbuf = FullName_save(*fnamep, FALSE);
23186 }
23187 else
23188 p = *fnamep;
23189
23190 has_fullname = 0;
23191
23192 if (p != NULL)
23193 {
23194 if (c == '.')
23195 {
23196 mch_dirname(dirname, MAXPATHL);
23197 s = shorten_fname(p, dirname);
23198 if (s != NULL)
23199 {
23200 *fnamep = s;
23201 if (pbuf != NULL)
23202 {
23203 vim_free(*bufp); /* free any allocated file name */
23204 *bufp = pbuf;
23205 pbuf = NULL;
23206 }
23207 }
23208 }
23209 else
23210 {
23211 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23212 /* Only replace it when it starts with '~' */
23213 if (*dirname == '~')
23214 {
23215 s = vim_strsave(dirname);
23216 if (s != NULL)
23217 {
23218 *fnamep = s;
23219 vim_free(*bufp);
23220 *bufp = s;
23221 }
23222 }
23223 }
23224 vim_free(pbuf);
23225 }
23226 }
23227
23228 tail = gettail(*fnamep);
23229 *fnamelen = (int)STRLEN(*fnamep);
23230
23231 /* ":h" - head, remove "/file_name", can be repeated */
23232 /* Don't remove the first "/" or "c:\" */
23233 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23234 {
23235 valid |= VALID_HEAD;
23236 *usedlen += 2;
23237 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023238 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023239 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 *fnamelen = (int)(tail - *fnamep);
23241#ifdef VMS
23242 if (*fnamelen > 0)
23243 *fnamelen += 1; /* the path separator is part of the path */
23244#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023245 if (*fnamelen == 0)
23246 {
23247 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23248 p = vim_strsave((char_u *)".");
23249 if (p == NULL)
23250 return -1;
23251 vim_free(*bufp);
23252 *bufp = *fnamep = tail = p;
23253 *fnamelen = 1;
23254 }
23255 else
23256 {
23257 while (tail > s && !after_pathsep(s, tail))
23258 mb_ptr_back(*fnamep, tail);
23259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 }
23261
23262 /* ":8" - shortname */
23263 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23264 {
23265 *usedlen += 2;
23266#ifdef WIN3264
23267 has_shortname = 1;
23268#endif
23269 }
23270
23271#ifdef WIN3264
23272 /* Check shortname after we have done 'heads' and before we do 'tails'
23273 */
23274 if (has_shortname)
23275 {
23276 pbuf = NULL;
23277 /* Copy the string if it is shortened by :h */
23278 if (*fnamelen < (int)STRLEN(*fnamep))
23279 {
23280 p = vim_strnsave(*fnamep, *fnamelen);
23281 if (p == 0)
23282 return -1;
23283 vim_free(*bufp);
23284 *bufp = *fnamep = p;
23285 }
23286
23287 /* Split into two implementations - makes it easier. First is where
23288 * there isn't a full name already, second is where there is.
23289 */
23290 if (!has_fullname && !vim_isAbsName(*fnamep))
23291 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023292 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 return -1;
23294 }
23295 else
23296 {
23297 int l;
23298
23299 /* Simple case, already have the full-name
23300 * Nearly always shorter, so try first time. */
23301 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023302 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023303 return -1;
23304
23305 if (l == 0)
23306 {
23307 /* Couldn't find the filename.. search the paths.
23308 */
23309 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023310 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 return -1;
23312 }
23313 *fnamelen = l;
23314 }
23315 }
23316#endif /* WIN3264 */
23317
23318 /* ":t" - tail, just the basename */
23319 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23320 {
23321 *usedlen += 2;
23322 *fnamelen -= (int)(tail - *fnamep);
23323 *fnamep = tail;
23324 }
23325
23326 /* ":e" - extension, can be repeated */
23327 /* ":r" - root, without extension, can be repeated */
23328 while (src[*usedlen] == ':'
23329 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23330 {
23331 /* find a '.' in the tail:
23332 * - for second :e: before the current fname
23333 * - otherwise: The last '.'
23334 */
23335 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23336 s = *fnamep - 2;
23337 else
23338 s = *fnamep + *fnamelen - 1;
23339 for ( ; s > tail; --s)
23340 if (s[0] == '.')
23341 break;
23342 if (src[*usedlen + 1] == 'e') /* :e */
23343 {
23344 if (s > tail)
23345 {
23346 *fnamelen += (int)(*fnamep - (s + 1));
23347 *fnamep = s + 1;
23348#ifdef VMS
23349 /* cut version from the extension */
23350 s = *fnamep + *fnamelen - 1;
23351 for ( ; s > *fnamep; --s)
23352 if (s[0] == ';')
23353 break;
23354 if (s > *fnamep)
23355 *fnamelen = s - *fnamep;
23356#endif
23357 }
23358 else if (*fnamep <= tail)
23359 *fnamelen = 0;
23360 }
23361 else /* :r */
23362 {
23363 if (s > tail) /* remove one extension */
23364 *fnamelen = (int)(s - *fnamep);
23365 }
23366 *usedlen += 2;
23367 }
23368
23369 /* ":s?pat?foo?" - substitute */
23370 /* ":gs?pat?foo?" - global substitute */
23371 if (src[*usedlen] == ':'
23372 && (src[*usedlen + 1] == 's'
23373 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23374 {
23375 char_u *str;
23376 char_u *pat;
23377 char_u *sub;
23378 int sep;
23379 char_u *flags;
23380 int didit = FALSE;
23381
23382 flags = (char_u *)"";
23383 s = src + *usedlen + 2;
23384 if (src[*usedlen + 1] == 'g')
23385 {
23386 flags = (char_u *)"g";
23387 ++s;
23388 }
23389
23390 sep = *s++;
23391 if (sep)
23392 {
23393 /* find end of pattern */
23394 p = vim_strchr(s, sep);
23395 if (p != NULL)
23396 {
23397 pat = vim_strnsave(s, (int)(p - s));
23398 if (pat != NULL)
23399 {
23400 s = p + 1;
23401 /* find end of substitution */
23402 p = vim_strchr(s, sep);
23403 if (p != NULL)
23404 {
23405 sub = vim_strnsave(s, (int)(p - s));
23406 str = vim_strnsave(*fnamep, *fnamelen);
23407 if (sub != NULL && str != NULL)
23408 {
23409 *usedlen = (int)(p + 1 - src);
23410 s = do_string_sub(str, pat, sub, flags);
23411 if (s != NULL)
23412 {
23413 *fnamep = s;
23414 *fnamelen = (int)STRLEN(s);
23415 vim_free(*bufp);
23416 *bufp = s;
23417 didit = TRUE;
23418 }
23419 }
23420 vim_free(sub);
23421 vim_free(str);
23422 }
23423 vim_free(pat);
23424 }
23425 }
23426 /* after using ":s", repeat all the modifiers */
23427 if (didit)
23428 goto repeat;
23429 }
23430 }
23431
23432 return valid;
23433}
23434
23435/*
23436 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23437 * "flags" can be "g" to do a global substitute.
23438 * Returns an allocated string, NULL for error.
23439 */
23440 char_u *
23441do_string_sub(str, pat, sub, flags)
23442 char_u *str;
23443 char_u *pat;
23444 char_u *sub;
23445 char_u *flags;
23446{
23447 int sublen;
23448 regmatch_T regmatch;
23449 int i;
23450 int do_all;
23451 char_u *tail;
23452 garray_T ga;
23453 char_u *ret;
23454 char_u *save_cpo;
23455
23456 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23457 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023458 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023459
23460 ga_init2(&ga, 1, 200);
23461
23462 do_all = (flags[0] == 'g');
23463
23464 regmatch.rm_ic = p_ic;
23465 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23466 if (regmatch.regprog != NULL)
23467 {
23468 tail = str;
23469 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23470 {
23471 /*
23472 * Get some space for a temporary buffer to do the substitution
23473 * into. It will contain:
23474 * - The text up to where the match is.
23475 * - The substituted text.
23476 * - The text after the match.
23477 */
23478 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23479 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23480 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23481 {
23482 ga_clear(&ga);
23483 break;
23484 }
23485
23486 /* copy the text up to where the match is */
23487 i = (int)(regmatch.startp[0] - tail);
23488 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23489 /* add the substituted text */
23490 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23491 + ga.ga_len + i, TRUE, TRUE, FALSE);
23492 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493 /* avoid getting stuck on a match with an empty string */
23494 if (tail == regmatch.endp[0])
23495 {
23496 if (*tail == NUL)
23497 break;
23498 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23499 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 }
23501 else
23502 {
23503 tail = regmatch.endp[0];
23504 if (*tail == NUL)
23505 break;
23506 }
23507 if (!do_all)
23508 break;
23509 }
23510
23511 if (ga.ga_data != NULL)
23512 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23513
23514 vim_free(regmatch.regprog);
23515 }
23516
23517 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23518 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023519 if (p_cpo == empty_option)
23520 p_cpo = save_cpo;
23521 else
23522 /* Darn, evaluating {sub} expression changed the value. */
23523 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524
23525 return ret;
23526}
23527
23528#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */