blob: d9dc3f10314bace6c14cf2da8c5ed9c068c135b6 [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 Moolenaar4228bec2011-03-27 16:03:15 +0200792static int var_check_func_name __ARGS((char_u *name, int new_var));
793static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000794static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000795static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
797static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
798static int eval_fname_script __ARGS((char_u *p));
799static int eval_fname_sid __ARGS((char_u *p));
800static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static ufunc_T *find_func __ARGS((char_u *name));
802static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000803static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804#ifdef FEAT_PROFILE
805static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000806static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
807static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
808static int
809# ifdef __BORLANDC__
810 _RTLENTRYF
811# endif
812 prof_total_cmp __ARGS((const void *s1, const void *s2));
813static int
814# ifdef __BORLANDC__
815 _RTLENTRYF
816# endif
817 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000819static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000820static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000821static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void func_free __ARGS((ufunc_T *fp));
823static void func_unref __ARGS((char_u *name));
824static void func_ref __ARGS((char_u *name));
825static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000826static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
827static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
830static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000831static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000832static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200835
836#ifdef EBCDIC
837static int compare_func_name __ARGS((const void *s1, const void *s2));
838static void sortFunctions __ARGS(());
839#endif
840
841
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000842/* Character used as separated in autoload function/variable names. */
843#define AUTOLOAD_CHAR '#'
844
Bram Moolenaar33570922005-01-25 22:26:29 +0000845/*
846 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000847 */
848 void
849eval_init()
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 int i;
852 struct vimvar *p;
853
854 init_var_dict(&globvardict, &globvars_var);
855 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000856 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000857 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000858
859 for (i = 0; i < VV_LEN; ++i)
860 {
861 p = &vimvars[i];
862 STRCPY(p->vv_di.di_key, p->vv_name);
863 if (p->vv_flags & VV_RO)
864 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
865 else if (p->vv_flags & VV_RO_SBX)
866 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
867 else
868 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000869
870 /* add to v: scope dict, unless the value is not always available */
871 if (p->vv_type != VAR_UNKNOWN)
872 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000874 /* add to compat scope dict */
875 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000877 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200878
879#ifdef EBCDIC
880 /*
881 * Sort the function table, to enable binary sort.
882 */
883 sortFunctions();
884#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000885}
886
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000887#if defined(EXITFREE) || defined(PROTO)
888 void
889eval_clear()
890{
891 int i;
892 struct vimvar *p;
893
894 for (i = 0; i < VV_LEN; ++i)
895 {
896 p = &vimvars[i];
897 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000898 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000899 vim_free(p->vv_str);
900 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000901 }
902 else if (p->vv_di.di_tv.v_type == VAR_LIST)
903 {
904 list_unref(p->vv_list);
905 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000906 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000907 }
908 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000909 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000910 hash_clear(&compat_hashtab);
911
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000913
914 /* global variables */
915 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000916
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000917 /* autoloaded script names */
918 ga_clear_strings(&ga_loaded);
919
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200920 /* script-local variables */
921 for (i = 1; i <= ga_scripts.ga_len; ++i)
922 {
923 vars_clear(&SCRIPT_VARS(i));
924 vim_free(SCRIPT_SV(i));
925 }
926 ga_clear(&ga_scripts);
927
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000928 /* unreferenced lists and dicts */
929 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000930
931 /* functions */
932 free_all_functions();
933 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000934}
935#endif
936
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 * Return the name of the executed function.
939 */
940 char_u *
941func_name(cookie)
942 void *cookie;
943{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000944 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945}
946
947/*
948 * Return the address holding the next breakpoint line for a funccall cookie.
949 */
950 linenr_T *
951func_breakpoint(cookie)
952 void *cookie;
953{
Bram Moolenaar33570922005-01-25 22:26:29 +0000954 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955}
956
957/*
958 * Return the address holding the debug tick for a funccall cookie.
959 */
960 int *
961func_dbg_tick(cookie)
962 void *cookie;
963{
Bram Moolenaar33570922005-01-25 22:26:29 +0000964 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965}
966
967/*
968 * Return the nesting level for a funccall cookie.
969 */
970 int
971func_level(cookie)
972 void *cookie;
973{
Bram Moolenaar33570922005-01-25 22:26:29 +0000974 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975}
976
977/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000978funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000980/* pointer to list of previously used funccal, still around because some
981 * item in it is still being used. */
982funccall_T *previous_funccal = NULL;
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984/*
985 * Return TRUE when a function was ended by a ":return" command.
986 */
987 int
988current_func_returned()
989{
990 return current_funccal->returned;
991}
992
993
994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 * Set an internal variable to a string value. Creates the variable if it does
996 * not already exist.
997 */
998 void
999set_internal_string_var(name, value)
1000 char_u *name;
1001 char_u *value;
1002{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001003 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001004 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
1006 val = vim_strsave(value);
1007 if (val != NULL)
1008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001012 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001013 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015 }
1016}
1017
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001019static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020static char_u *redir_endp = NULL;
1021static char_u *redir_varname = NULL;
1022
1023/*
1024 * Start recording command output to a variable
1025 * Returns OK if successfully completed the setup. FAIL otherwise.
1026 */
1027 int
1028var_redir_start(name, append)
1029 char_u *name;
1030 int append; /* append to an existing variable */
1031{
1032 int save_emsg;
1033 int err;
1034 typval_T tv;
1035
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001036 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001037 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038 {
1039 EMSG(_(e_invarg));
1040 return FAIL;
1041 }
1042
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001043 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044 redir_varname = vim_strsave(name);
1045 if (redir_varname == NULL)
1046 return FAIL;
1047
1048 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1049 if (redir_lval == NULL)
1050 {
1051 var_redir_stop();
1052 return FAIL;
1053 }
1054
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001055 /* The output is stored in growarray "redir_ga" until redirection ends. */
1056 ga_init2(&redir_ga, (int)sizeof(char), 500);
1057
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001058 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001059 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1060 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1062 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001063 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 if (redir_endp != NULL && *redir_endp != NUL)
1065 /* Trailing characters are present after the variable name */
1066 EMSG(_(e_trailing));
1067 else
1068 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001069 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 var_redir_stop();
1071 return FAIL;
1072 }
1073
1074 /* check if we can write to the variable: set it to or append an empty
1075 * string */
1076 save_emsg = did_emsg;
1077 did_emsg = FALSE;
1078 tv.v_type = VAR_STRING;
1079 tv.vval.v_string = (char_u *)"";
1080 if (append)
1081 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1082 else
1083 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001084 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001086 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 if (err)
1088 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001089 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 var_redir_stop();
1091 return FAIL;
1092 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093
1094 return OK;
1095}
1096
1097/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001098 * Append "value[value_len]" to the variable set by var_redir_start().
1099 * The actual appending is postponed until redirection ends, because the value
1100 * appended may in fact be the string we write to, changing it may cause freed
1101 * memory to be used:
1102 * :redir => foo
1103 * :let foo
1104 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 */
1106 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001111 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112
1113 if (redir_lval == NULL)
1114 return;
1115
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001121 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122 {
1123 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125 }
1126 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128}
1129
1130/*
1131 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001132 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 */
1134 void
1135var_redir_stop()
1136{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001137 typval_T tv;
1138
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 if (redir_lval != NULL)
1140 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001141 /* If there was no error: assign the text to the variable. */
1142 if (redir_endp != NULL)
1143 {
1144 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1145 tv.v_type = VAR_STRING;
1146 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001147 /* Call get_lval() again, if it's inside a Dict or List it may
1148 * have changed. */
1149 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1150 FALSE, FALSE, FALSE, FNE_CHECK_START);
1151 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1152 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1153 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 /* free the collected output */
1157 vim_free(redir_ga.ga_data);
1158 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001159
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 vim_free(redir_lval);
1161 redir_lval = NULL;
1162 }
1163 vim_free(redir_varname);
1164 redir_varname = NULL;
1165}
1166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167# if defined(FEAT_MBYTE) || defined(PROTO)
1168 int
1169eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1170 char_u *enc_from;
1171 char_u *enc_to;
1172 char_u *fname_from;
1173 char_u *fname_to;
1174{
1175 int err = FALSE;
1176
1177 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1178 set_vim_var_string(VV_CC_TO, enc_to, -1);
1179 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1180 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1181 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1182 err = TRUE;
1183 set_vim_var_string(VV_CC_FROM, NULL, -1);
1184 set_vim_var_string(VV_CC_TO, NULL, -1);
1185 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1186 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1187
1188 if (err)
1189 return FAIL;
1190 return OK;
1191}
1192# endif
1193
1194# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1195 int
1196eval_printexpr(fname, args)
1197 char_u *fname;
1198 char_u *args;
1199{
1200 int err = FALSE;
1201
1202 set_vim_var_string(VV_FNAME_IN, fname, -1);
1203 set_vim_var_string(VV_CMDARG, args, -1);
1204 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1205 err = TRUE;
1206 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1207 set_vim_var_string(VV_CMDARG, NULL, -1);
1208
1209 if (err)
1210 {
1211 mch_remove(fname);
1212 return FAIL;
1213 }
1214 return OK;
1215}
1216# endif
1217
1218# if defined(FEAT_DIFF) || defined(PROTO)
1219 void
1220eval_diff(origfile, newfile, outfile)
1221 char_u *origfile;
1222 char_u *newfile;
1223 char_u *outfile;
1224{
1225 int err = FALSE;
1226
1227 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1228 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1229 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1230 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1231 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1232 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1233 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1234}
1235
1236 void
1237eval_patch(origfile, difffile, outfile)
1238 char_u *origfile;
1239 char_u *difffile;
1240 char_u *outfile;
1241{
1242 int err;
1243
1244 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1245 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1246 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1247 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1248 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1249 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1250 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1251}
1252# endif
1253
1254/*
1255 * Top level evaluation function, returning a boolean.
1256 * Sets "error" to TRUE if there was an error.
1257 * Return TRUE or FALSE.
1258 */
1259 int
1260eval_to_bool(arg, error, nextcmd, skip)
1261 char_u *arg;
1262 int *error;
1263 char_u **nextcmd;
1264 int skip; /* only parse, don't execute */
1265{
Bram Moolenaar33570922005-01-25 22:26:29 +00001266 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 int retval = FALSE;
1268
1269 if (skip)
1270 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001271 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 else
1274 {
1275 *error = FALSE;
1276 if (!skip)
1277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001278 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001279 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
1281 }
1282 if (skip)
1283 --emsg_skip;
1284
1285 return retval;
1286}
1287
1288/*
1289 * Top level evaluation function, returning a string. If "skip" is TRUE,
1290 * only parsing to "nextcmd" is done, without reporting errors. Return
1291 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1292 */
1293 char_u *
1294eval_to_string_skip(arg, nextcmd, skip)
1295 char_u *arg;
1296 char_u **nextcmd;
1297 int skip; /* only parse, don't execute */
1298{
Bram Moolenaar33570922005-01-25 22:26:29 +00001299 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 char_u *retval;
1301
1302 if (skip)
1303 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 retval = NULL;
1306 else
1307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001308 retval = vim_strsave(get_tv_string(&tv));
1309 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 if (skip)
1312 --emsg_skip;
1313
1314 return retval;
1315}
1316
1317/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001318 * Skip over an expression at "*pp".
1319 * Return FAIL for an error, OK otherwise.
1320 */
1321 int
1322skip_expr(pp)
1323 char_u **pp;
1324{
Bram Moolenaar33570922005-01-25 22:26:29 +00001325 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001326
1327 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001328 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329}
1330
1331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001333 * When "convert" is TRUE convert a List into a sequence of lines and convert
1334 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 * Return pointer to allocated memory, or NULL for failure.
1336 */
1337 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 char_u *arg;
1340 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001345 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001346#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001347 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001350 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 retval = NULL;
1352 else
1353 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001354 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001355 {
1356 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001357 if (tv.vval.v_list != NULL)
1358 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001359 ga_append(&ga, NUL);
1360 retval = (char_u *)ga.ga_data;
1361 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001362#ifdef FEAT_FLOAT
1363 else if (convert && tv.v_type == VAR_FLOAT)
1364 {
1365 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1366 retval = vim_strsave(numbuf);
1367 }
1368#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001369 else
1370 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373
1374 return retval;
1375}
1376
1377/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001378 * Call eval_to_string() without using current local variables and using
1379 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 */
1381 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001382eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 char_u *arg;
1384 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001385 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 char_u *retval;
1388 void *save_funccalp;
1389
1390 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 if (use_sandbox)
1392 ++sandbox;
1393 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001394 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001395 if (use_sandbox)
1396 --sandbox;
1397 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 restore_funccal(save_funccalp);
1399 return retval;
1400}
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402/*
1403 * Top level evaluation function, returning a number.
1404 * Evaluates "expr" silently.
1405 * Returns -1 for an error.
1406 */
1407 int
1408eval_to_number(expr)
1409 char_u *expr;
1410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001413 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
1415 ++emsg_off;
1416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 retval = -1;
1419 else
1420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001421 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424 --emsg_off;
1425
1426 return retval;
1427}
1428
Bram Moolenaara40058a2005-07-11 22:42:07 +00001429/*
1430 * Prepare v: variable "idx" to be used.
1431 * Save the current typeval in "save_tv".
1432 * When not used yet add the variable to the v: hashtable.
1433 */
1434 static void
1435prepare_vimvar(idx, save_tv)
1436 int idx;
1437 typval_T *save_tv;
1438{
1439 *save_tv = vimvars[idx].vv_tv;
1440 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1441 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1442}
1443
1444/*
1445 * Restore v: variable "idx" to typeval "save_tv".
1446 * When no longer defined, remove the variable from the v: hashtable.
1447 */
1448 static void
1449restore_vimvar(idx, save_tv)
1450 int idx;
1451 typval_T *save_tv;
1452{
1453 hashitem_T *hi;
1454
Bram Moolenaara40058a2005-07-11 22:42:07 +00001455 vimvars[idx].vv_tv = *save_tv;
1456 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1457 {
1458 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1459 if (HASHITEM_EMPTY(hi))
1460 EMSG2(_(e_intern2), "restore_vimvar()");
1461 else
1462 hash_remove(&vimvarht, hi);
1463 }
1464}
1465
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001466#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001467/*
1468 * Evaluate an expression to a list with suggestions.
1469 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001470 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001471 */
1472 list_T *
1473eval_spell_expr(badword, expr)
1474 char_u *badword;
1475 char_u *expr;
1476{
1477 typval_T save_val;
1478 typval_T rettv;
1479 list_T *list = NULL;
1480 char_u *p = skipwhite(expr);
1481
1482 /* Set "v:val" to the bad word. */
1483 prepare_vimvar(VV_VAL, &save_val);
1484 vimvars[VV_VAL].vv_type = VAR_STRING;
1485 vimvars[VV_VAL].vv_str = badword;
1486 if (p_verbose == 0)
1487 ++emsg_off;
1488
1489 if (eval1(&p, &rettv, TRUE) == OK)
1490 {
1491 if (rettv.v_type != VAR_LIST)
1492 clear_tv(&rettv);
1493 else
1494 list = rettv.vval.v_list;
1495 }
1496
1497 if (p_verbose == 0)
1498 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001499 restore_vimvar(VV_VAL, &save_val);
1500
1501 return list;
1502}
1503
1504/*
1505 * "list" is supposed to contain two items: a word and a number. Return the
1506 * word in "pp" and the number as the return value.
1507 * Return -1 if anything isn't right.
1508 * Used to get the good word and score from the eval_spell_expr() result.
1509 */
1510 int
1511get_spellword(list, pp)
1512 list_T *list;
1513 char_u **pp;
1514{
1515 listitem_T *li;
1516
1517 li = list->lv_first;
1518 if (li == NULL)
1519 return -1;
1520 *pp = get_tv_string(&li->li_tv);
1521
1522 li = li->li_next;
1523 if (li == NULL)
1524 return -1;
1525 return get_tv_number(&li->li_tv);
1526}
1527#endif
1528
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001529/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001530 * Top level evaluation function.
1531 * Returns an allocated typval_T with the result.
1532 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001533 */
1534 typval_T *
1535eval_expr(arg, nextcmd)
1536 char_u *arg;
1537 char_u **nextcmd;
1538{
1539 typval_T *tv;
1540
1541 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001542 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001543 {
1544 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001546 }
1547
1548 return tv;
1549}
1550
1551
Bram Moolenaar4f688582007-07-24 12:34:30 +00001552#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1553 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001556 * Uses argv[argc] for the function arguments. Only Number and String
1557 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001560 static int
1561call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 char_u *func;
1563 int argc;
1564 char_u **argv;
1565 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567{
Bram Moolenaar33570922005-01-25 22:26:29 +00001568 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 long n;
1570 int len;
1571 int i;
1572 int doesrange;
1573 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001576 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 for (i = 0; i < argc; i++)
1581 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001582 /* Pass a NULL or empty argument as an empty string */
1583 if (argv[i] == NULL || *argv[i] == NUL)
1584 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001585 argvars[i].v_type = VAR_STRING;
1586 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001587 continue;
1588 }
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 /* Recognize a number argument, the others must be strings. */
1591 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1592 if (len != 0 && len == (int)STRLEN(argv[i]))
1593 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001594 argvars[i].v_type = VAR_NUMBER;
1595 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 else
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_STRING;
1600 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602 }
1603
1604 if (safe)
1605 {
1606 save_funccalp = save_funccal();
1607 ++sandbox;
1608 }
1609
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1611 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (safe)
1615 {
1616 --sandbox;
1617 restore_funccal(save_funccalp);
1618 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 vim_free(argvars);
1620
1621 if (ret == FAIL)
1622 clear_tv(rettv);
1623
1624 return ret;
1625}
1626
Bram Moolenaar4f688582007-07-24 12:34:30 +00001627# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001628/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001629 * Call vimL function "func" and return the result as a string.
1630 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001631 * Uses argv[argc] for the function arguments.
1632 */
1633 void *
1634call_func_retstr(func, argc, argv, safe)
1635 char_u *func;
1636 int argc;
1637 char_u **argv;
1638 int safe; /* use the sandbox */
1639{
1640 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001641 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642
1643 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1644 return NULL;
1645
1646 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001647 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 return retval;
1649}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001650# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651
Bram Moolenaar4f688582007-07-24 12:34:30 +00001652# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001654 * Call vimL function "func" and return the result as a number.
1655 * Returns -1 when calling the function fails.
1656 * Uses argv[argc] for the function arguments.
1657 */
1658 long
1659call_func_retnr(func, argc, argv, safe)
1660 char_u *func;
1661 int argc;
1662 char_u **argv;
1663 int safe; /* use the sandbox */
1664{
1665 typval_T rettv;
1666 long retval;
1667
1668 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1669 return -1;
1670
1671 retval = get_tv_number_chk(&rettv, NULL);
1672 clear_tv(&rettv);
1673 return retval;
1674}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001675# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001676
1677/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001678 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001680 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 */
1682 void *
1683call_func_retlist(func, argc, argv, safe)
1684 char_u *func;
1685 int argc;
1686 char_u **argv;
1687 int safe; /* use the sandbox */
1688{
1689 typval_T rettv;
1690
1691 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1692 return NULL;
1693
1694 if (rettv.v_type != VAR_LIST)
1695 {
1696 clear_tv(&rettv);
1697 return NULL;
1698 }
1699
1700 return rettv.vval.v_list;
1701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#endif
1703
Bram Moolenaar4f688582007-07-24 12:34:30 +00001704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705/*
1706 * Save the current function call pointer, and set it to NULL.
1707 * Used when executing autocommands and for ":source".
1708 */
1709 void *
1710save_funccal()
1711{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001712 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 current_funccal = NULL;
1715 return (void *)fc;
1716}
1717
1718 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001719restore_funccal(vfc)
1720 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001722 funccall_T *fc = (funccall_T *)vfc;
1723
1724 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725}
1726
Bram Moolenaar05159a02005-02-26 23:04:13 +00001727#if defined(FEAT_PROFILE) || defined(PROTO)
1728/*
1729 * Prepare profiling for entering a child or something else that is not
1730 * counted for the script/function itself.
1731 * Should always be called in pair with prof_child_exit().
1732 */
1733 void
1734prof_child_enter(tm)
1735 proftime_T *tm; /* place to store waittime */
1736{
1737 funccall_T *fc = current_funccal;
1738
1739 if (fc != NULL && fc->func->uf_profiling)
1740 profile_start(&fc->prof_child);
1741 script_prof_save(tm);
1742}
1743
1744/*
1745 * Take care of time spent in a child.
1746 * Should always be called after prof_child_enter().
1747 */
1748 void
1749prof_child_exit(tm)
1750 proftime_T *tm; /* where waittime was stored */
1751{
1752 funccall_T *fc = current_funccal;
1753
1754 if (fc != NULL && fc->func->uf_profiling)
1755 {
1756 profile_end(&fc->prof_child);
1757 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1758 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1759 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1760 }
1761 script_prof_restore(tm);
1762}
1763#endif
1764
1765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#ifdef FEAT_FOLDING
1767/*
1768 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1769 * it in "*cp". Doesn't give error messages.
1770 */
1771 int
1772eval_foldexpr(arg, cp)
1773 char_u *arg;
1774 int *cp;
1775{
Bram Moolenaar33570922005-01-25 22:26:29 +00001776 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 int retval;
1778 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001779 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1780 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001783 if (use_sandbox)
1784 ++sandbox;
1785 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 retval = 0;
1789 else
1790 {
1791 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 if (tv.v_type == VAR_NUMBER)
1793 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001794 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 retval = 0;
1796 else
1797 {
1798 /* If the result is a string, check if there is a non-digit before
1799 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (!VIM_ISDIGIT(*s) && *s != '-')
1802 *cp = *s++;
1803 retval = atol((char *)s);
1804 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 }
1807 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001808 if (use_sandbox)
1809 --sandbox;
1810 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
1812 return retval;
1813}
1814#endif
1815
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 * ":let" list all variable values
1818 * ":let var1 var2" list variable values
1819 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001820 * ":let var += expr" assignment command.
1821 * ":let var -= expr" assignment command.
1822 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 */
1825 void
1826ex_let(eap)
1827 exarg_T *eap;
1828{
1829 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001831 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 int var_count = 0;
1834 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001835 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001836 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001837 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
Bram Moolenaardb552d602006-03-23 22:59:57 +00001839 argend = skip_var_list(arg, &var_count, &semicolon);
1840 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001842 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1843 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001844 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001847 /*
1848 * ":let" without "=": list variables
1849 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 if (*arg == '[')
1851 EMSG(_(e_invarg));
1852 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001854 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001858 list_glob_vars(&first);
1859 list_buf_vars(&first);
1860 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001861#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001862 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001863#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_script_vars(&first);
1865 list_func_vars(&first);
1866 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 eap->nextcmd = check_nextcmd(arg);
1869 }
1870 else
1871 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001872 op[0] = '=';
1873 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001874 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001875 {
1876 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1877 op[0] = expr[-1]; /* +=, -= or .= */
1878 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (eap->skip)
1882 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (eap->skip)
1885 {
1886 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 --emsg_skip;
1889 }
1890 else if (i != FAIL)
1891 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896 }
1897}
1898
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899/*
1900 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1901 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001902 * When "nextchars" is not NULL it points to a string with characters that
1903 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1904 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 * Returns OK or FAIL;
1906 */
1907 static int
1908ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1909 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001910 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 int copy; /* copy values from "tv", don't move */
1912 int semicolon; /* from skip_var_list() */
1913 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915{
1916 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 listitem_T *item;
1920 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921
1922 if (*arg != '[')
1923 {
1924 /*
1925 * ":let var = expr" or ":for var in list"
1926 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 return FAIL;
1929 return OK;
1930 }
1931
1932 /*
1933 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1934 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001935 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 {
1937 EMSG(_(e_listreq));
1938 return FAIL;
1939 }
1940
1941 i = list_len(l);
1942 if (semicolon == 0 && var_count < i)
1943 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001944 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 return FAIL;
1946 }
1947 if (var_count - semicolon > i)
1948 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001949 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 return FAIL;
1951 }
1952
1953 item = l->lv_first;
1954 while (*arg != ']')
1955 {
1956 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 item = item->li_next;
1959 if (arg == NULL)
1960 return FAIL;
1961
1962 arg = skipwhite(arg);
1963 if (*arg == ';')
1964 {
1965 /* Put the rest of the list (may be empty) in the var after ';'.
1966 * Create a new list for this. */
1967 l = list_alloc();
1968 if (l == NULL)
1969 return FAIL;
1970 while (item != NULL)
1971 {
1972 list_append_tv(l, &item->li_tv);
1973 item = item->li_next;
1974 }
1975
1976 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001977 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 ltv.vval.v_list = l;
1979 l->lv_refcount = 1;
1980
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1982 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 clear_tv(&ltv);
1984 if (arg == NULL)
1985 return FAIL;
1986 break;
1987 }
1988 else if (*arg != ',' && *arg != ']')
1989 {
1990 EMSG2(_(e_intern2), "ex_let_vars()");
1991 return FAIL;
1992 }
1993 }
1994
1995 return OK;
1996}
1997
1998/*
1999 * Skip over assignable variable "var" or list of variables "[var, var]".
2000 * Used for ":let varvar = expr" and ":for varvar in expr".
2001 * For "[var, var]" increment "*var_count" for each variable.
2002 * for "[var, var; var]" set "semicolon".
2003 * Return NULL for an error.
2004 */
2005 static char_u *
2006skip_var_list(arg, var_count, semicolon)
2007 char_u *arg;
2008 int *var_count;
2009 int *semicolon;
2010{
2011 char_u *p, *s;
2012
2013 if (*arg == '[')
2014 {
2015 /* "[var, var]": find the matching ']'. */
2016 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002017 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018 {
2019 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2020 s = skip_var_one(p);
2021 if (s == p)
2022 {
2023 EMSG2(_(e_invarg2), p);
2024 return NULL;
2025 }
2026 ++*var_count;
2027
2028 p = skipwhite(s);
2029 if (*p == ']')
2030 break;
2031 else if (*p == ';')
2032 {
2033 if (*semicolon == 1)
2034 {
2035 EMSG(_("Double ; in list of variables"));
2036 return NULL;
2037 }
2038 *semicolon = 1;
2039 }
2040 else if (*p != ',')
2041 {
2042 EMSG2(_(e_invarg2), p);
2043 return NULL;
2044 }
2045 }
2046 return p + 1;
2047 }
2048 else
2049 return skip_var_one(arg);
2050}
2051
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002052/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002053 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002054 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002055 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056 static char_u *
2057skip_var_one(arg)
2058 char_u *arg;
2059{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002060 if (*arg == '@' && arg[1] != NUL)
2061 return arg + 2;
2062 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2063 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064}
2065
Bram Moolenaara7043832005-01-21 11:56:39 +00002066/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002067 * List variables for hashtab "ht" with prefix "prefix".
2068 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002071list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002072 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002075 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002076{
Bram Moolenaar33570922005-01-25 22:26:29 +00002077 hashitem_T *hi;
2078 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 int todo;
2080
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002081 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002082 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2083 {
2084 if (!HASHITEM_EMPTY(hi))
2085 {
2086 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 di = HI2DI(hi);
2088 if (empty || di->di_tv.v_type != VAR_STRING
2089 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002090 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002091 }
2092 }
2093}
2094
2095/*
2096 * List global variables.
2097 */
2098 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002099list_glob_vars(first)
2100 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002101{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002102 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002103}
2104
2105/*
2106 * List buffer variables.
2107 */
2108 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109list_buf_vars(first)
2110 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002111{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002112 char_u numbuf[NUMBUFLEN];
2113
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2115 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002116
2117 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2119 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002120}
2121
2122/*
2123 * List window variables.
2124 */
2125 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126list_win_vars(first)
2127 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002128{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2130 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002131}
2132
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002133#ifdef FEAT_WINDOWS
2134/*
2135 * List tab page variables.
2136 */
2137 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138list_tab_vars(first)
2139 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002140{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2142 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002143}
2144#endif
2145
Bram Moolenaara7043832005-01-21 11:56:39 +00002146/*
2147 * List Vim variables.
2148 */
2149 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150list_vim_vars(first)
2151 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154}
2155
2156/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002157 * List script-local variables, if there is a script.
2158 */
2159 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160list_script_vars(first)
2161 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002162{
2163 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2165 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002166}
2167
2168/*
2169 * List function variables, if there is a function.
2170 */
2171 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172list_func_vars(first)
2173 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002174{
2175 if (current_funccal != NULL)
2176 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002178}
2179
2180/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 * List variables in "arg".
2182 */
2183 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 exarg_T *eap;
2186 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188{
2189 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 char_u *name_start;
2193 char_u *arg_subsc;
2194 char_u *tofree;
2195 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196
2197 while (!ends_excmd(*arg) && !got_int)
2198 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002199 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002201 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002202 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2203 {
2204 emsg_severe = TRUE;
2205 EMSG(_(e_trailing));
2206 break;
2207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 /* get_name_len() takes care of expanding curly braces */
2212 name_start = name = arg;
2213 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2214 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 /* This is mainly to keep test 49 working: when expanding
2217 * curly braces fails overrule the exception error message. */
2218 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 emsg_severe = TRUE;
2221 EMSG2(_(e_invarg2), arg);
2222 break;
2223 }
2224 error = TRUE;
2225 }
2226 else
2227 {
2228 if (tofree != NULL)
2229 name = tofree;
2230 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 else
2233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 /* handle d.key, l[idx], f(expr) */
2235 arg_subsc = arg;
2236 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002237 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002244 case 'g': list_glob_vars(first); break;
2245 case 'b': list_buf_vars(first); break;
2246 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002247#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002248 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 'v': list_vim_vars(first); break;
2251 case 's': list_script_vars(first); break;
2252 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 default:
2254 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002255 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002256 }
2257 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 {
2259 char_u numbuf[NUMBUFLEN];
2260 char_u *tf;
2261 int c;
2262 char_u *s;
2263
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002264 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 c = *arg;
2266 *arg = NUL;
2267 list_one_var_a((char_u *)"",
2268 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002269 tv.v_type,
2270 s == NULL ? (char_u *)"" : s,
2271 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 *arg = c;
2273 vim_free(tf);
2274 }
2275 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 }
2278 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279
2280 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282
2283 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285
2286 return arg;
2287}
2288
2289/*
2290 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2291 * Returns a pointer to the char just after the var name.
2292 * Returns NULL if there is an error.
2293 */
2294 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002297 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002299 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301{
2302 int c1;
2303 char_u *name;
2304 char_u *p;
2305 char_u *arg_end = NULL;
2306 int len;
2307 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309
2310 /*
2311 * ":let $VAR = expr": Set environment variable.
2312 */
2313 if (*arg == '$')
2314 {
2315 /* Find the end of the name. */
2316 ++arg;
2317 name = arg;
2318 len = get_env_len(&arg);
2319 if (len == 0)
2320 EMSG2(_(e_invarg2), name - 1);
2321 else
2322 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 if (op != NULL && (*op == '+' || *op == '-'))
2324 EMSG2(_(e_letwrong), op);
2325 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002326 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002328 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 {
2330 c1 = name[len];
2331 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002332 p = get_tv_string_chk(tv);
2333 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002334 {
2335 int mustfree = FALSE;
2336 char_u *s = vim_getenv(name, &mustfree);
2337
2338 if (s != NULL)
2339 {
2340 p = tofree = concat_str(s, p);
2341 if (mustfree)
2342 vim_free(s);
2343 }
2344 }
2345 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 if (STRICMP(name, "HOME") == 0)
2349 init_homedir();
2350 else if (didset_vim && STRICMP(name, "VIM") == 0)
2351 didset_vim = FALSE;
2352 else if (didset_vimruntime
2353 && STRICMP(name, "VIMRUNTIME") == 0)
2354 didset_vimruntime = FALSE;
2355 arg_end = arg;
2356 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 }
2360 }
2361 }
2362
2363 /*
2364 * ":let &option = expr": Set option value.
2365 * ":let &l:option = expr": Set local option value.
2366 * ":let &g:option = expr": Set global option value.
2367 */
2368 else if (*arg == '&')
2369 {
2370 /* Find the end of the name. */
2371 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002372 if (p == NULL || (endchars != NULL
2373 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 EMSG(_(e_letunexp));
2375 else
2376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 long n;
2378 int opt_type;
2379 long numval;
2380 char_u *stringval = NULL;
2381 char_u *s;
2382
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 c1 = *p;
2384 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385
2386 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002387 s = get_tv_string_chk(tv); /* != NULL if number or string */
2388 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 {
2390 opt_type = get_option_value(arg, &numval,
2391 &stringval, opt_flags);
2392 if ((opt_type == 1 && *op == '.')
2393 || (opt_type == 0 && *op != '.'))
2394 EMSG2(_(e_letwrong), op);
2395 else
2396 {
2397 if (opt_type == 1) /* number */
2398 {
2399 if (*op == '+')
2400 n = numval + n;
2401 else
2402 n = numval - n;
2403 }
2404 else if (opt_type == 0 && stringval != NULL) /* string */
2405 {
2406 s = concat_str(stringval, s);
2407 vim_free(stringval);
2408 stringval = s;
2409 }
2410 }
2411 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002412 if (s != NULL)
2413 {
2414 set_option_value(arg, n, s, opt_flags);
2415 arg_end = p;
2416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 }
2420 }
2421
2422 /*
2423 * ":let @r = expr": Set register contents.
2424 */
2425 else if (*arg == '@')
2426 {
2427 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 if (op != NULL && (*op == '+' || *op == '-'))
2429 EMSG2(_(e_letwrong), op);
2430 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002431 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 EMSG(_(e_letunexp));
2433 else
2434 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002435 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 char_u *s;
2437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002438 p = get_tv_string_chk(tv);
2439 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002441 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 if (s != NULL)
2443 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002444 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445 vim_free(s);
2446 }
2447 }
2448 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002449 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 arg_end = arg + 1;
2452 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002453 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 }
2455 }
2456
2457 /*
2458 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002461 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002463 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002465 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2469 EMSG(_(e_letunexp));
2470 else
2471 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 arg_end = p;
2474 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
2478
2479 else
2480 EMSG2(_(e_invarg2), arg);
2481
2482 return arg_end;
2483}
2484
2485/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2487 */
2488 static int
2489check_changedtick(arg)
2490 char_u *arg;
2491{
2492 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2493 {
2494 EMSG2(_(e_readonlyvar), arg);
2495 return TRUE;
2496 }
2497 return FALSE;
2498}
2499
2500/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 * Get an lval: variable, Dict item or List item that can be assigned a value
2502 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2503 * "name.key", "name.key[expr]" etc.
2504 * Indexing only works if "name" is an existing List or Dictionary.
2505 * "name" points to the start of the name.
2506 * If "rettv" is not NULL it points to the value to be assigned.
2507 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2508 * wrong; must end in space or cmd separator.
2509 *
2510 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002511 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 */
2514 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002515get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002517 typval_T *rettv;
2518 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 int unlet;
2520 int skip;
2521 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002522 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 char_u *expr_start, *expr_end;
2526 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 dictitem_T *v;
2528 typval_T var1;
2529 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002531 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538
2539 if (skip)
2540 {
2541 /* When skipping just find the end of the name. */
2542 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002543 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 }
2545
2546 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002547 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 if (expr_start != NULL)
2549 {
2550 /* Don't expand the name when we already know there is an error. */
2551 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2552 && *p != '[' && *p != '.')
2553 {
2554 EMSG(_(e_trailing));
2555 return NULL;
2556 }
2557
2558 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2559 if (lp->ll_exp_name == NULL)
2560 {
2561 /* Report an invalid expression in braces, unless the
2562 * expression evaluation has been cancelled due to an
2563 * aborting error, an interrupt, or an exception. */
2564 if (!aborting() && !quiet)
2565 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002566 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 EMSG2(_(e_invarg2), name);
2568 return NULL;
2569 }
2570 }
2571 lp->ll_name = lp->ll_exp_name;
2572 }
2573 else
2574 lp->ll_name = name;
2575
2576 /* Without [idx] or .key we are done. */
2577 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2578 return p;
2579
2580 cc = *p;
2581 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002582 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (v == NULL && !quiet)
2584 EMSG2(_(e_undefvar), lp->ll_name);
2585 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586 if (v == NULL)
2587 return NULL;
2588
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 /*
2590 * Loop until no more [idx] or .key is following.
2591 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2596 && !(lp->ll_tv->v_type == VAR_DICT
2597 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (!quiet)
2600 EMSG(_("E689: Can only index a List or Dictionary"));
2601 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!quiet)
2606 EMSG(_("E708: [:] must come last"));
2607 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002609
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 len = -1;
2611 if (*p == '.')
2612 {
2613 key = p + 1;
2614 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2615 ;
2616 if (len == 0)
2617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (!quiet)
2619 EMSG(_(e_emptykey));
2620 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 }
2622 p = key + len;
2623 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002624 else
2625 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002627 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 if (*p == ':')
2629 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 else
2631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 empty1 = FALSE;
2633 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002635 if (get_tv_string_chk(&var1) == NULL)
2636 {
2637 /* not a number or string */
2638 clear_tv(&var1);
2639 return NULL;
2640 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 }
2642
2643 /* Optionally get the second index [ :expr]. */
2644 if (*p == ':')
2645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002649 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 if (!empty1)
2651 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002653 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (rettv != NULL && (rettv->v_type != VAR_LIST
2655 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (!quiet)
2658 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 if (!empty1)
2660 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 }
2663 p = skipwhite(p + 1);
2664 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 else
2667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2670 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (!empty1)
2672 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002675 if (get_tv_string_chk(&var2) == NULL)
2676 {
2677 /* not a number or string */
2678 if (!empty1)
2679 clear_tv(&var1);
2680 clear_tv(&var2);
2681 return NULL;
2682 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
2692 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 }
2699
2700 /* Skip to past ']'. */
2701 ++p;
2702 }
2703
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 {
2706 if (len == -1)
2707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002709 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 if (*key == NUL)
2711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (!quiet)
2713 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 }
2717 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 lp->ll_list = NULL;
2719 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002720 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002721
2722 /* When assigning to g: check that a function and variable name is
2723 * valid. */
2724 if (rettv != NULL && lp->ll_dict == &globvardict)
2725 {
2726 if (rettv->v_type == VAR_FUNC
2727 && var_check_func_name(key, lp->ll_di == NULL))
2728 return NULL;
2729 if (!valid_varname(key))
2730 return NULL;
2731 }
2732
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002735 /* Can't add "v:" variable. */
2736 if (lp->ll_dict == &vimvardict)
2737 {
2738 EMSG2(_(e_illvar), name);
2739 return NULL;
2740 }
2741
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002742 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 if (len == -1)
2748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
2751 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 if (len == -1)
2756 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 p = NULL;
2759 break;
2760 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002761 /* existing variable, need to check if it can be changed */
2762 else if (var_check_ro(lp->ll_di->di_flags, name))
2763 return NULL;
2764
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (len == -1)
2766 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 }
2769 else
2770 {
2771 /*
2772 * Get the number and item for the only or first index of the List.
2773 */
2774 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 else
2777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002778 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 clear_tv(&var1);
2780 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002781 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 lp->ll_list = lp->ll_tv->vval.v_list;
2783 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2784 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002786 if (lp->ll_n1 < 0)
2787 {
2788 lp->ll_n1 = 0;
2789 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2790 }
2791 }
2792 if (lp->ll_li == NULL)
2793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 }
2798
2799 /*
2800 * May need to find the item or absolute index for the second
2801 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 * When no index given: "lp->ll_empty2" is TRUE.
2803 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002807 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 }
2816
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2818 if (lp->ll_n1 < 0)
2819 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2820 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002822 }
2823
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002825 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002826 }
2827
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 return p;
2829}
2830
2831/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002832 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 */
2834 static void
2835clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002836 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837{
2838 vim_free(lp->ll_exp_name);
2839 vim_free(lp->ll_newkey);
2840}
2841
2842/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002843 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002845 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 */
2847 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002848set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002849 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002851 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002853 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854{
2855 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002856 listitem_T *ri;
2857 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858
2859 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002860 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002862 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 cc = *endp;
2864 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002865 if (op != NULL && *op != '=')
2866 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002868
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002869 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002870 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002871 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002872 {
2873 if (tv_op(&tv, rettv, op) == OK)
2874 set_var(lp->ll_name, &tv, FALSE);
2875 clear_tv(&tv);
2876 }
2877 }
2878 else
2879 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002881 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002883 else if (tv_check_lock(lp->ll_newkey == NULL
2884 ? lp->ll_tv->v_lock
2885 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2886 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 else if (lp->ll_range)
2888 {
2889 /*
2890 * Assign the List values to the list items.
2891 */
2892 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002893 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894 if (op != NULL && *op != '=')
2895 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2896 else
2897 {
2898 clear_tv(&lp->ll_li->li_tv);
2899 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2900 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 ri = ri->li_next;
2902 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2903 break;
2904 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002907 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002908 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 ri = NULL;
2910 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002911 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 lp->ll_li = lp->ll_li->li_next;
2914 ++lp->ll_n1;
2915 }
2916 if (ri != NULL)
2917 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002918 else if (lp->ll_empty2
2919 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 : lp->ll_n1 != lp->ll_n2)
2921 EMSG(_("E711: List value has not enough items"));
2922 }
2923 else
2924 {
2925 /*
2926 * Assign to a List or Dictionary item.
2927 */
2928 if (lp->ll_newkey != NULL)
2929 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 if (op != NULL && *op != '=')
2931 {
2932 EMSG2(_(e_letwrong), op);
2933 return;
2934 }
2935
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002937 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 if (di == NULL)
2939 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002940 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2941 {
2942 vim_free(di);
2943 return;
2944 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002946 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 else if (op != NULL && *op != '=')
2948 {
2949 tv_op(lp->ll_tv, rettv, op);
2950 return;
2951 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002952 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002954
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 /*
2956 * Assign the value to the variable or list item.
2957 */
2958 if (copy)
2959 copy_tv(rettv, lp->ll_tv);
2960 else
2961 {
2962 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002963 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002965 }
2966 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002967}
2968
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002969/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2971 * Returns OK or FAIL.
2972 */
2973 static int
2974tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002975 typval_T *tv1;
2976 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002977 char_u *op;
2978{
2979 long n;
2980 char_u numbuf[NUMBUFLEN];
2981 char_u *s;
2982
2983 /* Can't do anything with a Funcref or a Dict on the right. */
2984 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2985 {
2986 switch (tv1->v_type)
2987 {
2988 case VAR_DICT:
2989 case VAR_FUNC:
2990 break;
2991
2992 case VAR_LIST:
2993 if (*op != '+' || tv2->v_type != VAR_LIST)
2994 break;
2995 /* List += List */
2996 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2997 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2998 return OK;
2999
3000 case VAR_NUMBER:
3001 case VAR_STRING:
3002 if (tv2->v_type == VAR_LIST)
3003 break;
3004 if (*op == '+' || *op == '-')
3005 {
3006 /* nr += nr or nr -= nr*/
3007 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003008#ifdef FEAT_FLOAT
3009 if (tv2->v_type == VAR_FLOAT)
3010 {
3011 float_T f = n;
3012
3013 if (*op == '+')
3014 f += tv2->vval.v_float;
3015 else
3016 f -= tv2->vval.v_float;
3017 clear_tv(tv1);
3018 tv1->v_type = VAR_FLOAT;
3019 tv1->vval.v_float = f;
3020 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003021 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003022#endif
3023 {
3024 if (*op == '+')
3025 n += get_tv_number(tv2);
3026 else
3027 n -= get_tv_number(tv2);
3028 clear_tv(tv1);
3029 tv1->v_type = VAR_NUMBER;
3030 tv1->vval.v_number = n;
3031 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003032 }
3033 else
3034 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003035 if (tv2->v_type == VAR_FLOAT)
3036 break;
3037
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003038 /* str .= str */
3039 s = get_tv_string(tv1);
3040 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3041 clear_tv(tv1);
3042 tv1->v_type = VAR_STRING;
3043 tv1->vval.v_string = s;
3044 }
3045 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003046
3047#ifdef FEAT_FLOAT
3048 case VAR_FLOAT:
3049 {
3050 float_T f;
3051
3052 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3053 && tv2->v_type != VAR_NUMBER
3054 && tv2->v_type != VAR_STRING))
3055 break;
3056 if (tv2->v_type == VAR_FLOAT)
3057 f = tv2->vval.v_float;
3058 else
3059 f = get_tv_number(tv2);
3060 if (*op == '+')
3061 tv1->vval.v_float += f;
3062 else
3063 tv1->vval.v_float -= f;
3064 }
3065 return OK;
3066#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067 }
3068 }
3069
3070 EMSG2(_(e_letwrong), op);
3071 return FAIL;
3072}
3073
3074/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003075 * Add a watcher to a list.
3076 */
3077 static void
3078list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003079 list_T *l;
3080 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003081{
3082 lw->lw_next = l->lv_watch;
3083 l->lv_watch = lw;
3084}
3085
3086/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003087 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003088 * No warning when it isn't found...
3089 */
3090 static void
3091list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003092 list_T *l;
3093 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003094{
Bram Moolenaar33570922005-01-25 22:26:29 +00003095 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003096
3097 lwp = &l->lv_watch;
3098 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3099 {
3100 if (lw == lwrem)
3101 {
3102 *lwp = lw->lw_next;
3103 break;
3104 }
3105 lwp = &lw->lw_next;
3106 }
3107}
3108
3109/*
3110 * Just before removing an item from a list: advance watchers to the next
3111 * item.
3112 */
3113 static void
3114list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 list_T *l;
3116 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117{
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119
3120 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3121 if (lw->lw_item == item)
3122 lw->lw_item = item->li_next;
3123}
3124
3125/*
3126 * Evaluate the expression used in a ":for var in expr" command.
3127 * "arg" points to "var".
3128 * Set "*errp" to TRUE for an error, FALSE otherwise;
3129 * Return a pointer that holds the info. Null when there is an error.
3130 */
3131 void *
3132eval_for_line(arg, errp, nextcmdp, skip)
3133 char_u *arg;
3134 int *errp;
3135 char_u **nextcmdp;
3136 int skip;
3137{
Bram Moolenaar33570922005-01-25 22:26:29 +00003138 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003139 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003140 typval_T tv;
3141 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003142
3143 *errp = TRUE; /* default: there is an error */
3144
Bram Moolenaar33570922005-01-25 22:26:29 +00003145 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146 if (fi == NULL)
3147 return NULL;
3148
3149 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3150 if (expr == NULL)
3151 return fi;
3152
3153 expr = skipwhite(expr);
3154 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3155 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003156 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157 return fi;
3158 }
3159
3160 if (skip)
3161 ++emsg_skip;
3162 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3163 {
3164 *errp = FALSE;
3165 if (!skip)
3166 {
3167 l = tv.vval.v_list;
3168 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003169 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003171 clear_tv(&tv);
3172 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173 else
3174 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003175 /* No need to increment the refcount, it's already set for the
3176 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 fi->fi_list = l;
3178 list_add_watch(l, &fi->fi_lw);
3179 fi->fi_lw.lw_item = l->lv_first;
3180 }
3181 }
3182 }
3183 if (skip)
3184 --emsg_skip;
3185
3186 return fi;
3187}
3188
3189/*
3190 * Use the first item in a ":for" list. Advance to the next.
3191 * Assign the values to the variable (list). "arg" points to the first one.
3192 * Return TRUE when a valid item was found, FALSE when at end of list or
3193 * something wrong.
3194 */
3195 int
3196next_for_item(fi_void, arg)
3197 void *fi_void;
3198 char_u *arg;
3199{
Bram Moolenaar33570922005-01-25 22:26:29 +00003200 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003202 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203
3204 item = fi->fi_lw.lw_item;
3205 if (item == NULL)
3206 result = FALSE;
3207 else
3208 {
3209 fi->fi_lw.lw_item = item->li_next;
3210 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3211 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3212 }
3213 return result;
3214}
3215
3216/*
3217 * Free the structure used to store info used by ":for".
3218 */
3219 void
3220free_for_info(fi_void)
3221 void *fi_void;
3222{
Bram Moolenaar33570922005-01-25 22:26:29 +00003223 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003225 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003226 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003228 list_unref(fi->fi_list);
3229 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 vim_free(fi);
3231}
3232
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3234
3235 void
3236set_context_for_expression(xp, arg, cmdidx)
3237 expand_T *xp;
3238 char_u *arg;
3239 cmdidx_T cmdidx;
3240{
3241 int got_eq = FALSE;
3242 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 if (cmdidx == CMD_let)
3246 {
3247 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003248 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 {
3250 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003251 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 {
3253 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 if (vim_iswhite(*p))
3256 break;
3257 }
3258 return;
3259 }
3260 }
3261 else
3262 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3263 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 while ((xp->xp_pattern = vim_strpbrk(arg,
3265 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3266 {
3267 c = *xp->xp_pattern;
3268 if (c == '&')
3269 {
3270 c = xp->xp_pattern[1];
3271 if (c == '&')
3272 {
3273 ++xp->xp_pattern;
3274 xp->xp_context = cmdidx != CMD_let || got_eq
3275 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3276 }
3277 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003280 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3281 xp->xp_pattern += 2;
3282
3283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
3285 else if (c == '$')
3286 {
3287 /* environment variable */
3288 xp->xp_context = EXPAND_ENV_VARS;
3289 }
3290 else if (c == '=')
3291 {
3292 got_eq = TRUE;
3293 xp->xp_context = EXPAND_EXPRESSION;
3294 }
3295 else if (c == '<'
3296 && xp->xp_context == EXPAND_FUNCTIONS
3297 && vim_strchr(xp->xp_pattern, '(') == NULL)
3298 {
3299 /* Function name can start with "<SNR>" */
3300 break;
3301 }
3302 else if (cmdidx != CMD_let || got_eq)
3303 {
3304 if (c == '"') /* string */
3305 {
3306 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3307 if (c == '\\' && xp->xp_pattern[1] != NUL)
3308 ++xp->xp_pattern;
3309 xp->xp_context = EXPAND_NOTHING;
3310 }
3311 else if (c == '\'') /* literal string */
3312 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003313 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3315 /* skip */ ;
3316 xp->xp_context = EXPAND_NOTHING;
3317 }
3318 else if (c == '|')
3319 {
3320 if (xp->xp_pattern[1] == '|')
3321 {
3322 ++xp->xp_pattern;
3323 xp->xp_context = EXPAND_EXPRESSION;
3324 }
3325 else
3326 xp->xp_context = EXPAND_COMMANDS;
3327 }
3328 else
3329 xp->xp_context = EXPAND_EXPRESSION;
3330 }
3331 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003332 /* Doesn't look like something valid, expand as an expression
3333 * anyway. */
3334 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 arg = xp->xp_pattern;
3336 if (*arg != NUL)
3337 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3338 /* skip */ ;
3339 }
3340 xp->xp_pattern = arg;
3341}
3342
3343#endif /* FEAT_CMDL_COMPL */
3344
3345/*
3346 * ":1,25call func(arg1, arg2)" function call.
3347 */
3348 void
3349ex_call(eap)
3350 exarg_T *eap;
3351{
3352 char_u *arg = eap->arg;
3353 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003355 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003357 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 linenr_T lnum;
3359 int doesrange;
3360 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003363 if (eap->skip)
3364 {
3365 /* trans_function_name() doesn't work well when skipping, use eval0()
3366 * instead to skip to any following command, e.g. for:
3367 * :if 0 | call dict.foo().bar() | endif */
3368 eval0(eap->arg, &rettv, &eap->nextcmd, FALSE);
3369 return;
3370 }
3371
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003372 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003373 if (fudi.fd_newkey != NULL)
3374 {
3375 /* Still need to give an error message for missing key. */
3376 EMSG2(_(e_dictkey), fudi.fd_newkey);
3377 vim_free(fudi.fd_newkey);
3378 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003379 if (tofree == NULL)
3380 return;
3381
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003382 /* Increase refcount on dictionary, it could get deleted when evaluating
3383 * the arguments. */
3384 if (fudi.fd_dict != NULL)
3385 ++fudi.fd_dict->dv_refcount;
3386
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003387 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003388 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003389 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
Bram Moolenaar532c7802005-01-27 14:44:31 +00003391 /* Skip white space to allow ":call func ()". Not good, but required for
3392 * backward compatibility. */
3393 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003394 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 if (*startarg != '(')
3397 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003398 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 goto end;
3400 }
3401
3402 /*
3403 * When skipping, evaluate the function once, to find the end of the
3404 * arguments.
3405 * When the function takes a range, this is discovered after the first
3406 * call, and the loop is broken.
3407 */
3408 if (eap->skip)
3409 {
3410 ++emsg_skip;
3411 lnum = eap->line2; /* do it once, also with an invalid range */
3412 }
3413 else
3414 lnum = eap->line1;
3415 for ( ; lnum <= eap->line2; ++lnum)
3416 {
3417 if (!eap->skip && eap->addr_count > 0)
3418 {
3419 curwin->w_cursor.lnum = lnum;
3420 curwin->w_cursor.col = 0;
3421 }
3422 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003423 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003424 eap->line1, eap->line2, &doesrange,
3425 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
3427 failed = TRUE;
3428 break;
3429 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003430
3431 /* Handle a function returning a Funcref, Dictionary or List. */
3432 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3433 {
3434 failed = TRUE;
3435 break;
3436 }
3437
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003438 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 if (doesrange || eap->skip)
3440 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003443 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003444 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003445 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (aborting())
3447 break;
3448 }
3449 if (eap->skip)
3450 --emsg_skip;
3451
3452 if (!failed)
3453 {
3454 /* Check for trailing illegal characters and a following command. */
3455 if (!ends_excmd(*arg))
3456 {
3457 emsg_severe = TRUE;
3458 EMSG(_(e_trailing));
3459 }
3460 else
3461 eap->nextcmd = check_nextcmd(arg);
3462 }
3463
3464end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003465 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003466 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467}
3468
3469/*
3470 * ":unlet[!] var1 ... " command.
3471 */
3472 void
3473ex_unlet(eap)
3474 exarg_T *eap;
3475{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003476 ex_unletlock(eap, eap->arg, 0);
3477}
3478
3479/*
3480 * ":lockvar" and ":unlockvar" commands
3481 */
3482 void
3483ex_lockvar(eap)
3484 exarg_T *eap;
3485{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003487 int deep = 2;
3488
3489 if (eap->forceit)
3490 deep = -1;
3491 else if (vim_isdigit(*arg))
3492 {
3493 deep = getdigits(&arg);
3494 arg = skipwhite(arg);
3495 }
3496
3497 ex_unletlock(eap, arg, deep);
3498}
3499
3500/*
3501 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3502 */
3503 static void
3504ex_unletlock(eap, argstart, deep)
3505 exarg_T *eap;
3506 char_u *argstart;
3507 int deep;
3508{
3509 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003512 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514 do
3515 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003516 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003517 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3518 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003519 if (lv.ll_name == NULL)
3520 error = TRUE; /* error but continue parsing */
3521 if (name_end == NULL || (!vim_iswhite(*name_end)
3522 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003524 if (name_end != NULL)
3525 {
3526 emsg_severe = TRUE;
3527 EMSG(_(e_trailing));
3528 }
3529 if (!(eap->skip || error))
3530 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 break;
3532 }
3533
3534 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003535 {
3536 if (eap->cmdidx == CMD_unlet)
3537 {
3538 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3539 error = TRUE;
3540 }
3541 else
3542 {
3543 if (do_lock_var(&lv, name_end, deep,
3544 eap->cmdidx == CMD_lockvar) == FAIL)
3545 error = TRUE;
3546 }
3547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003549 if (!eap->skip)
3550 clear_lval(&lv);
3551
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 arg = skipwhite(name_end);
3553 } while (!ends_excmd(*arg));
3554
3555 eap->nextcmd = check_nextcmd(arg);
3556}
3557
Bram Moolenaar8c711452005-01-14 21:53:12 +00003558 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003559do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003560 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003562 int forceit;
3563{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003564 int ret = OK;
3565 int cc;
3566
3567 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003568 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003569 cc = *name_end;
3570 *name_end = NUL;
3571
3572 /* Normal name or expanded name. */
3573 if (check_changedtick(lp->ll_name))
3574 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003575 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003576 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003577 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003578 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003579 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3580 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003581 else if (lp->ll_range)
3582 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003583 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003584
3585 /* Delete a range of List items. */
3586 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3587 {
3588 li = lp->ll_li->li_next;
3589 listitem_remove(lp->ll_list, lp->ll_li);
3590 lp->ll_li = li;
3591 ++lp->ll_n1;
3592 }
3593 }
3594 else
3595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597 /* unlet a List item. */
3598 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003600 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003601 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003602 }
3603
3604 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003605}
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607/*
3608 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 */
3611 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003614 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
Bram Moolenaar33570922005-01-25 22:26:29 +00003616 hashtab_T *ht;
3617 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003618 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003619 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
Bram Moolenaar33570922005-01-25 22:26:29 +00003621 ht = find_var_ht(name, &varname);
3622 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003624 hi = hash_find(ht, varname);
3625 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003626 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003627 di = HI2DI(hi);
3628 if (var_check_fixed(di->di_flags, name)
3629 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630 return FAIL;
3631 delete_var(ht, hi);
3632 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003635 if (forceit)
3636 return OK;
3637 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 return FAIL;
3639}
3640
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003641/*
3642 * Lock or unlock variable indicated by "lp".
3643 * "deep" is the levels to go (-1 for unlimited);
3644 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3645 */
3646 static int
3647do_lock_var(lp, name_end, deep, lock)
3648 lval_T *lp;
3649 char_u *name_end;
3650 int deep;
3651 int lock;
3652{
3653 int ret = OK;
3654 int cc;
3655 dictitem_T *di;
3656
3657 if (deep == 0) /* nothing to do */
3658 return OK;
3659
3660 if (lp->ll_tv == NULL)
3661 {
3662 cc = *name_end;
3663 *name_end = NUL;
3664
3665 /* Normal name or expanded name. */
3666 if (check_changedtick(lp->ll_name))
3667 ret = FAIL;
3668 else
3669 {
3670 di = find_var(lp->ll_name, NULL);
3671 if (di == NULL)
3672 ret = FAIL;
3673 else
3674 {
3675 if (lock)
3676 di->di_flags |= DI_FLAGS_LOCK;
3677 else
3678 di->di_flags &= ~DI_FLAGS_LOCK;
3679 item_lock(&di->di_tv, deep, lock);
3680 }
3681 }
3682 *name_end = cc;
3683 }
3684 else if (lp->ll_range)
3685 {
3686 listitem_T *li = lp->ll_li;
3687
3688 /* (un)lock a range of List items. */
3689 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3690 {
3691 item_lock(&li->li_tv, deep, lock);
3692 li = li->li_next;
3693 ++lp->ll_n1;
3694 }
3695 }
3696 else if (lp->ll_list != NULL)
3697 /* (un)lock a List item. */
3698 item_lock(&lp->ll_li->li_tv, deep, lock);
3699 else
3700 /* un(lock) a Dictionary item. */
3701 item_lock(&lp->ll_di->di_tv, deep, lock);
3702
3703 return ret;
3704}
3705
3706/*
3707 * Lock or unlock an item. "deep" is nr of levels to go.
3708 */
3709 static void
3710item_lock(tv, deep, lock)
3711 typval_T *tv;
3712 int deep;
3713 int lock;
3714{
3715 static int recurse = 0;
3716 list_T *l;
3717 listitem_T *li;
3718 dict_T *d;
3719 hashitem_T *hi;
3720 int todo;
3721
3722 if (recurse >= DICT_MAXNEST)
3723 {
3724 EMSG(_("E743: variable nested too deep for (un)lock"));
3725 return;
3726 }
3727 if (deep == 0)
3728 return;
3729 ++recurse;
3730
3731 /* lock/unlock the item itself */
3732 if (lock)
3733 tv->v_lock |= VAR_LOCKED;
3734 else
3735 tv->v_lock &= ~VAR_LOCKED;
3736
3737 switch (tv->v_type)
3738 {
3739 case VAR_LIST:
3740 if ((l = tv->vval.v_list) != NULL)
3741 {
3742 if (lock)
3743 l->lv_lock |= VAR_LOCKED;
3744 else
3745 l->lv_lock &= ~VAR_LOCKED;
3746 if (deep < 0 || deep > 1)
3747 /* recursive: lock/unlock the items the List contains */
3748 for (li = l->lv_first; li != NULL; li = li->li_next)
3749 item_lock(&li->li_tv, deep - 1, lock);
3750 }
3751 break;
3752 case VAR_DICT:
3753 if ((d = tv->vval.v_dict) != NULL)
3754 {
3755 if (lock)
3756 d->dv_lock |= VAR_LOCKED;
3757 else
3758 d->dv_lock &= ~VAR_LOCKED;
3759 if (deep < 0 || deep > 1)
3760 {
3761 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003762 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003763 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3764 {
3765 if (!HASHITEM_EMPTY(hi))
3766 {
3767 --todo;
3768 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3769 }
3770 }
3771 }
3772 }
3773 }
3774 --recurse;
3775}
3776
Bram Moolenaara40058a2005-07-11 22:42:07 +00003777/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003778 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3779 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003780 */
3781 static int
3782tv_islocked(tv)
3783 typval_T *tv;
3784{
3785 return (tv->v_lock & VAR_LOCKED)
3786 || (tv->v_type == VAR_LIST
3787 && tv->vval.v_list != NULL
3788 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3789 || (tv->v_type == VAR_DICT
3790 && tv->vval.v_dict != NULL
3791 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3792}
3793
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3795/*
3796 * Delete all "menutrans_" variables.
3797 */
3798 void
3799del_menutrans_vars()
3800{
Bram Moolenaar33570922005-01-25 22:26:29 +00003801 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003802 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
Bram Moolenaar33570922005-01-25 22:26:29 +00003804 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003805 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003806 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003807 {
3808 if (!HASHITEM_EMPTY(hi))
3809 {
3810 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003811 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3812 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003813 }
3814 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003815 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816}
3817#endif
3818
3819#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3820
3821/*
3822 * Local string buffer for the next two functions to store a variable name
3823 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3824 * get_user_var_name().
3825 */
3826
3827static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3828
3829static char_u *varnamebuf = NULL;
3830static int varnamebuflen = 0;
3831
3832/*
3833 * Function to concatenate a prefix and a variable name.
3834 */
3835 static char_u *
3836cat_prefix_varname(prefix, name)
3837 int prefix;
3838 char_u *name;
3839{
3840 int len;
3841
3842 len = (int)STRLEN(name) + 3;
3843 if (len > varnamebuflen)
3844 {
3845 vim_free(varnamebuf);
3846 len += 10; /* some additional space */
3847 varnamebuf = alloc(len);
3848 if (varnamebuf == NULL)
3849 {
3850 varnamebuflen = 0;
3851 return NULL;
3852 }
3853 varnamebuflen = len;
3854 }
3855 *varnamebuf = prefix;
3856 varnamebuf[1] = ':';
3857 STRCPY(varnamebuf + 2, name);
3858 return varnamebuf;
3859}
3860
3861/*
3862 * Function given to ExpandGeneric() to obtain the list of user defined
3863 * (global/buffer/window/built-in) variable names.
3864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 char_u *
3866get_user_var_name(xp, idx)
3867 expand_T *xp;
3868 int idx;
3869{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003870 static long_u gdone;
3871 static long_u bdone;
3872 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003873#ifdef FEAT_WINDOWS
3874 static long_u tdone;
3875#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003876 static int vidx;
3877 static hashitem_T *hi;
3878 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
3880 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003881 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003882 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003883#ifdef FEAT_WINDOWS
3884 tdone = 0;
3885#endif
3886 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003887
3888 /* Global variables */
3889 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003891 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003892 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003893 else
3894 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003895 while (HASHITEM_EMPTY(hi))
3896 ++hi;
3897 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3898 return cat_prefix_varname('g', hi->hi_key);
3899 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003901
3902 /* b: variables */
3903 ht = &curbuf->b_vars.dv_hashtab;
3904 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003906 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003907 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003908 else
3909 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003910 while (HASHITEM_EMPTY(hi))
3911 ++hi;
3912 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003916 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 return (char_u *)"b:changedtick";
3918 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003919
3920 /* w: variables */
3921 ht = &curwin->w_vars.dv_hashtab;
3922 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003924 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003926 else
3927 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 while (HASHITEM_EMPTY(hi))
3929 ++hi;
3930 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003932
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003933#ifdef FEAT_WINDOWS
3934 /* t: variables */
3935 ht = &curtab->tp_vars.dv_hashtab;
3936 if (tdone < ht->ht_used)
3937 {
3938 if (tdone++ == 0)
3939 hi = ht->ht_array;
3940 else
3941 ++hi;
3942 while (HASHITEM_EMPTY(hi))
3943 ++hi;
3944 return cat_prefix_varname('t', hi->hi_key);
3945 }
3946#endif
3947
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 /* v: variables */
3949 if (vidx < VV_LEN)
3950 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
3952 vim_free(varnamebuf);
3953 varnamebuf = NULL;
3954 varnamebuflen = 0;
3955 return NULL;
3956}
3957
3958#endif /* FEAT_CMDL_COMPL */
3959
3960/*
3961 * types for expressions.
3962 */
3963typedef enum
3964{
3965 TYPE_UNKNOWN = 0
3966 , TYPE_EQUAL /* == */
3967 , TYPE_NEQUAL /* != */
3968 , TYPE_GREATER /* > */
3969 , TYPE_GEQUAL /* >= */
3970 , TYPE_SMALLER /* < */
3971 , TYPE_SEQUAL /* <= */
3972 , TYPE_MATCH /* =~ */
3973 , TYPE_NOMATCH /* !~ */
3974} exptype_T;
3975
3976/*
3977 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003978 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3980 */
3981
3982/*
3983 * Handle zero level expression.
3984 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003985 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003986 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 * Return OK or FAIL.
3988 */
3989 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003990eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 char_u **nextcmd;
3994 int evaluate;
3995{
3996 int ret;
3997 char_u *p;
3998
3999 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004000 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 if (ret == FAIL || !ends_excmd(*p))
4002 {
4003 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004004 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 /*
4006 * Report the invalid expression unless the expression evaluation has
4007 * been cancelled due to an aborting error, an interrupt, or an
4008 * exception.
4009 */
4010 if (!aborting())
4011 EMSG2(_(e_invexpr2), arg);
4012 ret = FAIL;
4013 }
4014 if (nextcmd != NULL)
4015 *nextcmd = check_nextcmd(p);
4016
4017 return ret;
4018}
4019
4020/*
4021 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004022 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 *
4024 * "arg" must point to the first non-white of the expression.
4025 * "arg" is advanced to the next non-white after the recognized expression.
4026 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004027 * Note: "rettv.v_lock" is not set.
4028 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 * Return OK or FAIL.
4030 */
4031 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004032eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 int evaluate;
4036{
4037 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004038 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
4040 /*
4041 * Get the first variable.
4042 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return FAIL;
4045
4046 if ((*arg)[0] == '?')
4047 {
4048 result = FALSE;
4049 if (evaluate)
4050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004051 int error = FALSE;
4052
4053 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004056 if (error)
4057 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
4059
4060 /*
4061 * Get the second variable.
4062 */
4063 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 return FAIL;
4066
4067 /*
4068 * Check for the ":".
4069 */
4070 if ((*arg)[0] != ':')
4071 {
4072 EMSG(_("E109: Missing ':' after '?'"));
4073 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 return FAIL;
4076 }
4077
4078 /*
4079 * Get the third variable.
4080 */
4081 *arg = skipwhite(*arg + 1);
4082 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4083 {
4084 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 return FAIL;
4087 }
4088 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004089 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091
4092 return OK;
4093}
4094
4095/*
4096 * Handle first level expression:
4097 * expr2 || expr2 || expr2 logical OR
4098 *
4099 * "arg" must point to the first non-white of the expression.
4100 * "arg" is advanced to the next non-white after the recognized expression.
4101 *
4102 * Return OK or FAIL.
4103 */
4104 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 int evaluate;
4109{
Bram Moolenaar33570922005-01-25 22:26:29 +00004110 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 long result;
4112 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004113 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115 /*
4116 * Get the first variable.
4117 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 return FAIL;
4120
4121 /*
4122 * Repeat until there is no following "||".
4123 */
4124 first = TRUE;
4125 result = FALSE;
4126 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4127 {
4128 if (evaluate && first)
4129 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004130 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004133 if (error)
4134 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 first = FALSE;
4136 }
4137
4138 /*
4139 * Get the second variable.
4140 */
4141 *arg = skipwhite(*arg + 2);
4142 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4143 return FAIL;
4144
4145 /*
4146 * Compute the result.
4147 */
4148 if (evaluate && !result)
4149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004150 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153 if (error)
4154 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156 if (evaluate)
4157 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004158 rettv->v_type = VAR_NUMBER;
4159 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 }
4161 }
4162
4163 return OK;
4164}
4165
4166/*
4167 * Handle second level expression:
4168 * expr3 && expr3 && expr3 logical AND
4169 *
4170 * "arg" must point to the first non-white of the expression.
4171 * "arg" is advanced to the next non-white after the recognized expression.
4172 *
4173 * Return OK or FAIL.
4174 */
4175 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 int evaluate;
4180{
Bram Moolenaar33570922005-01-25 22:26:29 +00004181 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 long result;
4183 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185
4186 /*
4187 * Get the first variable.
4188 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 return FAIL;
4191
4192 /*
4193 * Repeat until there is no following "&&".
4194 */
4195 first = TRUE;
4196 result = TRUE;
4197 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4198 {
4199 if (evaluate && first)
4200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004204 if (error)
4205 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 first = FALSE;
4207 }
4208
4209 /*
4210 * Get the second variable.
4211 */
4212 *arg = skipwhite(*arg + 2);
4213 if (eval4(arg, &var2, evaluate && result) == FAIL)
4214 return FAIL;
4215
4216 /*
4217 * Compute the result.
4218 */
4219 if (evaluate && result)
4220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004224 if (error)
4225 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227 if (evaluate)
4228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 rettv->v_type = VAR_NUMBER;
4230 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232 }
4233
4234 return OK;
4235}
4236
4237/*
4238 * Handle third level expression:
4239 * var1 == var2
4240 * var1 =~ var2
4241 * var1 != var2
4242 * var1 !~ var2
4243 * var1 > var2
4244 * var1 >= var2
4245 * var1 < var2
4246 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004247 * var1 is var2
4248 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 *
4250 * "arg" must point to the first non-white of the expression.
4251 * "arg" is advanced to the next non-white after the recognized expression.
4252 *
4253 * Return OK or FAIL.
4254 */
4255 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 int evaluate;
4260{
Bram Moolenaar33570922005-01-25 22:26:29 +00004261 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 char_u *p;
4263 int i;
4264 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004265 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 int len = 2;
4267 long n1, n2;
4268 char_u *s1, *s2;
4269 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4270 regmatch_T regmatch;
4271 int ic;
4272 char_u *save_cpo;
4273
4274 /*
4275 * Get the first variable.
4276 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 return FAIL;
4279
4280 p = *arg;
4281 switch (p[0])
4282 {
4283 case '=': if (p[1] == '=')
4284 type = TYPE_EQUAL;
4285 else if (p[1] == '~')
4286 type = TYPE_MATCH;
4287 break;
4288 case '!': if (p[1] == '=')
4289 type = TYPE_NEQUAL;
4290 else if (p[1] == '~')
4291 type = TYPE_NOMATCH;
4292 break;
4293 case '>': if (p[1] != '=')
4294 {
4295 type = TYPE_GREATER;
4296 len = 1;
4297 }
4298 else
4299 type = TYPE_GEQUAL;
4300 break;
4301 case '<': if (p[1] != '=')
4302 {
4303 type = TYPE_SMALLER;
4304 len = 1;
4305 }
4306 else
4307 type = TYPE_SEQUAL;
4308 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004309 case 'i': if (p[1] == 's')
4310 {
4311 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4312 len = 5;
4313 if (!vim_isIDc(p[len]))
4314 {
4315 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4316 type_is = TRUE;
4317 }
4318 }
4319 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
4321
4322 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004323 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 */
4325 if (type != TYPE_UNKNOWN)
4326 {
4327 /* extra question mark appended: ignore case */
4328 if (p[len] == '?')
4329 {
4330 ic = TRUE;
4331 ++len;
4332 }
4333 /* extra '#' appended: match case */
4334 else if (p[len] == '#')
4335 {
4336 ic = FALSE;
4337 ++len;
4338 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004339 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 else
4341 ic = p_ic;
4342
4343 /*
4344 * Get the second variable.
4345 */
4346 *arg = skipwhite(p + len);
4347 if (eval5(arg, &var2, evaluate) == FAIL)
4348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 return FAIL;
4351 }
4352
4353 if (evaluate)
4354 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004355 if (type_is && rettv->v_type != var2.v_type)
4356 {
4357 /* For "is" a different type always means FALSE, for "notis"
4358 * it means TRUE. */
4359 n1 = (type == TYPE_NEQUAL);
4360 }
4361 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4362 {
4363 if (type_is)
4364 {
4365 n1 = (rettv->v_type == var2.v_type
4366 && rettv->vval.v_list == var2.vval.v_list);
4367 if (type == TYPE_NEQUAL)
4368 n1 = !n1;
4369 }
4370 else if (rettv->v_type != var2.v_type
4371 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4372 {
4373 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004374 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004375 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004376 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004377 clear_tv(rettv);
4378 clear_tv(&var2);
4379 return FAIL;
4380 }
4381 else
4382 {
4383 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004384 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4385 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386 if (type == TYPE_NEQUAL)
4387 n1 = !n1;
4388 }
4389 }
4390
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004391 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4392 {
4393 if (type_is)
4394 {
4395 n1 = (rettv->v_type == var2.v_type
4396 && rettv->vval.v_dict == var2.vval.v_dict);
4397 if (type == TYPE_NEQUAL)
4398 n1 = !n1;
4399 }
4400 else if (rettv->v_type != var2.v_type
4401 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4402 {
4403 if (rettv->v_type != var2.v_type)
4404 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4405 else
4406 EMSG(_("E736: Invalid operation for Dictionary"));
4407 clear_tv(rettv);
4408 clear_tv(&var2);
4409 return FAIL;
4410 }
4411 else
4412 {
4413 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004414 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4415 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004416 if (type == TYPE_NEQUAL)
4417 n1 = !n1;
4418 }
4419 }
4420
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004421 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4422 {
4423 if (rettv->v_type != var2.v_type
4424 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4425 {
4426 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004427 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004428 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004429 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 clear_tv(rettv);
4431 clear_tv(&var2);
4432 return FAIL;
4433 }
4434 else
4435 {
4436 /* Compare two Funcrefs for being equal or unequal. */
4437 if (rettv->vval.v_string == NULL
4438 || var2.vval.v_string == NULL)
4439 n1 = FALSE;
4440 else
4441 n1 = STRCMP(rettv->vval.v_string,
4442 var2.vval.v_string) == 0;
4443 if (type == TYPE_NEQUAL)
4444 n1 = !n1;
4445 }
4446 }
4447
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004448#ifdef FEAT_FLOAT
4449 /*
4450 * If one of the two variables is a float, compare as a float.
4451 * When using "=~" or "!~", always compare as string.
4452 */
4453 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4454 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4455 {
4456 float_T f1, f2;
4457
4458 if (rettv->v_type == VAR_FLOAT)
4459 f1 = rettv->vval.v_float;
4460 else
4461 f1 = get_tv_number(rettv);
4462 if (var2.v_type == VAR_FLOAT)
4463 f2 = var2.vval.v_float;
4464 else
4465 f2 = get_tv_number(&var2);
4466 n1 = FALSE;
4467 switch (type)
4468 {
4469 case TYPE_EQUAL: n1 = (f1 == f2); break;
4470 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4471 case TYPE_GREATER: n1 = (f1 > f2); break;
4472 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4473 case TYPE_SMALLER: n1 = (f1 < f2); break;
4474 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4475 case TYPE_UNKNOWN:
4476 case TYPE_MATCH:
4477 case TYPE_NOMATCH: break; /* avoid gcc warning */
4478 }
4479 }
4480#endif
4481
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 /*
4483 * If one of the two variables is a number, compare as a number.
4484 * When using "=~" or "!~", always compare as string.
4485 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004489 n1 = get_tv_number(rettv);
4490 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 switch (type)
4492 {
4493 case TYPE_EQUAL: n1 = (n1 == n2); break;
4494 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4495 case TYPE_GREATER: n1 = (n1 > n2); break;
4496 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4497 case TYPE_SMALLER: n1 = (n1 < n2); break;
4498 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4499 case TYPE_UNKNOWN:
4500 case TYPE_MATCH:
4501 case TYPE_NOMATCH: break; /* avoid gcc warning */
4502 }
4503 }
4504 else
4505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004506 s1 = get_tv_string_buf(rettv, buf1);
4507 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4509 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4510 else
4511 i = 0;
4512 n1 = FALSE;
4513 switch (type)
4514 {
4515 case TYPE_EQUAL: n1 = (i == 0); break;
4516 case TYPE_NEQUAL: n1 = (i != 0); break;
4517 case TYPE_GREATER: n1 = (i > 0); break;
4518 case TYPE_GEQUAL: n1 = (i >= 0); break;
4519 case TYPE_SMALLER: n1 = (i < 0); break;
4520 case TYPE_SEQUAL: n1 = (i <= 0); break;
4521
4522 case TYPE_MATCH:
4523 case TYPE_NOMATCH:
4524 /* avoid 'l' flag in 'cpoptions' */
4525 save_cpo = p_cpo;
4526 p_cpo = (char_u *)"";
4527 regmatch.regprog = vim_regcomp(s2,
4528 RE_MAGIC + RE_STRING);
4529 regmatch.rm_ic = ic;
4530 if (regmatch.regprog != NULL)
4531 {
4532 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4533 vim_free(regmatch.regprog);
4534 if (type == TYPE_NOMATCH)
4535 n1 = !n1;
4536 }
4537 p_cpo = save_cpo;
4538 break;
4539
4540 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4541 }
4542 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004543 clear_tv(rettv);
4544 clear_tv(&var2);
4545 rettv->v_type = VAR_NUMBER;
4546 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
4548 }
4549
4550 return OK;
4551}
4552
4553/*
4554 * Handle fourth level expression:
4555 * + number addition
4556 * - number subtraction
4557 * . string concatenation
4558 *
4559 * "arg" must point to the first non-white of the expression.
4560 * "arg" is advanced to the next non-white after the recognized expression.
4561 *
4562 * Return OK or FAIL.
4563 */
4564 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004565eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 int evaluate;
4569{
Bram Moolenaar33570922005-01-25 22:26:29 +00004570 typval_T var2;
4571 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 int op;
4573 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004574#ifdef FEAT_FLOAT
4575 float_T f1 = 0, f2 = 0;
4576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 char_u *s1, *s2;
4578 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4579 char_u *p;
4580
4581 /*
4582 * Get the first variable.
4583 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004584 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 return FAIL;
4586
4587 /*
4588 * Repeat computing, until no '+', '-' or '.' is following.
4589 */
4590 for (;;)
4591 {
4592 op = **arg;
4593 if (op != '+' && op != '-' && op != '.')
4594 break;
4595
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004596 if ((op != '+' || rettv->v_type != VAR_LIST)
4597#ifdef FEAT_FLOAT
4598 && (op == '.' || rettv->v_type != VAR_FLOAT)
4599#endif
4600 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004601 {
4602 /* For "list + ...", an illegal use of the first operand as
4603 * a number cannot be determined before evaluating the 2nd
4604 * operand: if this is also a list, all is ok.
4605 * For "something . ...", "something - ..." or "non-list + ...",
4606 * we know that the first operand needs to be a string or number
4607 * without evaluating the 2nd operand. So check before to avoid
4608 * side effects after an error. */
4609 if (evaluate && get_tv_string_chk(rettv) == NULL)
4610 {
4611 clear_tv(rettv);
4612 return FAIL;
4613 }
4614 }
4615
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 /*
4617 * Get the second variable.
4618 */
4619 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004620 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 return FAIL;
4624 }
4625
4626 if (evaluate)
4627 {
4628 /*
4629 * Compute the result.
4630 */
4631 if (op == '.')
4632 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004633 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4634 s2 = get_tv_string_buf_chk(&var2, buf2);
4635 if (s2 == NULL) /* type error ? */
4636 {
4637 clear_tv(rettv);
4638 clear_tv(&var2);
4639 return FAIL;
4640 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004641 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004642 clear_tv(rettv);
4643 rettv->v_type = VAR_STRING;
4644 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004646 else if (op == '+' && rettv->v_type == VAR_LIST
4647 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004648 {
4649 /* concatenate Lists */
4650 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4651 &var3) == FAIL)
4652 {
4653 clear_tv(rettv);
4654 clear_tv(&var2);
4655 return FAIL;
4656 }
4657 clear_tv(rettv);
4658 *rettv = var3;
4659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 else
4661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004662 int error = FALSE;
4663
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004664#ifdef FEAT_FLOAT
4665 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004666 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004667 f1 = rettv->vval.v_float;
4668 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004669 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004670 else
4671#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004672 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004673 n1 = get_tv_number_chk(rettv, &error);
4674 if (error)
4675 {
4676 /* This can only happen for "list + non-list". For
4677 * "non-list + ..." or "something - ...", we returned
4678 * before evaluating the 2nd operand. */
4679 clear_tv(rettv);
4680 return FAIL;
4681 }
4682#ifdef FEAT_FLOAT
4683 if (var2.v_type == VAR_FLOAT)
4684 f1 = n1;
4685#endif
4686 }
4687#ifdef FEAT_FLOAT
4688 if (var2.v_type == VAR_FLOAT)
4689 {
4690 f2 = var2.vval.v_float;
4691 n2 = 0;
4692 }
4693 else
4694#endif
4695 {
4696 n2 = get_tv_number_chk(&var2, &error);
4697 if (error)
4698 {
4699 clear_tv(rettv);
4700 clear_tv(&var2);
4701 return FAIL;
4702 }
4703#ifdef FEAT_FLOAT
4704 if (rettv->v_type == VAR_FLOAT)
4705 f2 = n2;
4706#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004707 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004708 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004709
4710#ifdef FEAT_FLOAT
4711 /* If there is a float on either side the result is a float. */
4712 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4713 {
4714 if (op == '+')
4715 f1 = f1 + f2;
4716 else
4717 f1 = f1 - f2;
4718 rettv->v_type = VAR_FLOAT;
4719 rettv->vval.v_float = f1;
4720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722#endif
4723 {
4724 if (op == '+')
4725 n1 = n1 + n2;
4726 else
4727 n1 = n1 - n2;
4728 rettv->v_type = VAR_NUMBER;
4729 rettv->vval.v_number = n1;
4730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004732 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 }
4734 }
4735 return OK;
4736}
4737
4738/*
4739 * Handle fifth level expression:
4740 * * number multiplication
4741 * / number division
4742 * % number modulo
4743 *
4744 * "arg" must point to the first non-white of the expression.
4745 * "arg" is advanced to the next non-white after the recognized expression.
4746 *
4747 * Return OK or FAIL.
4748 */
4749 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004750eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004754 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
Bram Moolenaar33570922005-01-25 22:26:29 +00004756 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 int op;
4758 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004759#ifdef FEAT_FLOAT
4760 int use_float = FALSE;
4761 float_T f1 = 0, f2;
4762#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004763 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
4765 /*
4766 * Get the first variable.
4767 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004768 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 return FAIL;
4770
4771 /*
4772 * Repeat computing, until no '*', '/' or '%' is following.
4773 */
4774 for (;;)
4775 {
4776 op = **arg;
4777 if (op != '*' && op != '/' && op != '%')
4778 break;
4779
4780 if (evaluate)
4781 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004782#ifdef FEAT_FLOAT
4783 if (rettv->v_type == VAR_FLOAT)
4784 {
4785 f1 = rettv->vval.v_float;
4786 use_float = TRUE;
4787 n1 = 0;
4788 }
4789 else
4790#endif
4791 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004792 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 if (error)
4794 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
4796 else
4797 n1 = 0;
4798
4799 /*
4800 * Get the second variable.
4801 */
4802 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004803 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 return FAIL;
4805
4806 if (evaluate)
4807 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808#ifdef FEAT_FLOAT
4809 if (var2.v_type == VAR_FLOAT)
4810 {
4811 if (!use_float)
4812 {
4813 f1 = n1;
4814 use_float = TRUE;
4815 }
4816 f2 = var2.vval.v_float;
4817 n2 = 0;
4818 }
4819 else
4820#endif
4821 {
4822 n2 = get_tv_number_chk(&var2, &error);
4823 clear_tv(&var2);
4824 if (error)
4825 return FAIL;
4826#ifdef FEAT_FLOAT
4827 if (use_float)
4828 f2 = n2;
4829#endif
4830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
4832 /*
4833 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004834 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836#ifdef FEAT_FLOAT
4837 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004839 if (op == '*')
4840 f1 = f1 * f2;
4841 else if (op == '/')
4842 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004843# ifdef VMS
4844 /* VMS crashes on divide by zero, work around it */
4845 if (f2 == 0.0)
4846 {
4847 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004848 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004849 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004850 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004851 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004852 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004853 }
4854 else
4855 f1 = f1 / f2;
4856# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857 /* We rely on the floating point library to handle divide
4858 * by zero to result in "inf" and not a crash. */
4859 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004860# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004863 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004864 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004865 return FAIL;
4866 }
4867 rettv->v_type = VAR_FLOAT;
4868 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
4870 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873 if (op == '*')
4874 n1 = n1 * n2;
4875 else if (op == '/')
4876 {
4877 if (n2 == 0) /* give an error message? */
4878 {
4879 if (n1 == 0)
4880 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4881 else if (n1 < 0)
4882 n1 = -0x7fffffffL;
4883 else
4884 n1 = 0x7fffffffL;
4885 }
4886 else
4887 n1 = n1 / n2;
4888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890 {
4891 if (n2 == 0) /* give an error message? */
4892 n1 = 0;
4893 else
4894 n1 = n1 % n2;
4895 }
4896 rettv->v_type = VAR_NUMBER;
4897 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 }
4901
4902 return OK;
4903}
4904
4905/*
4906 * Handle sixth level expression:
4907 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004908 * "string" string constant
4909 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 * &option-name option value
4911 * @r register contents
4912 * identifier variable value
4913 * function() function call
4914 * $VAR environment variable
4915 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004916 * [expr, expr] List
4917 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 *
4919 * Also handle:
4920 * ! in front logical NOT
4921 * - in front unary minus
4922 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004923 * trailing [] subscript in String or List
4924 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 *
4926 * "arg" must point to the first non-white of the expression.
4927 * "arg" is advanced to the next non-white after the recognized expression.
4928 *
4929 * Return OK or FAIL.
4930 */
4931 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004932eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004936 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 long n;
4939 int len;
4940 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 char_u *start_leader, *end_leader;
4942 int ret = OK;
4943 char_u *alias;
4944
4945 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004946 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004947 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004949 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 /*
4952 * Skip '!' and '-' characters. They are handled later.
4953 */
4954 start_leader = *arg;
4955 while (**arg == '!' || **arg == '-' || **arg == '+')
4956 *arg = skipwhite(*arg + 1);
4957 end_leader = *arg;
4958
4959 switch (**arg)
4960 {
4961 /*
4962 * Number constant.
4963 */
4964 case '0':
4965 case '1':
4966 case '2':
4967 case '3':
4968 case '4':
4969 case '5':
4970 case '6':
4971 case '7':
4972 case '8':
4973 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974 {
4975#ifdef FEAT_FLOAT
4976 char_u *p = skipdigits(*arg + 1);
4977 int get_float = FALSE;
4978
4979 /* We accept a float when the format matches
4980 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004981 * strict to avoid backwards compatibility problems.
4982 * Don't look for a float after the "." operator, so that
4983 * ":let vers = 1.2.3" doesn't fail. */
4984 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 get_float = TRUE;
4987 p = skipdigits(p + 2);
4988 if (*p == 'e' || *p == 'E')
4989 {
4990 ++p;
4991 if (*p == '-' || *p == '+')
4992 ++p;
4993 if (!vim_isdigit(*p))
4994 get_float = FALSE;
4995 else
4996 p = skipdigits(p + 1);
4997 }
4998 if (ASCII_ISALPHA(*p) || *p == '.')
4999 get_float = FALSE;
5000 }
5001 if (get_float)
5002 {
5003 float_T f;
5004
5005 *arg += string2float(*arg, &f);
5006 if (evaluate)
5007 {
5008 rettv->v_type = VAR_FLOAT;
5009 rettv->vval.v_float = f;
5010 }
5011 }
5012 else
5013#endif
5014 {
5015 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5016 *arg += len;
5017 if (evaluate)
5018 {
5019 rettv->v_type = VAR_NUMBER;
5020 rettv->vval.v_number = n;
5021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025
5026 /*
5027 * String constant: "string".
5028 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005029 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 break;
5031
5032 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005035 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005036 break;
5037
5038 /*
5039 * List: [expr, expr]
5040 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 break;
5043
5044 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 * Dictionary: {key: val, key: val}
5046 */
5047 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5048 break;
5049
5050 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005051 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005053 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 break;
5055
5056 /*
5057 * Environment variable: $VAR.
5058 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 break;
5061
5062 /*
5063 * Register contents: @r.
5064 */
5065 case '@': ++*arg;
5066 if (evaluate)
5067 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005069 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 if (**arg != NUL)
5072 ++*arg;
5073 break;
5074
5075 /*
5076 * nested expression: (expression).
5077 */
5078 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 if (**arg == ')')
5081 ++*arg;
5082 else if (ret == OK)
5083 {
5084 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005085 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 ret = FAIL;
5087 }
5088 break;
5089
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093
5094 if (ret == NOTDONE)
5095 {
5096 /*
5097 * Must be a variable or function name.
5098 * Can also be a curly-braces kind of name: {expr}.
5099 */
5100 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005101 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 if (alias != NULL)
5103 s = alias;
5104
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005105 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 ret = FAIL;
5107 else
5108 {
5109 if (**arg == '(') /* recursive! */
5110 {
5111 /* If "s" is the name of a variable of type VAR_FUNC
5112 * use its contents. */
5113 s = deref_func_name(s, &len);
5114
5115 /* Invoke the function. */
5116 ret = get_func_tv(s, len, rettv, arg,
5117 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005118 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 /* Stop the expression evaluation when immediately
5120 * aborting on error, or when an interrupt occurred or
5121 * an exception was thrown but not caught. */
5122 if (aborting())
5123 {
5124 if (ret == OK)
5125 clear_tv(rettv);
5126 ret = FAIL;
5127 }
5128 }
5129 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005130 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005131 else
5132 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005134 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 }
5136
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 *arg = skipwhite(*arg);
5138
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005139 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5140 * expr(expr). */
5141 if (ret == OK)
5142 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
5144 /*
5145 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5146 */
5147 if (ret == OK && evaluate && end_leader > start_leader)
5148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005149 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005150 int val = 0;
5151#ifdef FEAT_FLOAT
5152 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005153
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005154 if (rettv->v_type == VAR_FLOAT)
5155 f = rettv->vval.v_float;
5156 else
5157#endif
5158 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005159 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005161 clear_tv(rettv);
5162 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005164 else
5165 {
5166 while (end_leader > start_leader)
5167 {
5168 --end_leader;
5169 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005170 {
5171#ifdef FEAT_FLOAT
5172 if (rettv->v_type == VAR_FLOAT)
5173 f = !f;
5174 else
5175#endif
5176 val = !val;
5177 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005178 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005179 {
5180#ifdef FEAT_FLOAT
5181 if (rettv->v_type == VAR_FLOAT)
5182 f = -f;
5183 else
5184#endif
5185 val = -val;
5186 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005187 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005188#ifdef FEAT_FLOAT
5189 if (rettv->v_type == VAR_FLOAT)
5190 {
5191 clear_tv(rettv);
5192 rettv->vval.v_float = f;
5193 }
5194 else
5195#endif
5196 {
5197 clear_tv(rettv);
5198 rettv->v_type = VAR_NUMBER;
5199 rettv->vval.v_number = val;
5200 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
5203
5204 return ret;
5205}
5206
5207/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005208 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5209 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005210 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5211 */
5212 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005213eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005215 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005216 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005217 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005218{
5219 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005220 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005221 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 long len = -1;
5223 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005227 if (rettv->v_type == VAR_FUNC
5228#ifdef FEAT_FLOAT
5229 || rettv->v_type == VAR_FLOAT
5230#endif
5231 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005233 if (verbose)
5234 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005235 return FAIL;
5236 }
5237
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 /*
5241 * dict.name
5242 */
5243 key = *arg + 1;
5244 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5245 ;
5246 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249 }
5250 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 /*
5253 * something[idx]
5254 *
5255 * Get the (first) variable from inside the [].
5256 */
5257 *arg = skipwhite(*arg + 1);
5258 if (**arg == ':')
5259 empty1 = TRUE;
5260 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5261 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005262 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5263 {
5264 /* not a number or string */
5265 clear_tv(&var1);
5266 return FAIL;
5267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268
5269 /*
5270 * Get the second variable from inside the [:].
5271 */
5272 if (**arg == ':')
5273 {
5274 range = TRUE;
5275 *arg = skipwhite(*arg + 1);
5276 if (**arg == ']')
5277 empty2 = TRUE;
5278 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 if (!empty1)
5281 clear_tv(&var1);
5282 return FAIL;
5283 }
5284 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5285 {
5286 /* not a number or string */
5287 if (!empty1)
5288 clear_tv(&var1);
5289 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 return FAIL;
5291 }
5292 }
5293
5294 /* Check for the ']'. */
5295 if (**arg != ']')
5296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005297 if (verbose)
5298 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 clear_tv(&var1);
5300 if (range)
5301 clear_tv(&var2);
5302 return FAIL;
5303 }
5304 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 }
5306
5307 if (evaluate)
5308 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 n1 = 0;
5310 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005312 n1 = get_tv_number(&var1);
5313 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 }
5315 if (range)
5316 {
5317 if (empty2)
5318 n2 = -1;
5319 else
5320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005321 n2 = get_tv_number(&var2);
5322 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005323 }
5324 }
5325
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005326 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 {
5328 case VAR_NUMBER:
5329 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005330 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 len = (long)STRLEN(s);
5332 if (range)
5333 {
5334 /* The resulting variable is a substring. If the indexes
5335 * are out of range the result is empty. */
5336 if (n1 < 0)
5337 {
5338 n1 = len + n1;
5339 if (n1 < 0)
5340 n1 = 0;
5341 }
5342 if (n2 < 0)
5343 n2 = len + n2;
5344 else if (n2 >= len)
5345 n2 = len;
5346 if (n1 >= len || n2 < 0 || n1 > n2)
5347 s = NULL;
5348 else
5349 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5350 }
5351 else
5352 {
5353 /* The resulting variable is a string of a single
5354 * character. If the index is too big or negative the
5355 * result is empty. */
5356 if (n1 >= len || n1 < 0)
5357 s = NULL;
5358 else
5359 s = vim_strnsave(s + n1, 1);
5360 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005361 clear_tv(rettv);
5362 rettv->v_type = VAR_STRING;
5363 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 break;
5365
5366 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005367 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 if (n1 < 0)
5369 n1 = len + n1;
5370 if (!empty1 && (n1 < 0 || n1 >= len))
5371 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005372 /* For a range we allow invalid values and return an empty
5373 * list. A list index out of range is an error. */
5374 if (!range)
5375 {
5376 if (verbose)
5377 EMSGN(_(e_listidx), n1);
5378 return FAIL;
5379 }
5380 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 }
5382 if (range)
5383 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005384 list_T *l;
5385 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386
5387 if (n2 < 0)
5388 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005389 else if (n2 >= len)
5390 n2 = len - 1;
5391 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005392 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 l = list_alloc();
5394 if (l == NULL)
5395 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005396 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 n1 <= n2; ++n1)
5398 {
5399 if (list_append_tv(l, &item->li_tv) == FAIL)
5400 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005401 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 return FAIL;
5403 }
5404 item = item->li_next;
5405 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005406 clear_tv(rettv);
5407 rettv->v_type = VAR_LIST;
5408 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005409 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 }
5411 else
5412 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005413 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005414 clear_tv(rettv);
5415 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 }
5417 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005418
5419 case VAR_DICT:
5420 if (range)
5421 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005422 if (verbose)
5423 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424 if (len == -1)
5425 clear_tv(&var1);
5426 return FAIL;
5427 }
5428 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005429 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430
5431 if (len == -1)
5432 {
5433 key = get_tv_string(&var1);
5434 if (*key == NUL)
5435 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005436 if (verbose)
5437 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005438 clear_tv(&var1);
5439 return FAIL;
5440 }
5441 }
5442
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005443 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005445 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005446 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 if (len == -1)
5448 clear_tv(&var1);
5449 if (item == NULL)
5450 return FAIL;
5451
5452 copy_tv(&item->di_tv, &var1);
5453 clear_tv(rettv);
5454 *rettv = var1;
5455 }
5456 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 }
5458 }
5459
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 return OK;
5461}
5462
5463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 * Get an option value.
5465 * "arg" points to the '&' or '+' before the option name.
5466 * "arg" is advanced to character after the option name.
5467 * Return OK or FAIL.
5468 */
5469 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005472 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 int evaluate;
5474{
5475 char_u *option_end;
5476 long numval;
5477 char_u *stringval;
5478 int opt_type;
5479 int c;
5480 int working = (**arg == '+'); /* has("+option") */
5481 int ret = OK;
5482 int opt_flags;
5483
5484 /*
5485 * Isolate the option name and find its value.
5486 */
5487 option_end = find_option_end(arg, &opt_flags);
5488 if (option_end == NULL)
5489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005490 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 EMSG2(_("E112: Option name missing: %s"), *arg);
5492 return FAIL;
5493 }
5494
5495 if (!evaluate)
5496 {
5497 *arg = option_end;
5498 return OK;
5499 }
5500
5501 c = *option_end;
5502 *option_end = NUL;
5503 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005504 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
5506 if (opt_type == -3) /* invalid name */
5507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 EMSG2(_("E113: Unknown option: %s"), *arg);
5510 ret = FAIL;
5511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 {
5514 if (opt_type == -2) /* hidden string option */
5515 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 rettv->v_type = VAR_STRING;
5517 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 }
5519 else if (opt_type == -1) /* hidden number option */
5520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521 rettv->v_type = VAR_NUMBER;
5522 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 }
5524 else if (opt_type == 1) /* number option */
5525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526 rettv->v_type = VAR_NUMBER;
5527 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 }
5529 else /* string option */
5530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005531 rettv->v_type = VAR_STRING;
5532 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
5534 }
5535 else if (working && (opt_type == -2 || opt_type == -1))
5536 ret = FAIL;
5537
5538 *option_end = c; /* put back for error messages */
5539 *arg = option_end;
5540
5541 return ret;
5542}
5543
5544/*
5545 * Allocate a variable for a string constant.
5546 * Return OK or FAIL.
5547 */
5548 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 int evaluate;
5553{
5554 char_u *p;
5555 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 int extra = 0;
5557
5558 /*
5559 * Find the end of the string, skipping backslashed characters.
5560 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005561 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
5563 if (*p == '\\' && p[1] != NUL)
5564 {
5565 ++p;
5566 /* A "\<x>" form occupies at least 4 characters, and produces up
5567 * to 6 characters: reserve space for 2 extra */
5568 if (*p == '<')
5569 extra += 2;
5570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
5572
5573 if (*p != '"')
5574 {
5575 EMSG2(_("E114: Missing quote: %s"), *arg);
5576 return FAIL;
5577 }
5578
5579 /* If only parsing, set *arg and return here */
5580 if (!evaluate)
5581 {
5582 *arg = p + 1;
5583 return OK;
5584 }
5585
5586 /*
5587 * Copy the string into allocated memory, handling backslashed
5588 * characters.
5589 */
5590 name = alloc((unsigned)(p - *arg + extra));
5591 if (name == NULL)
5592 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 rettv->v_type = VAR_STRING;
5594 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 {
5598 if (*p == '\\')
5599 {
5600 switch (*++p)
5601 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 case 'b': *name++ = BS; ++p; break;
5603 case 'e': *name++ = ESC; ++p; break;
5604 case 'f': *name++ = FF; ++p; break;
5605 case 'n': *name++ = NL; ++p; break;
5606 case 'r': *name++ = CAR; ++p; break;
5607 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
5609 case 'X': /* hex: "\x1", "\x12" */
5610 case 'x':
5611 case 'u': /* Unicode: "\u0023" */
5612 case 'U':
5613 if (vim_isxdigit(p[1]))
5614 {
5615 int n, nr;
5616 int c = toupper(*p);
5617
5618 if (c == 'X')
5619 n = 2;
5620 else
5621 n = 4;
5622 nr = 0;
5623 while (--n >= 0 && vim_isxdigit(p[1]))
5624 {
5625 ++p;
5626 nr = (nr << 4) + hex2nr(*p);
5627 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005628 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629#ifdef FEAT_MBYTE
5630 /* For "\u" store the number according to
5631 * 'encoding'. */
5632 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005633 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 else
5635#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 break;
5639
5640 /* octal: "\1", "\12", "\123" */
5641 case '0':
5642 case '1':
5643 case '2':
5644 case '3':
5645 case '4':
5646 case '5':
5647 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648 case '7': *name = *p++ - '0';
5649 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 *name = (*name << 3) + *p++ - '0';
5652 if (*p >= '0' && *p <= '7')
5653 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005655 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 break;
5657
5658 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 if (extra != 0)
5661 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 break;
5664 }
5665 /* FALLTHROUGH */
5666
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 break;
5669 }
5670 }
5671 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 *arg = p + 1;
5677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 return OK;
5679}
5680
5681/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 * Return OK or FAIL.
5684 */
5685 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 int evaluate;
5690{
5691 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005692 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005693 int reduce = 0;
5694
5695 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005697 */
5698 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005703 break;
5704 ++reduce;
5705 ++p;
5706 }
5707 }
5708
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005710 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712 return FAIL;
5713 }
5714
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716 if (!evaluate)
5717 {
5718 *arg = p + 1;
5719 return OK;
5720 }
5721
5722 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005724 */
5725 str = alloc((unsigned)((p - *arg) - reduce));
5726 if (str == NULL)
5727 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 rettv->v_type = VAR_STRING;
5729 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005730
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005736 break;
5737 ++p;
5738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005740 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005742 *arg = p + 1;
5743
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 return OK;
5745}
5746
5747/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748 * Allocate a variable for a List and fill it from "*arg".
5749 * Return OK or FAIL.
5750 */
5751 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005752get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005754 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755 int evaluate;
5756{
Bram Moolenaar33570922005-01-25 22:26:29 +00005757 list_T *l = NULL;
5758 typval_T tv;
5759 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760
5761 if (evaluate)
5762 {
5763 l = list_alloc();
5764 if (l == NULL)
5765 return FAIL;
5766 }
5767
5768 *arg = skipwhite(*arg + 1);
5769 while (**arg != ']' && **arg != NUL)
5770 {
5771 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5772 goto failret;
5773 if (evaluate)
5774 {
5775 item = listitem_alloc();
5776 if (item != NULL)
5777 {
5778 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005779 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780 list_append(l, item);
5781 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005782 else
5783 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005784 }
5785
5786 if (**arg == ']')
5787 break;
5788 if (**arg != ',')
5789 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005791 goto failret;
5792 }
5793 *arg = skipwhite(*arg + 1);
5794 }
5795
5796 if (**arg != ']')
5797 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799failret:
5800 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005801 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802 return FAIL;
5803 }
5804
5805 *arg = skipwhite(*arg + 1);
5806 if (evaluate)
5807 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005808 rettv->v_type = VAR_LIST;
5809 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810 ++l->lv_refcount;
5811 }
5812
5813 return OK;
5814}
5815
5816/*
5817 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005818 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005819 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005820 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821list_alloc()
5822{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005823 list_T *l;
5824
5825 l = (list_T *)alloc_clear(sizeof(list_T));
5826 if (l != NULL)
5827 {
5828 /* Prepend the list to the list of lists for garbage collection. */
5829 if (first_list != NULL)
5830 first_list->lv_used_prev = l;
5831 l->lv_used_prev = NULL;
5832 l->lv_used_next = first_list;
5833 first_list = l;
5834 }
5835 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836}
5837
5838/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005839 * Allocate an empty list for a return value.
5840 * Returns OK or FAIL.
5841 */
5842 static int
5843rettv_list_alloc(rettv)
5844 typval_T *rettv;
5845{
5846 list_T *l = list_alloc();
5847
5848 if (l == NULL)
5849 return FAIL;
5850
5851 rettv->vval.v_list = l;
5852 rettv->v_type = VAR_LIST;
5853 ++l->lv_refcount;
5854 return OK;
5855}
5856
5857/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 * Unreference a list: decrement the reference count and free it when it
5859 * becomes zero.
5860 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005861 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005863 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005865 if (l != NULL && --l->lv_refcount <= 0)
5866 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867}
5868
5869/*
5870 * Free a list, including all items it points to.
5871 * Ignores the reference count.
5872 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005873 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005874list_free(l, recurse)
5875 list_T *l;
5876 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877{
Bram Moolenaar33570922005-01-25 22:26:29 +00005878 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005880 /* Remove the list from the list of lists for garbage collection. */
5881 if (l->lv_used_prev == NULL)
5882 first_list = l->lv_used_next;
5883 else
5884 l->lv_used_prev->lv_used_next = l->lv_used_next;
5885 if (l->lv_used_next != NULL)
5886 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5887
Bram Moolenaard9fba312005-06-26 22:34:35 +00005888 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005890 /* Remove the item before deleting it. */
5891 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005892 if (recurse || (item->li_tv.v_type != VAR_LIST
5893 && item->li_tv.v_type != VAR_DICT))
5894 clear_tv(&item->li_tv);
5895 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896 }
5897 vim_free(l);
5898}
5899
5900/*
5901 * Allocate a list item.
5902 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904listitem_alloc()
5905{
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907}
5908
5909/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005910 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911 */
5912 static void
5913listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005916 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917 vim_free(item);
5918}
5919
5920/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005921 * Remove a list item from a List and free it. Also clears the value.
5922 */
5923 static void
5924listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005925 list_T *l;
5926 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005927{
5928 list_remove(l, item, item);
5929 listitem_free(item);
5930}
5931
5932/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 * Get the number of items in a list.
5934 */
5935 static long
5936list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 if (l == NULL)
5940 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005941 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942}
5943
5944/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005945 * Return TRUE when two lists have exactly the same values.
5946 */
5947 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005948list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005949 list_T *l1;
5950 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005951 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005952 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005953{
Bram Moolenaar33570922005-01-25 22:26:29 +00005954 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005955
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005956 if (l1 == NULL || l2 == NULL)
5957 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005958 if (l1 == l2)
5959 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005960 if (list_len(l1) != list_len(l2))
5961 return FALSE;
5962
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005963 for (item1 = l1->lv_first, item2 = l2->lv_first;
5964 item1 != NULL && item2 != NULL;
5965 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005966 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005967 return FALSE;
5968 return item1 == NULL && item2 == NULL;
5969}
5970
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005971#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5972 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005973/*
5974 * Return the dictitem that an entry in a hashtable points to.
5975 */
5976 dictitem_T *
5977dict_lookup(hi)
5978 hashitem_T *hi;
5979{
5980 return HI2DI(hi);
5981}
5982#endif
5983
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005984/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005985 * Return TRUE when two dictionaries have exactly the same key/values.
5986 */
5987 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005988dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 dict_T *d1;
5990 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005991 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005992 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005993{
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 hashitem_T *hi;
5995 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005996 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005997
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005998 if (d1 == NULL || d2 == NULL)
5999 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006000 if (d1 == d2)
6001 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006002 if (dict_len(d1) != dict_len(d2))
6003 return FALSE;
6004
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006005 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006007 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006008 if (!HASHITEM_EMPTY(hi))
6009 {
6010 item2 = dict_find(d2, hi->hi_key, -1);
6011 if (item2 == NULL)
6012 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006013 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006014 return FALSE;
6015 --todo;
6016 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006017 }
6018 return TRUE;
6019}
6020
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006021static int tv_equal_recurse_limit;
6022
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006023/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006025 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006026 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006027 */
6028 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006029tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006030 typval_T *tv1;
6031 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032 int ic; /* ignore case */
6033 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006034{
6035 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006036 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006037 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006038 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006039
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006040 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006041 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006042
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006043 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006044 * recursiveness to a limit. We guess they are equal then.
6045 * A fixed limit has the problem of still taking an awful long time.
6046 * Reduce the limit every time running into it. That should work fine for
6047 * deeply linked structures that are not recursively linked and catch
6048 * recursiveness quickly. */
6049 if (!recursive)
6050 tv_equal_recurse_limit = 1000;
6051 if (recursive_cnt >= tv_equal_recurse_limit)
6052 {
6053 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006054 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006056
6057 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006058 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006059 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006060 ++recursive_cnt;
6061 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6062 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006063 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006064
6065 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006066 ++recursive_cnt;
6067 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6068 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006069 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006070
6071 case VAR_FUNC:
6072 return (tv1->vval.v_string != NULL
6073 && tv2->vval.v_string != NULL
6074 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6075
6076 case VAR_NUMBER:
6077 return tv1->vval.v_number == tv2->vval.v_number;
6078
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006079#ifdef FEAT_FLOAT
6080 case VAR_FLOAT:
6081 return tv1->vval.v_float == tv2->vval.v_float;
6082#endif
6083
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006084 case VAR_STRING:
6085 s1 = get_tv_string_buf(tv1, buf1);
6086 s2 = get_tv_string_buf(tv2, buf2);
6087 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006088 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006089
6090 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006091 return TRUE;
6092}
6093
6094/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095 * Locate item with index "n" in list "l" and return it.
6096 * A negative index is counted from the end; -1 is the last item.
6097 * Returns NULL when "n" is out of range.
6098 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006099 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006100list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006101 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102 long n;
6103{
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006105 long idx;
6106
6107 if (l == NULL)
6108 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006109
6110 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006112 n = l->lv_len + n;
6113
6114 /* Check for index out of range. */
6115 if (n < 0 || n >= l->lv_len)
6116 return NULL;
6117
6118 /* When there is a cached index may start search from there. */
6119 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006121 if (n < l->lv_idx / 2)
6122 {
6123 /* closest to the start of the list */
6124 item = l->lv_first;
6125 idx = 0;
6126 }
6127 else if (n > (l->lv_idx + l->lv_len) / 2)
6128 {
6129 /* closest to the end of the list */
6130 item = l->lv_last;
6131 idx = l->lv_len - 1;
6132 }
6133 else
6134 {
6135 /* closest to the cached index */
6136 item = l->lv_idx_item;
6137 idx = l->lv_idx;
6138 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139 }
6140 else
6141 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006142 if (n < l->lv_len / 2)
6143 {
6144 /* closest to the start of the list */
6145 item = l->lv_first;
6146 idx = 0;
6147 }
6148 else
6149 {
6150 /* closest to the end of the list */
6151 item = l->lv_last;
6152 idx = l->lv_len - 1;
6153 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006155
6156 while (n > idx)
6157 {
6158 /* search forward */
6159 item = item->li_next;
6160 ++idx;
6161 }
6162 while (n < idx)
6163 {
6164 /* search backward */
6165 item = item->li_prev;
6166 --idx;
6167 }
6168
6169 /* cache the used index */
6170 l->lv_idx = idx;
6171 l->lv_idx_item = item;
6172
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 return item;
6174}
6175
6176/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006177 * Get list item "l[idx]" as a number.
6178 */
6179 static long
6180list_find_nr(l, idx, errorp)
6181 list_T *l;
6182 long idx;
6183 int *errorp; /* set to TRUE when something wrong */
6184{
6185 listitem_T *li;
6186
6187 li = list_find(l, idx);
6188 if (li == NULL)
6189 {
6190 if (errorp != NULL)
6191 *errorp = TRUE;
6192 return -1L;
6193 }
6194 return get_tv_number_chk(&li->li_tv, errorp);
6195}
6196
6197/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006198 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6199 */
6200 char_u *
6201list_find_str(l, idx)
6202 list_T *l;
6203 long idx;
6204{
6205 listitem_T *li;
6206
6207 li = list_find(l, idx - 1);
6208 if (li == NULL)
6209 {
6210 EMSGN(_(e_listidx), idx);
6211 return NULL;
6212 }
6213 return get_tv_string(&li->li_tv);
6214}
6215
6216/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006217 * Locate "item" list "l" and return its index.
6218 * Returns -1 when "item" is not in the list.
6219 */
6220 static long
6221list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006222 list_T *l;
6223 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006224{
6225 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006226 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006227
6228 if (l == NULL)
6229 return -1;
6230 idx = 0;
6231 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6232 ++idx;
6233 if (li == NULL)
6234 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006235 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006236}
6237
6238/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006239 * Append item "item" to the end of list "l".
6240 */
6241 static void
6242list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 list_T *l;
6244 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006245{
6246 if (l->lv_last == NULL)
6247 {
6248 /* empty list */
6249 l->lv_first = item;
6250 l->lv_last = item;
6251 item->li_prev = NULL;
6252 }
6253 else
6254 {
6255 l->lv_last->li_next = item;
6256 item->li_prev = l->lv_last;
6257 l->lv_last = item;
6258 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006259 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 item->li_next = NULL;
6261}
6262
6263/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006264 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006265 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006267 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006268list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006269 list_T *l;
6270 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006272 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006273
Bram Moolenaar05159a02005-02-26 23:04:13 +00006274 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006276 copy_tv(tv, &li->li_tv);
6277 list_append(l, li);
6278 return OK;
6279}
6280
6281/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006282 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006283 * Return FAIL when out of memory.
6284 */
6285 int
6286list_append_dict(list, dict)
6287 list_T *list;
6288 dict_T *dict;
6289{
6290 listitem_T *li = listitem_alloc();
6291
6292 if (li == NULL)
6293 return FAIL;
6294 li->li_tv.v_type = VAR_DICT;
6295 li->li_tv.v_lock = 0;
6296 li->li_tv.vval.v_dict = dict;
6297 list_append(list, li);
6298 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006299 return OK;
6300}
6301
6302/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006303 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006304 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006305 * Returns FAIL when out of memory.
6306 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006307 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006308list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006309 list_T *l;
6310 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006311 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006312{
6313 listitem_T *li = listitem_alloc();
6314
6315 if (li == NULL)
6316 return FAIL;
6317 list_append(l, li);
6318 li->li_tv.v_type = VAR_STRING;
6319 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006320 if (str == NULL)
6321 li->li_tv.vval.v_string = NULL;
6322 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006323 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006324 return FAIL;
6325 return OK;
6326}
6327
6328/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006329 * Append "n" to list "l".
6330 * Returns FAIL when out of memory.
6331 */
6332 static int
6333list_append_number(l, n)
6334 list_T *l;
6335 varnumber_T n;
6336{
6337 listitem_T *li;
6338
6339 li = listitem_alloc();
6340 if (li == NULL)
6341 return FAIL;
6342 li->li_tv.v_type = VAR_NUMBER;
6343 li->li_tv.v_lock = 0;
6344 li->li_tv.vval.v_number = n;
6345 list_append(l, li);
6346 return OK;
6347}
6348
6349/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351 * If "item" is NULL append at the end.
6352 * Return FAIL when out of memory.
6353 */
6354 static int
6355list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 list_T *l;
6357 typval_T *tv;
6358 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006359{
Bram Moolenaar33570922005-01-25 22:26:29 +00006360 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006361
6362 if (ni == NULL)
6363 return FAIL;
6364 copy_tv(tv, &ni->li_tv);
6365 if (item == NULL)
6366 /* Append new item at end of list. */
6367 list_append(l, ni);
6368 else
6369 {
6370 /* Insert new item before existing item. */
6371 ni->li_prev = item->li_prev;
6372 ni->li_next = item;
6373 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006374 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006375 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006376 ++l->lv_idx;
6377 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006378 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006379 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006380 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006381 l->lv_idx_item = NULL;
6382 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006383 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006384 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006385 }
6386 return OK;
6387}
6388
6389/*
6390 * Extend "l1" with "l2".
6391 * If "bef" is NULL append at the end, otherwise insert before this item.
6392 * Returns FAIL when out of memory.
6393 */
6394 static int
6395list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 list_T *l1;
6397 list_T *l2;
6398 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399{
Bram Moolenaar33570922005-01-25 22:26:29 +00006400 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006401 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006403 /* We also quit the loop when we have inserted the original item count of
6404 * the list, avoid a hang when we extend a list with itself. */
6405 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6407 return FAIL;
6408 return OK;
6409}
6410
6411/*
6412 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6413 * Return FAIL when out of memory.
6414 */
6415 static int
6416list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006417 list_T *l1;
6418 list_T *l2;
6419 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006420{
Bram Moolenaar33570922005-01-25 22:26:29 +00006421 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006423 if (l1 == NULL || l2 == NULL)
6424 return FAIL;
6425
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006427 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428 if (l == NULL)
6429 return FAIL;
6430 tv->v_type = VAR_LIST;
6431 tv->vval.v_list = l;
6432
6433 /* append all items from the second list */
6434 return list_extend(l, l2, NULL);
6435}
6436
6437/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006438 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006440 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006441 * Returns NULL when out of memory.
6442 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006444list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006445 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006446 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006447 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006448{
Bram Moolenaar33570922005-01-25 22:26:29 +00006449 list_T *copy;
6450 listitem_T *item;
6451 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006452
6453 if (orig == NULL)
6454 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455
6456 copy = list_alloc();
6457 if (copy != NULL)
6458 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006459 if (copyID != 0)
6460 {
6461 /* Do this before adding the items, because one of the items may
6462 * refer back to this list. */
6463 orig->lv_copyID = copyID;
6464 orig->lv_copylist = copy;
6465 }
6466 for (item = orig->lv_first; item != NULL && !got_int;
6467 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006468 {
6469 ni = listitem_alloc();
6470 if (ni == NULL)
6471 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006472 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006473 {
6474 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6475 {
6476 vim_free(ni);
6477 break;
6478 }
6479 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006480 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006481 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006482 list_append(copy, ni);
6483 }
6484 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006485 if (item != NULL)
6486 {
6487 list_unref(copy);
6488 copy = NULL;
6489 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490 }
6491
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492 return copy;
6493}
6494
6495/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006497 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006498 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006500list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 list_T *l;
6502 listitem_T *item;
6503 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504{
Bram Moolenaar33570922005-01-25 22:26:29 +00006505 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 /* notify watchers */
6508 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006510 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511 list_fix_watch(l, ip);
6512 if (ip == item2)
6513 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515
6516 if (item2->li_next == NULL)
6517 l->lv_last = item->li_prev;
6518 else
6519 item2->li_next->li_prev = item->li_prev;
6520 if (item->li_prev == NULL)
6521 l->lv_first = item2->li_next;
6522 else
6523 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006524 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525}
6526
6527/*
6528 * Return an allocated string with the string representation of a list.
6529 * May return NULL.
6530 */
6531 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006532list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006533 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006534 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006535{
6536 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537
6538 if (tv->vval.v_list == NULL)
6539 return NULL;
6540 ga_init2(&ga, (int)sizeof(char), 80);
6541 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006542 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006543 {
6544 vim_free(ga.ga_data);
6545 return NULL;
6546 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547 ga_append(&ga, ']');
6548 ga_append(&ga, NUL);
6549 return (char_u *)ga.ga_data;
6550}
6551
6552/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006553 * Join list "l" into a string in "*gap", using separator "sep".
6554 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006555 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006556 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006557 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006558list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006559 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006561 char_u *sep;
6562 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006563 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006564{
6565 int first = TRUE;
6566 char_u *tofree;
6567 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006568 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006569 char_u *s;
6570
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006571 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006572 {
6573 if (first)
6574 first = FALSE;
6575 else
6576 ga_concat(gap, sep);
6577
6578 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006579 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006580 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006581 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006582 if (s != NULL)
6583 ga_concat(gap, s);
6584 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006585 if (s == NULL)
6586 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006587 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006588 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006589 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006590}
6591
6592/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006593 * Garbage collection for lists and dictionaries.
6594 *
6595 * We use reference counts to be able to free most items right away when they
6596 * are no longer used. But for composite items it's possible that it becomes
6597 * unused while the reference count is > 0: When there is a recursive
6598 * reference. Example:
6599 * :let l = [1, 2, 3]
6600 * :let d = {9: l}
6601 * :let l[1] = d
6602 *
6603 * Since this is quite unusual we handle this with garbage collection: every
6604 * once in a while find out which lists and dicts are not referenced from any
6605 * variable.
6606 *
6607 * Here is a good reference text about garbage collection (refers to Python
6608 * but it applies to all reference-counting mechanisms):
6609 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006610 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006611
6612/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006613 * Do garbage collection for lists and dicts.
6614 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006615 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006616 int
6617garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006618{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006619 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006620 buf_T *buf;
6621 win_T *wp;
6622 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006623 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006624 int did_free;
6625 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006626#ifdef FEAT_WINDOWS
6627 tabpage_T *tp;
6628#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006629
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006630 /* Only do this once. */
6631 want_garbage_collect = FALSE;
6632 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006633 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006634
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006635 /* We advance by two because we add one for items referenced through
6636 * previous_funccal. */
6637 current_copyID += COPYID_INC;
6638 copyID = current_copyID;
6639
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006640 /*
6641 * 1. Go through all accessible variables and mark all lists and dicts
6642 * with copyID.
6643 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006644
6645 /* Don't free variables in the previous_funccal list unless they are only
6646 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006647 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006648 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6649 {
6650 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6651 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6652 }
6653
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006654 /* script-local variables */
6655 for (i = 1; i <= ga_scripts.ga_len; ++i)
6656 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6657
6658 /* buffer-local variables */
6659 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6660 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6661
6662 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006663 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006664 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6665
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006666#ifdef FEAT_WINDOWS
6667 /* tabpage-local variables */
6668 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6669 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6670#endif
6671
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006672 /* global variables */
6673 set_ref_in_ht(&globvarht, copyID);
6674
6675 /* function-local variables */
6676 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6677 {
6678 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6679 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6680 }
6681
Bram Moolenaard812df62008-11-09 12:46:09 +00006682 /* v: vars */
6683 set_ref_in_ht(&vimvarht, copyID);
6684
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006685 /*
6686 * 2. Free lists and dictionaries that are not referenced.
6687 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006688 did_free = free_unref_items(copyID);
6689
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006690 /*
6691 * 3. Check if any funccal can be freed now.
6692 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006693 for (pfc = &previous_funccal; *pfc != NULL; )
6694 {
6695 if (can_free_funccal(*pfc, copyID))
6696 {
6697 fc = *pfc;
6698 *pfc = fc->caller;
6699 free_funccal(fc, TRUE);
6700 did_free = TRUE;
6701 did_free_funccal = TRUE;
6702 }
6703 else
6704 pfc = &(*pfc)->caller;
6705 }
6706 if (did_free_funccal)
6707 /* When a funccal was freed some more items might be garbage
6708 * collected, so run again. */
6709 (void)garbage_collect();
6710
6711 return did_free;
6712}
6713
6714/*
6715 * Free lists and dictionaries that are no longer referenced.
6716 */
6717 static int
6718free_unref_items(copyID)
6719 int copyID;
6720{
6721 dict_T *dd;
6722 list_T *ll;
6723 int did_free = FALSE;
6724
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006725 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006726 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006727 */
6728 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006729 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006730 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006731 /* Free the Dictionary and ordinary items it contains, but don't
6732 * recurse into Lists and Dictionaries, they will be in the list
6733 * of dicts or list of lists. */
6734 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006735 did_free = TRUE;
6736
6737 /* restart, next dict may also have been freed */
6738 dd = first_dict;
6739 }
6740 else
6741 dd = dd->dv_used_next;
6742
6743 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006744 * Go through the list of lists and free items without the copyID.
6745 * But don't free a list that has a watcher (used in a for loop), these
6746 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747 */
6748 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006749 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6750 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006751 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006752 /* Free the List and ordinary items it contains, but don't recurse
6753 * into Lists and Dictionaries, they will be in the list of dicts
6754 * or list of lists. */
6755 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006756 did_free = TRUE;
6757
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006758 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006759 ll = first_list;
6760 }
6761 else
6762 ll = ll->lv_used_next;
6763
6764 return did_free;
6765}
6766
6767/*
6768 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6769 */
6770 static void
6771set_ref_in_ht(ht, copyID)
6772 hashtab_T *ht;
6773 int copyID;
6774{
6775 int todo;
6776 hashitem_T *hi;
6777
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006778 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006779 for (hi = ht->ht_array; todo > 0; ++hi)
6780 if (!HASHITEM_EMPTY(hi))
6781 {
6782 --todo;
6783 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6784 }
6785}
6786
6787/*
6788 * Mark all lists and dicts referenced through list "l" with "copyID".
6789 */
6790 static void
6791set_ref_in_list(l, copyID)
6792 list_T *l;
6793 int copyID;
6794{
6795 listitem_T *li;
6796
6797 for (li = l->lv_first; li != NULL; li = li->li_next)
6798 set_ref_in_item(&li->li_tv, copyID);
6799}
6800
6801/*
6802 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6803 */
6804 static void
6805set_ref_in_item(tv, copyID)
6806 typval_T *tv;
6807 int copyID;
6808{
6809 dict_T *dd;
6810 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006811
6812 switch (tv->v_type)
6813 {
6814 case VAR_DICT:
6815 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006816 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006817 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006818 /* Didn't see this dict yet. */
6819 dd->dv_copyID = copyID;
6820 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006821 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006822 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006823
6824 case VAR_LIST:
6825 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006826 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006827 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006828 /* Didn't see this list yet. */
6829 ll->lv_copyID = copyID;
6830 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006831 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006833 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006835}
6836
6837/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006838 * Allocate an empty header for a dictionary.
6839 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006840 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006841dict_alloc()
6842{
Bram Moolenaar33570922005-01-25 22:26:29 +00006843 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006844
Bram Moolenaar33570922005-01-25 22:26:29 +00006845 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006846 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006847 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006848 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006849 if (first_dict != NULL)
6850 first_dict->dv_used_prev = d;
6851 d->dv_used_next = first_dict;
6852 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006853 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854
Bram Moolenaar33570922005-01-25 22:26:29 +00006855 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006856 d->dv_lock = 0;
6857 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006858 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006859 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006860 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006861}
6862
6863/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006864 * Allocate an empty dict for a return value.
6865 * Returns OK or FAIL.
6866 */
6867 static int
6868rettv_dict_alloc(rettv)
6869 typval_T *rettv;
6870{
6871 dict_T *d = dict_alloc();
6872
6873 if (d == NULL)
6874 return FAIL;
6875
6876 rettv->vval.v_dict = d;
6877 rettv->v_type = VAR_DICT;
6878 ++d->dv_refcount;
6879 return OK;
6880}
6881
6882
6883/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006884 * Unreference a Dictionary: decrement the reference count and free it when it
6885 * becomes zero.
6886 */
6887 static void
6888dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006889 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006890{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006891 if (d != NULL && --d->dv_refcount <= 0)
6892 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006893}
6894
6895/*
6896 * Free a Dictionary, including all items it contains.
6897 * Ignores the reference count.
6898 */
6899 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006900dict_free(d, recurse)
6901 dict_T *d;
6902 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006903{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006904 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006905 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006906 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006907
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908 /* Remove the dict from the list of dicts for garbage collection. */
6909 if (d->dv_used_prev == NULL)
6910 first_dict = d->dv_used_next;
6911 else
6912 d->dv_used_prev->dv_used_next = d->dv_used_next;
6913 if (d->dv_used_next != NULL)
6914 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6915
6916 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006917 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006918 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006920 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006921 if (!HASHITEM_EMPTY(hi))
6922 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006923 /* Remove the item before deleting it, just in case there is
6924 * something recursive causing trouble. */
6925 di = HI2DI(hi);
6926 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006927 if (recurse || (di->di_tv.v_type != VAR_LIST
6928 && di->di_tv.v_type != VAR_DICT))
6929 clear_tv(&di->di_tv);
6930 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006931 --todo;
6932 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006933 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006934 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006935 vim_free(d);
6936}
6937
6938/*
6939 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006940 * The "key" is copied to the new item.
6941 * Note that the value of the item "di_tv" still needs to be initialized!
6942 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006943 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006944 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006945dictitem_alloc(key)
6946 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006947{
Bram Moolenaar33570922005-01-25 22:26:29 +00006948 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006949
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006950 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006951 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006952 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006953 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006954 di->di_flags = 0;
6955 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006956 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006957}
6958
6959/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006960 * Make a copy of a Dictionary item.
6961 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006962 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006963dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006964 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006965{
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006967
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006968 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6969 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006970 if (di != NULL)
6971 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006972 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006973 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006974 copy_tv(&org->di_tv, &di->di_tv);
6975 }
6976 return di;
6977}
6978
6979/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006980 * Remove item "item" from Dictionary "dict" and free it.
6981 */
6982 static void
6983dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 dict_T *dict;
6985 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006986{
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006988
Bram Moolenaar33570922005-01-25 22:26:29 +00006989 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006990 if (HASHITEM_EMPTY(hi))
6991 EMSG2(_(e_intern2), "dictitem_remove()");
6992 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006993 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006994 dictitem_free(item);
6995}
6996
6997/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006998 * Free a dict item. Also clears the value.
6999 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007000 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007001dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007002 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007003{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007004 clear_tv(&item->di_tv);
7005 vim_free(item);
7006}
7007
7008/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007009 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7010 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007011 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007012 * Returns NULL when out of memory.
7013 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007014 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007015dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007016 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007017 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007018 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007019{
Bram Moolenaar33570922005-01-25 22:26:29 +00007020 dict_T *copy;
7021 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007022 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007023 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007024
7025 if (orig == NULL)
7026 return NULL;
7027
7028 copy = dict_alloc();
7029 if (copy != NULL)
7030 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007031 if (copyID != 0)
7032 {
7033 orig->dv_copyID = copyID;
7034 orig->dv_copydict = copy;
7035 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007036 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007037 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007038 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007039 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007040 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007041 --todo;
7042
7043 di = dictitem_alloc(hi->hi_key);
7044 if (di == NULL)
7045 break;
7046 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007047 {
7048 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7049 copyID) == FAIL)
7050 {
7051 vim_free(di);
7052 break;
7053 }
7054 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007055 else
7056 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7057 if (dict_add(copy, di) == FAIL)
7058 {
7059 dictitem_free(di);
7060 break;
7061 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007062 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007063 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007064
Bram Moolenaare9a41262005-01-15 22:18:47 +00007065 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007066 if (todo > 0)
7067 {
7068 dict_unref(copy);
7069 copy = NULL;
7070 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007071 }
7072
7073 return copy;
7074}
7075
7076/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007077 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007078 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007079 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007080 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007082 dict_T *d;
7083 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007084{
Bram Moolenaar33570922005-01-25 22:26:29 +00007085 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007086}
7087
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007089 * Add a number or string entry to dictionary "d".
7090 * When "str" is NULL use number "nr", otherwise use "str".
7091 * Returns FAIL when out of memory and when key already exists.
7092 */
7093 int
7094dict_add_nr_str(d, key, nr, str)
7095 dict_T *d;
7096 char *key;
7097 long nr;
7098 char_u *str;
7099{
7100 dictitem_T *item;
7101
7102 item = dictitem_alloc((char_u *)key);
7103 if (item == NULL)
7104 return FAIL;
7105 item->di_tv.v_lock = 0;
7106 if (str == NULL)
7107 {
7108 item->di_tv.v_type = VAR_NUMBER;
7109 item->di_tv.vval.v_number = nr;
7110 }
7111 else
7112 {
7113 item->di_tv.v_type = VAR_STRING;
7114 item->di_tv.vval.v_string = vim_strsave(str);
7115 }
7116 if (dict_add(d, item) == FAIL)
7117 {
7118 dictitem_free(item);
7119 return FAIL;
7120 }
7121 return OK;
7122}
7123
7124/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007125 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007126 * Returns FAIL when out of memory and when key already exists.
7127 */
7128 int
7129dict_add_list(d, key, list)
7130 dict_T *d;
7131 char *key;
7132 list_T *list;
7133{
7134 dictitem_T *item;
7135
7136 item = dictitem_alloc((char_u *)key);
7137 if (item == NULL)
7138 return FAIL;
7139 item->di_tv.v_lock = 0;
7140 item->di_tv.v_type = VAR_LIST;
7141 item->di_tv.vval.v_list = list;
7142 if (dict_add(d, item) == FAIL)
7143 {
7144 dictitem_free(item);
7145 return FAIL;
7146 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007147 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007148 return OK;
7149}
7150
7151/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 * Get the number of items in a Dictionary.
7153 */
7154 static long
7155dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007158 if (d == NULL)
7159 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007160 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007161}
7162
7163/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007164 * Find item "key[len]" in Dictionary "d".
7165 * If "len" is negative use strlen(key).
7166 * Returns NULL when not found.
7167 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007168 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007169dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007170 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 char_u *key;
7172 int len;
7173{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007174#define AKEYLEN 200
7175 char_u buf[AKEYLEN];
7176 char_u *akey;
7177 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007178 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007180 if (len < 0)
7181 akey = key;
7182 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007183 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 tofree = akey = vim_strnsave(key, len);
7185 if (akey == NULL)
7186 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007187 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188 else
7189 {
7190 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007191 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007192 akey = buf;
7193 }
7194
Bram Moolenaar33570922005-01-25 22:26:29 +00007195 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 vim_free(tofree);
7197 if (HASHITEM_EMPTY(hi))
7198 return NULL;
7199 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200}
7201
7202/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007203 * Get a string item from a dictionary.
7204 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007205 * Returns NULL if the entry doesn't exist or out of memory.
7206 */
7207 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007208get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007209 dict_T *d;
7210 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007211 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007212{
7213 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007214 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007215
7216 di = dict_find(d, key, -1);
7217 if (di == NULL)
7218 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007219 s = get_tv_string(&di->di_tv);
7220 if (save && s != NULL)
7221 s = vim_strsave(s);
7222 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007223}
7224
7225/*
7226 * Get a number item from a dictionary.
7227 * Returns 0 if the entry doesn't exist or out of memory.
7228 */
7229 long
7230get_dict_number(d, key)
7231 dict_T *d;
7232 char_u *key;
7233{
7234 dictitem_T *di;
7235
7236 di = dict_find(d, key, -1);
7237 if (di == NULL)
7238 return 0;
7239 return get_tv_number(&di->di_tv);
7240}
7241
7242/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243 * Return an allocated string with the string representation of a Dictionary.
7244 * May return NULL.
7245 */
7246 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007247dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007249 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250{
7251 garray_T ga;
7252 int first = TRUE;
7253 char_u *tofree;
7254 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007257 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007258 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007260 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261 return NULL;
7262 ga_init2(&ga, (int)sizeof(char), 80);
7263 ga_append(&ga, '{');
7264
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007265 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007266 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007268 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 --todo;
7271
7272 if (first)
7273 first = FALSE;
7274 else
7275 ga_concat(&ga, (char_u *)", ");
7276
7277 tofree = string_quote(hi->hi_key, FALSE);
7278 if (tofree != NULL)
7279 {
7280 ga_concat(&ga, tofree);
7281 vim_free(tofree);
7282 }
7283 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007284 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 if (s != NULL)
7286 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007287 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007288 if (s == NULL)
7289 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007292 if (todo > 0)
7293 {
7294 vim_free(ga.ga_data);
7295 return NULL;
7296 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297
7298 ga_append(&ga, '}');
7299 ga_append(&ga, NUL);
7300 return (char_u *)ga.ga_data;
7301}
7302
7303/*
7304 * Allocate a variable for a Dictionary and fill it from "*arg".
7305 * Return OK or FAIL. Returns NOTDONE for {expr}.
7306 */
7307 static int
7308get_dict_tv(arg, rettv, evaluate)
7309 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 int evaluate;
7312{
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 dict_T *d = NULL;
7314 typval_T tvkey;
7315 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007316 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007318 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007320
7321 /*
7322 * First check if it's not a curly-braces thing: {expr}.
7323 * Must do this without evaluating, otherwise a function may be called
7324 * twice. Unfortunately this means we need to call eval1() twice for the
7325 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007327 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328 if (*start != '}')
7329 {
7330 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7331 return FAIL;
7332 if (*start == '}')
7333 return NOTDONE;
7334 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335
7336 if (evaluate)
7337 {
7338 d = dict_alloc();
7339 if (d == NULL)
7340 return FAIL;
7341 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342 tvkey.v_type = VAR_UNKNOWN;
7343 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344
7345 *arg = skipwhite(*arg + 1);
7346 while (**arg != '}' && **arg != NUL)
7347 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007348 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349 goto failret;
7350 if (**arg != ':')
7351 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007352 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354 goto failret;
7355 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007356 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007358 key = get_tv_string_buf_chk(&tvkey, buf);
7359 if (key == NULL || *key == NUL)
7360 {
7361 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7362 if (key != NULL)
7363 EMSG(_(e_emptykey));
7364 clear_tv(&tvkey);
7365 goto failret;
7366 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007368
7369 *arg = skipwhite(*arg + 1);
7370 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7371 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007372 if (evaluate)
7373 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007374 goto failret;
7375 }
7376 if (evaluate)
7377 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007378 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379 if (item != NULL)
7380 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007381 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007383 clear_tv(&tv);
7384 goto failret;
7385 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007386 item = dictitem_alloc(key);
7387 clear_tv(&tvkey);
7388 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007389 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007391 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392 if (dict_add(d, item) == FAIL)
7393 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 }
7395 }
7396
7397 if (**arg == '}')
7398 break;
7399 if (**arg != ',')
7400 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007401 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 goto failret;
7403 }
7404 *arg = skipwhite(*arg + 1);
7405 }
7406
7407 if (**arg != '}')
7408 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007409 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410failret:
7411 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007412 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 return FAIL;
7414 }
7415
7416 *arg = skipwhite(*arg + 1);
7417 if (evaluate)
7418 {
7419 rettv->v_type = VAR_DICT;
7420 rettv->vval.v_dict = d;
7421 ++d->dv_refcount;
7422 }
7423
7424 return OK;
7425}
7426
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007428 * Return a string with the string representation of a variable.
7429 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007430 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007431 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007432 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007433 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007434 */
7435 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007436echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007438 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007439 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007440 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007441{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007442 static int recurse = 0;
7443 char_u *r = NULL;
7444
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007446 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007447 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007448 *tofree = NULL;
7449 return NULL;
7450 }
7451 ++recurse;
7452
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007453 switch (tv->v_type)
7454 {
7455 case VAR_FUNC:
7456 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 r = tv->vval.v_string;
7458 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007459
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007460 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007461 if (tv->vval.v_list == NULL)
7462 {
7463 *tofree = NULL;
7464 r = NULL;
7465 }
7466 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7467 {
7468 *tofree = NULL;
7469 r = (char_u *)"[...]";
7470 }
7471 else
7472 {
7473 tv->vval.v_list->lv_copyID = copyID;
7474 *tofree = list2string(tv, copyID);
7475 r = *tofree;
7476 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007478
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007480 if (tv->vval.v_dict == NULL)
7481 {
7482 *tofree = NULL;
7483 r = NULL;
7484 }
7485 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7486 {
7487 *tofree = NULL;
7488 r = (char_u *)"{...}";
7489 }
7490 else
7491 {
7492 tv->vval.v_dict->dv_copyID = copyID;
7493 *tofree = dict2string(tv, copyID);
7494 r = *tofree;
7495 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007496 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007497
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007498 case VAR_STRING:
7499 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007500 *tofree = NULL;
7501 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007502 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007503
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007504#ifdef FEAT_FLOAT
7505 case VAR_FLOAT:
7506 *tofree = NULL;
7507 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7508 r = numbuf;
7509 break;
7510#endif
7511
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007512 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007513 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007514 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007515 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007516
7517 --recurse;
7518 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007519}
7520
7521/*
7522 * Return a string with the string representation of a variable.
7523 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7524 * "numbuf" is used for a number.
7525 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007526 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007527 */
7528 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007529tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007531 char_u **tofree;
7532 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007533 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007534{
7535 switch (tv->v_type)
7536 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007537 case VAR_FUNC:
7538 *tofree = string_quote(tv->vval.v_string, TRUE);
7539 return *tofree;
7540 case VAR_STRING:
7541 *tofree = string_quote(tv->vval.v_string, FALSE);
7542 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007543#ifdef FEAT_FLOAT
7544 case VAR_FLOAT:
7545 *tofree = NULL;
7546 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7547 return numbuf;
7548#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007549 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007550 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007553 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007554 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007555 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007556 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007557}
7558
7559/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 * Return string "str" in ' quotes, doubling ' characters.
7561 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007563 */
7564 static char_u *
7565string_quote(str, function)
7566 char_u *str;
7567 int function;
7568{
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007570 char_u *p, *r, *s;
7571
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 len = (function ? 13 : 3);
7573 if (str != NULL)
7574 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007575 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 for (p = str; *p != NUL; mb_ptr_adv(p))
7577 if (*p == '\'')
7578 ++len;
7579 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007580 s = r = alloc(len);
7581 if (r != NULL)
7582 {
7583 if (function)
7584 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007586 r += 10;
7587 }
7588 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007590 if (str != NULL)
7591 for (p = str; *p != NUL; )
7592 {
7593 if (*p == '\'')
7594 *r++ = '\'';
7595 MB_COPY_CHAR(p, r);
7596 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007598 if (function)
7599 *r++ = ')';
7600 *r++ = NUL;
7601 }
7602 return s;
7603}
7604
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007605#ifdef FEAT_FLOAT
7606/*
7607 * Convert the string "text" to a floating point number.
7608 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7609 * this always uses a decimal point.
7610 * Returns the length of the text that was consumed.
7611 */
7612 static int
7613string2float(text, value)
7614 char_u *text;
7615 float_T *value; /* result stored here */
7616{
7617 char *s = (char *)text;
7618 float_T f;
7619
7620 f = strtod(s, &s);
7621 *value = f;
7622 return (int)((char_u *)s - text);
7623}
7624#endif
7625
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 * Get the value of an environment variable.
7628 * "arg" is pointing to the '$'. It is advanced to after the name.
7629 * If the environment variable was not set, silently assume it is empty.
7630 * Always return OK.
7631 */
7632 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007633get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 int evaluate;
7637{
7638 char_u *string = NULL;
7639 int len;
7640 int cc;
7641 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007642 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643
7644 ++*arg;
7645 name = *arg;
7646 len = get_env_len(arg);
7647 if (evaluate)
7648 {
7649 if (len != 0)
7650 {
7651 cc = name[len];
7652 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007653 /* first try vim_getenv(), fast for normal environment vars */
7654 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007656 {
7657 if (!mustfree)
7658 string = vim_strsave(string);
7659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 else
7661 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007662 if (mustfree)
7663 vim_free(string);
7664
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 /* next try expanding things like $VIM and ${HOME} */
7666 string = expand_env_save(name - 1);
7667 if (string != NULL && *string == '$')
7668 {
7669 vim_free(string);
7670 string = NULL;
7671 }
7672 }
7673 name[len] = cc;
7674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007675 rettv->v_type = VAR_STRING;
7676 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
7678
7679 return OK;
7680}
7681
7682/*
7683 * Array with names and number of arguments of all internal functions
7684 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7685 */
7686static struct fst
7687{
7688 char *f_name; /* function name */
7689 char f_min_argc; /* minimal number of arguments */
7690 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007692 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693} functions[] =
7694{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007695#ifdef FEAT_FLOAT
7696 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007697 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007698#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007699 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 {"append", 2, 2, f_append},
7701 {"argc", 0, 0, f_argc},
7702 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007703 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007704#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007705 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007706 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007707 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007710 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {"bufexists", 1, 1, f_bufexists},
7712 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7713 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7714 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7715 {"buflisted", 1, 1, f_buflisted},
7716 {"bufloaded", 1, 1, f_bufloaded},
7717 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007718 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {"bufwinnr", 1, 1, f_bufwinnr},
7720 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007721 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007723#ifdef FEAT_FLOAT
7724 {"ceil", 1, 1, f_ceil},
7725#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007726 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 {"char2nr", 1, 1, f_char2nr},
7728 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007729 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007731#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007732 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007733 {"complete_add", 1, 1, f_complete_add},
7734 {"complete_check", 0, 0, f_complete_check},
7735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007737 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007738#ifdef FEAT_FLOAT
7739 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007740 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007741#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007742 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007744 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007745 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 {"delete", 1, 1, f_delete},
7747 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007748 {"diff_filler", 1, 1, f_diff_filler},
7749 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007750 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007752 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 {"eventhandler", 0, 0, f_eventhandler},
7754 {"executable", 1, 1, f_executable},
7755 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007756#ifdef FEAT_FLOAT
7757 {"exp", 1, 1, f_exp},
7758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007760 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007761 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7763 {"filereadable", 1, 1, f_filereadable},
7764 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007765 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007766 {"finddir", 1, 3, f_finddir},
7767 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007768#ifdef FEAT_FLOAT
7769 {"float2nr", 1, 1, f_float2nr},
7770 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007771 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007772#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007773 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 {"fnamemodify", 2, 2, f_fnamemodify},
7775 {"foldclosed", 1, 1, f_foldclosed},
7776 {"foldclosedend", 1, 1, f_foldclosedend},
7777 {"foldlevel", 1, 1, f_foldlevel},
7778 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007779 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007781 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007782 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007783 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007784 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 {"getbufvar", 2, 2, f_getbufvar},
7786 {"getchar", 0, 1, f_getchar},
7787 {"getcharmod", 0, 0, f_getcharmod},
7788 {"getcmdline", 0, 0, f_getcmdline},
7789 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007790 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007792 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007793 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 {"getfsize", 1, 1, f_getfsize},
7795 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007796 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007797 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007798 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007799 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007800 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007801 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007802 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007803 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007805 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007806 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 {"getwinposx", 0, 0, f_getwinposx},
7808 {"getwinposy", 0, 0, f_getwinposy},
7809 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007810 {"glob", 1, 2, f_glob},
7811 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007813 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007814 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007815 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7817 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7818 {"histadd", 2, 2, f_histadd},
7819 {"histdel", 1, 2, f_histdel},
7820 {"histget", 1, 2, f_histget},
7821 {"histnr", 1, 1, f_histnr},
7822 {"hlID", 1, 1, f_hlID},
7823 {"hlexists", 1, 1, f_hlexists},
7824 {"hostname", 0, 0, f_hostname},
7825 {"iconv", 3, 3, f_iconv},
7826 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007827 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007828 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007830 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {"inputrestore", 0, 0, f_inputrestore},
7832 {"inputsave", 0, 0, f_inputsave},
7833 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007834 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007836 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007837 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007838 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007839 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007841 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"libcall", 3, 3, f_libcall},
7843 {"libcallnr", 3, 3, f_libcallnr},
7844 {"line", 1, 1, f_line},
7845 {"line2byte", 1, 1, f_line2byte},
7846 {"lispindent", 1, 1, f_lispindent},
7847 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007848#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007849 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007850 {"log10", 1, 1, f_log10},
7851#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007852 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007853 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007854 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007855 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007856 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007857 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007858 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007859 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007860 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007861 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007862 {"max", 1, 1, f_max},
7863 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007864#ifdef vim_mkdir
7865 {"mkdir", 1, 3, f_mkdir},
7866#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007867 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007868#ifdef FEAT_MZSCHEME
7869 {"mzeval", 1, 1, f_mzeval},
7870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {"nextnonblank", 1, 1, f_nextnonblank},
7872 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007873 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007874#ifdef FEAT_FLOAT
7875 {"pow", 2, 2, f_pow},
7876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007878 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007879 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007880 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007881 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007882 {"reltime", 0, 2, f_reltime},
7883 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"remote_expr", 2, 3, f_remote_expr},
7885 {"remote_foreground", 1, 1, f_remote_foreground},
7886 {"remote_peek", 1, 2, f_remote_peek},
7887 {"remote_read", 1, 1, f_remote_read},
7888 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007889 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007891 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007893 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007894#ifdef FEAT_FLOAT
7895 {"round", 1, 1, f_round},
7896#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007897 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007898 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007899 {"searchpair", 3, 7, f_searchpair},
7900 {"searchpairpos", 3, 7, f_searchpairpos},
7901 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {"server2client", 2, 2, f_server2client},
7903 {"serverlist", 0, 0, f_serverlist},
7904 {"setbufvar", 3, 3, f_setbufvar},
7905 {"setcmdpos", 1, 1, f_setcmdpos},
7906 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007907 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007908 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007909 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007910 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007912 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007913 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007915 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007917#ifdef FEAT_FLOAT
7918 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007919 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007920#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007921 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007922 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007923 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007924 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007925 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007926#ifdef FEAT_FLOAT
7927 {"sqrt", 1, 1, f_sqrt},
7928 {"str2float", 1, 1, f_str2float},
7929#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007930 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007931 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007932 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933#ifdef HAVE_STRFTIME
7934 {"strftime", 1, 2, f_strftime},
7935#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007936 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007937 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {"strlen", 1, 1, f_strlen},
7939 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007940 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007942 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {"submatch", 1, 1, f_submatch},
7944 {"substitute", 4, 4, f_substitute},
7945 {"synID", 3, 3, f_synID},
7946 {"synIDattr", 2, 3, f_synIDattr},
7947 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007948 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007949 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007950 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007951 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007952 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007953 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007954 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007955 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007956#ifdef FEAT_FLOAT
7957 {"tan", 1, 1, f_tan},
7958 {"tanh", 1, 1, f_tanh},
7959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007961 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {"tolower", 1, 1, f_tolower},
7963 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007964 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007965#ifdef FEAT_FLOAT
7966 {"trunc", 1, 1, f_trunc},
7967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007969 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007970 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 {"virtcol", 1, 1, f_virtcol},
7973 {"visualmode", 0, 1, f_visualmode},
7974 {"winbufnr", 1, 1, f_winbufnr},
7975 {"wincol", 0, 0, f_wincol},
7976 {"winheight", 1, 1, f_winheight},
7977 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007978 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007980 {"winrestview", 1, 1, f_winrestview},
7981 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007983 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984};
7985
7986#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7987
7988/*
7989 * Function given to ExpandGeneric() to obtain the list of internal
7990 * or user defined function names.
7991 */
7992 char_u *
7993get_function_name(xp, idx)
7994 expand_T *xp;
7995 int idx;
7996{
7997 static int intidx = -1;
7998 char_u *name;
7999
8000 if (idx == 0)
8001 intidx = -1;
8002 if (intidx < 0)
8003 {
8004 name = get_user_func_name(xp, idx);
8005 if (name != NULL)
8006 return name;
8007 }
8008 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8009 {
8010 STRCPY(IObuff, functions[intidx].f_name);
8011 STRCAT(IObuff, "(");
8012 if (functions[intidx].f_max_argc == 0)
8013 STRCAT(IObuff, ")");
8014 return IObuff;
8015 }
8016
8017 return NULL;
8018}
8019
8020/*
8021 * Function given to ExpandGeneric() to obtain the list of internal or
8022 * user defined variable or function names.
8023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 char_u *
8025get_expr_name(xp, idx)
8026 expand_T *xp;
8027 int idx;
8028{
8029 static int intidx = -1;
8030 char_u *name;
8031
8032 if (idx == 0)
8033 intidx = -1;
8034 if (intidx < 0)
8035 {
8036 name = get_function_name(xp, idx);
8037 if (name != NULL)
8038 return name;
8039 }
8040 return get_user_var_name(xp, ++intidx);
8041}
8042
8043#endif /* FEAT_CMDL_COMPL */
8044
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008045#if defined(EBCDIC) || defined(PROTO)
8046/*
8047 * Compare struct fst by function name.
8048 */
8049 static int
8050compare_func_name(s1, s2)
8051 const void *s1;
8052 const void *s2;
8053{
8054 struct fst *p1 = (struct fst *)s1;
8055 struct fst *p2 = (struct fst *)s2;
8056
8057 return STRCMP(p1->f_name, p2->f_name);
8058}
8059
8060/*
8061 * Sort the function table by function name.
8062 * The sorting of the table above is ASCII dependant.
8063 * On machines using EBCDIC we have to sort it.
8064 */
8065 static void
8066sortFunctions()
8067{
8068 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8069
8070 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8071}
8072#endif
8073
8074
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075/*
8076 * Find internal function in table above.
8077 * Return index, or -1 if not found
8078 */
8079 static int
8080find_internal_func(name)
8081 char_u *name; /* name of the function */
8082{
8083 int first = 0;
8084 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8085 int cmp;
8086 int x;
8087
8088 /*
8089 * Find the function name in the table. Binary search.
8090 */
8091 while (first <= last)
8092 {
8093 x = first + ((unsigned)(last - first) >> 1);
8094 cmp = STRCMP(name, functions[x].f_name);
8095 if (cmp < 0)
8096 last = x - 1;
8097 else if (cmp > 0)
8098 first = x + 1;
8099 else
8100 return x;
8101 }
8102 return -1;
8103}
8104
8105/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008106 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8107 * name it contains, otherwise return "name".
8108 */
8109 static char_u *
8110deref_func_name(name, lenp)
8111 char_u *name;
8112 int *lenp;
8113{
Bram Moolenaar33570922005-01-25 22:26:29 +00008114 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008115 int cc;
8116
8117 cc = name[*lenp];
8118 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008119 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008120 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008121 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008122 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008123 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008124 {
8125 *lenp = 0;
8126 return (char_u *)""; /* just in case */
8127 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008128 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008129 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008130 }
8131
8132 return name;
8133}
8134
8135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 * Allocate a variable for the result of a function.
8137 * Return OK or FAIL.
8138 */
8139 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008140get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8141 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 char_u *name; /* name of the function */
8143 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 char_u **arg; /* argument, pointing to the '(' */
8146 linenr_T firstline; /* first line of range */
8147 linenr_T lastline; /* last line of range */
8148 int *doesrange; /* return: function handled range */
8149 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008150 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
8152 char_u *argp;
8153 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008154 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 int argcount = 0; /* number of arguments found */
8156
8157 /*
8158 * Get the arguments.
8159 */
8160 argp = *arg;
8161 while (argcount < MAX_FUNC_ARGS)
8162 {
8163 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8164 if (*argp == ')' || *argp == ',' || *argp == NUL)
8165 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8167 {
8168 ret = FAIL;
8169 break;
8170 }
8171 ++argcount;
8172 if (*argp != ',')
8173 break;
8174 }
8175 if (*argp == ')')
8176 ++argp;
8177 else
8178 ret = FAIL;
8179
8180 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008181 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008182 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008184 {
8185 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008186 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008187 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008188 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190
8191 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008192 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193
8194 *arg = skipwhite(argp);
8195 return ret;
8196}
8197
8198
8199/*
8200 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008201 * Return OK when the function can't be called, FAIL otherwise.
8202 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 */
8204 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008205call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008206 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008207 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008209 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008211 typval_T *argvars; /* vars for arguments, must have "argcount"
8212 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 linenr_T firstline; /* first line of range */
8214 linenr_T lastline; /* last line of range */
8215 int *doesrange; /* return: function handled range */
8216 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008217 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218{
8219 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220#define ERROR_UNKNOWN 0
8221#define ERROR_TOOMANY 1
8222#define ERROR_TOOFEW 2
8223#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008224#define ERROR_DICT 4
8225#define ERROR_NONE 5
8226#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 int error = ERROR_NONE;
8228 int i;
8229 int llen;
8230 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231#define FLEN_FIXED 40
8232 char_u fname_buf[FLEN_FIXED + 1];
8233 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008234 char_u *name;
8235
8236 /* Make a copy of the name, if it comes from a funcref variable it could
8237 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008238 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008239 if (name == NULL)
8240 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241
8242 /*
8243 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8244 * Change <SNR>123_name() to K_SNR 123_name().
8245 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 llen = eval_fname_script(name);
8248 if (llen > 0)
8249 {
8250 fname_buf[0] = K_SPECIAL;
8251 fname_buf[1] = KS_EXTRA;
8252 fname_buf[2] = (int)KE_SNR;
8253 i = 3;
8254 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8255 {
8256 if (current_SID <= 0)
8257 error = ERROR_SCRIPT;
8258 else
8259 {
8260 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8261 i = (int)STRLEN(fname_buf);
8262 }
8263 }
8264 if (i + STRLEN(name + llen) < FLEN_FIXED)
8265 {
8266 STRCPY(fname_buf + i, name + llen);
8267 fname = fname_buf;
8268 }
8269 else
8270 {
8271 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8272 if (fname == NULL)
8273 error = ERROR_OTHER;
8274 else
8275 {
8276 mch_memmove(fname, fname_buf, (size_t)i);
8277 STRCPY(fname + i, name + llen);
8278 }
8279 }
8280 }
8281 else
8282 fname = name;
8283
8284 *doesrange = FALSE;
8285
8286
8287 /* execute the function if no errors detected and executing */
8288 if (evaluate && error == ERROR_NONE)
8289 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008290 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8291 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 error = ERROR_UNKNOWN;
8293
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008294 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {
8296 /*
8297 * User defined function.
8298 */
8299 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008300
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008302 /* Trigger FuncUndefined event, may load the function. */
8303 if (fp == NULL
8304 && apply_autocmds(EVENT_FUNCUNDEFINED,
8305 fname, fname, TRUE, NULL)
8306 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008308 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 fp = find_func(fname);
8310 }
8311#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008312 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008313 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008314 {
8315 /* loaded a package, search for the function again */
8316 fp = find_func(fname);
8317 }
8318
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 if (fp != NULL)
8320 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008321 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008323 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008325 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008327 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008328 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 else
8330 {
8331 /*
8332 * Call the user function.
8333 * Save and restore search patterns, script variables and
8334 * redo buffer.
8335 */
8336 save_search_patterns();
8337 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008338 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008339 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008340 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008341 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8342 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8343 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008344 /* Function was unreferenced while being used, free it
8345 * now. */
8346 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 restoreRedobuff();
8348 restore_search_patterns();
8349 error = ERROR_NONE;
8350 }
8351 }
8352 }
8353 else
8354 {
8355 /*
8356 * Find the function name in the table, call its implementation.
8357 */
8358 i = find_internal_func(fname);
8359 if (i >= 0)
8360 {
8361 if (argcount < functions[i].f_min_argc)
8362 error = ERROR_TOOFEW;
8363 else if (argcount > functions[i].f_max_argc)
8364 error = ERROR_TOOMANY;
8365 else
8366 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008367 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008368 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 error = ERROR_NONE;
8370 }
8371 }
8372 }
8373 /*
8374 * The function call (or "FuncUndefined" autocommand sequence) might
8375 * have been aborted by an error, an interrupt, or an explicitly thrown
8376 * exception that has not been caught so far. This situation can be
8377 * tested for by calling aborting(). For an error in an internal
8378 * function or for the "E132" error in call_user_func(), however, the
8379 * throw point at which the "force_abort" flag (temporarily reset by
8380 * emsg()) is normally updated has not been reached yet. We need to
8381 * update that flag first to make aborting() reliable.
8382 */
8383 update_force_abort();
8384 }
8385 if (error == ERROR_NONE)
8386 ret = OK;
8387
8388 /*
8389 * Report an error unless the argument evaluation or function call has been
8390 * cancelled due to an aborting error, an interrupt, or an exception.
8391 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008392 if (!aborting())
8393 {
8394 switch (error)
8395 {
8396 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008397 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008398 break;
8399 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008400 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008401 break;
8402 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008403 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008404 name);
8405 break;
8406 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008407 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008408 name);
8409 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008410 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008411 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008412 name);
8413 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008414 }
8415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 if (fname != name && fname != fname_buf)
8418 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008419 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420
8421 return ret;
8422}
8423
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008424/*
8425 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008426 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008427 */
8428 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008429emsg_funcname(ermsg, name)
8430 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008431 char_u *name;
8432{
8433 char_u *p;
8434
8435 if (*name == K_SPECIAL)
8436 p = concat_str((char_u *)"<SNR>", name + 3);
8437 else
8438 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008439 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008440 if (p != name)
8441 vim_free(p);
8442}
8443
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008444/*
8445 * Return TRUE for a non-zero Number and a non-empty String.
8446 */
8447 static int
8448non_zero_arg(argvars)
8449 typval_T *argvars;
8450{
8451 return ((argvars[0].v_type == VAR_NUMBER
8452 && argvars[0].vval.v_number != 0)
8453 || (argvars[0].v_type == VAR_STRING
8454 && argvars[0].vval.v_string != NULL
8455 && *argvars[0].vval.v_string != NUL));
8456}
8457
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458/*********************************************
8459 * Implementation of the built-in functions
8460 */
8461
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008462#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008463static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8464
8465/*
8466 * Get the float value of "argvars[0]" into "f".
8467 * Returns FAIL when the argument is not a Number or Float.
8468 */
8469 static int
8470get_float_arg(argvars, f)
8471 typval_T *argvars;
8472 float_T *f;
8473{
8474 if (argvars[0].v_type == VAR_FLOAT)
8475 {
8476 *f = argvars[0].vval.v_float;
8477 return OK;
8478 }
8479 if (argvars[0].v_type == VAR_NUMBER)
8480 {
8481 *f = (float_T)argvars[0].vval.v_number;
8482 return OK;
8483 }
8484 EMSG(_("E808: Number or Float required"));
8485 return FAIL;
8486}
8487
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008488/*
8489 * "abs(expr)" function
8490 */
8491 static void
8492f_abs(argvars, rettv)
8493 typval_T *argvars;
8494 typval_T *rettv;
8495{
8496 if (argvars[0].v_type == VAR_FLOAT)
8497 {
8498 rettv->v_type = VAR_FLOAT;
8499 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8500 }
8501 else
8502 {
8503 varnumber_T n;
8504 int error = FALSE;
8505
8506 n = get_tv_number_chk(&argvars[0], &error);
8507 if (error)
8508 rettv->vval.v_number = -1;
8509 else if (n > 0)
8510 rettv->vval.v_number = n;
8511 else
8512 rettv->vval.v_number = -n;
8513 }
8514}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008515
8516/*
8517 * "acos()" function
8518 */
8519 static void
8520f_acos(argvars, rettv)
8521 typval_T *argvars;
8522 typval_T *rettv;
8523{
8524 float_T f;
8525
8526 rettv->v_type = VAR_FLOAT;
8527 if (get_float_arg(argvars, &f) == OK)
8528 rettv->vval.v_float = acos(f);
8529 else
8530 rettv->vval.v_float = 0.0;
8531}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008532#endif
8533
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008535 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 */
8537 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008538f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008539 typval_T *argvars;
8540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541{
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008544 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008545 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008547 if ((l = argvars[0].vval.v_list) != NULL
8548 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8549 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008550 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008551 }
8552 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008553 EMSG(_(e_listreq));
8554}
8555
8556/*
8557 * "append(lnum, string/list)" function
8558 */
8559 static void
8560f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008561 typval_T *argvars;
8562 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008563{
8564 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008565 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 list_T *l = NULL;
8567 listitem_T *li = NULL;
8568 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008569 long added = 0;
8570
Bram Moolenaar0d660222005-01-07 21:51:51 +00008571 lnum = get_tv_lnum(argvars);
8572 if (lnum >= 0
8573 && lnum <= curbuf->b_ml.ml_line_count
8574 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008575 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008576 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008577 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008578 l = argvars[1].vval.v_list;
8579 if (l == NULL)
8580 return;
8581 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008582 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008583 for (;;)
8584 {
8585 if (l == NULL)
8586 tv = &argvars[1]; /* append a string */
8587 else if (li == NULL)
8588 break; /* end of list */
8589 else
8590 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008591 line = get_tv_string_chk(tv);
8592 if (line == NULL) /* type error */
8593 {
8594 rettv->vval.v_number = 1; /* Failed */
8595 break;
8596 }
8597 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008598 ++added;
8599 if (l == NULL)
8600 break;
8601 li = li->li_next;
8602 }
8603
8604 appended_lines_mark(lnum, added);
8605 if (curwin->w_cursor.lnum > lnum)
8606 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008608 else
8609 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610}
8611
8612/*
8613 * "argc()" function
8614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008616f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008617 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008620 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621}
8622
8623/*
8624 * "argidx()" function
8625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008627f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008628 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632}
8633
8634/*
8635 * "argv(nr)" function
8636 */
8637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008639 typval_T *argvars;
8640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
8642 int idx;
8643
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008644 if (argvars[0].v_type != VAR_UNKNOWN)
8645 {
8646 idx = get_tv_number_chk(&argvars[0], NULL);
8647 if (idx >= 0 && idx < ARGCOUNT)
8648 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8649 else
8650 rettv->vval.v_string = NULL;
8651 rettv->v_type = VAR_STRING;
8652 }
8653 else if (rettv_list_alloc(rettv) == OK)
8654 for (idx = 0; idx < ARGCOUNT; ++idx)
8655 list_append_string(rettv->vval.v_list,
8656 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657}
8658
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008659#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008660/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008661 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008662 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008663 static void
8664f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008665 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008666 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008667{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008668 float_T f;
8669
8670 rettv->v_type = VAR_FLOAT;
8671 if (get_float_arg(argvars, &f) == OK)
8672 rettv->vval.v_float = asin(f);
8673 else
8674 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008675}
8676
8677/*
8678 * "atan()" function
8679 */
8680 static void
8681f_atan(argvars, rettv)
8682 typval_T *argvars;
8683 typval_T *rettv;
8684{
8685 float_T f;
8686
8687 rettv->v_type = VAR_FLOAT;
8688 if (get_float_arg(argvars, &f) == OK)
8689 rettv->vval.v_float = atan(f);
8690 else
8691 rettv->vval.v_float = 0.0;
8692}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008693
8694/*
8695 * "atan2()" function
8696 */
8697 static void
8698f_atan2(argvars, rettv)
8699 typval_T *argvars;
8700 typval_T *rettv;
8701{
8702 float_T fx, fy;
8703
8704 rettv->v_type = VAR_FLOAT;
8705 if (get_float_arg(argvars, &fx) == OK
8706 && get_float_arg(&argvars[1], &fy) == OK)
8707 rettv->vval.v_float = atan2(fx, fy);
8708 else
8709 rettv->vval.v_float = 0.0;
8710}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008711#endif
8712
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713/*
8714 * "browse(save, title, initdir, default)" function
8715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008717f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008718 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720{
8721#ifdef FEAT_BROWSE
8722 int save;
8723 char_u *title;
8724 char_u *initdir;
8725 char_u *defname;
8726 char_u buf[NUMBUFLEN];
8727 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008728 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008730 save = get_tv_number_chk(&argvars[0], &error);
8731 title = get_tv_string_chk(&argvars[1]);
8732 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8733 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008735 if (error || title == NULL || initdir == NULL || defname == NULL)
8736 rettv->vval.v_string = NULL;
8737 else
8738 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008739 do_browse(save ? BROWSE_SAVE : 0,
8740 title, defname, NULL, initdir, NULL, curbuf);
8741#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008743#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008745}
8746
8747/*
8748 * "browsedir(title, initdir)" function
8749 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008752 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008753 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008754{
8755#ifdef FEAT_BROWSE
8756 char_u *title;
8757 char_u *initdir;
8758 char_u buf[NUMBUFLEN];
8759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008760 title = get_tv_string_chk(&argvars[0]);
8761 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008763 if (title == NULL || initdir == NULL)
8764 rettv->vval.v_string = NULL;
8765 else
8766 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008767 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772}
8773
Bram Moolenaar33570922005-01-25 22:26:29 +00008774static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008775
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776/*
8777 * Find a buffer by number or exact name.
8778 */
8779 static buf_T *
8780find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008781 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
8783 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008785 if (avar->v_type == VAR_NUMBER)
8786 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008787 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008789 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008790 if (buf == NULL)
8791 {
8792 /* No full path name match, try a match with a URL or a "nofile"
8793 * buffer, these don't use the full path. */
8794 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8795 if (buf->b_fname != NULL
8796 && (path_with_url(buf->b_fname)
8797#ifdef FEAT_QUICKFIX
8798 || bt_nofile(buf)
8799#endif
8800 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008801 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008802 break;
8803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 }
8805 return buf;
8806}
8807
8808/*
8809 * "bufexists(expr)" function
8810 */
8811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008813 typval_T *argvars;
8814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008816 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817}
8818
8819/*
8820 * "buflisted(expr)" function
8821 */
8822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 typval_T *argvars;
8825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826{
8827 buf_T *buf;
8828
8829 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831}
8832
8833/*
8834 * "bufloaded(expr)" function
8835 */
8836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008837f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008838 typval_T *argvars;
8839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840{
8841 buf_T *buf;
8842
8843 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008844 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845}
8846
Bram Moolenaar33570922005-01-25 22:26:29 +00008847static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008848
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849/*
8850 * Get buffer by number or pattern.
8851 */
8852 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008854 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 int save_magic;
8858 char_u *save_cpo;
8859 buf_T *buf;
8860
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861 if (tv->v_type == VAR_NUMBER)
8862 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008863 if (tv->v_type != VAR_STRING)
8864 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 if (name == NULL || *name == NUL)
8866 return curbuf;
8867 if (name[0] == '$' && name[1] == NUL)
8868 return lastbuf;
8869
8870 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8871 save_magic = p_magic;
8872 p_magic = TRUE;
8873 save_cpo = p_cpo;
8874 p_cpo = (char_u *)"";
8875
8876 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8877 TRUE, FALSE));
8878
8879 p_magic = save_magic;
8880 p_cpo = save_cpo;
8881
8882 /* If not found, try expanding the name, like done for bufexists(). */
8883 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008884 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885
8886 return buf;
8887}
8888
8889/*
8890 * "bufname(expr)" function
8891 */
8892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008894 typval_T *argvars;
8895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896{
8897 buf_T *buf;
8898
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]);
8902 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008906 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 --emsg_off;
8908}
8909
8910/*
8911 * "bufnr(expr)" function
8912 */
8913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008914f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008915 typval_T *argvars;
8916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917{
8918 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008919 int error = FALSE;
8920 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008922 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008925 --emsg_off;
8926
8927 /* If the buffer isn't found and the second argument is not zero create a
8928 * new buffer. */
8929 if (buf == NULL
8930 && argvars[1].v_type != VAR_UNKNOWN
8931 && get_tv_number_chk(&argvars[1], &error) != 0
8932 && !error
8933 && (name = get_tv_string_chk(&argvars[0])) != NULL
8934 && !error)
8935 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8936
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008940 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941}
8942
8943/*
8944 * "bufwinnr(nr)" function
8945 */
8946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008948 typval_T *argvars;
8949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950{
8951#ifdef FEAT_WINDOWS
8952 win_T *wp;
8953 int winnr = 0;
8954#endif
8955 buf_T *buf;
8956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008957 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960#ifdef FEAT_WINDOWS
8961 for (wp = firstwin; wp; wp = wp->w_next)
8962 {
8963 ++winnr;
8964 if (wp->w_buffer == buf)
8965 break;
8966 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970#endif
8971 --emsg_off;
8972}
8973
8974/*
8975 * "byte2line(byte)" function
8976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008979 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981{
8982#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984#else
8985 long boff = 0;
8986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 (linenr_T)0, &boff);
8993#endif
8994}
8995
8996/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008997 * "byteidx()" function
8998 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 typval_T *argvars;
9002 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009003{
9004#ifdef FEAT_MBYTE
9005 char_u *t;
9006#endif
9007 char_u *str;
9008 long idx;
9009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009010 str = get_tv_string_chk(&argvars[0]);
9011 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009013 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009014 return;
9015
9016#ifdef FEAT_MBYTE
9017 t = str;
9018 for ( ; idx > 0; idx--)
9019 {
9020 if (*t == NUL) /* EOL reached */
9021 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009022 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009023 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009024 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009025#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009026 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009028#endif
9029}
9030
9031/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009032 * "call(func, arglist)" function
9033 */
9034 static void
9035f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009036 typval_T *argvars;
9037 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009038{
9039 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009040 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009041 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009042 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009043 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009044 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009045
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009046 if (argvars[1].v_type != VAR_LIST)
9047 {
9048 EMSG(_(e_listreq));
9049 return;
9050 }
9051 if (argvars[1].vval.v_list == NULL)
9052 return;
9053
9054 if (argvars[0].v_type == VAR_FUNC)
9055 func = argvars[0].vval.v_string;
9056 else
9057 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009058 if (*func == NUL)
9059 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009060
Bram Moolenaare9a41262005-01-15 22:18:47 +00009061 if (argvars[2].v_type != VAR_UNKNOWN)
9062 {
9063 if (argvars[2].v_type != VAR_DICT)
9064 {
9065 EMSG(_(e_dictreq));
9066 return;
9067 }
9068 selfdict = argvars[2].vval.v_dict;
9069 }
9070
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009071 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9072 item = item->li_next)
9073 {
9074 if (argc == MAX_FUNC_ARGS)
9075 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009076 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009077 break;
9078 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009079 /* Make a copy of each argument. This is needed to be able to set
9080 * v_lock to VAR_FIXED in the copy without changing the original list.
9081 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009082 copy_tv(&item->li_tv, &argv[argc++]);
9083 }
9084
9085 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009086 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009087 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9088 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009089
9090 /* Free the arguments. */
9091 while (argc > 0)
9092 clear_tv(&argv[--argc]);
9093}
9094
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009095#ifdef FEAT_FLOAT
9096/*
9097 * "ceil({float})" function
9098 */
9099 static void
9100f_ceil(argvars, rettv)
9101 typval_T *argvars;
9102 typval_T *rettv;
9103{
9104 float_T f;
9105
9106 rettv->v_type = VAR_FLOAT;
9107 if (get_float_arg(argvars, &f) == OK)
9108 rettv->vval.v_float = ceil(f);
9109 else
9110 rettv->vval.v_float = 0.0;
9111}
9112#endif
9113
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009114/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009115 * "changenr()" function
9116 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009117 static void
9118f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009119 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009120 typval_T *rettv;
9121{
9122 rettv->vval.v_number = curbuf->b_u_seq_cur;
9123}
9124
9125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 * "char2nr(string)" function
9127 */
9128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009130 typval_T *argvars;
9131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
9133#ifdef FEAT_MBYTE
9134 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009135 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 else
9137#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139}
9140
9141/*
9142 * "cindent(lnum)" function
9143 */
9144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009145f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009146 typval_T *argvars;
9147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148{
9149#ifdef FEAT_CINDENT
9150 pos_T pos;
9151 linenr_T lnum;
9152
9153 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9156 {
9157 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 curwin->w_cursor = pos;
9160 }
9161 else
9162#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009163 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164}
9165
9166/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009167 * "clearmatches()" function
9168 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009169 static void
9170f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009171 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009172 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009173{
9174#ifdef FEAT_SEARCH_EXTRA
9175 clear_matches(curwin);
9176#endif
9177}
9178
9179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 * "col(string)" function
9181 */
9182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009184 typval_T *argvars;
9185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186{
9187 colnr_T col = 0;
9188 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009189 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009191 fp = var2fpos(&argvars[0], FALSE, &fnum);
9192 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 {
9194 if (fp->col == MAXCOL)
9195 {
9196 /* '> can be MAXCOL, get the length of the line then */
9197 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009198 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 else
9200 col = MAXCOL;
9201 }
9202 else
9203 {
9204 col = fp->col + 1;
9205#ifdef FEAT_VIRTUALEDIT
9206 /* col(".") when the cursor is on the NUL at the end of the line
9207 * because of "coladd" can be seen as an extra column. */
9208 if (virtual_active() && fp == &curwin->w_cursor)
9209 {
9210 char_u *p = ml_get_cursor();
9211
9212 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9213 curwin->w_virtcol - curwin->w_cursor.coladd))
9214 {
9215# ifdef FEAT_MBYTE
9216 int l;
9217
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009218 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 col += l;
9220# else
9221 if (*p != NUL && p[1] == NUL)
9222 ++col;
9223# endif
9224 }
9225 }
9226#endif
9227 }
9228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230}
9231
Bram Moolenaar572cb562005-08-05 21:35:02 +00009232#if defined(FEAT_INS_EXPAND)
9233/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009234 * "complete()" function
9235 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009236 static void
9237f_complete(argvars, rettv)
9238 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009239 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009240{
9241 int startcol;
9242
9243 if ((State & INSERT) == 0)
9244 {
9245 EMSG(_("E785: complete() can only be used in Insert mode"));
9246 return;
9247 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009248
9249 /* Check for undo allowed here, because if something was already inserted
9250 * the line was already saved for undo and this check isn't done. */
9251 if (!undo_allowed())
9252 return;
9253
Bram Moolenaarade00832006-03-10 21:46:58 +00009254 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9255 {
9256 EMSG(_(e_invarg));
9257 return;
9258 }
9259
9260 startcol = get_tv_number_chk(&argvars[0], NULL);
9261 if (startcol <= 0)
9262 return;
9263
9264 set_completion(startcol - 1, argvars[1].vval.v_list);
9265}
9266
9267/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009268 * "complete_add()" function
9269 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009270 static void
9271f_complete_add(argvars, rettv)
9272 typval_T *argvars;
9273 typval_T *rettv;
9274{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009275 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009276}
9277
9278/*
9279 * "complete_check()" function
9280 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009281 static void
9282f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009283 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009284 typval_T *rettv;
9285{
9286 int saved = RedrawingDisabled;
9287
9288 RedrawingDisabled = 0;
9289 ins_compl_check_keys(0);
9290 rettv->vval.v_number = compl_interrupted;
9291 RedrawingDisabled = saved;
9292}
9293#endif
9294
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295/*
9296 * "confirm(message, buttons[, default [, type]])" function
9297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009299f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009300 typval_T *argvars UNUSED;
9301 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302{
9303#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9304 char_u *message;
9305 char_u *buttons = NULL;
9306 char_u buf[NUMBUFLEN];
9307 char_u buf2[NUMBUFLEN];
9308 int def = 1;
9309 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009310 char_u *typestr;
9311 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009313 message = get_tv_string_chk(&argvars[0]);
9314 if (message == NULL)
9315 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009316 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009318 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9319 if (buttons == NULL)
9320 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009321 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009324 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009326 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9327 if (typestr == NULL)
9328 error = TRUE;
9329 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009331 switch (TOUPPER_ASC(*typestr))
9332 {
9333 case 'E': type = VIM_ERROR; break;
9334 case 'Q': type = VIM_QUESTION; break;
9335 case 'I': type = VIM_INFO; break;
9336 case 'W': type = VIM_WARNING; break;
9337 case 'G': type = VIM_GENERIC; break;
9338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 }
9340 }
9341 }
9342 }
9343
9344 if (buttons == NULL || *buttons == NUL)
9345 buttons = (char_u *)_("&Ok");
9346
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009347 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009348 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009349 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350#endif
9351}
9352
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009353/*
9354 * "copy()" function
9355 */
9356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009357f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009358 typval_T *argvars;
9359 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009360{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009361 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009362}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009364#ifdef FEAT_FLOAT
9365/*
9366 * "cos()" function
9367 */
9368 static void
9369f_cos(argvars, rettv)
9370 typval_T *argvars;
9371 typval_T *rettv;
9372{
9373 float_T f;
9374
9375 rettv->v_type = VAR_FLOAT;
9376 if (get_float_arg(argvars, &f) == OK)
9377 rettv->vval.v_float = cos(f);
9378 else
9379 rettv->vval.v_float = 0.0;
9380}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009381
9382/*
9383 * "cosh()" function
9384 */
9385 static void
9386f_cosh(argvars, rettv)
9387 typval_T *argvars;
9388 typval_T *rettv;
9389{
9390 float_T f;
9391
9392 rettv->v_type = VAR_FLOAT;
9393 if (get_float_arg(argvars, &f) == OK)
9394 rettv->vval.v_float = cosh(f);
9395 else
9396 rettv->vval.v_float = 0.0;
9397}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009398#endif
9399
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009401 * "count()" function
9402 */
9403 static void
9404f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009405 typval_T *argvars;
9406 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009407{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009408 long n = 0;
9409 int ic = FALSE;
9410
Bram Moolenaare9a41262005-01-15 22:18:47 +00009411 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009412 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009413 listitem_T *li;
9414 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009415 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009416
Bram Moolenaare9a41262005-01-15 22:18:47 +00009417 if ((l = argvars[0].vval.v_list) != NULL)
9418 {
9419 li = l->lv_first;
9420 if (argvars[2].v_type != VAR_UNKNOWN)
9421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009422 int error = FALSE;
9423
9424 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009425 if (argvars[3].v_type != VAR_UNKNOWN)
9426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009427 idx = get_tv_number_chk(&argvars[3], &error);
9428 if (!error)
9429 {
9430 li = list_find(l, idx);
9431 if (li == NULL)
9432 EMSGN(_(e_listidx), idx);
9433 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009435 if (error)
9436 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009437 }
9438
9439 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009440 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009441 ++n;
9442 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009443 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009444 else if (argvars[0].v_type == VAR_DICT)
9445 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 int todo;
9447 dict_T *d;
9448 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009449
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009450 if ((d = argvars[0].vval.v_dict) != NULL)
9451 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009452 int error = FALSE;
9453
Bram Moolenaare9a41262005-01-15 22:18:47 +00009454 if (argvars[2].v_type != VAR_UNKNOWN)
9455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009456 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009457 if (argvars[3].v_type != VAR_UNKNOWN)
9458 EMSG(_(e_invarg));
9459 }
9460
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009461 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009462 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009463 {
9464 if (!HASHITEM_EMPTY(hi))
9465 {
9466 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009467 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009468 ++n;
9469 }
9470 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009471 }
9472 }
9473 else
9474 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009475 rettv->vval.v_number = n;
9476}
9477
9478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9480 *
9481 * Checks the existence of a cscope connection.
9482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009485 typval_T *argvars UNUSED;
9486 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487{
9488#ifdef FEAT_CSCOPE
9489 int num = 0;
9490 char_u *dbpath = NULL;
9491 char_u *prepend = NULL;
9492 char_u buf[NUMBUFLEN];
9493
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009494 if (argvars[0].v_type != VAR_UNKNOWN
9495 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 num = (int)get_tv_number(&argvars[0]);
9498 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009499 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009500 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 }
9502
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009503 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504#endif
9505}
9506
9507/*
9508 * "cursor(lnum, col)" function
9509 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009510 * Moves the cursor to the specified line and column.
9511 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009515 typval_T *argvars;
9516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
9518 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009519#ifdef FEAT_VIRTUALEDIT
9520 long coladd = 0;
9521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009523 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009524 if (argvars[1].v_type == VAR_UNKNOWN)
9525 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009526 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009527
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009528 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009529 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009530 line = pos.lnum;
9531 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009532#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009533 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009534#endif
9535 }
9536 else
9537 {
9538 line = get_tv_lnum(argvars);
9539 col = get_tv_number_chk(&argvars[1], NULL);
9540#ifdef FEAT_VIRTUALEDIT
9541 if (argvars[2].v_type != VAR_UNKNOWN)
9542 coladd = get_tv_number_chk(&argvars[2], NULL);
9543#endif
9544 }
9545 if (line < 0 || col < 0
9546#ifdef FEAT_VIRTUALEDIT
9547 || coladd < 0
9548#endif
9549 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009550 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 if (line > 0)
9552 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 if (col > 0)
9554 curwin->w_cursor.col = col - 1;
9555#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009556 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557#endif
9558
9559 /* Make sure the cursor is in a valid position. */
9560 check_cursor();
9561#ifdef FEAT_MBYTE
9562 /* Correct cursor for multi-byte character. */
9563 if (has_mbyte)
9564 mb_adjust_cursor();
9565#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009566
9567 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009568 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569}
9570
9571/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009572 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 */
9574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009576 typval_T *argvars;
9577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009579 int noref = 0;
9580
9581 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009582 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009583 if (noref < 0 || noref > 1)
9584 EMSG(_(e_invarg));
9585 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009586 {
9587 current_copyID += COPYID_INC;
9588 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590}
9591
9592/*
9593 * "delete()" function
9594 */
9595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009597 typval_T *argvars;
9598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599{
9600 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009601 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009603 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604}
9605
9606/*
9607 * "did_filetype()" function
9608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009611 typval_T *argvars UNUSED;
9612 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
9614#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009615 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616#endif
9617}
9618
9619/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009620 * "diff_filler()" function
9621 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009624 typval_T *argvars UNUSED;
9625 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009626{
9627#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009629#endif
9630}
9631
9632/*
9633 * "diff_hlID()" function
9634 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009636f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009637 typval_T *argvars UNUSED;
9638 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009639{
9640#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009642 static linenr_T prev_lnum = 0;
9643 static int changedtick = 0;
9644 static int fnum = 0;
9645 static int change_start = 0;
9646 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009647 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009648 int filler_lines;
9649 int col;
9650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 if (lnum < 0) /* ignore type error in {lnum} arg */
9652 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009653 if (lnum != prev_lnum
9654 || changedtick != curbuf->b_changedtick
9655 || fnum != curbuf->b_fnum)
9656 {
9657 /* New line, buffer, change: need to get the values. */
9658 filler_lines = diff_check(curwin, lnum);
9659 if (filler_lines < 0)
9660 {
9661 if (filler_lines == -1)
9662 {
9663 change_start = MAXCOL;
9664 change_end = -1;
9665 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9666 hlID = HLF_ADD; /* added line */
9667 else
9668 hlID = HLF_CHD; /* changed line */
9669 }
9670 else
9671 hlID = HLF_ADD; /* added line */
9672 }
9673 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009674 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009675 prev_lnum = lnum;
9676 changedtick = curbuf->b_changedtick;
9677 fnum = curbuf->b_fnum;
9678 }
9679
9680 if (hlID == HLF_CHD || hlID == HLF_TXD)
9681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009682 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009683 if (col >= change_start && col <= change_end)
9684 hlID = HLF_TXD; /* changed text */
9685 else
9686 hlID = HLF_CHD; /* changed line */
9687 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009688 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009689#endif
9690}
9691
9692/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009693 * "empty({expr})" function
9694 */
9695 static void
9696f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009697 typval_T *argvars;
9698 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009699{
9700 int n;
9701
9702 switch (argvars[0].v_type)
9703 {
9704 case VAR_STRING:
9705 case VAR_FUNC:
9706 n = argvars[0].vval.v_string == NULL
9707 || *argvars[0].vval.v_string == NUL;
9708 break;
9709 case VAR_NUMBER:
9710 n = argvars[0].vval.v_number == 0;
9711 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009712#ifdef FEAT_FLOAT
9713 case VAR_FLOAT:
9714 n = argvars[0].vval.v_float == 0.0;
9715 break;
9716#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009717 case VAR_LIST:
9718 n = argvars[0].vval.v_list == NULL
9719 || argvars[0].vval.v_list->lv_first == NULL;
9720 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009721 case VAR_DICT:
9722 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009723 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009724 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009725 default:
9726 EMSG2(_(e_intern2), "f_empty()");
9727 n = 0;
9728 }
9729
9730 rettv->vval.v_number = n;
9731}
9732
9733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 * "escape({string}, {chars})" function
9735 */
9736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009738 typval_T *argvars;
9739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
9741 char_u buf[NUMBUFLEN];
9742
Bram Moolenaar758711c2005-02-02 23:11:38 +00009743 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9744 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746}
9747
9748/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009749 * "eval()" function
9750 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009751 static void
9752f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009753 typval_T *argvars;
9754 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009755{
9756 char_u *s;
9757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009758 s = get_tv_string_chk(&argvars[0]);
9759 if (s != NULL)
9760 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009762 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9763 {
9764 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009765 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009766 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009767 else if (*s != NUL)
9768 EMSG(_(e_trailing));
9769}
9770
9771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 * "eventhandler()" function
9773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009776 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780}
9781
9782/*
9783 * "executable()" function
9784 */
9785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009787 typval_T *argvars;
9788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791}
9792
9793/*
9794 * "exists()" function
9795 */
9796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009798 typval_T *argvars;
9799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
9801 char_u *p;
9802 char_u *name;
9803 int n = FALSE;
9804 int len = 0;
9805
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009806 no_autoload = TRUE;
9807
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 if (*p == '$') /* environment variable */
9810 {
9811 /* first try "normal" environment variables (fast) */
9812 if (mch_getenv(p + 1) != NULL)
9813 n = TRUE;
9814 else
9815 {
9816 /* try expanding things like $VIM and ${HOME} */
9817 p = expand_env_save(p);
9818 if (p != NULL && *p != '$')
9819 n = TRUE;
9820 vim_free(p);
9821 }
9822 }
9823 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009826 if (*skipwhite(p) != NUL)
9827 n = FALSE; /* trailing garbage */
9828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 else if (*p == '*') /* internal or user defined function */
9830 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009831 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 }
9833 else if (*p == ':')
9834 {
9835 n = cmd_exists(p + 1);
9836 }
9837 else if (*p == '#')
9838 {
9839#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009840 if (p[1] == '#')
9841 n = autocmd_supported(p + 2);
9842 else
9843 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844#endif
9845 }
9846 else /* internal variable */
9847 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009848 char_u *tofree;
9849 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009851 /* get_name_len() takes care of expanding curly braces */
9852 name = p;
9853 len = get_name_len(&p, &tofree, TRUE, FALSE);
9854 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009856 if (tofree != NULL)
9857 name = tofree;
9858 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9859 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009861 /* handle d.key, l[idx], f(expr) */
9862 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9863 if (n)
9864 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 }
9866 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009867 if (*p != NUL)
9868 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009870 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 }
9872
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009873 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009874
9875 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876}
9877
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009878#ifdef FEAT_FLOAT
9879/*
9880 * "exp()" function
9881 */
9882 static void
9883f_exp(argvars, rettv)
9884 typval_T *argvars;
9885 typval_T *rettv;
9886{
9887 float_T f;
9888
9889 rettv->v_type = VAR_FLOAT;
9890 if (get_float_arg(argvars, &f) == OK)
9891 rettv->vval.v_float = exp(f);
9892 else
9893 rettv->vval.v_float = 0.0;
9894}
9895#endif
9896
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897/*
9898 * "expand()" function
9899 */
9900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009902 typval_T *argvars;
9903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904{
9905 char_u *s;
9906 int len;
9907 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009908 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009910 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912 rettv->v_type = VAR_STRING;
9913 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 if (*s == '%' || *s == '#' || *s == '<')
9915 {
9916 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009917 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 --emsg_off;
9919 }
9920 else
9921 {
9922 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009923 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009924 if (argvars[1].v_type != VAR_UNKNOWN
9925 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009926 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009927 if (!error)
9928 {
9929 ExpandInit(&xpc);
9930 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009931 if (p_wic)
9932 options += WILD_ICASE;
9933 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009934 }
9935 else
9936 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 }
9938}
9939
9940/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009941 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009942 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009943 */
9944 static void
9945f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009946 typval_T *argvars;
9947 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009948{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009949 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009950 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009951 list_T *l1, *l2;
9952 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009953 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009954 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009955
Bram Moolenaare9a41262005-01-15 22:18:47 +00009956 l1 = argvars[0].vval.v_list;
9957 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009958 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9959 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960 {
9961 if (argvars[2].v_type != VAR_UNKNOWN)
9962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009963 before = get_tv_number_chk(&argvars[2], &error);
9964 if (error)
9965 return; /* type error; errmsg already given */
9966
Bram Moolenaar758711c2005-02-02 23:11:38 +00009967 if (before == l1->lv_len)
9968 item = NULL;
9969 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009971 item = list_find(l1, before);
9972 if (item == NULL)
9973 {
9974 EMSGN(_(e_listidx), before);
9975 return;
9976 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009977 }
9978 }
9979 else
9980 item = NULL;
9981 list_extend(l1, l2, item);
9982
Bram Moolenaare9a41262005-01-15 22:18:47 +00009983 copy_tv(&argvars[0], rettv);
9984 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009985 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009986 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9987 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009988 dict_T *d1, *d2;
9989 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009990 char_u *action;
9991 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009992 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009993 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009994
9995 d1 = argvars[0].vval.v_dict;
9996 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009997 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9998 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009999 {
10000 /* Check the third argument. */
10001 if (argvars[2].v_type != VAR_UNKNOWN)
10002 {
10003 static char *(av[]) = {"keep", "force", "error"};
10004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010005 action = get_tv_string_chk(&argvars[2]);
10006 if (action == NULL)
10007 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010008 for (i = 0; i < 3; ++i)
10009 if (STRCMP(action, av[i]) == 0)
10010 break;
10011 if (i == 3)
10012 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010013 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010014 return;
10015 }
10016 }
10017 else
10018 action = (char_u *)"force";
10019
10020 /* Go over all entries in the second dict and add them to the
10021 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010022 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010023 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010024 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010025 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010026 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010027 --todo;
10028 di1 = dict_find(d1, hi2->hi_key, -1);
10029 if (di1 == NULL)
10030 {
10031 di1 = dictitem_copy(HI2DI(hi2));
10032 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10033 dictitem_free(di1);
10034 }
10035 else if (*action == 'e')
10036 {
10037 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10038 break;
10039 }
10040 else if (*action == 'f')
10041 {
10042 clear_tv(&di1->di_tv);
10043 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10044 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010045 }
10046 }
10047
Bram Moolenaare9a41262005-01-15 22:18:47 +000010048 copy_tv(&argvars[0], rettv);
10049 }
10050 }
10051 else
10052 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010053}
10054
10055/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010056 * "feedkeys()" function
10057 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010058 static void
10059f_feedkeys(argvars, rettv)
10060 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010061 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010062{
10063 int remap = TRUE;
10064 char_u *keys, *flags;
10065 char_u nbuf[NUMBUFLEN];
10066 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010067 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010068
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010069 /* This is not allowed in the sandbox. If the commands would still be
10070 * executed in the sandbox it would be OK, but it probably happens later,
10071 * when "sandbox" is no longer set. */
10072 if (check_secure())
10073 return;
10074
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010075 keys = get_tv_string(&argvars[0]);
10076 if (*keys != NUL)
10077 {
10078 if (argvars[1].v_type != VAR_UNKNOWN)
10079 {
10080 flags = get_tv_string_buf(&argvars[1], nbuf);
10081 for ( ; *flags != NUL; ++flags)
10082 {
10083 switch (*flags)
10084 {
10085 case 'n': remap = FALSE; break;
10086 case 'm': remap = TRUE; break;
10087 case 't': typed = TRUE; break;
10088 }
10089 }
10090 }
10091
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010092 /* Need to escape K_SPECIAL and CSI before putting the string in the
10093 * typeahead buffer. */
10094 keys_esc = vim_strsave_escape_csi(keys);
10095 if (keys_esc != NULL)
10096 {
10097 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010098 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010099 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010100 if (vgetc_busy)
10101 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010102 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010103 }
10104}
10105
10106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 * "filereadable()" function
10108 */
10109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010111 typval_T *argvars;
10112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010114 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 char_u *p;
10116 int n;
10117
Bram Moolenaarc236c162008-07-13 17:41:49 +000010118#ifndef O_NONBLOCK
10119# define O_NONBLOCK 0
10120#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010121 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010122 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10123 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 {
10125 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010126 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 }
10128 else
10129 n = FALSE;
10130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132}
10133
10134/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010135 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 * rights to write into.
10137 */
10138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010140 typval_T *argvars;
10141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010143 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144}
10145
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010146static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010147
10148 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010149findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010150 typval_T *argvars;
10151 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010152 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010153{
10154#ifdef FEAT_SEARCHPATH
10155 char_u *fname;
10156 char_u *fresult = NULL;
10157 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10158 char_u *p;
10159 char_u pathbuf[NUMBUFLEN];
10160 int count = 1;
10161 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010162 int error = FALSE;
10163#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010164
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010165 rettv->vval.v_string = NULL;
10166 rettv->v_type = VAR_STRING;
10167
10168#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010169 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010170
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010171 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010172 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010173 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10174 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010175 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176 else
10177 {
10178 if (*p != NUL)
10179 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010180
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010181 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010182 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010183 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010184 }
10185
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010186 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10187 error = TRUE;
10188
10189 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010191 do
10192 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010193 if (rettv->v_type == VAR_STRING)
10194 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 fresult = find_file_in_path_option(first ? fname : NULL,
10196 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010197 0, first, path,
10198 find_what,
10199 curbuf->b_ffname,
10200 find_what == FINDFILE_DIR
10201 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010202 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010203
10204 if (fresult != NULL && rettv->v_type == VAR_LIST)
10205 list_append_string(rettv->vval.v_list, fresult, -1);
10206
10207 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010209
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010210 if (rettv->v_type == VAR_STRING)
10211 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010212#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010213}
10214
Bram Moolenaar33570922005-01-25 22:26:29 +000010215static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10216static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010217
10218/*
10219 * Implementation of map() and filter().
10220 */
10221 static void
10222filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010223 typval_T *argvars;
10224 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010225 int map;
10226{
10227 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010228 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010229 listitem_T *li, *nli;
10230 list_T *l = NULL;
10231 dictitem_T *di;
10232 hashtab_T *ht;
10233 hashitem_T *hi;
10234 dict_T *d = NULL;
10235 typval_T save_val;
10236 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010237 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010238 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010239 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010240 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010241 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010242
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010244 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010245 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010246 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010247 return;
10248 }
10249 else if (argvars[0].v_type == VAR_DICT)
10250 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010251 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010252 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010253 return;
10254 }
10255 else
10256 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010257 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010258 return;
10259 }
10260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261 expr = get_tv_string_buf_chk(&argvars[1], buf);
10262 /* On type errors, the preceding call has already displayed an error
10263 * message. Avoid a misleading error message for an empty string that
10264 * was not passed as argument. */
10265 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 prepare_vimvar(VV_VAL, &save_val);
10268 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010269
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010270 /* We reset "did_emsg" to be able to detect whether an error
10271 * occurred during evaluation of the expression. */
10272 save_did_emsg = did_emsg;
10273 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010274
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010275 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010276 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010278 vimvars[VV_KEY].vv_type = VAR_STRING;
10279
10280 ht = &d->dv_hashtab;
10281 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010282 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010285 if (!HASHITEM_EMPTY(hi))
10286 {
10287 --todo;
10288 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010289 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010290 break;
10291 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010292 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010293 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010294 break;
10295 if (!map && rem)
10296 dictitem_remove(d, di);
10297 clear_tv(&vimvars[VV_KEY].vv_tv);
10298 }
10299 }
10300 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010301 }
10302 else
10303 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010304 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 for (li = l->lv_first; li != NULL; li = nli)
10307 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010308 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010309 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010310 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010311 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010312 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010313 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010314 break;
10315 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010316 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010317 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010318 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010319 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010320
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010321 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010322 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010323
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010324 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010325 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010326
10327 copy_tv(&argvars[0], rettv);
10328}
10329
10330 static int
10331filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010332 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010333 char_u *expr;
10334 int map;
10335 int *remp;
10336{
Bram Moolenaar33570922005-01-25 22:26:29 +000010337 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010338 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010339 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010340
Bram Moolenaar33570922005-01-25 22:26:29 +000010341 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342 s = expr;
10343 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010344 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010345 if (*s != NUL) /* check for trailing chars after expr */
10346 {
10347 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010348 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010349 }
10350 if (map)
10351 {
10352 /* map(): replace the list item value */
10353 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010354 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010355 *tv = rettv;
10356 }
10357 else
10358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010359 int error = FALSE;
10360
Bram Moolenaare9a41262005-01-15 22:18:47 +000010361 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010362 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010363 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010364 /* On type error, nothing has been removed; return FAIL to stop the
10365 * loop. The error message was given by get_tv_number_chk(). */
10366 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010367 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010368 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010369 retval = OK;
10370theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010371 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010372 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010373}
10374
10375/*
10376 * "filter()" function
10377 */
10378 static void
10379f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010380 typval_T *argvars;
10381 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010382{
10383 filter_map(argvars, rettv, FALSE);
10384}
10385
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010386/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010387 * "finddir({fname}[, {path}[, {count}]])" function
10388 */
10389 static void
10390f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010391 typval_T *argvars;
10392 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010393{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010394 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010395}
10396
10397/*
10398 * "findfile({fname}[, {path}[, {count}]])" function
10399 */
10400 static void
10401f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010402 typval_T *argvars;
10403 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010404{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010405 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010406}
10407
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010408#ifdef FEAT_FLOAT
10409/*
10410 * "float2nr({float})" function
10411 */
10412 static void
10413f_float2nr(argvars, rettv)
10414 typval_T *argvars;
10415 typval_T *rettv;
10416{
10417 float_T f;
10418
10419 if (get_float_arg(argvars, &f) == OK)
10420 {
10421 if (f < -0x7fffffff)
10422 rettv->vval.v_number = -0x7fffffff;
10423 else if (f > 0x7fffffff)
10424 rettv->vval.v_number = 0x7fffffff;
10425 else
10426 rettv->vval.v_number = (varnumber_T)f;
10427 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010428}
10429
10430/*
10431 * "floor({float})" function
10432 */
10433 static void
10434f_floor(argvars, rettv)
10435 typval_T *argvars;
10436 typval_T *rettv;
10437{
10438 float_T f;
10439
10440 rettv->v_type = VAR_FLOAT;
10441 if (get_float_arg(argvars, &f) == OK)
10442 rettv->vval.v_float = floor(f);
10443 else
10444 rettv->vval.v_float = 0.0;
10445}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010446
10447/*
10448 * "fmod()" function
10449 */
10450 static void
10451f_fmod(argvars, rettv)
10452 typval_T *argvars;
10453 typval_T *rettv;
10454{
10455 float_T fx, fy;
10456
10457 rettv->v_type = VAR_FLOAT;
10458 if (get_float_arg(argvars, &fx) == OK
10459 && get_float_arg(&argvars[1], &fy) == OK)
10460 rettv->vval.v_float = fmod(fx, fy);
10461 else
10462 rettv->vval.v_float = 0.0;
10463}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010464#endif
10465
Bram Moolenaar0d660222005-01-07 21:51:51 +000010466/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010467 * "fnameescape({string})" function
10468 */
10469 static void
10470f_fnameescape(argvars, rettv)
10471 typval_T *argvars;
10472 typval_T *rettv;
10473{
10474 rettv->vval.v_string = vim_strsave_fnameescape(
10475 get_tv_string(&argvars[0]), FALSE);
10476 rettv->v_type = VAR_STRING;
10477}
10478
10479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 * "fnamemodify({fname}, {mods})" function
10481 */
10482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010483f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010484 typval_T *argvars;
10485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486{
10487 char_u *fname;
10488 char_u *mods;
10489 int usedlen = 0;
10490 int len;
10491 char_u *fbuf = NULL;
10492 char_u buf[NUMBUFLEN];
10493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010494 fname = get_tv_string_chk(&argvars[0]);
10495 mods = get_tv_string_buf_chk(&argvars[1], buf);
10496 if (fname == NULL || mods == NULL)
10497 fname = NULL;
10498 else
10499 {
10500 len = (int)STRLEN(fname);
10501 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010504 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 vim_free(fbuf);
10510}
10511
Bram Moolenaar33570922005-01-25 22:26:29 +000010512static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513
10514/*
10515 * "foldclosed()" function
10516 */
10517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010518foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010519 typval_T *argvars;
10520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 int end;
10522{
10523#ifdef FEAT_FOLDING
10524 linenr_T lnum;
10525 linenr_T first, last;
10526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010527 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10529 {
10530 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10531 {
10532 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010533 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 return;
10537 }
10538 }
10539#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010540 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541}
10542
10543/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010544 * "foldclosed()" function
10545 */
10546 static void
10547f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010548 typval_T *argvars;
10549 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010550{
10551 foldclosed_both(argvars, rettv, FALSE);
10552}
10553
10554/*
10555 * "foldclosedend()" function
10556 */
10557 static void
10558f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010559 typval_T *argvars;
10560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010561{
10562 foldclosed_both(argvars, rettv, TRUE);
10563}
10564
10565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 * "foldlevel()" function
10567 */
10568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010569f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010570 typval_T *argvars;
10571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572{
10573#ifdef FEAT_FOLDING
10574 linenr_T lnum;
10575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010578 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580}
10581
10582/*
10583 * "foldtext()" function
10584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010586f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010587 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589{
10590#ifdef FEAT_FOLDING
10591 linenr_T lnum;
10592 char_u *s;
10593 char_u *r;
10594 int len;
10595 char *txt;
10596#endif
10597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598 rettv->v_type = VAR_STRING;
10599 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010601 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10602 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10603 <= curbuf->b_ml.ml_line_count
10604 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 {
10606 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010607 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10608 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 {
10610 if (!linewhite(lnum))
10611 break;
10612 ++lnum;
10613 }
10614
10615 /* Find interesting text in this line. */
10616 s = skipwhite(ml_get(lnum));
10617 /* skip C comment-start */
10618 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010621 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010622 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010623 {
10624 s = skipwhite(ml_get(lnum + 1));
10625 if (*s == '*')
10626 s = skipwhite(s + 1);
10627 }
10628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 txt = _("+-%s%3ld lines: ");
10630 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010631 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 + 20 /* for %3ld */
10633 + STRLEN(s))); /* concatenated */
10634 if (r != NULL)
10635 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010636 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10637 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10638 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 len = (int)STRLEN(r);
10640 STRCAT(r, s);
10641 /* remove 'foldmarker' and 'commentstring' */
10642 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 }
10645 }
10646#endif
10647}
10648
10649/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010650 * "foldtextresult(lnum)" function
10651 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010653f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010654 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010655 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010656{
10657#ifdef FEAT_FOLDING
10658 linenr_T lnum;
10659 char_u *text;
10660 char_u buf[51];
10661 foldinfo_T foldinfo;
10662 int fold_count;
10663#endif
10664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665 rettv->v_type = VAR_STRING;
10666 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010667#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010668 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010669 /* treat illegal types and illegal string values for {lnum} the same */
10670 if (lnum < 0)
10671 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010672 fold_count = foldedCount(curwin, lnum, &foldinfo);
10673 if (fold_count > 0)
10674 {
10675 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10676 &foldinfo, buf);
10677 if (text == buf)
10678 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010680 }
10681#endif
10682}
10683
10684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685 * "foreground()" function
10686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010688f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010689 typval_T *argvars UNUSED;
10690 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692#ifdef FEAT_GUI
10693 if (gui.in_use)
10694 gui_mch_set_foreground();
10695#else
10696# ifdef WIN32
10697 win32_set_foreground();
10698# endif
10699#endif
10700}
10701
10702/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010703 * "function()" function
10704 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010706f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010707 typval_T *argvars;
10708 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010709{
10710 char_u *s;
10711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010713 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010714 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010715 /* Don't check an autoload name for existence here. */
10716 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010717 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010718 else
10719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010720 rettv->vval.v_string = vim_strsave(s);
10721 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010722 }
10723}
10724
10725/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010726 * "garbagecollect()" function
10727 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010728 static void
10729f_garbagecollect(argvars, rettv)
10730 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010731 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010732{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010733 /* This is postponed until we are back at the toplevel, because we may be
10734 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10735 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010736
10737 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10738 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010739}
10740
10741/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010742 * "get()" function
10743 */
10744 static void
10745f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010746 typval_T *argvars;
10747 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010748{
Bram Moolenaar33570922005-01-25 22:26:29 +000010749 listitem_T *li;
10750 list_T *l;
10751 dictitem_T *di;
10752 dict_T *d;
10753 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010754
Bram Moolenaare9a41262005-01-15 22:18:47 +000010755 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010756 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010757 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010759 int error = FALSE;
10760
10761 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10762 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010763 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010764 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010765 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010766 else if (argvars[0].v_type == VAR_DICT)
10767 {
10768 if ((d = argvars[0].vval.v_dict) != NULL)
10769 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010770 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010771 if (di != NULL)
10772 tv = &di->di_tv;
10773 }
10774 }
10775 else
10776 EMSG2(_(e_listdictarg), "get()");
10777
10778 if (tv == NULL)
10779 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010780 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010781 copy_tv(&argvars[2], rettv);
10782 }
10783 else
10784 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010785}
10786
Bram Moolenaar342337a2005-07-21 21:11:17 +000010787static 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 +000010788
10789/*
10790 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010791 * Return a range (from start to end) of lines in rettv from the specified
10792 * buffer.
10793 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010794 */
10795 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010796get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010797 buf_T *buf;
10798 linenr_T start;
10799 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010800 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010801 typval_T *rettv;
10802{
10803 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010804
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010805 if (retlist && rettv_list_alloc(rettv) == FAIL)
10806 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010807
10808 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10809 return;
10810
10811 if (!retlist)
10812 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010813 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10814 p = ml_get_buf(buf, start, FALSE);
10815 else
10816 p = (char_u *)"";
10817
10818 rettv->v_type = VAR_STRING;
10819 rettv->vval.v_string = vim_strsave(p);
10820 }
10821 else
10822 {
10823 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010824 return;
10825
10826 if (start < 1)
10827 start = 1;
10828 if (end > buf->b_ml.ml_line_count)
10829 end = buf->b_ml.ml_line_count;
10830 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010831 if (list_append_string(rettv->vval.v_list,
10832 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010833 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010834 }
10835}
10836
10837/*
10838 * "getbufline()" function
10839 */
10840 static void
10841f_getbufline(argvars, rettv)
10842 typval_T *argvars;
10843 typval_T *rettv;
10844{
10845 linenr_T lnum;
10846 linenr_T end;
10847 buf_T *buf;
10848
10849 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10850 ++emsg_off;
10851 buf = get_buf_tv(&argvars[0]);
10852 --emsg_off;
10853
Bram Moolenaar661b1822005-07-28 22:36:45 +000010854 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010855 if (argvars[2].v_type == VAR_UNKNOWN)
10856 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010857 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010858 end = get_tv_lnum_buf(&argvars[2], buf);
10859
Bram Moolenaar342337a2005-07-21 21:11:17 +000010860 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010861}
10862
Bram Moolenaar0d660222005-01-07 21:51:51 +000010863/*
10864 * "getbufvar()" function
10865 */
10866 static void
10867f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010868 typval_T *argvars;
10869 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010870{
10871 buf_T *buf;
10872 buf_T *save_curbuf;
10873 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010874 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010876 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10877 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010878 ++emsg_off;
10879 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010880
10881 rettv->v_type = VAR_STRING;
10882 rettv->vval.v_string = NULL;
10883
10884 if (buf != NULL && varname != NULL)
10885 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010886 /* set curbuf to be our buf, temporarily */
10887 save_curbuf = curbuf;
10888 curbuf = buf;
10889
Bram Moolenaar0d660222005-01-07 21:51:51 +000010890 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010891 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010010892 else if (STRCMP(varname, "changedtick") == 0)
10893 {
10894 rettv->v_type = VAR_NUMBER;
10895 rettv->vval.v_number = curbuf->b_changedtick;
10896 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010897 else
10898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010899 if (*varname == NUL)
10900 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10901 * scope prefix before the NUL byte is required by
10902 * find_var_in_ht(). */
10903 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010904 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010905 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010906 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010907 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010908 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010909
10910 /* restore previous notion of curbuf */
10911 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010912 }
10913
10914 --emsg_off;
10915}
10916
10917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 * "getchar()" function
10919 */
10920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010922 typval_T *argvars;
10923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924{
10925 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010926 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010928 /* Position the cursor. Needed after a message that ends in a space. */
10929 windgoto(msg_row, msg_col);
10930
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 ++no_mapping;
10932 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010933 for (;;)
10934 {
10935 if (argvars[0].v_type == VAR_UNKNOWN)
10936 /* getchar(): blocking wait. */
10937 n = safe_vgetc();
10938 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10939 /* getchar(1): only check if char avail */
10940 n = vpeekc();
10941 else if (error || vpeekc() == NUL)
10942 /* illegal argument or getchar(0) and no char avail: return zero */
10943 n = 0;
10944 else
10945 /* getchar(0) and char avail: return char */
10946 n = safe_vgetc();
10947 if (n == K_IGNORE)
10948 continue;
10949 break;
10950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 --no_mapping;
10952 --allow_keys;
10953
Bram Moolenaar219b8702006-11-01 14:32:36 +000010954 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10955 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10956 vimvars[VV_MOUSE_COL].vv_nr = 0;
10957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010958 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 if (IS_SPECIAL(n) || mod_mask != 0)
10960 {
10961 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10962 int i = 0;
10963
10964 /* Turn a special key into three bytes, plus modifier. */
10965 if (mod_mask != 0)
10966 {
10967 temp[i++] = K_SPECIAL;
10968 temp[i++] = KS_MODIFIER;
10969 temp[i++] = mod_mask;
10970 }
10971 if (IS_SPECIAL(n))
10972 {
10973 temp[i++] = K_SPECIAL;
10974 temp[i++] = K_SECOND(n);
10975 temp[i++] = K_THIRD(n);
10976 }
10977#ifdef FEAT_MBYTE
10978 else if (has_mbyte)
10979 i += (*mb_char2bytes)(n, temp + i);
10980#endif
10981 else
10982 temp[i++] = n;
10983 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984 rettv->v_type = VAR_STRING;
10985 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010986
10987#ifdef FEAT_MOUSE
10988 if (n == K_LEFTMOUSE
10989 || n == K_LEFTMOUSE_NM
10990 || n == K_LEFTDRAG
10991 || n == K_LEFTRELEASE
10992 || n == K_LEFTRELEASE_NM
10993 || n == K_MIDDLEMOUSE
10994 || n == K_MIDDLEDRAG
10995 || n == K_MIDDLERELEASE
10996 || n == K_RIGHTMOUSE
10997 || n == K_RIGHTDRAG
10998 || n == K_RIGHTRELEASE
10999 || n == K_X1MOUSE
11000 || n == K_X1DRAG
11001 || n == K_X1RELEASE
11002 || n == K_X2MOUSE
11003 || n == K_X2DRAG
11004 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011005 || n == K_MOUSELEFT
11006 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011007 || n == K_MOUSEDOWN
11008 || n == K_MOUSEUP)
11009 {
11010 int row = mouse_row;
11011 int col = mouse_col;
11012 win_T *win;
11013 linenr_T lnum;
11014# ifdef FEAT_WINDOWS
11015 win_T *wp;
11016# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011017 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011018
11019 if (row >= 0 && col >= 0)
11020 {
11021 /* Find the window at the mouse coordinates and compute the
11022 * text position. */
11023 win = mouse_find_win(&row, &col);
11024 (void)mouse_comp_pos(win, &row, &col, &lnum);
11025# ifdef FEAT_WINDOWS
11026 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011027 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011028# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011029 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011030 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11031 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11032 }
11033 }
11034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 }
11036}
11037
11038/*
11039 * "getcharmod()" function
11040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011043 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047}
11048
11049/*
11050 * "getcmdline()" function
11051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011053f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011054 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057 rettv->v_type = VAR_STRING;
11058 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059}
11060
11061/*
11062 * "getcmdpos()" function
11063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065f_getcmdpos(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070}
11071
11072/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011073 * "getcmdtype()" function
11074 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011075 static void
11076f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011077 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011078 typval_T *rettv;
11079{
11080 rettv->v_type = VAR_STRING;
11081 rettv->vval.v_string = alloc(2);
11082 if (rettv->vval.v_string != NULL)
11083 {
11084 rettv->vval.v_string[0] = get_cmdline_type();
11085 rettv->vval.v_string[1] = NUL;
11086 }
11087}
11088
11089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 * "getcwd()" function
11091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011093f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011094 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096{
11097 char_u cwd[MAXPATHL];
11098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011099 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 else
11103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011104 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011106 if (rettv->vval.v_string != NULL)
11107 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108#endif
11109 }
11110}
11111
11112/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011113 * "getfontname()" function
11114 */
11115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011117 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011118 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011119{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011120 rettv->v_type = VAR_STRING;
11121 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011122#ifdef FEAT_GUI
11123 if (gui.in_use)
11124 {
11125 GuiFont font;
11126 char_u *name = NULL;
11127
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011128 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011129 {
11130 /* Get the "Normal" font. Either the name saved by
11131 * hl_set_font_name() or from the font ID. */
11132 font = gui.norm_font;
11133 name = hl_get_font_name();
11134 }
11135 else
11136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011138 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11139 return;
11140 font = gui_mch_get_font(name, FALSE);
11141 if (font == NOFONT)
11142 return; /* Invalid font name, return empty string. */
11143 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011145 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011146 gui_mch_free_font(font);
11147 }
11148#endif
11149}
11150
11151/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011152 * "getfperm({fname})" function
11153 */
11154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011156 typval_T *argvars;
11157 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011158{
11159 char_u *fname;
11160 struct stat st;
11161 char_u *perm = NULL;
11162 char_u flags[] = "rwx";
11163 int i;
11164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011168 if (mch_stat((char *)fname, &st) >= 0)
11169 {
11170 perm = vim_strsave((char_u *)"---------");
11171 if (perm != NULL)
11172 {
11173 for (i = 0; i < 9; i++)
11174 {
11175 if (st.st_mode & (1 << (8 - i)))
11176 perm[i] = flags[i % 3];
11177 }
11178 }
11179 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011181}
11182
11183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 * "getfsize({fname})" function
11185 */
11186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011188 typval_T *argvars;
11189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190{
11191 char_u *fname;
11192 struct stat st;
11193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197
11198 if (mch_stat((char *)fname, &st) >= 0)
11199 {
11200 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011201 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011204 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011205
11206 /* non-perfect check for overflow */
11207 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11208 rettv->vval.v_number = -2;
11209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 }
11211 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011212 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213}
11214
11215/*
11216 * "getftime({fname})" function
11217 */
11218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011220 typval_T *argvars;
11221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222{
11223 char_u *fname;
11224 struct stat st;
11225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227
11228 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011231 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232}
11233
11234/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011235 * "getftype({fname})" function
11236 */
11237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011238f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011239 typval_T *argvars;
11240 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011241{
11242 char_u *fname;
11243 struct stat st;
11244 char_u *type = NULL;
11245 char *t;
11246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011247 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011249 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011250 if (mch_lstat((char *)fname, &st) >= 0)
11251 {
11252#ifdef S_ISREG
11253 if (S_ISREG(st.st_mode))
11254 t = "file";
11255 else if (S_ISDIR(st.st_mode))
11256 t = "dir";
11257# ifdef S_ISLNK
11258 else if (S_ISLNK(st.st_mode))
11259 t = "link";
11260# endif
11261# ifdef S_ISBLK
11262 else if (S_ISBLK(st.st_mode))
11263 t = "bdev";
11264# endif
11265# ifdef S_ISCHR
11266 else if (S_ISCHR(st.st_mode))
11267 t = "cdev";
11268# endif
11269# ifdef S_ISFIFO
11270 else if (S_ISFIFO(st.st_mode))
11271 t = "fifo";
11272# endif
11273# ifdef S_ISSOCK
11274 else if (S_ISSOCK(st.st_mode))
11275 t = "fifo";
11276# endif
11277 else
11278 t = "other";
11279#else
11280# ifdef S_IFMT
11281 switch (st.st_mode & S_IFMT)
11282 {
11283 case S_IFREG: t = "file"; break;
11284 case S_IFDIR: t = "dir"; break;
11285# ifdef S_IFLNK
11286 case S_IFLNK: t = "link"; break;
11287# endif
11288# ifdef S_IFBLK
11289 case S_IFBLK: t = "bdev"; break;
11290# endif
11291# ifdef S_IFCHR
11292 case S_IFCHR: t = "cdev"; break;
11293# endif
11294# ifdef S_IFIFO
11295 case S_IFIFO: t = "fifo"; break;
11296# endif
11297# ifdef S_IFSOCK
11298 case S_IFSOCK: t = "socket"; break;
11299# endif
11300 default: t = "other";
11301 }
11302# else
11303 if (mch_isdir(fname))
11304 t = "dir";
11305 else
11306 t = "file";
11307# endif
11308#endif
11309 type = vim_strsave((char_u *)t);
11310 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011312}
11313
11314/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011315 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011316 */
11317 static void
11318f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 typval_T *argvars;
11320 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011321{
11322 linenr_T lnum;
11323 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011324 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011325
11326 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011327 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011328 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011329 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011330 retlist = FALSE;
11331 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011332 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011333 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011334 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011335 retlist = TRUE;
11336 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011337
Bram Moolenaar342337a2005-07-21 21:11:17 +000011338 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011339}
11340
11341/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011342 * "getmatches()" function
11343 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011344 static void
11345f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011346 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011347 typval_T *rettv;
11348{
11349#ifdef FEAT_SEARCH_EXTRA
11350 dict_T *dict;
11351 matchitem_T *cur = curwin->w_match_head;
11352
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011353 if (rettv_list_alloc(rettv) == OK)
11354 {
11355 while (cur != NULL)
11356 {
11357 dict = dict_alloc();
11358 if (dict == NULL)
11359 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011360 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11361 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11362 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11363 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11364 list_append_dict(rettv->vval.v_list, dict);
11365 cur = cur->next;
11366 }
11367 }
11368#endif
11369}
11370
11371/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011372 * "getpid()" function
11373 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011374 static void
11375f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011376 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011377 typval_T *rettv;
11378{
11379 rettv->vval.v_number = mch_get_pid();
11380}
11381
11382/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011383 * "getpos(string)" function
11384 */
11385 static void
11386f_getpos(argvars, rettv)
11387 typval_T *argvars;
11388 typval_T *rettv;
11389{
11390 pos_T *fp;
11391 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011392 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011393
11394 if (rettv_list_alloc(rettv) == OK)
11395 {
11396 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011397 fp = var2fpos(&argvars[0], TRUE, &fnum);
11398 if (fnum != -1)
11399 list_append_number(l, (varnumber_T)fnum);
11400 else
11401 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011402 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11403 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011404 list_append_number(l, (fp != NULL)
11405 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011406 : (varnumber_T)0);
11407 list_append_number(l,
11408#ifdef FEAT_VIRTUALEDIT
11409 (fp != NULL) ? (varnumber_T)fp->coladd :
11410#endif
11411 (varnumber_T)0);
11412 }
11413 else
11414 rettv->vval.v_number = FALSE;
11415}
11416
11417/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011418 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011419 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011420 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011421f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011422 typval_T *argvars UNUSED;
11423 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011424{
11425#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011426 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011427#endif
11428
Bram Moolenaar2641f772005-03-25 21:58:17 +000011429#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011430 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011431 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011432 wp = NULL;
11433 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11434 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011435 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011436 if (wp == NULL)
11437 return;
11438 }
11439
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011440 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011441 }
11442#endif
11443}
11444
11445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 * "getreg()" function
11447 */
11448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011450 typval_T *argvars;
11451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452{
11453 char_u *strregname;
11454 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011455 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011456 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011458 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011460 strregname = get_tv_string_chk(&argvars[0]);
11461 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011462 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011463 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011466 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 regname = (strregname == NULL ? '"' : *strregname);
11468 if (regname == 0)
11469 regname = '"';
11470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011472 rettv->vval.v_string = error ? NULL :
11473 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474}
11475
11476/*
11477 * "getregtype()" function
11478 */
11479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011481 typval_T *argvars;
11482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483{
11484 char_u *strregname;
11485 int regname;
11486 char_u buf[NUMBUFLEN + 2];
11487 long reglen = 0;
11488
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011489 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011490 {
11491 strregname = get_tv_string_chk(&argvars[0]);
11492 if (strregname == NULL) /* type error; errmsg already given */
11493 {
11494 rettv->v_type = VAR_STRING;
11495 rettv->vval.v_string = NULL;
11496 return;
11497 }
11498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 else
11500 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011501 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502
11503 regname = (strregname == NULL ? '"' : *strregname);
11504 if (regname == 0)
11505 regname = '"';
11506
11507 buf[0] = NUL;
11508 buf[1] = NUL;
11509 switch (get_reg_type(regname, &reglen))
11510 {
11511 case MLINE: buf[0] = 'V'; break;
11512 case MCHAR: buf[0] = 'v'; break;
11513#ifdef FEAT_VISUAL
11514 case MBLOCK:
11515 buf[0] = Ctrl_V;
11516 sprintf((char *)buf + 1, "%ld", reglen + 1);
11517 break;
11518#endif
11519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520 rettv->v_type = VAR_STRING;
11521 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522}
11523
11524/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011525 * "gettabvar()" function
11526 */
11527 static void
11528f_gettabvar(argvars, rettv)
11529 typval_T *argvars;
11530 typval_T *rettv;
11531{
11532 tabpage_T *tp;
11533 dictitem_T *v;
11534 char_u *varname;
11535
11536 rettv->v_type = VAR_STRING;
11537 rettv->vval.v_string = NULL;
11538
11539 varname = get_tv_string_chk(&argvars[1]);
11540 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11541 if (tp != NULL && varname != NULL)
11542 {
11543 /* look up the variable */
11544 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11545 if (v != NULL)
11546 copy_tv(&v->di_tv, rettv);
11547 }
11548}
11549
11550/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011551 * "gettabwinvar()" function
11552 */
11553 static void
11554f_gettabwinvar(argvars, rettv)
11555 typval_T *argvars;
11556 typval_T *rettv;
11557{
11558 getwinvar(argvars, rettv, 1);
11559}
11560
11561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 * "getwinposx()" function
11563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011565f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011566 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011569 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570#ifdef FEAT_GUI
11571 if (gui.in_use)
11572 {
11573 int x, y;
11574
11575 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011576 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 }
11578#endif
11579}
11580
11581/*
11582 * "getwinposy()" function
11583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011586 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590#ifdef FEAT_GUI
11591 if (gui.in_use)
11592 {
11593 int x, y;
11594
11595 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 }
11598#endif
11599}
11600
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011601/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011602 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011603 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011604 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011605find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011606 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011607 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011608{
11609#ifdef FEAT_WINDOWS
11610 win_T *wp;
11611#endif
11612 int nr;
11613
11614 nr = get_tv_number_chk(vp, NULL);
11615
11616#ifdef FEAT_WINDOWS
11617 if (nr < 0)
11618 return NULL;
11619 if (nr == 0)
11620 return curwin;
11621
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011622 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11623 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011624 if (--nr <= 0)
11625 break;
11626 return wp;
11627#else
11628 if (nr == 0 || nr == 1)
11629 return curwin;
11630 return NULL;
11631#endif
11632}
11633
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634/*
11635 * "getwinvar()" function
11636 */
11637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011638f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011639 typval_T *argvars;
11640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011642 getwinvar(argvars, rettv, 0);
11643}
11644
11645/*
11646 * getwinvar() and gettabwinvar()
11647 */
11648 static void
11649getwinvar(argvars, rettv, off)
11650 typval_T *argvars;
11651 typval_T *rettv;
11652 int off; /* 1 for gettabwinvar() */
11653{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 win_T *win, *oldcurwin;
11655 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011656 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011657 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011659#ifdef FEAT_WINDOWS
11660 if (off == 1)
11661 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11662 else
11663 tp = curtab;
11664#endif
11665 win = find_win_by_nr(&argvars[off], tp);
11666 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011669 rettv->v_type = VAR_STRING;
11670 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671
11672 if (win != NULL && varname != NULL)
11673 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011674 /* Set curwin to be our win, temporarily. Also set curbuf, so
11675 * that we can get buffer-local options. */
11676 oldcurwin = curwin;
11677 curwin = win;
11678 curbuf = win->w_buffer;
11679
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 else
11683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011684 if (*varname == NUL)
11685 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11686 * scope prefix before the NUL byte is required by
11687 * find_var_in_ht(). */
11688 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011690 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011692 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011694
11695 /* restore previous notion of curwin */
11696 curwin = oldcurwin;
11697 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 }
11699
11700 --emsg_off;
11701}
11702
11703/*
11704 * "glob()" function
11705 */
11706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011707f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011708 typval_T *argvars;
11709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011711 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011713 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011715 /* When the optional second argument is non-zero, don't remove matches
11716 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11717 if (argvars[1].v_type != VAR_UNKNOWN
11718 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011719 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011721 if (!error)
11722 {
11723 ExpandInit(&xpc);
11724 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011725 if (p_wic)
11726 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011727 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011728 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011729 }
11730 else
11731 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732}
11733
11734/*
11735 * "globpath()" function
11736 */
11737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011738f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011739 typval_T *argvars;
11740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011742 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011745 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011747 /* When the optional second argument is non-zero, don't remove matches
11748 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11749 if (argvars[2].v_type != VAR_UNKNOWN
11750 && get_tv_number_chk(&argvars[2], &error))
11751 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011753 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011754 rettv->vval.v_string = NULL;
11755 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011756 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11757 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758}
11759
11760/*
11761 * "has()" function
11762 */
11763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011765 typval_T *argvars;
11766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767{
11768 int i;
11769 char_u *name;
11770 int n = FALSE;
11771 static char *(has_list[]) =
11772 {
11773#ifdef AMIGA
11774 "amiga",
11775# ifdef FEAT_ARP
11776 "arp",
11777# endif
11778#endif
11779#ifdef __BEOS__
11780 "beos",
11781#endif
11782#ifdef MSDOS
11783# ifdef DJGPP
11784 "dos32",
11785# else
11786 "dos16",
11787# endif
11788#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011789#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 "mac",
11791#endif
11792#if defined(MACOS_X_UNIX)
11793 "macunix",
11794#endif
11795#ifdef OS2
11796 "os2",
11797#endif
11798#ifdef __QNX__
11799 "qnx",
11800#endif
11801#ifdef RISCOS
11802 "riscos",
11803#endif
11804#ifdef UNIX
11805 "unix",
11806#endif
11807#ifdef VMS
11808 "vms",
11809#endif
11810#ifdef WIN16
11811 "win16",
11812#endif
11813#ifdef WIN32
11814 "win32",
11815#endif
11816#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11817 "win32unix",
11818#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011819#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820 "win64",
11821#endif
11822#ifdef EBCDIC
11823 "ebcdic",
11824#endif
11825#ifndef CASE_INSENSITIVE_FILENAME
11826 "fname_case",
11827#endif
11828#ifdef FEAT_ARABIC
11829 "arabic",
11830#endif
11831#ifdef FEAT_AUTOCMD
11832 "autocmd",
11833#endif
11834#ifdef FEAT_BEVAL
11835 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011836# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11837 "balloon_multiline",
11838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839#endif
11840#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11841 "builtin_terms",
11842# ifdef ALL_BUILTIN_TCAPS
11843 "all_builtin_terms",
11844# endif
11845#endif
11846#ifdef FEAT_BYTEOFF
11847 "byte_offset",
11848#endif
11849#ifdef FEAT_CINDENT
11850 "cindent",
11851#endif
11852#ifdef FEAT_CLIENTSERVER
11853 "clientserver",
11854#endif
11855#ifdef FEAT_CLIPBOARD
11856 "clipboard",
11857#endif
11858#ifdef FEAT_CMDL_COMPL
11859 "cmdline_compl",
11860#endif
11861#ifdef FEAT_CMDHIST
11862 "cmdline_hist",
11863#endif
11864#ifdef FEAT_COMMENTS
11865 "comments",
11866#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011867#ifdef FEAT_CONCEAL
11868 "conceal",
11869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870#ifdef FEAT_CRYPT
11871 "cryptv",
11872#endif
11873#ifdef FEAT_CSCOPE
11874 "cscope",
11875#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011876#ifdef FEAT_CURSORBIND
11877 "cursorbind",
11878#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011879#ifdef CURSOR_SHAPE
11880 "cursorshape",
11881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882#ifdef DEBUG
11883 "debug",
11884#endif
11885#ifdef FEAT_CON_DIALOG
11886 "dialog_con",
11887#endif
11888#ifdef FEAT_GUI_DIALOG
11889 "dialog_gui",
11890#endif
11891#ifdef FEAT_DIFF
11892 "diff",
11893#endif
11894#ifdef FEAT_DIGRAPHS
11895 "digraphs",
11896#endif
11897#ifdef FEAT_DND
11898 "dnd",
11899#endif
11900#ifdef FEAT_EMACS_TAGS
11901 "emacs_tags",
11902#endif
11903 "eval", /* always present, of course! */
11904#ifdef FEAT_EX_EXTRA
11905 "ex_extra",
11906#endif
11907#ifdef FEAT_SEARCH_EXTRA
11908 "extra_search",
11909#endif
11910#ifdef FEAT_FKMAP
11911 "farsi",
11912#endif
11913#ifdef FEAT_SEARCHPATH
11914 "file_in_path",
11915#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011916#if defined(UNIX) && !defined(USE_SYSTEM)
11917 "filterpipe",
11918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919#ifdef FEAT_FIND_ID
11920 "find_in_path",
11921#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011922#ifdef FEAT_FLOAT
11923 "float",
11924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925#ifdef FEAT_FOLDING
11926 "folding",
11927#endif
11928#ifdef FEAT_FOOTER
11929 "footer",
11930#endif
11931#if !defined(USE_SYSTEM) && defined(UNIX)
11932 "fork",
11933#endif
11934#ifdef FEAT_GETTEXT
11935 "gettext",
11936#endif
11937#ifdef FEAT_GUI
11938 "gui",
11939#endif
11940#ifdef FEAT_GUI_ATHENA
11941# ifdef FEAT_GUI_NEXTAW
11942 "gui_neXtaw",
11943# else
11944 "gui_athena",
11945# endif
11946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947#ifdef FEAT_GUI_GTK
11948 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011951#ifdef FEAT_GUI_GNOME
11952 "gui_gnome",
11953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954#ifdef FEAT_GUI_MAC
11955 "gui_mac",
11956#endif
11957#ifdef FEAT_GUI_MOTIF
11958 "gui_motif",
11959#endif
11960#ifdef FEAT_GUI_PHOTON
11961 "gui_photon",
11962#endif
11963#ifdef FEAT_GUI_W16
11964 "gui_win16",
11965#endif
11966#ifdef FEAT_GUI_W32
11967 "gui_win32",
11968#endif
11969#ifdef FEAT_HANGULIN
11970 "hangul_input",
11971#endif
11972#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11973 "iconv",
11974#endif
11975#ifdef FEAT_INS_EXPAND
11976 "insert_expand",
11977#endif
11978#ifdef FEAT_JUMPLIST
11979 "jumplist",
11980#endif
11981#ifdef FEAT_KEYMAP
11982 "keymap",
11983#endif
11984#ifdef FEAT_LANGMAP
11985 "langmap",
11986#endif
11987#ifdef FEAT_LIBCALL
11988 "libcall",
11989#endif
11990#ifdef FEAT_LINEBREAK
11991 "linebreak",
11992#endif
11993#ifdef FEAT_LISP
11994 "lispindent",
11995#endif
11996#ifdef FEAT_LISTCMDS
11997 "listcmds",
11998#endif
11999#ifdef FEAT_LOCALMAP
12000 "localmap",
12001#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012002#ifdef FEAT_LUA
12003# ifndef DYNAMIC_LUA
12004 "lua",
12005# endif
12006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007#ifdef FEAT_MENU
12008 "menu",
12009#endif
12010#ifdef FEAT_SESSION
12011 "mksession",
12012#endif
12013#ifdef FEAT_MODIFY_FNAME
12014 "modify_fname",
12015#endif
12016#ifdef FEAT_MOUSE
12017 "mouse",
12018#endif
12019#ifdef FEAT_MOUSESHAPE
12020 "mouseshape",
12021#endif
12022#if defined(UNIX) || defined(VMS)
12023# ifdef FEAT_MOUSE_DEC
12024 "mouse_dec",
12025# endif
12026# ifdef FEAT_MOUSE_GPM
12027 "mouse_gpm",
12028# endif
12029# ifdef FEAT_MOUSE_JSB
12030 "mouse_jsbterm",
12031# endif
12032# ifdef FEAT_MOUSE_NET
12033 "mouse_netterm",
12034# endif
12035# ifdef FEAT_MOUSE_PTERM
12036 "mouse_pterm",
12037# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012038# ifdef FEAT_SYSMOUSE
12039 "mouse_sysmouse",
12040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041# ifdef FEAT_MOUSE_XTERM
12042 "mouse_xterm",
12043# endif
12044#endif
12045#ifdef FEAT_MBYTE
12046 "multi_byte",
12047#endif
12048#ifdef FEAT_MBYTE_IME
12049 "multi_byte_ime",
12050#endif
12051#ifdef FEAT_MULTI_LANG
12052 "multi_lang",
12053#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012054#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012055#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012056 "mzscheme",
12057#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059#ifdef FEAT_OLE
12060 "ole",
12061#endif
12062#ifdef FEAT_OSFILETYPE
12063 "osfiletype",
12064#endif
12065#ifdef FEAT_PATH_EXTRA
12066 "path_extra",
12067#endif
12068#ifdef FEAT_PERL
12069#ifndef DYNAMIC_PERL
12070 "perl",
12071#endif
12072#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012073#ifdef FEAT_PERSISTENT_UNDO
12074 "persistent_undo",
12075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076#ifdef FEAT_PYTHON
12077#ifndef DYNAMIC_PYTHON
12078 "python",
12079#endif
12080#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012081#ifdef FEAT_PYTHON3
12082#ifndef DYNAMIC_PYTHON3
12083 "python3",
12084#endif
12085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086#ifdef FEAT_POSTSCRIPT
12087 "postscript",
12088#endif
12089#ifdef FEAT_PRINTER
12090 "printer",
12091#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012092#ifdef FEAT_PROFILE
12093 "profile",
12094#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012095#ifdef FEAT_RELTIME
12096 "reltime",
12097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098#ifdef FEAT_QUICKFIX
12099 "quickfix",
12100#endif
12101#ifdef FEAT_RIGHTLEFT
12102 "rightleft",
12103#endif
12104#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12105 "ruby",
12106#endif
12107#ifdef FEAT_SCROLLBIND
12108 "scrollbind",
12109#endif
12110#ifdef FEAT_CMDL_INFO
12111 "showcmd",
12112 "cmdline_info",
12113#endif
12114#ifdef FEAT_SIGNS
12115 "signs",
12116#endif
12117#ifdef FEAT_SMARTINDENT
12118 "smartindent",
12119#endif
12120#ifdef FEAT_SNIFF
12121 "sniff",
12122#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012123#ifdef STARTUPTIME
12124 "startuptime",
12125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126#ifdef FEAT_STL_OPT
12127 "statusline",
12128#endif
12129#ifdef FEAT_SUN_WORKSHOP
12130 "sun_workshop",
12131#endif
12132#ifdef FEAT_NETBEANS_INTG
12133 "netbeans_intg",
12134#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012135#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012136 "spell",
12137#endif
12138#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139 "syntax",
12140#endif
12141#if defined(USE_SYSTEM) || !defined(UNIX)
12142 "system",
12143#endif
12144#ifdef FEAT_TAG_BINS
12145 "tag_binary",
12146#endif
12147#ifdef FEAT_TAG_OLDSTATIC
12148 "tag_old_static",
12149#endif
12150#ifdef FEAT_TAG_ANYWHITE
12151 "tag_any_white",
12152#endif
12153#ifdef FEAT_TCL
12154# ifndef DYNAMIC_TCL
12155 "tcl",
12156# endif
12157#endif
12158#ifdef TERMINFO
12159 "terminfo",
12160#endif
12161#ifdef FEAT_TERMRESPONSE
12162 "termresponse",
12163#endif
12164#ifdef FEAT_TEXTOBJ
12165 "textobjects",
12166#endif
12167#ifdef HAVE_TGETENT
12168 "tgetent",
12169#endif
12170#ifdef FEAT_TITLE
12171 "title",
12172#endif
12173#ifdef FEAT_TOOLBAR
12174 "toolbar",
12175#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012176#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12177 "unnamedplus",
12178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179#ifdef FEAT_USR_CMDS
12180 "user-commands", /* was accidentally included in 5.4 */
12181 "user_commands",
12182#endif
12183#ifdef FEAT_VIMINFO
12184 "viminfo",
12185#endif
12186#ifdef FEAT_VERTSPLIT
12187 "vertsplit",
12188#endif
12189#ifdef FEAT_VIRTUALEDIT
12190 "virtualedit",
12191#endif
12192#ifdef FEAT_VISUAL
12193 "visual",
12194#endif
12195#ifdef FEAT_VISUALEXTRA
12196 "visualextra",
12197#endif
12198#ifdef FEAT_VREPLACE
12199 "vreplace",
12200#endif
12201#ifdef FEAT_WILDIGN
12202 "wildignore",
12203#endif
12204#ifdef FEAT_WILDMENU
12205 "wildmenu",
12206#endif
12207#ifdef FEAT_WINDOWS
12208 "windows",
12209#endif
12210#ifdef FEAT_WAK
12211 "winaltkeys",
12212#endif
12213#ifdef FEAT_WRITEBACKUP
12214 "writebackup",
12215#endif
12216#ifdef FEAT_XIM
12217 "xim",
12218#endif
12219#ifdef FEAT_XFONTSET
12220 "xfontset",
12221#endif
12222#ifdef USE_XSMP
12223 "xsmp",
12224#endif
12225#ifdef USE_XSMP_INTERACT
12226 "xsmp_interact",
12227#endif
12228#ifdef FEAT_XCLIPBOARD
12229 "xterm_clipboard",
12230#endif
12231#ifdef FEAT_XTERM_SAVE
12232 "xterm_save",
12233#endif
12234#if defined(UNIX) && defined(FEAT_X11)
12235 "X11",
12236#endif
12237 NULL
12238 };
12239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012240 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241 for (i = 0; has_list[i] != NULL; ++i)
12242 if (STRICMP(name, has_list[i]) == 0)
12243 {
12244 n = TRUE;
12245 break;
12246 }
12247
12248 if (n == FALSE)
12249 {
12250 if (STRNICMP(name, "patch", 5) == 0)
12251 n = has_patch(atoi((char *)name + 5));
12252 else if (STRICMP(name, "vim_starting") == 0)
12253 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012254#ifdef FEAT_MBYTE
12255 else if (STRICMP(name, "multi_byte_encoding") == 0)
12256 n = has_mbyte;
12257#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012258#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12259 else if (STRICMP(name, "balloon_multiline") == 0)
12260 n = multiline_balloon_available();
12261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262#ifdef DYNAMIC_TCL
12263 else if (STRICMP(name, "tcl") == 0)
12264 n = tcl_enabled(FALSE);
12265#endif
12266#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12267 else if (STRICMP(name, "iconv") == 0)
12268 n = iconv_enabled(FALSE);
12269#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012270#ifdef DYNAMIC_LUA
12271 else if (STRICMP(name, "lua") == 0)
12272 n = lua_enabled(FALSE);
12273#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012274#ifdef DYNAMIC_MZSCHEME
12275 else if (STRICMP(name, "mzscheme") == 0)
12276 n = mzscheme_enabled(FALSE);
12277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278#ifdef DYNAMIC_RUBY
12279 else if (STRICMP(name, "ruby") == 0)
12280 n = ruby_enabled(FALSE);
12281#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012282#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283#ifdef DYNAMIC_PYTHON
12284 else if (STRICMP(name, "python") == 0)
12285 n = python_enabled(FALSE);
12286#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012287#endif
12288#ifdef FEAT_PYTHON3
12289#ifdef DYNAMIC_PYTHON3
12290 else if (STRICMP(name, "python3") == 0)
12291 n = python3_enabled(FALSE);
12292#endif
12293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294#ifdef DYNAMIC_PERL
12295 else if (STRICMP(name, "perl") == 0)
12296 n = perl_enabled(FALSE);
12297#endif
12298#ifdef FEAT_GUI
12299 else if (STRICMP(name, "gui_running") == 0)
12300 n = (gui.in_use || gui.starting);
12301# ifdef FEAT_GUI_W32
12302 else if (STRICMP(name, "gui_win32s") == 0)
12303 n = gui_is_win32s();
12304# endif
12305# ifdef FEAT_BROWSE
12306 else if (STRICMP(name, "browse") == 0)
12307 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12308# endif
12309#endif
12310#ifdef FEAT_SYN_HL
12311 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012312 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313#endif
12314#if defined(WIN3264)
12315 else if (STRICMP(name, "win95") == 0)
12316 n = mch_windows95();
12317#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012318#ifdef FEAT_NETBEANS_INTG
12319 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012320 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 }
12323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325}
12326
12327/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012328 * "has_key()" function
12329 */
12330 static void
12331f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012332 typval_T *argvars;
12333 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012334{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012335 if (argvars[0].v_type != VAR_DICT)
12336 {
12337 EMSG(_(e_dictreq));
12338 return;
12339 }
12340 if (argvars[0].vval.v_dict == NULL)
12341 return;
12342
12343 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012344 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012345}
12346
12347/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012348 * "haslocaldir()" function
12349 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012350 static void
12351f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012352 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012353 typval_T *rettv;
12354{
12355 rettv->vval.v_number = (curwin->w_localdir != NULL);
12356}
12357
12358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359 * "hasmapto()" function
12360 */
12361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012362f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012363 typval_T *argvars;
12364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365{
12366 char_u *name;
12367 char_u *mode;
12368 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012369 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012372 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 mode = (char_u *)"nvo";
12374 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012377 if (argvars[2].v_type != VAR_UNKNOWN)
12378 abbr = get_tv_number(&argvars[2]);
12379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380
Bram Moolenaar2c932302006-03-18 21:42:09 +000012381 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012382 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385}
12386
12387/*
12388 * "histadd()" function
12389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012392 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394{
12395#ifdef FEAT_CMDHIST
12396 int histype;
12397 char_u *str;
12398 char_u buf[NUMBUFLEN];
12399#endif
12400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012401 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402 if (check_restricted() || check_secure())
12403 return;
12404#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12406 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 if (histype >= 0)
12408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 if (*str != NUL)
12411 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012412 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012414 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415 return;
12416 }
12417 }
12418#endif
12419}
12420
12421/*
12422 * "histdel()" function
12423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012425f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012426 typval_T *argvars UNUSED;
12427 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428{
12429#ifdef FEAT_CMDHIST
12430 int n;
12431 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012432 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012434 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12435 if (str == NULL)
12436 n = 0;
12437 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012439 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012440 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012442 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012443 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444 else
12445 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012446 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012447 get_tv_string_buf(&argvars[1], buf));
12448 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449#endif
12450}
12451
12452/*
12453 * "histget()" function
12454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012456f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012457 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459{
12460#ifdef FEAT_CMDHIST
12461 int type;
12462 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012463 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012465 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12466 if (str == NULL)
12467 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012469 {
12470 type = get_histtype(str);
12471 if (argvars[1].v_type == VAR_UNKNOWN)
12472 idx = get_history_idx(type);
12473 else
12474 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12475 /* -1 on type error */
12476 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482}
12483
12484/*
12485 * "histnr()" function
12486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012488f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012489 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491{
12492 int i;
12493
12494#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012495 char_u *history = get_tv_string_chk(&argvars[0]);
12496
12497 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498 if (i >= HIST_CMD && i < HIST_COUNT)
12499 i = get_history_idx(i);
12500 else
12501#endif
12502 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012503 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504}
12505
12506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 * "highlightID(name)" function
12508 */
12509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012510f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012511 typval_T *argvars;
12512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012514 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515}
12516
12517/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012518 * "highlight_exists()" function
12519 */
12520 static void
12521f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012522 typval_T *argvars;
12523 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012524{
12525 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12526}
12527
12528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 * "hostname()" function
12530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012533 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535{
12536 char_u hostname[256];
12537
12538 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012539 rettv->v_type = VAR_STRING;
12540 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541}
12542
12543/*
12544 * iconv() function
12545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012547f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012548 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550{
12551#ifdef FEAT_MBYTE
12552 char_u buf1[NUMBUFLEN];
12553 char_u buf2[NUMBUFLEN];
12554 char_u *from, *to, *str;
12555 vimconv_T vimconv;
12556#endif
12557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558 rettv->v_type = VAR_STRING;
12559 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560
12561#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012562 str = get_tv_string(&argvars[0]);
12563 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12564 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 vimconv.vc_type = CONV_NONE;
12566 convert_setup(&vimconv, from, to);
12567
12568 /* If the encodings are equal, no conversion needed. */
12569 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012570 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012572 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573
12574 convert_setup(&vimconv, NULL, NULL);
12575 vim_free(from);
12576 vim_free(to);
12577#endif
12578}
12579
12580/*
12581 * "indent()" function
12582 */
12583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012584f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012585 typval_T *argvars;
12586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587{
12588 linenr_T lnum;
12589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012590 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012592 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012594 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595}
12596
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012597/*
12598 * "index()" function
12599 */
12600 static void
12601f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012602 typval_T *argvars;
12603 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012604{
Bram Moolenaar33570922005-01-25 22:26:29 +000012605 list_T *l;
12606 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012607 long idx = 0;
12608 int ic = FALSE;
12609
12610 rettv->vval.v_number = -1;
12611 if (argvars[0].v_type != VAR_LIST)
12612 {
12613 EMSG(_(e_listreq));
12614 return;
12615 }
12616 l = argvars[0].vval.v_list;
12617 if (l != NULL)
12618 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012619 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012620 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012622 int error = FALSE;
12623
Bram Moolenaar758711c2005-02-02 23:11:38 +000012624 /* Start at specified item. Use the cached index that list_find()
12625 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012626 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012627 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012628 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012629 ic = get_tv_number_chk(&argvars[3], &error);
12630 if (error)
12631 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012632 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012633
Bram Moolenaar758711c2005-02-02 23:11:38 +000012634 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012635 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012636 {
12637 rettv->vval.v_number = idx;
12638 break;
12639 }
12640 }
12641}
12642
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643static int inputsecret_flag = 0;
12644
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012645static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12646
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012648 * This function is used by f_input() and f_inputdialog() functions. The third
12649 * argument to f_input() specifies the type of completion to use at the
12650 * prompt. The third argument to f_inputdialog() specifies the value to return
12651 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 */
12653 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012654get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012655 typval_T *argvars;
12656 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012657 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012659 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660 char_u *p = NULL;
12661 int c;
12662 char_u buf[NUMBUFLEN];
12663 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012664 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012665 int xp_type = EXPAND_NOTHING;
12666 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012668 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012669 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670
12671#ifdef NO_CONSOLE_INPUT
12672 /* While starting up, there is no place to enter text. */
12673 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675#endif
12676
12677 cmd_silent = FALSE; /* Want to see the prompt. */
12678 if (prompt != NULL)
12679 {
12680 /* Only the part of the message after the last NL is considered as
12681 * prompt for the command line */
12682 p = vim_strrchr(prompt, '\n');
12683 if (p == NULL)
12684 p = prompt;
12685 else
12686 {
12687 ++p;
12688 c = *p;
12689 *p = NUL;
12690 msg_start();
12691 msg_clr_eos();
12692 msg_puts_attr(prompt, echo_attr);
12693 msg_didout = FALSE;
12694 msg_starthere();
12695 *p = c;
12696 }
12697 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012699 if (argvars[1].v_type != VAR_UNKNOWN)
12700 {
12701 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12702 if (defstr != NULL)
12703 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012705 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012706 {
12707 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012708 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012709 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012710
Bram Moolenaar4463f292005-09-25 22:20:24 +000012711 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012712
Bram Moolenaar4463f292005-09-25 22:20:24 +000012713 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12714 if (xp_name == NULL)
12715 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012716
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012717 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012718
Bram Moolenaar4463f292005-09-25 22:20:24 +000012719 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12720 &xp_arg) == FAIL)
12721 return;
12722 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012723 }
12724
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012725 if (defstr != NULL)
12726 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012727 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12728 xp_type, xp_arg);
12729
12730 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012732 /* since the user typed this, no need to wait for return */
12733 need_wait_return = FALSE;
12734 msg_didout = FALSE;
12735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 cmd_silent = cmd_silent_save;
12737}
12738
12739/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012740 * "input()" function
12741 * Also handles inputsecret() when inputsecret is set.
12742 */
12743 static void
12744f_input(argvars, rettv)
12745 typval_T *argvars;
12746 typval_T *rettv;
12747{
12748 get_user_input(argvars, rettv, FALSE);
12749}
12750
12751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752 * "inputdialog()" function
12753 */
12754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012756 typval_T *argvars;
12757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758{
12759#if defined(FEAT_GUI_TEXTDIALOG)
12760 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12761 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12762 {
12763 char_u *message;
12764 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012765 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012767 message = get_tv_string_chk(&argvars[0]);
12768 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012769 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012770 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771 else
12772 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012773 if (message != NULL && defstr != NULL
12774 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012775 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 else
12778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012779 if (message != NULL && defstr != NULL
12780 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012781 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782 rettv->vval.v_string = vim_strsave(
12783 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 }
12789 else
12790#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012791 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792}
12793
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012794/*
12795 * "inputlist()" function
12796 */
12797 static void
12798f_inputlist(argvars, rettv)
12799 typval_T *argvars;
12800 typval_T *rettv;
12801{
12802 listitem_T *li;
12803 int selected;
12804 int mouse_used;
12805
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012806#ifdef NO_CONSOLE_INPUT
12807 /* While starting up, there is no place to enter text. */
12808 if (no_console_input())
12809 return;
12810#endif
12811 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12812 {
12813 EMSG2(_(e_listarg), "inputlist()");
12814 return;
12815 }
12816
12817 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012818 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012819 lines_left = Rows; /* avoid more prompt */
12820 msg_scroll = TRUE;
12821 msg_clr_eos();
12822
12823 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12824 {
12825 msg_puts(get_tv_string(&li->li_tv));
12826 msg_putchar('\n');
12827 }
12828
12829 /* Ask for choice. */
12830 selected = prompt_for_number(&mouse_used);
12831 if (mouse_used)
12832 selected -= lines_left;
12833
12834 rettv->vval.v_number = selected;
12835}
12836
12837
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12839
12840/*
12841 * "inputrestore()" function
12842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012845 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847{
12848 if (ga_userinput.ga_len > 0)
12849 {
12850 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12852 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012853 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 }
12855 else if (p_verbose > 1)
12856 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012857 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012858 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859 }
12860}
12861
12862/*
12863 * "inputsave()" function
12864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012867 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012870 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 if (ga_grow(&ga_userinput, 1) == OK)
12872 {
12873 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12874 + ga_userinput.ga_len);
12875 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012876 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 }
12878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880}
12881
12882/*
12883 * "inputsecret()" function
12884 */
12885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012887 typval_T *argvars;
12888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889{
12890 ++cmdline_star;
12891 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 --cmdline_star;
12894 --inputsecret_flag;
12895}
12896
12897/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012898 * "insert()" function
12899 */
12900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012902 typval_T *argvars;
12903 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012904{
12905 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012906 listitem_T *item;
12907 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012908 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012909
12910 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012911 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012912 else if ((l = argvars[0].vval.v_list) != NULL
12913 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012914 {
12915 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012916 before = get_tv_number_chk(&argvars[2], &error);
12917 if (error)
12918 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012919
Bram Moolenaar758711c2005-02-02 23:11:38 +000012920 if (before == l->lv_len)
12921 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012922 else
12923 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012924 item = list_find(l, before);
12925 if (item == NULL)
12926 {
12927 EMSGN(_(e_listidx), before);
12928 l = NULL;
12929 }
12930 }
12931 if (l != NULL)
12932 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012933 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012934 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012935 }
12936 }
12937}
12938
12939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 * "isdirectory()" function
12941 */
12942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012944 typval_T *argvars;
12945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012947 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948}
12949
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012950/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012951 * "islocked()" function
12952 */
12953 static void
12954f_islocked(argvars, rettv)
12955 typval_T *argvars;
12956 typval_T *rettv;
12957{
12958 lval_T lv;
12959 char_u *end;
12960 dictitem_T *di;
12961
12962 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012963 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12964 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012965 if (end != NULL && lv.ll_name != NULL)
12966 {
12967 if (*end != NUL)
12968 EMSG(_(e_trailing));
12969 else
12970 {
12971 if (lv.ll_tv == NULL)
12972 {
12973 if (check_changedtick(lv.ll_name))
12974 rettv->vval.v_number = 1; /* always locked */
12975 else
12976 {
12977 di = find_var(lv.ll_name, NULL);
12978 if (di != NULL)
12979 {
12980 /* Consider a variable locked when:
12981 * 1. the variable itself is locked
12982 * 2. the value of the variable is locked.
12983 * 3. the List or Dict value is locked.
12984 */
12985 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12986 || tv_islocked(&di->di_tv));
12987 }
12988 }
12989 }
12990 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012991 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012992 else if (lv.ll_newkey != NULL)
12993 EMSG2(_(e_dictkey), lv.ll_newkey);
12994 else if (lv.ll_list != NULL)
12995 /* List item. */
12996 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12997 else
12998 /* Dictionary item. */
12999 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13000 }
13001 }
13002
13003 clear_lval(&lv);
13004}
13005
Bram Moolenaar33570922005-01-25 22:26:29 +000013006static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013007
13008/*
13009 * Turn a dict into a list:
13010 * "what" == 0: list of keys
13011 * "what" == 1: list of values
13012 * "what" == 2: list of items
13013 */
13014 static void
13015dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013016 typval_T *argvars;
13017 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013018 int what;
13019{
Bram Moolenaar33570922005-01-25 22:26:29 +000013020 list_T *l2;
13021 dictitem_T *di;
13022 hashitem_T *hi;
13023 listitem_T *li;
13024 listitem_T *li2;
13025 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013026 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013027
Bram Moolenaar8c711452005-01-14 21:53:12 +000013028 if (argvars[0].v_type != VAR_DICT)
13029 {
13030 EMSG(_(e_dictreq));
13031 return;
13032 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013033 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013034 return;
13035
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013036 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013037 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013038
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013039 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013040 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013041 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013042 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013043 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013044 --todo;
13045 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013046
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013047 li = listitem_alloc();
13048 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013049 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013050 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013051
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013052 if (what == 0)
13053 {
13054 /* keys() */
13055 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013056 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013057 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13058 }
13059 else if (what == 1)
13060 {
13061 /* values() */
13062 copy_tv(&di->di_tv, &li->li_tv);
13063 }
13064 else
13065 {
13066 /* items() */
13067 l2 = list_alloc();
13068 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013069 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013070 li->li_tv.vval.v_list = l2;
13071 if (l2 == NULL)
13072 break;
13073 ++l2->lv_refcount;
13074
13075 li2 = listitem_alloc();
13076 if (li2 == NULL)
13077 break;
13078 list_append(l2, li2);
13079 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013080 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013081 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13082
13083 li2 = listitem_alloc();
13084 if (li2 == NULL)
13085 break;
13086 list_append(l2, li2);
13087 copy_tv(&di->di_tv, &li2->li_tv);
13088 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013089 }
13090 }
13091}
13092
13093/*
13094 * "items(dict)" function
13095 */
13096 static void
13097f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013098 typval_T *argvars;
13099 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013100{
13101 dict_list(argvars, rettv, 2);
13102}
13103
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013105 * "join()" function
13106 */
13107 static void
13108f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013109 typval_T *argvars;
13110 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013111{
13112 garray_T ga;
13113 char_u *sep;
13114
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013115 if (argvars[0].v_type != VAR_LIST)
13116 {
13117 EMSG(_(e_listreq));
13118 return;
13119 }
13120 if (argvars[0].vval.v_list == NULL)
13121 return;
13122 if (argvars[1].v_type == VAR_UNKNOWN)
13123 sep = (char_u *)" ";
13124 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013125 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013126
13127 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013128
13129 if (sep != NULL)
13130 {
13131 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013132 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013133 ga_append(&ga, NUL);
13134 rettv->vval.v_string = (char_u *)ga.ga_data;
13135 }
13136 else
13137 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013138}
13139
13140/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013141 * "keys()" function
13142 */
13143 static void
13144f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013145 typval_T *argvars;
13146 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013147{
13148 dict_list(argvars, rettv, 0);
13149}
13150
13151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152 * "last_buffer_nr()" function.
13153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013155f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013156 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158{
13159 int n = 0;
13160 buf_T *buf;
13161
13162 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13163 if (n < buf->b_fnum)
13164 n = buf->b_fnum;
13165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013166 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013167}
13168
13169/*
13170 * "len()" function
13171 */
13172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013174 typval_T *argvars;
13175 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013176{
13177 switch (argvars[0].v_type)
13178 {
13179 case VAR_STRING:
13180 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013181 rettv->vval.v_number = (varnumber_T)STRLEN(
13182 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013183 break;
13184 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013186 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013187 case VAR_DICT:
13188 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13189 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013190 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013191 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013192 break;
13193 }
13194}
13195
Bram Moolenaar33570922005-01-25 22:26:29 +000013196static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013197
13198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013200 typval_T *argvars;
13201 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013202 int type;
13203{
13204#ifdef FEAT_LIBCALL
13205 char_u *string_in;
13206 char_u **string_result;
13207 int nr_result;
13208#endif
13209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013210 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013211 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013212 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013213
13214 if (check_restricted() || check_secure())
13215 return;
13216
13217#ifdef FEAT_LIBCALL
13218 /* The first two args must be strings, otherwise its meaningless */
13219 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13220 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013221 string_in = NULL;
13222 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013223 string_in = argvars[2].vval.v_string;
13224 if (type == VAR_NUMBER)
13225 string_result = NULL;
13226 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013227 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013228 if (mch_libcall(argvars[0].vval.v_string,
13229 argvars[1].vval.v_string,
13230 string_in,
13231 argvars[2].vval.v_number,
13232 string_result,
13233 &nr_result) == OK
13234 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013236 }
13237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238}
13239
13240/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013241 * "libcall()" function
13242 */
13243 static void
13244f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013245 typval_T *argvars;
13246 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013247{
13248 libcall_common(argvars, rettv, VAR_STRING);
13249}
13250
13251/*
13252 * "libcallnr()" function
13253 */
13254 static void
13255f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013256 typval_T *argvars;
13257 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013258{
13259 libcall_common(argvars, rettv, VAR_NUMBER);
13260}
13261
13262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 * "line(string)" function
13264 */
13265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013266f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013267 typval_T *argvars;
13268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269{
13270 linenr_T lnum = 0;
13271 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013272 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013274 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 if (fp != NULL)
13276 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278}
13279
13280/*
13281 * "line2byte(lnum)" function
13282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013284f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013285 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287{
13288#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013289 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290#else
13291 linenr_T lnum;
13292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013293 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013295 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013297 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13298 if (rettv->vval.v_number >= 0)
13299 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300#endif
13301}
13302
13303/*
13304 * "lispindent(lnum)" function
13305 */
13306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013308 typval_T *argvars;
13309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310{
13311#ifdef FEAT_LISP
13312 pos_T pos;
13313 linenr_T lnum;
13314
13315 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013316 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13318 {
13319 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013320 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321 curwin->w_cursor = pos;
13322 }
13323 else
13324#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013325 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326}
13327
13328/*
13329 * "localtime()" function
13330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013332f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013333 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013336 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337}
13338
Bram Moolenaar33570922005-01-25 22:26:29 +000013339static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340
13341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013342get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013343 typval_T *argvars;
13344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345 int exact;
13346{
13347 char_u *keys;
13348 char_u *which;
13349 char_u buf[NUMBUFLEN];
13350 char_u *keys_buf = NULL;
13351 char_u *rhs;
13352 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013353 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013354 int get_dict = FALSE;
13355 mapblock_T *mp;
13356 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357
13358 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359 rettv->v_type = VAR_STRING;
13360 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013362 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363 if (*keys == NUL)
13364 return;
13365
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013366 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013368 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013369 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013370 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013371 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013372 if (argvars[3].v_type != VAR_UNKNOWN)
13373 get_dict = get_tv_number(&argvars[3]);
13374 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376 else
13377 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013378 if (which == NULL)
13379 return;
13380
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381 mode = get_map_mode(&which, 0);
13382
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013383 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013384 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013386
13387 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013389 /* Return a string. */
13390 if (rhs != NULL)
13391 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392
Bram Moolenaarbd743252010-10-20 21:23:33 +020013393 }
13394 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13395 {
13396 /* Return a dictionary. */
13397 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13398 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13399 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400
Bram Moolenaarbd743252010-10-20 21:23:33 +020013401 dict_add_nr_str(dict, "lhs", 0L, lhs);
13402 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13403 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13404 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13405 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13406 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13407 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13408 dict_add_nr_str(dict, "mode", 0L, mapmode);
13409
13410 vim_free(lhs);
13411 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 }
13413}
13414
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013415#ifdef FEAT_FLOAT
13416/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013417 * "log()" function
13418 */
13419 static void
13420f_log(argvars, rettv)
13421 typval_T *argvars;
13422 typval_T *rettv;
13423{
13424 float_T f;
13425
13426 rettv->v_type = VAR_FLOAT;
13427 if (get_float_arg(argvars, &f) == OK)
13428 rettv->vval.v_float = log(f);
13429 else
13430 rettv->vval.v_float = 0.0;
13431}
13432
13433/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013434 * "log10()" function
13435 */
13436 static void
13437f_log10(argvars, rettv)
13438 typval_T *argvars;
13439 typval_T *rettv;
13440{
13441 float_T f;
13442
13443 rettv->v_type = VAR_FLOAT;
13444 if (get_float_arg(argvars, &f) == OK)
13445 rettv->vval.v_float = log10(f);
13446 else
13447 rettv->vval.v_float = 0.0;
13448}
13449#endif
13450
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013452 * "map()" function
13453 */
13454 static void
13455f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013456 typval_T *argvars;
13457 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013458{
13459 filter_map(argvars, rettv, TRUE);
13460}
13461
13462/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013463 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 */
13465 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013466f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013467 typval_T *argvars;
13468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013470 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471}
13472
13473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013474 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 */
13476 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013477f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 typval_T *argvars;
13479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013481 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482}
13483
Bram Moolenaar33570922005-01-25 22:26:29 +000013484static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485
13486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013487find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013488 typval_T *argvars;
13489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490 int type;
13491{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013492 char_u *str = NULL;
13493 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494 char_u *pat;
13495 regmatch_T regmatch;
13496 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013497 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 char_u *save_cpo;
13499 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013500 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013501 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013502 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013503 list_T *l = NULL;
13504 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013505 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013506 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507
13508 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13509 save_cpo = p_cpo;
13510 p_cpo = (char_u *)"";
13511
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013512 rettv->vval.v_number = -1;
13513 if (type == 3)
13514 {
13515 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013516 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013517 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013518 }
13519 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521 rettv->v_type = VAR_STRING;
13522 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013525 if (argvars[0].v_type == VAR_LIST)
13526 {
13527 if ((l = argvars[0].vval.v_list) == NULL)
13528 goto theend;
13529 li = l->lv_first;
13530 }
13531 else
13532 expr = str = get_tv_string(&argvars[0]);
13533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013534 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13535 if (pat == NULL)
13536 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013537
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013538 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013540 int error = FALSE;
13541
13542 start = get_tv_number_chk(&argvars[2], &error);
13543 if (error)
13544 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013545 if (l != NULL)
13546 {
13547 li = list_find(l, start);
13548 if (li == NULL)
13549 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013550 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013551 }
13552 else
13553 {
13554 if (start < 0)
13555 start = 0;
13556 if (start > (long)STRLEN(str))
13557 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013558 /* When "count" argument is there ignore matches before "start",
13559 * otherwise skip part of the string. Differs when pattern is "^"
13560 * or "\<". */
13561 if (argvars[3].v_type != VAR_UNKNOWN)
13562 startcol = start;
13563 else
13564 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013565 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013566
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013567 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013568 nth = get_tv_number_chk(&argvars[3], &error);
13569 if (error)
13570 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 }
13572
13573 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13574 if (regmatch.regprog != NULL)
13575 {
13576 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013577
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013578 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013579 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013580 if (l != NULL)
13581 {
13582 if (li == NULL)
13583 {
13584 match = FALSE;
13585 break;
13586 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013587 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013588 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013589 if (str == NULL)
13590 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013591 }
13592
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013593 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013594
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013595 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013596 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013597 if (l == NULL && !match)
13598 break;
13599
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013600 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013601 if (l != NULL)
13602 {
13603 li = li->li_next;
13604 ++idx;
13605 }
13606 else
13607 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013608#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013609 startcol = (colnr_T)(regmatch.startp[0]
13610 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013611#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013612 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013613#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013614 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013615 }
13616
13617 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013619 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013620 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013621 int i;
13622
13623 /* return list with matched string and submatches */
13624 for (i = 0; i < NSUBEXP; ++i)
13625 {
13626 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013627 {
13628 if (list_append_string(rettv->vval.v_list,
13629 (char_u *)"", 0) == FAIL)
13630 break;
13631 }
13632 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013633 regmatch.startp[i],
13634 (int)(regmatch.endp[i] - regmatch.startp[i]))
13635 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013636 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013637 }
13638 }
13639 else if (type == 2)
13640 {
13641 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013642 if (l != NULL)
13643 copy_tv(&li->li_tv, rettv);
13644 else
13645 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013647 }
13648 else if (l != NULL)
13649 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650 else
13651 {
13652 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654 (varnumber_T)(regmatch.startp[0] - str);
13655 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013656 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013658 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 }
13660 }
13661 vim_free(regmatch.regprog);
13662 }
13663
13664theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013665 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 p_cpo = save_cpo;
13667}
13668
13669/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013670 * "match()" function
13671 */
13672 static void
13673f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013674 typval_T *argvars;
13675 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013676{
13677 find_some_match(argvars, rettv, 1);
13678}
13679
13680/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013681 * "matchadd()" function
13682 */
13683 static void
13684f_matchadd(argvars, rettv)
13685 typval_T *argvars;
13686 typval_T *rettv;
13687{
13688#ifdef FEAT_SEARCH_EXTRA
13689 char_u buf[NUMBUFLEN];
13690 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13691 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13692 int prio = 10; /* default priority */
13693 int id = -1;
13694 int error = FALSE;
13695
13696 rettv->vval.v_number = -1;
13697
13698 if (grp == NULL || pat == NULL)
13699 return;
13700 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013701 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013702 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013703 if (argvars[3].v_type != VAR_UNKNOWN)
13704 id = get_tv_number_chk(&argvars[3], &error);
13705 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013706 if (error == TRUE)
13707 return;
13708 if (id >= 1 && id <= 3)
13709 {
13710 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13711 return;
13712 }
13713
13714 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13715#endif
13716}
13717
13718/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013719 * "matcharg()" function
13720 */
13721 static void
13722f_matcharg(argvars, rettv)
13723 typval_T *argvars;
13724 typval_T *rettv;
13725{
13726 if (rettv_list_alloc(rettv) == OK)
13727 {
13728#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013729 int id = get_tv_number(&argvars[0]);
13730 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013731
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013732 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013733 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013734 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13735 {
13736 list_append_string(rettv->vval.v_list,
13737 syn_id2name(m->hlg_id), -1);
13738 list_append_string(rettv->vval.v_list, m->pattern, -1);
13739 }
13740 else
13741 {
13742 list_append_string(rettv->vval.v_list, NUL, -1);
13743 list_append_string(rettv->vval.v_list, NUL, -1);
13744 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013745 }
13746#endif
13747 }
13748}
13749
13750/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013751 * "matchdelete()" function
13752 */
13753 static void
13754f_matchdelete(argvars, rettv)
13755 typval_T *argvars;
13756 typval_T *rettv;
13757{
13758#ifdef FEAT_SEARCH_EXTRA
13759 rettv->vval.v_number = match_delete(curwin,
13760 (int)get_tv_number(&argvars[0]), TRUE);
13761#endif
13762}
13763
13764/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013765 * "matchend()" function
13766 */
13767 static void
13768f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013769 typval_T *argvars;
13770 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013771{
13772 find_some_match(argvars, rettv, 0);
13773}
13774
13775/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013776 * "matchlist()" function
13777 */
13778 static void
13779f_matchlist(argvars, rettv)
13780 typval_T *argvars;
13781 typval_T *rettv;
13782{
13783 find_some_match(argvars, rettv, 3);
13784}
13785
13786/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013787 * "matchstr()" function
13788 */
13789 static void
13790f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013791 typval_T *argvars;
13792 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013793{
13794 find_some_match(argvars, rettv, 2);
13795}
13796
Bram Moolenaar33570922005-01-25 22:26:29 +000013797static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013798
13799 static void
13800max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013801 typval_T *argvars;
13802 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013803 int domax;
13804{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013805 long n = 0;
13806 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013807 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013808
13809 if (argvars[0].v_type == VAR_LIST)
13810 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013811 list_T *l;
13812 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013813
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013814 l = argvars[0].vval.v_list;
13815 if (l != NULL)
13816 {
13817 li = l->lv_first;
13818 if (li != NULL)
13819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013820 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013821 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013822 {
13823 li = li->li_next;
13824 if (li == NULL)
13825 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013827 if (domax ? i > n : i < n)
13828 n = i;
13829 }
13830 }
13831 }
13832 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013833 else if (argvars[0].v_type == VAR_DICT)
13834 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013835 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013836 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013837 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013838 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013839
13840 d = argvars[0].vval.v_dict;
13841 if (d != NULL)
13842 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013843 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013844 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013845 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013846 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013847 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013848 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013849 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013850 if (first)
13851 {
13852 n = i;
13853 first = FALSE;
13854 }
13855 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013856 n = i;
13857 }
13858 }
13859 }
13860 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013861 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013862 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013864}
13865
13866/*
13867 * "max()" function
13868 */
13869 static void
13870f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013871 typval_T *argvars;
13872 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013873{
13874 max_min(argvars, rettv, TRUE);
13875}
13876
13877/*
13878 * "min()" function
13879 */
13880 static void
13881f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013882 typval_T *argvars;
13883 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013884{
13885 max_min(argvars, rettv, FALSE);
13886}
13887
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013888static int mkdir_recurse __ARGS((char_u *dir, int prot));
13889
13890/*
13891 * Create the directory in which "dir" is located, and higher levels when
13892 * needed.
13893 */
13894 static int
13895mkdir_recurse(dir, prot)
13896 char_u *dir;
13897 int prot;
13898{
13899 char_u *p;
13900 char_u *updir;
13901 int r = FAIL;
13902
13903 /* Get end of directory name in "dir".
13904 * We're done when it's "/" or "c:/". */
13905 p = gettail_sep(dir);
13906 if (p <= get_past_head(dir))
13907 return OK;
13908
13909 /* If the directory exists we're done. Otherwise: create it.*/
13910 updir = vim_strnsave(dir, (int)(p - dir));
13911 if (updir == NULL)
13912 return FAIL;
13913 if (mch_isdir(updir))
13914 r = OK;
13915 else if (mkdir_recurse(updir, prot) == OK)
13916 r = vim_mkdir_emsg(updir, prot);
13917 vim_free(updir);
13918 return r;
13919}
13920
13921#ifdef vim_mkdir
13922/*
13923 * "mkdir()" function
13924 */
13925 static void
13926f_mkdir(argvars, rettv)
13927 typval_T *argvars;
13928 typval_T *rettv;
13929{
13930 char_u *dir;
13931 char_u buf[NUMBUFLEN];
13932 int prot = 0755;
13933
13934 rettv->vval.v_number = FAIL;
13935 if (check_restricted() || check_secure())
13936 return;
13937
13938 dir = get_tv_string_buf(&argvars[0], buf);
13939 if (argvars[1].v_type != VAR_UNKNOWN)
13940 {
13941 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013942 prot = get_tv_number_chk(&argvars[2], NULL);
13943 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013944 mkdir_recurse(dir, prot);
13945 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013946 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013947}
13948#endif
13949
Bram Moolenaar0d660222005-01-07 21:51:51 +000013950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 * "mode()" function
13952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013954f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013955 typval_T *argvars;
13956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013958 char_u buf[3];
13959
13960 buf[1] = NUL;
13961 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962
13963#ifdef FEAT_VISUAL
13964 if (VIsual_active)
13965 {
13966 if (VIsual_select)
13967 buf[0] = VIsual_mode + 's' - 'v';
13968 else
13969 buf[0] = VIsual_mode;
13970 }
13971 else
13972#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013973 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13974 || State == CONFIRM)
13975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013977 if (State == ASKMORE)
13978 buf[1] = 'm';
13979 else if (State == CONFIRM)
13980 buf[1] = '?';
13981 }
13982 else if (State == EXTERNCMD)
13983 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 else if (State & INSERT)
13985 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013986#ifdef FEAT_VREPLACE
13987 if (State & VREPLACE_FLAG)
13988 {
13989 buf[0] = 'R';
13990 buf[1] = 'v';
13991 }
13992 else
13993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994 if (State & REPLACE_FLAG)
13995 buf[0] = 'R';
13996 else
13997 buf[0] = 'i';
13998 }
13999 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014002 if (exmode_active)
14003 buf[1] = 'v';
14004 }
14005 else if (exmode_active)
14006 {
14007 buf[0] = 'c';
14008 buf[1] = 'e';
14009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014013 if (finish_op)
14014 buf[1] = 'o';
14015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014017 /* Clear out the minor mode when the argument is not a non-zero number or
14018 * non-empty string. */
14019 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014020 buf[1] = NUL;
14021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014022 rettv->vval.v_string = vim_strsave(buf);
14023 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024}
14025
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014026#ifdef FEAT_MZSCHEME
14027/*
14028 * "mzeval()" function
14029 */
14030 static void
14031f_mzeval(argvars, rettv)
14032 typval_T *argvars;
14033 typval_T *rettv;
14034{
14035 char_u *str;
14036 char_u buf[NUMBUFLEN];
14037
14038 str = get_tv_string_buf(&argvars[0], buf);
14039 do_mzeval(str, rettv);
14040}
14041#endif
14042
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014044 * "nextnonblank()" function
14045 */
14046 static void
14047f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014048 typval_T *argvars;
14049 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014050{
14051 linenr_T lnum;
14052
14053 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014055 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014056 {
14057 lnum = 0;
14058 break;
14059 }
14060 if (*skipwhite(ml_get(lnum)) != NUL)
14061 break;
14062 }
14063 rettv->vval.v_number = lnum;
14064}
14065
14066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 * "nr2char()" function
14068 */
14069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014070f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014071 typval_T *argvars;
14072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073{
14074 char_u buf[NUMBUFLEN];
14075
14076#ifdef FEAT_MBYTE
14077 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014078 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079 else
14080#endif
14081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083 buf[1] = NUL;
14084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085 rettv->v_type = VAR_STRING;
14086 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014087}
14088
14089/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014090 * "pathshorten()" function
14091 */
14092 static void
14093f_pathshorten(argvars, rettv)
14094 typval_T *argvars;
14095 typval_T *rettv;
14096{
14097 char_u *p;
14098
14099 rettv->v_type = VAR_STRING;
14100 p = get_tv_string_chk(&argvars[0]);
14101 if (p == NULL)
14102 rettv->vval.v_string = NULL;
14103 else
14104 {
14105 p = vim_strsave(p);
14106 rettv->vval.v_string = p;
14107 if (p != NULL)
14108 shorten_dir(p);
14109 }
14110}
14111
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014112#ifdef FEAT_FLOAT
14113/*
14114 * "pow()" function
14115 */
14116 static void
14117f_pow(argvars, rettv)
14118 typval_T *argvars;
14119 typval_T *rettv;
14120{
14121 float_T fx, fy;
14122
14123 rettv->v_type = VAR_FLOAT;
14124 if (get_float_arg(argvars, &fx) == OK
14125 && get_float_arg(&argvars[1], &fy) == OK)
14126 rettv->vval.v_float = pow(fx, fy);
14127 else
14128 rettv->vval.v_float = 0.0;
14129}
14130#endif
14131
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014132/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014133 * "prevnonblank()" function
14134 */
14135 static void
14136f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014137 typval_T *argvars;
14138 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014139{
14140 linenr_T lnum;
14141
14142 lnum = get_tv_lnum(argvars);
14143 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14144 lnum = 0;
14145 else
14146 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14147 --lnum;
14148 rettv->vval.v_number = lnum;
14149}
14150
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014151#ifdef HAVE_STDARG_H
14152/* This dummy va_list is here because:
14153 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14154 * - locally in the function results in a "used before set" warning
14155 * - using va_start() to initialize it gives "function with fixed args" error */
14156static va_list ap;
14157#endif
14158
Bram Moolenaar8c711452005-01-14 21:53:12 +000014159/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014160 * "printf()" function
14161 */
14162 static void
14163f_printf(argvars, rettv)
14164 typval_T *argvars;
14165 typval_T *rettv;
14166{
14167 rettv->v_type = VAR_STRING;
14168 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014169#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014170 {
14171 char_u buf[NUMBUFLEN];
14172 int len;
14173 char_u *s;
14174 int saved_did_emsg = did_emsg;
14175 char *fmt;
14176
14177 /* Get the required length, allocate the buffer and do it for real. */
14178 did_emsg = FALSE;
14179 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014180 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014181 if (!did_emsg)
14182 {
14183 s = alloc(len + 1);
14184 if (s != NULL)
14185 {
14186 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014187 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014188 }
14189 }
14190 did_emsg |= saved_did_emsg;
14191 }
14192#endif
14193}
14194
14195/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014196 * "pumvisible()" function
14197 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014198 static void
14199f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014200 typval_T *argvars UNUSED;
14201 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014202{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014203#ifdef FEAT_INS_EXPAND
14204 if (pum_visible())
14205 rettv->vval.v_number = 1;
14206#endif
14207}
14208
14209/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014210 * "range()" function
14211 */
14212 static void
14213f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014214 typval_T *argvars;
14215 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014216{
14217 long start;
14218 long end;
14219 long stride = 1;
14220 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014221 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014223 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014224 if (argvars[1].v_type == VAR_UNKNOWN)
14225 {
14226 end = start - 1;
14227 start = 0;
14228 }
14229 else
14230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014232 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014234 }
14235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 if (error)
14237 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014238 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014239 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014240 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014241 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014242 else
14243 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014244 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014245 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014246 if (list_append_number(rettv->vval.v_list,
14247 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014248 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014249 }
14250}
14251
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014252/*
14253 * "readfile()" function
14254 */
14255 static void
14256f_readfile(argvars, rettv)
14257 typval_T *argvars;
14258 typval_T *rettv;
14259{
14260 int binary = FALSE;
14261 char_u *fname;
14262 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014263 listitem_T *li;
14264#define FREAD_SIZE 200 /* optimized for text lines */
14265 char_u buf[FREAD_SIZE];
14266 int readlen; /* size of last fread() */
14267 int buflen; /* nr of valid chars in buf[] */
14268 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14269 int tolist; /* first byte in buf[] still to be put in list */
14270 int chop; /* how many CR to chop off */
14271 char_u *prev = NULL; /* previously read bytes, if any */
14272 int prevlen = 0; /* length of "prev" if not NULL */
14273 char_u *s;
14274 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014275 long maxline = MAXLNUM;
14276 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014277
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014278 if (argvars[1].v_type != VAR_UNKNOWN)
14279 {
14280 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14281 binary = TRUE;
14282 if (argvars[2].v_type != VAR_UNKNOWN)
14283 maxline = get_tv_number(&argvars[2]);
14284 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014285
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014286 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014287 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014288
14289 /* Always open the file in binary mode, library functions have a mind of
14290 * their own about CR-LF conversion. */
14291 fname = get_tv_string(&argvars[0]);
14292 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14293 {
14294 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14295 return;
14296 }
14297
14298 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014299 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014300 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014301 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014302 buflen = filtd + readlen;
14303 tolist = 0;
14304 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14305 {
14306 if (buf[filtd] == '\n' || readlen <= 0)
14307 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014308 /* In binary mode add an empty list item when the last
14309 * non-empty line ends in a '\n'. */
14310 if (!binary && readlen == 0 && filtd == 0 && prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014311 break;
14312
14313 /* Found end-of-line or end-of-file: add a text line to the
14314 * list. */
14315 chop = 0;
14316 if (!binary)
14317 while (filtd - chop - 1 >= tolist
14318 && buf[filtd - chop - 1] == '\r')
14319 ++chop;
14320 len = filtd - tolist - chop;
14321 if (prev == NULL)
14322 s = vim_strnsave(buf + tolist, len);
14323 else
14324 {
14325 s = alloc((unsigned)(prevlen + len + 1));
14326 if (s != NULL)
14327 {
14328 mch_memmove(s, prev, prevlen);
14329 vim_free(prev);
14330 prev = NULL;
14331 mch_memmove(s + prevlen, buf + tolist, len);
14332 s[prevlen + len] = NUL;
14333 }
14334 }
14335 tolist = filtd + 1;
14336
14337 li = listitem_alloc();
14338 if (li == NULL)
14339 {
14340 vim_free(s);
14341 break;
14342 }
14343 li->li_tv.v_type = VAR_STRING;
14344 li->li_tv.v_lock = 0;
14345 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014346 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014347
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014348 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014349 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014350 if (readlen <= 0)
14351 break;
14352 }
14353 else if (buf[filtd] == NUL)
14354 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014355#ifdef FEAT_MBYTE
14356 else if (buf[filtd] == 0xef
14357 && enc_utf8
14358 && filtd + 2 < buflen
14359 && !binary
14360 && buf[filtd + 1] == 0xbb
14361 && buf[filtd + 2] == 0xbf)
14362 {
14363 /* remove utf-8 byte order mark */
14364 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14365 --filtd;
14366 buflen -= 3;
14367 }
14368#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014369 }
14370 if (readlen <= 0)
14371 break;
14372
14373 if (tolist == 0)
14374 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014375 if (buflen >= FREAD_SIZE / 2)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014376 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014377 /* "buf" is full, need to move text to an allocated buffer */
14378 if (prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014379 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014380 prev = vim_strnsave(buf, buflen);
14381 prevlen = buflen;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014382 }
Bram Moolenaar27b60562011-04-01 16:07:46 +020014383 else
14384 {
14385 s = alloc((unsigned)(prevlen + buflen));
14386 if (s != NULL)
14387 {
14388 mch_memmove(s, prev, prevlen);
14389 mch_memmove(s + prevlen, buf, buflen);
14390 vim_free(prev);
14391 prev = s;
14392 prevlen += buflen;
14393 }
14394 }
14395 filtd = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014396 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014397 }
14398 else
14399 {
14400 mch_memmove(buf, buf + tolist, buflen - tolist);
14401 filtd -= tolist;
14402 }
14403 }
14404
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014405 /*
14406 * For a negative line count use only the lines at the end of the file,
14407 * free the rest.
14408 */
14409 if (maxline < 0)
14410 while (cnt > -maxline)
14411 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014412 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014413 --cnt;
14414 }
14415
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014416 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014417 fclose(fd);
14418}
14419
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014420#if defined(FEAT_RELTIME)
14421static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14422
14423/*
14424 * Convert a List to proftime_T.
14425 * Return FAIL when there is something wrong.
14426 */
14427 static int
14428list2proftime(arg, tm)
14429 typval_T *arg;
14430 proftime_T *tm;
14431{
14432 long n1, n2;
14433 int error = FALSE;
14434
14435 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14436 || arg->vval.v_list->lv_len != 2)
14437 return FAIL;
14438 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14439 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14440# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014441 tm->HighPart = n1;
14442 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014443# else
14444 tm->tv_sec = n1;
14445 tm->tv_usec = n2;
14446# endif
14447 return error ? FAIL : OK;
14448}
14449#endif /* FEAT_RELTIME */
14450
14451/*
14452 * "reltime()" function
14453 */
14454 static void
14455f_reltime(argvars, rettv)
14456 typval_T *argvars;
14457 typval_T *rettv;
14458{
14459#ifdef FEAT_RELTIME
14460 proftime_T res;
14461 proftime_T start;
14462
14463 if (argvars[0].v_type == VAR_UNKNOWN)
14464 {
14465 /* No arguments: get current time. */
14466 profile_start(&res);
14467 }
14468 else if (argvars[1].v_type == VAR_UNKNOWN)
14469 {
14470 if (list2proftime(&argvars[0], &res) == FAIL)
14471 return;
14472 profile_end(&res);
14473 }
14474 else
14475 {
14476 /* Two arguments: compute the difference. */
14477 if (list2proftime(&argvars[0], &start) == FAIL
14478 || list2proftime(&argvars[1], &res) == FAIL)
14479 return;
14480 profile_sub(&res, &start);
14481 }
14482
14483 if (rettv_list_alloc(rettv) == OK)
14484 {
14485 long n1, n2;
14486
14487# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014488 n1 = res.HighPart;
14489 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014490# else
14491 n1 = res.tv_sec;
14492 n2 = res.tv_usec;
14493# endif
14494 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14495 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14496 }
14497#endif
14498}
14499
14500/*
14501 * "reltimestr()" function
14502 */
14503 static void
14504f_reltimestr(argvars, rettv)
14505 typval_T *argvars;
14506 typval_T *rettv;
14507{
14508#ifdef FEAT_RELTIME
14509 proftime_T tm;
14510#endif
14511
14512 rettv->v_type = VAR_STRING;
14513 rettv->vval.v_string = NULL;
14514#ifdef FEAT_RELTIME
14515 if (list2proftime(&argvars[0], &tm) == OK)
14516 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14517#endif
14518}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014519
Bram Moolenaar0d660222005-01-07 21:51:51 +000014520#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14521static void make_connection __ARGS((void));
14522static int check_connection __ARGS((void));
14523
14524 static void
14525make_connection()
14526{
14527 if (X_DISPLAY == NULL
14528# ifdef FEAT_GUI
14529 && !gui.in_use
14530# endif
14531 )
14532 {
14533 x_force_connect = TRUE;
14534 setup_term_clip();
14535 x_force_connect = FALSE;
14536 }
14537}
14538
14539 static int
14540check_connection()
14541{
14542 make_connection();
14543 if (X_DISPLAY == NULL)
14544 {
14545 EMSG(_("E240: No connection to Vim server"));
14546 return FAIL;
14547 }
14548 return OK;
14549}
14550#endif
14551
14552#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014553static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014554
14555 static void
14556remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014557 typval_T *argvars;
14558 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014559 int expr;
14560{
14561 char_u *server_name;
14562 char_u *keys;
14563 char_u *r = NULL;
14564 char_u buf[NUMBUFLEN];
14565# ifdef WIN32
14566 HWND w;
14567# else
14568 Window w;
14569# endif
14570
14571 if (check_restricted() || check_secure())
14572 return;
14573
14574# ifdef FEAT_X11
14575 if (check_connection() == FAIL)
14576 return;
14577# endif
14578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014579 server_name = get_tv_string_chk(&argvars[0]);
14580 if (server_name == NULL)
14581 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014582 keys = get_tv_string_buf(&argvars[1], buf);
14583# ifdef WIN32
14584 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14585# else
14586 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14587 < 0)
14588# endif
14589 {
14590 if (r != NULL)
14591 EMSG(r); /* sending worked but evaluation failed */
14592 else
14593 EMSG2(_("E241: Unable to send to %s"), server_name);
14594 return;
14595 }
14596
14597 rettv->vval.v_string = r;
14598
14599 if (argvars[2].v_type != VAR_UNKNOWN)
14600 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014601 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014602 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014603 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014605 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014606 v.di_tv.v_type = VAR_STRING;
14607 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014608 idvar = get_tv_string_chk(&argvars[2]);
14609 if (idvar != NULL)
14610 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014611 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014612 }
14613}
14614#endif
14615
14616/*
14617 * "remote_expr()" function
14618 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014619 static void
14620f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014621 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014622 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014623{
14624 rettv->v_type = VAR_STRING;
14625 rettv->vval.v_string = NULL;
14626#ifdef FEAT_CLIENTSERVER
14627 remote_common(argvars, rettv, TRUE);
14628#endif
14629}
14630
14631/*
14632 * "remote_foreground()" function
14633 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014634 static void
14635f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014636 typval_T *argvars UNUSED;
14637 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014639#ifdef FEAT_CLIENTSERVER
14640# ifdef WIN32
14641 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014642 {
14643 char_u *server_name = get_tv_string_chk(&argvars[0]);
14644
14645 if (server_name != NULL)
14646 serverForeground(server_name);
14647 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648# else
14649 /* Send a foreground() expression to the server. */
14650 argvars[1].v_type = VAR_STRING;
14651 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14652 argvars[2].v_type = VAR_UNKNOWN;
14653 remote_common(argvars, rettv, TRUE);
14654 vim_free(argvars[1].vval.v_string);
14655# endif
14656#endif
14657}
14658
Bram Moolenaar0d660222005-01-07 21:51:51 +000014659 static void
14660f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014661 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014662 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014663{
14664#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014665 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014666 char_u *s = NULL;
14667# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014668 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014669# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014670 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014671
14672 if (check_restricted() || check_secure())
14673 {
14674 rettv->vval.v_number = -1;
14675 return;
14676 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014677 serverid = get_tv_string_chk(&argvars[0]);
14678 if (serverid == NULL)
14679 {
14680 rettv->vval.v_number = -1;
14681 return; /* type error; errmsg already given */
14682 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014683# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014684 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014685 if (n == 0)
14686 rettv->vval.v_number = -1;
14687 else
14688 {
14689 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14690 rettv->vval.v_number = (s != NULL);
14691 }
14692# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014693 if (check_connection() == FAIL)
14694 return;
14695
14696 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014697 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014698# endif
14699
14700 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014702 char_u *retvar;
14703
Bram Moolenaar33570922005-01-25 22:26:29 +000014704 v.di_tv.v_type = VAR_STRING;
14705 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014706 retvar = get_tv_string_chk(&argvars[1]);
14707 if (retvar != NULL)
14708 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014709 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014710 }
14711#else
14712 rettv->vval.v_number = -1;
14713#endif
14714}
14715
Bram Moolenaar0d660222005-01-07 21:51:51 +000014716 static void
14717f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014718 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014719 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014720{
14721 char_u *r = NULL;
14722
14723#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014724 char_u *serverid = get_tv_string_chk(&argvars[0]);
14725
14726 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014727 {
14728# ifdef WIN32
14729 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014730 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014731
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014732 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014733 if (n != 0)
14734 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14735 if (r == NULL)
14736# else
14737 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014738 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014739# endif
14740 EMSG(_("E277: Unable to read a server reply"));
14741 }
14742#endif
14743 rettv->v_type = VAR_STRING;
14744 rettv->vval.v_string = r;
14745}
14746
14747/*
14748 * "remote_send()" function
14749 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014750 static void
14751f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014752 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014753 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014754{
14755 rettv->v_type = VAR_STRING;
14756 rettv->vval.v_string = NULL;
14757#ifdef FEAT_CLIENTSERVER
14758 remote_common(argvars, rettv, FALSE);
14759#endif
14760}
14761
14762/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014763 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014764 */
14765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014766f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014767 typval_T *argvars;
14768 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014769{
Bram Moolenaar33570922005-01-25 22:26:29 +000014770 list_T *l;
14771 listitem_T *item, *item2;
14772 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014773 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014774 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014775 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014776 dict_T *d;
14777 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014778
Bram Moolenaar8c711452005-01-14 21:53:12 +000014779 if (argvars[0].v_type == VAR_DICT)
14780 {
14781 if (argvars[2].v_type != VAR_UNKNOWN)
14782 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014783 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014784 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014786 key = get_tv_string_chk(&argvars[1]);
14787 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014789 di = dict_find(d, key, -1);
14790 if (di == NULL)
14791 EMSG2(_(e_dictkey), key);
14792 else
14793 {
14794 *rettv = di->di_tv;
14795 init_tv(&di->di_tv);
14796 dictitem_remove(d, di);
14797 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014799 }
14800 }
14801 else if (argvars[0].v_type != VAR_LIST)
14802 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014803 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014804 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014806 int error = FALSE;
14807
14808 idx = get_tv_number_chk(&argvars[1], &error);
14809 if (error)
14810 ; /* type error: do nothing, errmsg already given */
14811 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014812 EMSGN(_(e_listidx), idx);
14813 else
14814 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014815 if (argvars[2].v_type == VAR_UNKNOWN)
14816 {
14817 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014818 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014819 *rettv = item->li_tv;
14820 vim_free(item);
14821 }
14822 else
14823 {
14824 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014825 end = get_tv_number_chk(&argvars[2], &error);
14826 if (error)
14827 ; /* type error: do nothing */
14828 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014829 EMSGN(_(e_listidx), end);
14830 else
14831 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014832 int cnt = 0;
14833
14834 for (li = item; li != NULL; li = li->li_next)
14835 {
14836 ++cnt;
14837 if (li == item2)
14838 break;
14839 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014840 if (li == NULL) /* didn't find "item2" after "item" */
14841 EMSG(_(e_invrange));
14842 else
14843 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014844 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014845 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014846 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014847 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014848 l->lv_first = item;
14849 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014850 item->li_prev = NULL;
14851 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014852 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014853 }
14854 }
14855 }
14856 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014857 }
14858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859}
14860
14861/*
14862 * "rename({from}, {to})" function
14863 */
14864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014865f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014866 typval_T *argvars;
14867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868{
14869 char_u buf[NUMBUFLEN];
14870
14871 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014872 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014874 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14875 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876}
14877
14878/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014879 * "repeat()" function
14880 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014881 static void
14882f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014883 typval_T *argvars;
14884 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014885{
14886 char_u *p;
14887 int n;
14888 int slen;
14889 int len;
14890 char_u *r;
14891 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014892
14893 n = get_tv_number(&argvars[1]);
14894 if (argvars[0].v_type == VAR_LIST)
14895 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014896 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014897 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014898 if (list_extend(rettv->vval.v_list,
14899 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014900 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014901 }
14902 else
14903 {
14904 p = get_tv_string(&argvars[0]);
14905 rettv->v_type = VAR_STRING;
14906 rettv->vval.v_string = NULL;
14907
14908 slen = (int)STRLEN(p);
14909 len = slen * n;
14910 if (len <= 0)
14911 return;
14912
14913 r = alloc(len + 1);
14914 if (r != NULL)
14915 {
14916 for (i = 0; i < n; i++)
14917 mch_memmove(r + i * slen, p, (size_t)slen);
14918 r[len] = NUL;
14919 }
14920
14921 rettv->vval.v_string = r;
14922 }
14923}
14924
14925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926 * "resolve()" function
14927 */
14928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014929f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014930 typval_T *argvars;
14931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932{
14933 char_u *p;
14934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014935 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936#ifdef FEAT_SHORTCUT
14937 {
14938 char_u *v = NULL;
14939
14940 v = mch_resolve_shortcut(p);
14941 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014942 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014944 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945 }
14946#else
14947# ifdef HAVE_READLINK
14948 {
14949 char_u buf[MAXPATHL + 1];
14950 char_u *cpy;
14951 int len;
14952 char_u *remain = NULL;
14953 char_u *q;
14954 int is_relative_to_current = FALSE;
14955 int has_trailing_pathsep = FALSE;
14956 int limit = 100;
14957
14958 p = vim_strsave(p);
14959
14960 if (p[0] == '.' && (vim_ispathsep(p[1])
14961 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14962 is_relative_to_current = TRUE;
14963
14964 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014965 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 has_trailing_pathsep = TRUE;
14967
14968 q = getnextcomp(p);
14969 if (*q != NUL)
14970 {
14971 /* Separate the first path component in "p", and keep the
14972 * remainder (beginning with the path separator). */
14973 remain = vim_strsave(q - 1);
14974 q[-1] = NUL;
14975 }
14976
14977 for (;;)
14978 {
14979 for (;;)
14980 {
14981 len = readlink((char *)p, (char *)buf, MAXPATHL);
14982 if (len <= 0)
14983 break;
14984 buf[len] = NUL;
14985
14986 if (limit-- == 0)
14987 {
14988 vim_free(p);
14989 vim_free(remain);
14990 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014991 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 goto fail;
14993 }
14994
14995 /* Ensure that the result will have a trailing path separator
14996 * if the argument has one. */
14997 if (remain == NULL && has_trailing_pathsep)
14998 add_pathsep(buf);
14999
15000 /* Separate the first path component in the link value and
15001 * concatenate the remainders. */
15002 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15003 if (*q != NUL)
15004 {
15005 if (remain == NULL)
15006 remain = vim_strsave(q - 1);
15007 else
15008 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015009 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010 if (cpy != NULL)
15011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012 vim_free(remain);
15013 remain = cpy;
15014 }
15015 }
15016 q[-1] = NUL;
15017 }
15018
15019 q = gettail(p);
15020 if (q > p && *q == NUL)
15021 {
15022 /* Ignore trailing path separator. */
15023 q[-1] = NUL;
15024 q = gettail(p);
15025 }
15026 if (q > p && !mch_isFullName(buf))
15027 {
15028 /* symlink is relative to directory of argument */
15029 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15030 if (cpy != NULL)
15031 {
15032 STRCPY(cpy, p);
15033 STRCPY(gettail(cpy), buf);
15034 vim_free(p);
15035 p = cpy;
15036 }
15037 }
15038 else
15039 {
15040 vim_free(p);
15041 p = vim_strsave(buf);
15042 }
15043 }
15044
15045 if (remain == NULL)
15046 break;
15047
15048 /* Append the first path component of "remain" to "p". */
15049 q = getnextcomp(remain + 1);
15050 len = q - remain - (*q != NUL);
15051 cpy = vim_strnsave(p, STRLEN(p) + len);
15052 if (cpy != NULL)
15053 {
15054 STRNCAT(cpy, remain, len);
15055 vim_free(p);
15056 p = cpy;
15057 }
15058 /* Shorten "remain". */
15059 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015060 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061 else
15062 {
15063 vim_free(remain);
15064 remain = NULL;
15065 }
15066 }
15067
15068 /* If the result is a relative path name, make it explicitly relative to
15069 * the current directory if and only if the argument had this form. */
15070 if (!vim_ispathsep(*p))
15071 {
15072 if (is_relative_to_current
15073 && *p != NUL
15074 && !(p[0] == '.'
15075 && (p[1] == NUL
15076 || vim_ispathsep(p[1])
15077 || (p[1] == '.'
15078 && (p[2] == NUL
15079 || vim_ispathsep(p[2]))))))
15080 {
15081 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015082 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083 if (cpy != NULL)
15084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085 vim_free(p);
15086 p = cpy;
15087 }
15088 }
15089 else if (!is_relative_to_current)
15090 {
15091 /* Strip leading "./". */
15092 q = p;
15093 while (q[0] == '.' && vim_ispathsep(q[1]))
15094 q += 2;
15095 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015096 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097 }
15098 }
15099
15100 /* Ensure that the result will have no trailing path separator
15101 * if the argument had none. But keep "/" or "//". */
15102 if (!has_trailing_pathsep)
15103 {
15104 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015105 if (after_pathsep(p, q))
15106 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 }
15108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015109 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110 }
15111# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015112 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113# endif
15114#endif
15115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015116 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117
15118#ifdef HAVE_READLINK
15119fail:
15120#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015121 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122}
15123
15124/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015125 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 */
15127 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015128f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015129 typval_T *argvars;
15130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131{
Bram Moolenaar33570922005-01-25 22:26:29 +000015132 list_T *l;
15133 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135 if (argvars[0].v_type != VAR_LIST)
15136 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015137 else if ((l = argvars[0].vval.v_list) != NULL
15138 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139 {
15140 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015141 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015142 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015143 while (li != NULL)
15144 {
15145 ni = li->li_prev;
15146 list_append(l, li);
15147 li = ni;
15148 }
15149 rettv->vval.v_list = l;
15150 rettv->v_type = VAR_LIST;
15151 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015152 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015154}
15155
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015156#define SP_NOMOVE 0x01 /* don't move cursor */
15157#define SP_REPEAT 0x02 /* repeat to find outer pair */
15158#define SP_RETCOUNT 0x04 /* return matchcount */
15159#define SP_SETPCMARK 0x08 /* set previous context mark */
15160#define SP_START 0x10 /* accept match at start position */
15161#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15162#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015163
Bram Moolenaar33570922005-01-25 22:26:29 +000015164static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165
15166/*
15167 * Get flags for a search function.
15168 * Possibly sets "p_ws".
15169 * Returns BACKWARD, FORWARD or zero (for an error).
15170 */
15171 static int
15172get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015173 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015174 int *flagsp;
15175{
15176 int dir = FORWARD;
15177 char_u *flags;
15178 char_u nbuf[NUMBUFLEN];
15179 int mask;
15180
15181 if (varp->v_type != VAR_UNKNOWN)
15182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015183 flags = get_tv_string_buf_chk(varp, nbuf);
15184 if (flags == NULL)
15185 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015186 while (*flags != NUL)
15187 {
15188 switch (*flags)
15189 {
15190 case 'b': dir = BACKWARD; break;
15191 case 'w': p_ws = TRUE; break;
15192 case 'W': p_ws = FALSE; break;
15193 default: mask = 0;
15194 if (flagsp != NULL)
15195 switch (*flags)
15196 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015197 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015198 case 'e': mask = SP_END; break;
15199 case 'm': mask = SP_RETCOUNT; break;
15200 case 'n': mask = SP_NOMOVE; break;
15201 case 'p': mask = SP_SUBPAT; break;
15202 case 'r': mask = SP_REPEAT; break;
15203 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015204 }
15205 if (mask == 0)
15206 {
15207 EMSG2(_(e_invarg2), flags);
15208 dir = 0;
15209 }
15210 else
15211 *flagsp |= mask;
15212 }
15213 if (dir == 0)
15214 break;
15215 ++flags;
15216 }
15217 }
15218 return dir;
15219}
15220
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015222 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015224 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015225search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015226 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015227 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015228 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015229{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015230 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015231 char_u *pat;
15232 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015233 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234 int save_p_ws = p_ws;
15235 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015236 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015237 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015238 proftime_T tm;
15239#ifdef FEAT_RELTIME
15240 long time_limit = 0;
15241#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015242 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015243 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015245 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015246 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015247 if (dir == 0)
15248 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015249 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015250 if (flags & SP_START)
15251 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015252 if (flags & SP_END)
15253 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015254
Bram Moolenaar76929292008-01-06 19:07:36 +000015255 /* Optional arguments: line number to stop searching and timeout. */
15256 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015257 {
15258 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15259 if (lnum_stop < 0)
15260 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015261#ifdef FEAT_RELTIME
15262 if (argvars[3].v_type != VAR_UNKNOWN)
15263 {
15264 time_limit = get_tv_number_chk(&argvars[3], NULL);
15265 if (time_limit < 0)
15266 goto theend;
15267 }
15268#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015269 }
15270
Bram Moolenaar76929292008-01-06 19:07:36 +000015271#ifdef FEAT_RELTIME
15272 /* Set the time limit, if there is one. */
15273 profile_setlimit(time_limit, &tm);
15274#endif
15275
Bram Moolenaar231334e2005-07-25 20:46:57 +000015276 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015277 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015278 * Check to make sure only those flags are set.
15279 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15280 * flags cannot be set. Check for that condition also.
15281 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015282 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015283 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015285 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015286 goto theend;
15287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015289 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015290 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015291 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015292 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015294 if (flags & SP_SUBPAT)
15295 retval = subpatnum;
15296 else
15297 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015298 if (flags & SP_SETPCMARK)
15299 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015301 if (match_pos != NULL)
15302 {
15303 /* Store the match cursor position */
15304 match_pos->lnum = pos.lnum;
15305 match_pos->col = pos.col + 1;
15306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307 /* "/$" will put the cursor after the end of the line, may need to
15308 * correct that here */
15309 check_cursor();
15310 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015311
15312 /* If 'n' flag is used: restore cursor position. */
15313 if (flags & SP_NOMOVE)
15314 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015315 else
15316 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015317theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015319
15320 return retval;
15321}
15322
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015323#ifdef FEAT_FLOAT
15324/*
15325 * "round({float})" function
15326 */
15327 static void
15328f_round(argvars, rettv)
15329 typval_T *argvars;
15330 typval_T *rettv;
15331{
15332 float_T f;
15333
15334 rettv->v_type = VAR_FLOAT;
15335 if (get_float_arg(argvars, &f) == OK)
15336 /* round() is not in C90, use ceil() or floor() instead. */
15337 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15338 else
15339 rettv->vval.v_float = 0.0;
15340}
15341#endif
15342
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015343/*
15344 * "search()" function
15345 */
15346 static void
15347f_search(argvars, rettv)
15348 typval_T *argvars;
15349 typval_T *rettv;
15350{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015351 int flags = 0;
15352
15353 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354}
15355
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015357 * "searchdecl()" function
15358 */
15359 static void
15360f_searchdecl(argvars, rettv)
15361 typval_T *argvars;
15362 typval_T *rettv;
15363{
15364 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015365 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015366 int error = FALSE;
15367 char_u *name;
15368
15369 rettv->vval.v_number = 1; /* default: FAIL */
15370
15371 name = get_tv_string_chk(&argvars[0]);
15372 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015373 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015374 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015375 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15376 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15377 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015378 if (!error && name != NULL)
15379 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015380 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015381}
15382
15383/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015384 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015386 static int
15387searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015388 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015389 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390{
15391 char_u *spat, *mpat, *epat;
15392 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394 int dir;
15395 int flags = 0;
15396 char_u nbuf1[NUMBUFLEN];
15397 char_u nbuf2[NUMBUFLEN];
15398 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015399 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015400 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015401 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015404 spat = get_tv_string_chk(&argvars[0]);
15405 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15406 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15407 if (spat == NULL || mpat == NULL || epat == NULL)
15408 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 /* Handle the optional fourth argument: flags */
15411 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015412 if (dir == 0)
15413 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015414
15415 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015416 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15417 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015418 if ((flags & (SP_END | SP_SUBPAT)) != 0
15419 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015420 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015421 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015422 goto theend;
15423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015425 /* Using 'r' implies 'W', otherwise it doesn't work. */
15426 if (flags & SP_REPEAT)
15427 p_ws = FALSE;
15428
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015429 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015430 if (argvars[3].v_type == VAR_UNKNOWN
15431 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432 skip = (char_u *)"";
15433 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015435 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015436 if (argvars[5].v_type != VAR_UNKNOWN)
15437 {
15438 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15439 if (lnum_stop < 0)
15440 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015441#ifdef FEAT_RELTIME
15442 if (argvars[6].v_type != VAR_UNKNOWN)
15443 {
15444 time_limit = get_tv_number_chk(&argvars[6], NULL);
15445 if (time_limit < 0)
15446 goto theend;
15447 }
15448#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015449 }
15450 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015451 if (skip == NULL)
15452 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015454 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015455 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015456
15457theend:
15458 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015459
15460 return retval;
15461}
15462
15463/*
15464 * "searchpair()" function
15465 */
15466 static void
15467f_searchpair(argvars, rettv)
15468 typval_T *argvars;
15469 typval_T *rettv;
15470{
15471 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15472}
15473
15474/*
15475 * "searchpairpos()" function
15476 */
15477 static void
15478f_searchpairpos(argvars, rettv)
15479 typval_T *argvars;
15480 typval_T *rettv;
15481{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015482 pos_T match_pos;
15483 int lnum = 0;
15484 int col = 0;
15485
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015486 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015487 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015488
15489 if (searchpair_cmn(argvars, &match_pos) > 0)
15490 {
15491 lnum = match_pos.lnum;
15492 col = match_pos.col;
15493 }
15494
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015495 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15496 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015497}
15498
15499/*
15500 * Search for a start/middle/end thing.
15501 * Used by searchpair(), see its documentation for the details.
15502 * Returns 0 or -1 for no match,
15503 */
15504 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015505do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15506 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015507 char_u *spat; /* start pattern */
15508 char_u *mpat; /* middle pattern */
15509 char_u *epat; /* end pattern */
15510 int dir; /* BACKWARD or FORWARD */
15511 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015512 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015513 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015514 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015515 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015516{
15517 char_u *save_cpo;
15518 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15519 long retval = 0;
15520 pos_T pos;
15521 pos_T firstpos;
15522 pos_T foundpos;
15523 pos_T save_cursor;
15524 pos_T save_pos;
15525 int n;
15526 int r;
15527 int nest = 1;
15528 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015529 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015530 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015531
15532 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15533 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015534 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015535
Bram Moolenaar76929292008-01-06 19:07:36 +000015536#ifdef FEAT_RELTIME
15537 /* Set the time limit, if there is one. */
15538 profile_setlimit(time_limit, &tm);
15539#endif
15540
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015541 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15542 * start/middle/end (pat3, for the top pair). */
15543 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15544 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15545 if (pat2 == NULL || pat3 == NULL)
15546 goto theend;
15547 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15548 if (*mpat == NUL)
15549 STRCPY(pat3, pat2);
15550 else
15551 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15552 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015553 if (flags & SP_START)
15554 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015555
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 save_cursor = curwin->w_cursor;
15557 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015558 clearpos(&firstpos);
15559 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 pat = pat3;
15561 for (;;)
15562 {
15563 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015564 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15566 /* didn't find it or found the first match again: FAIL */
15567 break;
15568
15569 if (firstpos.lnum == 0)
15570 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015571 if (equalpos(pos, foundpos))
15572 {
15573 /* Found the same position again. Can happen with a pattern that
15574 * has "\zs" at the end and searching backwards. Advance one
15575 * character and try again. */
15576 if (dir == BACKWARD)
15577 decl(&pos);
15578 else
15579 incl(&pos);
15580 }
15581 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015583 /* clear the start flag to avoid getting stuck here */
15584 options &= ~SEARCH_START;
15585
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 /* If the skip pattern matches, ignore this match. */
15587 if (*skip != NUL)
15588 {
15589 save_pos = curwin->w_cursor;
15590 curwin->w_cursor = pos;
15591 r = eval_to_bool(skip, &err, NULL, FALSE);
15592 curwin->w_cursor = save_pos;
15593 if (err)
15594 {
15595 /* Evaluating {skip} caused an error, break here. */
15596 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015597 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 break;
15599 }
15600 if (r)
15601 continue;
15602 }
15603
15604 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15605 {
15606 /* Found end when searching backwards or start when searching
15607 * forward: nested pair. */
15608 ++nest;
15609 pat = pat2; /* nested, don't search for middle */
15610 }
15611 else
15612 {
15613 /* Found end when searching forward or start when searching
15614 * backward: end of (nested) pair; or found middle in outer pair. */
15615 if (--nest == 1)
15616 pat = pat3; /* outer level, search for middle */
15617 }
15618
15619 if (nest == 0)
15620 {
15621 /* Found the match: return matchcount or line number. */
15622 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015623 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015625 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015626 if (flags & SP_SETPCMARK)
15627 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 curwin->w_cursor = pos;
15629 if (!(flags & SP_REPEAT))
15630 break;
15631 nest = 1; /* search for next unmatched */
15632 }
15633 }
15634
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015635 if (match_pos != NULL)
15636 {
15637 /* Store the match cursor position */
15638 match_pos->lnum = curwin->w_cursor.lnum;
15639 match_pos->col = curwin->w_cursor.col + 1;
15640 }
15641
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015643 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015644 curwin->w_cursor = save_cursor;
15645
15646theend:
15647 vim_free(pat2);
15648 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015649 if (p_cpo == empty_option)
15650 p_cpo = save_cpo;
15651 else
15652 /* Darn, evaluating the {skip} expression changed the value. */
15653 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015654
15655 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656}
15657
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015658/*
15659 * "searchpos()" function
15660 */
15661 static void
15662f_searchpos(argvars, rettv)
15663 typval_T *argvars;
15664 typval_T *rettv;
15665{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015666 pos_T match_pos;
15667 int lnum = 0;
15668 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015669 int n;
15670 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015671
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015672 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015673 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015674
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015675 n = search_cmn(argvars, &match_pos, &flags);
15676 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015677 {
15678 lnum = match_pos.lnum;
15679 col = match_pos.col;
15680 }
15681
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015682 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15683 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015684 if (flags & SP_SUBPAT)
15685 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015686}
15687
15688
Bram Moolenaar0d660222005-01-07 21:51:51 +000015689 static void
15690f_server2client(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 Moolenaar071d4272004-06-13 20:20:40 +000015693{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015694#ifdef FEAT_CLIENTSERVER
15695 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015696 char_u *server = get_tv_string_chk(&argvars[0]);
15697 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698
Bram Moolenaar0d660222005-01-07 21:51:51 +000015699 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015700 if (server == NULL || reply == NULL)
15701 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015702 if (check_restricted() || check_secure())
15703 return;
15704# ifdef FEAT_X11
15705 if (check_connection() == FAIL)
15706 return;
15707# endif
15708
15709 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015711 EMSG(_("E258: Unable to send to client"));
15712 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015714 rettv->vval.v_number = 0;
15715#else
15716 rettv->vval.v_number = -1;
15717#endif
15718}
15719
Bram Moolenaar0d660222005-01-07 21:51:51 +000015720 static void
15721f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015722 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015723 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015724{
15725 char_u *r = NULL;
15726
15727#ifdef FEAT_CLIENTSERVER
15728# ifdef WIN32
15729 r = serverGetVimNames();
15730# else
15731 make_connection();
15732 if (X_DISPLAY != NULL)
15733 r = serverGetVimNames(X_DISPLAY);
15734# endif
15735#endif
15736 rettv->v_type = VAR_STRING;
15737 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738}
15739
15740/*
15741 * "setbufvar()" function
15742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015744f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015745 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015746 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747{
15748 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015751 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 char_u nbuf[NUMBUFLEN];
15753
15754 if (check_restricted() || check_secure())
15755 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015756 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15757 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015758 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 varp = &argvars[2];
15760
15761 if (buf != NULL && varname != NULL && varp != NULL)
15762 {
15763 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765
15766 if (*varname == '&')
15767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015768 long numval;
15769 char_u *strval;
15770 int error = FALSE;
15771
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015773 numval = get_tv_number_chk(varp, &error);
15774 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015775 if (!error && strval != NULL)
15776 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 }
15778 else
15779 {
15780 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15781 if (bufvarname != NULL)
15782 {
15783 STRCPY(bufvarname, "b:");
15784 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015785 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 vim_free(bufvarname);
15787 }
15788 }
15789
15790 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793}
15794
15795/*
15796 * "setcmdpos()" function
15797 */
15798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015799f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015800 typval_T *argvars;
15801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015803 int pos = (int)get_tv_number(&argvars[0]) - 1;
15804
15805 if (pos >= 0)
15806 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015807}
15808
15809/*
15810 * "setline()" function
15811 */
15812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015813f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015814 typval_T *argvars;
15815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816{
15817 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015818 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015819 list_T *l = NULL;
15820 listitem_T *li = NULL;
15821 long added = 0;
15822 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015824 lnum = get_tv_lnum(&argvars[0]);
15825 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015827 l = argvars[1].vval.v_list;
15828 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015830 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015831 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015832
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015833 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015834 for (;;)
15835 {
15836 if (l != NULL)
15837 {
15838 /* list argument, get next string */
15839 if (li == NULL)
15840 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015841 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015842 li = li->li_next;
15843 }
15844
15845 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015846 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015847 break;
15848 if (lnum <= curbuf->b_ml.ml_line_count)
15849 {
15850 /* existing line, replace it */
15851 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15852 {
15853 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015854 if (lnum == curwin->w_cursor.lnum)
15855 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015856 rettv->vval.v_number = 0; /* OK */
15857 }
15858 }
15859 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15860 {
15861 /* lnum is one past the last line, append the line */
15862 ++added;
15863 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15864 rettv->vval.v_number = 0; /* OK */
15865 }
15866
15867 if (l == NULL) /* only one string argument */
15868 break;
15869 ++lnum;
15870 }
15871
15872 if (added > 0)
15873 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874}
15875
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015876static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15877
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015879 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015880 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015881 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015882set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015883 win_T *wp UNUSED;
15884 typval_T *list_arg UNUSED;
15885 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015886 typval_T *rettv;
15887{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015888#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015889 char_u *act;
15890 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015891#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015892
Bram Moolenaar2641f772005-03-25 21:58:17 +000015893 rettv->vval.v_number = -1;
15894
15895#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015896 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015897 EMSG(_(e_listreq));
15898 else
15899 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015900 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015901
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015902 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015903 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015904 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015905 if (act == NULL)
15906 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015907 if (*act == 'a' || *act == 'r')
15908 action = *act;
15909 }
15910
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015911 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015912 rettv->vval.v_number = 0;
15913 }
15914#endif
15915}
15916
15917/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015918 * "setloclist()" function
15919 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015920 static void
15921f_setloclist(argvars, rettv)
15922 typval_T *argvars;
15923 typval_T *rettv;
15924{
15925 win_T *win;
15926
15927 rettv->vval.v_number = -1;
15928
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015929 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015930 if (win != NULL)
15931 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15932}
15933
15934/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015935 * "setmatches()" function
15936 */
15937 static void
15938f_setmatches(argvars, rettv)
15939 typval_T *argvars;
15940 typval_T *rettv;
15941{
15942#ifdef FEAT_SEARCH_EXTRA
15943 list_T *l;
15944 listitem_T *li;
15945 dict_T *d;
15946
15947 rettv->vval.v_number = -1;
15948 if (argvars[0].v_type != VAR_LIST)
15949 {
15950 EMSG(_(e_listreq));
15951 return;
15952 }
15953 if ((l = argvars[0].vval.v_list) != NULL)
15954 {
15955
15956 /* To some extent make sure that we are dealing with a list from
15957 * "getmatches()". */
15958 li = l->lv_first;
15959 while (li != NULL)
15960 {
15961 if (li->li_tv.v_type != VAR_DICT
15962 || (d = li->li_tv.vval.v_dict) == NULL)
15963 {
15964 EMSG(_(e_invarg));
15965 return;
15966 }
15967 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15968 && dict_find(d, (char_u *)"pattern", -1) != NULL
15969 && dict_find(d, (char_u *)"priority", -1) != NULL
15970 && dict_find(d, (char_u *)"id", -1) != NULL))
15971 {
15972 EMSG(_(e_invarg));
15973 return;
15974 }
15975 li = li->li_next;
15976 }
15977
15978 clear_matches(curwin);
15979 li = l->lv_first;
15980 while (li != NULL)
15981 {
15982 d = li->li_tv.vval.v_dict;
15983 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15984 get_dict_string(d, (char_u *)"pattern", FALSE),
15985 (int)get_dict_number(d, (char_u *)"priority"),
15986 (int)get_dict_number(d, (char_u *)"id"));
15987 li = li->li_next;
15988 }
15989 rettv->vval.v_number = 0;
15990 }
15991#endif
15992}
15993
15994/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015995 * "setpos()" function
15996 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015997 static void
15998f_setpos(argvars, rettv)
15999 typval_T *argvars;
16000 typval_T *rettv;
16001{
16002 pos_T pos;
16003 int fnum;
16004 char_u *name;
16005
Bram Moolenaar08250432008-02-13 11:42:46 +000016006 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016007 name = get_tv_string_chk(argvars);
16008 if (name != NULL)
16009 {
16010 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16011 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016012 if (--pos.col < 0)
16013 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016014 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016015 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016016 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016017 if (fnum == curbuf->b_fnum)
16018 {
16019 curwin->w_cursor = pos;
16020 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016021 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016022 }
16023 else
16024 EMSG(_(e_invarg));
16025 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016026 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16027 {
16028 /* set mark */
16029 if (setmark_pos(name[1], &pos, fnum) == OK)
16030 rettv->vval.v_number = 0;
16031 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016032 else
16033 EMSG(_(e_invarg));
16034 }
16035 }
16036}
16037
16038/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016039 * "setqflist()" function
16040 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016041 static void
16042f_setqflist(argvars, rettv)
16043 typval_T *argvars;
16044 typval_T *rettv;
16045{
16046 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16047}
16048
16049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 * "setreg()" function
16051 */
16052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016053f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016054 typval_T *argvars;
16055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056{
16057 int regname;
16058 char_u *strregname;
16059 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016060 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 int append;
16062 char_u yank_type;
16063 long block_len;
16064
16065 block_len = -1;
16066 yank_type = MAUTO;
16067 append = FALSE;
16068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016069 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016070 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016072 if (strregname == NULL)
16073 return; /* type error; errmsg already given */
16074 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 if (regname == 0 || regname == '@')
16076 regname = '"';
16077 else if (regname == '=')
16078 return;
16079
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016080 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016082 stropt = get_tv_string_chk(&argvars[2]);
16083 if (stropt == NULL)
16084 return; /* type error */
16085 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 switch (*stropt)
16087 {
16088 case 'a': case 'A': /* append */
16089 append = TRUE;
16090 break;
16091 case 'v': case 'c': /* character-wise selection */
16092 yank_type = MCHAR;
16093 break;
16094 case 'V': case 'l': /* line-wise selection */
16095 yank_type = MLINE;
16096 break;
16097#ifdef FEAT_VISUAL
16098 case 'b': case Ctrl_V: /* block-wise selection */
16099 yank_type = MBLOCK;
16100 if (VIM_ISDIGIT(stropt[1]))
16101 {
16102 ++stropt;
16103 block_len = getdigits(&stropt) - 1;
16104 --stropt;
16105 }
16106 break;
16107#endif
16108 }
16109 }
16110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016111 strval = get_tv_string_chk(&argvars[1]);
16112 if (strval != NULL)
16113 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016115 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116}
16117
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016118/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016119 * "settabvar()" function
16120 */
16121 static void
16122f_settabvar(argvars, rettv)
16123 typval_T *argvars;
16124 typval_T *rettv;
16125{
16126 tabpage_T *save_curtab;
16127 char_u *varname, *tabvarname;
16128 typval_T *varp;
16129 tabpage_T *tp;
16130
16131 rettv->vval.v_number = 0;
16132
16133 if (check_restricted() || check_secure())
16134 return;
16135
16136 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16137 varname = get_tv_string_chk(&argvars[1]);
16138 varp = &argvars[2];
16139
16140 if (tp != NULL && varname != NULL && varp != NULL)
16141 {
16142 save_curtab = curtab;
16143 goto_tabpage_tp(tp);
16144
16145 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16146 if (tabvarname != NULL)
16147 {
16148 STRCPY(tabvarname, "t:");
16149 STRCPY(tabvarname + 2, varname);
16150 set_var(tabvarname, varp, TRUE);
16151 vim_free(tabvarname);
16152 }
16153
16154 /* Restore current tabpage */
16155 if (valid_tabpage(save_curtab))
16156 goto_tabpage_tp(save_curtab);
16157 }
16158}
16159
16160/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016161 * "settabwinvar()" function
16162 */
16163 static void
16164f_settabwinvar(argvars, rettv)
16165 typval_T *argvars;
16166 typval_T *rettv;
16167{
16168 setwinvar(argvars, rettv, 1);
16169}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170
16171/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016172 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016175f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 typval_T *argvars;
16177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016179 setwinvar(argvars, rettv, 0);
16180}
16181
16182/*
16183 * "setwinvar()" and "settabwinvar()" functions
16184 */
16185 static void
16186setwinvar(argvars, rettv, off)
16187 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016188 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016189 int off;
16190{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191 win_T *win;
16192#ifdef FEAT_WINDOWS
16193 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016194 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195#endif
16196 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016197 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016199 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200
16201 if (check_restricted() || check_secure())
16202 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016203
16204#ifdef FEAT_WINDOWS
16205 if (off == 1)
16206 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16207 else
16208 tp = curtab;
16209#endif
16210 win = find_win_by_nr(&argvars[off], tp);
16211 varname = get_tv_string_chk(&argvars[off + 1]);
16212 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213
16214 if (win != NULL && varname != NULL && varp != NULL)
16215 {
16216#ifdef FEAT_WINDOWS
16217 /* set curwin to be our win, temporarily */
16218 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016219 save_curtab = curtab;
16220 goto_tabpage_tp(tp);
16221 if (!win_valid(win))
16222 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223 curwin = win;
16224 curbuf = curwin->w_buffer;
16225#endif
16226
16227 if (*varname == '&')
16228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016229 long numval;
16230 char_u *strval;
16231 int error = FALSE;
16232
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016234 numval = get_tv_number_chk(varp, &error);
16235 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016236 if (!error && strval != NULL)
16237 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 }
16239 else
16240 {
16241 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16242 if (winvarname != NULL)
16243 {
16244 STRCPY(winvarname, "w:");
16245 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016246 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 vim_free(winvarname);
16248 }
16249 }
16250
16251#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016252 /* Restore current tabpage and window, if still valid (autocomands can
16253 * make them invalid). */
16254 if (valid_tabpage(save_curtab))
16255 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 if (win_valid(save_curwin))
16257 {
16258 curwin = save_curwin;
16259 curbuf = curwin->w_buffer;
16260 }
16261#endif
16262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263}
16264
16265/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016266 * "shellescape({string})" function
16267 */
16268 static void
16269f_shellescape(argvars, rettv)
16270 typval_T *argvars;
16271 typval_T *rettv;
16272{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016273 rettv->vval.v_string = vim_strsave_shellescape(
16274 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016275 rettv->v_type = VAR_STRING;
16276}
16277
16278/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016279 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280 */
16281 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016283 typval_T *argvars;
16284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016286 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287
Bram Moolenaar0d660222005-01-07 21:51:51 +000016288 p = get_tv_string(&argvars[0]);
16289 rettv->vval.v_string = vim_strsave(p);
16290 simplify_filename(rettv->vval.v_string); /* simplify in place */
16291 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292}
16293
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016294#ifdef FEAT_FLOAT
16295/*
16296 * "sin()" function
16297 */
16298 static void
16299f_sin(argvars, rettv)
16300 typval_T *argvars;
16301 typval_T *rettv;
16302{
16303 float_T f;
16304
16305 rettv->v_type = VAR_FLOAT;
16306 if (get_float_arg(argvars, &f) == OK)
16307 rettv->vval.v_float = sin(f);
16308 else
16309 rettv->vval.v_float = 0.0;
16310}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016311
16312/*
16313 * "sinh()" function
16314 */
16315 static void
16316f_sinh(argvars, rettv)
16317 typval_T *argvars;
16318 typval_T *rettv;
16319{
16320 float_T f;
16321
16322 rettv->v_type = VAR_FLOAT;
16323 if (get_float_arg(argvars, &f) == OK)
16324 rettv->vval.v_float = sinh(f);
16325 else
16326 rettv->vval.v_float = 0.0;
16327}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016328#endif
16329
Bram Moolenaar0d660222005-01-07 21:51:51 +000016330static int
16331#ifdef __BORLANDC__
16332 _RTLENTRYF
16333#endif
16334 item_compare __ARGS((const void *s1, const void *s2));
16335static int
16336#ifdef __BORLANDC__
16337 _RTLENTRYF
16338#endif
16339 item_compare2 __ARGS((const void *s1, const void *s2));
16340
16341static int item_compare_ic;
16342static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016343static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016344#define ITEM_COMPARE_FAIL 999
16345
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016347 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016349 static int
16350#ifdef __BORLANDC__
16351_RTLENTRYF
16352#endif
16353item_compare(s1, s2)
16354 const void *s1;
16355 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016357 char_u *p1, *p2;
16358 char_u *tofree1, *tofree2;
16359 int res;
16360 char_u numbuf1[NUMBUFLEN];
16361 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016363 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16364 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016365 if (p1 == NULL)
16366 p1 = (char_u *)"";
16367 if (p2 == NULL)
16368 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016369 if (item_compare_ic)
16370 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016372 res = STRCMP(p1, p2);
16373 vim_free(tofree1);
16374 vim_free(tofree2);
16375 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376}
16377
16378 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016379#ifdef __BORLANDC__
16380_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016382item_compare2(s1, s2)
16383 const void *s1;
16384 const void *s2;
16385{
16386 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016387 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016388 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016389 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016391 /* shortcut after failure in previous call; compare all items equal */
16392 if (item_compare_func_err)
16393 return 0;
16394
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016395 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16396 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016397 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16398 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399
16400 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016401 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016402 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016403 clear_tv(&argv[0]);
16404 clear_tv(&argv[1]);
16405
16406 if (res == FAIL)
16407 res = ITEM_COMPARE_FAIL;
16408 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016409 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16410 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016411 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412 clear_tv(&rettv);
16413 return res;
16414}
16415
16416/*
16417 * "sort({list})" function
16418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016420f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016421 typval_T *argvars;
16422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016423{
Bram Moolenaar33570922005-01-25 22:26:29 +000016424 list_T *l;
16425 listitem_T *li;
16426 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427 long len;
16428 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429
Bram Moolenaar0d660222005-01-07 21:51:51 +000016430 if (argvars[0].v_type != VAR_LIST)
16431 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432 else
16433 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016434 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016435 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436 return;
16437 rettv->vval.v_list = l;
16438 rettv->v_type = VAR_LIST;
16439 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440
Bram Moolenaar0d660222005-01-07 21:51:51 +000016441 len = list_len(l);
16442 if (len <= 1)
16443 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444
Bram Moolenaar0d660222005-01-07 21:51:51 +000016445 item_compare_ic = FALSE;
16446 item_compare_func = NULL;
16447 if (argvars[1].v_type != VAR_UNKNOWN)
16448 {
16449 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016450 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016451 else
16452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016453 int error = FALSE;
16454
16455 i = get_tv_number_chk(&argvars[1], &error);
16456 if (error)
16457 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458 if (i == 1)
16459 item_compare_ic = TRUE;
16460 else
16461 item_compare_func = get_tv_string(&argvars[1]);
16462 }
16463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464
Bram Moolenaar0d660222005-01-07 21:51:51 +000016465 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016466 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016467 if (ptrs == NULL)
16468 return;
16469 i = 0;
16470 for (li = l->lv_first; li != NULL; li = li->li_next)
16471 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016473 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474 /* test the compare function */
16475 if (item_compare_func != NULL
16476 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16477 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016478 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480 {
16481 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016482 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016483 item_compare_func == NULL ? item_compare : item_compare2);
16484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016485 if (!item_compare_func_err)
16486 {
16487 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016488 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016489 l->lv_len = 0;
16490 for (i = 0; i < len; ++i)
16491 list_append(l, ptrs[i]);
16492 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016493 }
16494
16495 vim_free(ptrs);
16496 }
16497}
16498
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016499/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016500 * "soundfold({word})" function
16501 */
16502 static void
16503f_soundfold(argvars, rettv)
16504 typval_T *argvars;
16505 typval_T *rettv;
16506{
16507 char_u *s;
16508
16509 rettv->v_type = VAR_STRING;
16510 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016511#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016512 rettv->vval.v_string = eval_soundfold(s);
16513#else
16514 rettv->vval.v_string = vim_strsave(s);
16515#endif
16516}
16517
16518/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016519 * "spellbadword()" function
16520 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016521 static void
16522f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016523 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016524 typval_T *rettv;
16525{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016526 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016527 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016528 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016529
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016530 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016531 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016532
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016533#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016534 if (argvars[0].v_type == VAR_UNKNOWN)
16535 {
16536 /* Find the start and length of the badly spelled word. */
16537 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16538 if (len != 0)
16539 word = ml_get_cursor();
16540 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016541 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016542 {
16543 char_u *str = get_tv_string_chk(&argvars[0]);
16544 int capcol = -1;
16545
16546 if (str != NULL)
16547 {
16548 /* Check the argument for spelling. */
16549 while (*str != NUL)
16550 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016551 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016552 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016553 {
16554 word = str;
16555 break;
16556 }
16557 str += len;
16558 }
16559 }
16560 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016561#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016562
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016563 list_append_string(rettv->vval.v_list, word, len);
16564 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016565 attr == HLF_SPB ? "bad" :
16566 attr == HLF_SPR ? "rare" :
16567 attr == HLF_SPL ? "local" :
16568 attr == HLF_SPC ? "caps" :
16569 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016570}
16571
16572/*
16573 * "spellsuggest()" function
16574 */
16575 static void
16576f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016577 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016578 typval_T *rettv;
16579{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016580#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016581 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016582 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016583 int maxcount;
16584 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016585 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016586 listitem_T *li;
16587 int need_capital = FALSE;
16588#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016589
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016590 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016591 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016592
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016593#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016594 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016595 {
16596 str = get_tv_string(&argvars[0]);
16597 if (argvars[1].v_type != VAR_UNKNOWN)
16598 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016599 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016600 if (maxcount <= 0)
16601 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016602 if (argvars[2].v_type != VAR_UNKNOWN)
16603 {
16604 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16605 if (typeerr)
16606 return;
16607 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016608 }
16609 else
16610 maxcount = 25;
16611
Bram Moolenaar4770d092006-01-12 23:22:24 +000016612 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016613
16614 for (i = 0; i < ga.ga_len; ++i)
16615 {
16616 str = ((char_u **)ga.ga_data)[i];
16617
16618 li = listitem_alloc();
16619 if (li == NULL)
16620 vim_free(str);
16621 else
16622 {
16623 li->li_tv.v_type = VAR_STRING;
16624 li->li_tv.v_lock = 0;
16625 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016626 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016627 }
16628 }
16629 ga_clear(&ga);
16630 }
16631#endif
16632}
16633
Bram Moolenaar0d660222005-01-07 21:51:51 +000016634 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016635f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016636 typval_T *argvars;
16637 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016638{
16639 char_u *str;
16640 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016641 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016642 regmatch_T regmatch;
16643 char_u patbuf[NUMBUFLEN];
16644 char_u *save_cpo;
16645 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016646 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016647 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016648 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016649
16650 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16651 save_cpo = p_cpo;
16652 p_cpo = (char_u *)"";
16653
16654 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016655 if (argvars[1].v_type != VAR_UNKNOWN)
16656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016657 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16658 if (pat == NULL)
16659 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016660 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016661 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016662 }
16663 if (pat == NULL || *pat == NUL)
16664 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016665
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016666 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016668 if (typeerr)
16669 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670
Bram Moolenaar0d660222005-01-07 21:51:51 +000016671 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16672 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016674 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016675 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016676 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016677 if (*str == NUL)
16678 match = FALSE; /* empty item at the end */
16679 else
16680 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016681 if (match)
16682 end = regmatch.startp[0];
16683 else
16684 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016685 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16686 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016687 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016688 if (list_append_string(rettv->vval.v_list, str,
16689 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016690 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016691 }
16692 if (!match)
16693 break;
16694 /* Advance to just after the match. */
16695 if (regmatch.endp[0] > str)
16696 col = 0;
16697 else
16698 {
16699 /* Don't get stuck at the same match. */
16700#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016701 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016702#else
16703 col = 1;
16704#endif
16705 }
16706 str = regmatch.endp[0];
16707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708
Bram Moolenaar0d660222005-01-07 21:51:51 +000016709 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711
Bram Moolenaar0d660222005-01-07 21:51:51 +000016712 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713}
16714
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016715#ifdef FEAT_FLOAT
16716/*
16717 * "sqrt()" function
16718 */
16719 static void
16720f_sqrt(argvars, rettv)
16721 typval_T *argvars;
16722 typval_T *rettv;
16723{
16724 float_T f;
16725
16726 rettv->v_type = VAR_FLOAT;
16727 if (get_float_arg(argvars, &f) == OK)
16728 rettv->vval.v_float = sqrt(f);
16729 else
16730 rettv->vval.v_float = 0.0;
16731}
16732
16733/*
16734 * "str2float()" function
16735 */
16736 static void
16737f_str2float(argvars, rettv)
16738 typval_T *argvars;
16739 typval_T *rettv;
16740{
16741 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16742
16743 if (*p == '+')
16744 p = skipwhite(p + 1);
16745 (void)string2float(p, &rettv->vval.v_float);
16746 rettv->v_type = VAR_FLOAT;
16747}
16748#endif
16749
Bram Moolenaar2c932302006-03-18 21:42:09 +000016750/*
16751 * "str2nr()" function
16752 */
16753 static void
16754f_str2nr(argvars, rettv)
16755 typval_T *argvars;
16756 typval_T *rettv;
16757{
16758 int base = 10;
16759 char_u *p;
16760 long n;
16761
16762 if (argvars[1].v_type != VAR_UNKNOWN)
16763 {
16764 base = get_tv_number(&argvars[1]);
16765 if (base != 8 && base != 10 && base != 16)
16766 {
16767 EMSG(_(e_invarg));
16768 return;
16769 }
16770 }
16771
16772 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016773 if (*p == '+')
16774 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016775 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16776 rettv->vval.v_number = n;
16777}
16778
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779#ifdef HAVE_STRFTIME
16780/*
16781 * "strftime({format}[, {time}])" function
16782 */
16783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016784f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016785 typval_T *argvars;
16786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787{
16788 char_u result_buf[256];
16789 struct tm *curtime;
16790 time_t seconds;
16791 char_u *p;
16792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016793 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016795 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016796 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 seconds = time(NULL);
16798 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016799 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 curtime = localtime(&seconds);
16801 /* MSVC returns NULL for an invalid value of seconds. */
16802 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016803 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 else
16805 {
16806# ifdef FEAT_MBYTE
16807 vimconv_T conv;
16808 char_u *enc;
16809
16810 conv.vc_type = CONV_NONE;
16811 enc = enc_locale();
16812 convert_setup(&conv, p_enc, enc);
16813 if (conv.vc_type != CONV_NONE)
16814 p = string_convert(&conv, p, NULL);
16815# endif
16816 if (p != NULL)
16817 (void)strftime((char *)result_buf, sizeof(result_buf),
16818 (char *)p, curtime);
16819 else
16820 result_buf[0] = NUL;
16821
16822# ifdef FEAT_MBYTE
16823 if (conv.vc_type != CONV_NONE)
16824 vim_free(p);
16825 convert_setup(&conv, enc, p_enc);
16826 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016827 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 else
16829# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016830 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831
16832# ifdef FEAT_MBYTE
16833 /* Release conversion descriptors */
16834 convert_setup(&conv, NULL, NULL);
16835 vim_free(enc);
16836# endif
16837 }
16838}
16839#endif
16840
16841/*
16842 * "stridx()" function
16843 */
16844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016845f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016846 typval_T *argvars;
16847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848{
16849 char_u buf[NUMBUFLEN];
16850 char_u *needle;
16851 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016852 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016854 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016856 needle = get_tv_string_chk(&argvars[1]);
16857 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016858 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016859 if (needle == NULL || haystack == NULL)
16860 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861
Bram Moolenaar33570922005-01-25 22:26:29 +000016862 if (argvars[2].v_type != VAR_UNKNOWN)
16863 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016864 int error = FALSE;
16865
16866 start_idx = get_tv_number_chk(&argvars[2], &error);
16867 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016868 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016869 if (start_idx >= 0)
16870 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016871 }
16872
16873 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16874 if (pos != NULL)
16875 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876}
16877
16878/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016879 * "string()" function
16880 */
16881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016882f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016883 typval_T *argvars;
16884 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016885{
16886 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016887 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016889 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016890 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016891 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016892 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016893 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894}
16895
16896/*
16897 * "strlen()" function
16898 */
16899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016900f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016901 typval_T *argvars;
16902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016904 rettv->vval.v_number = (varnumber_T)(STRLEN(
16905 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906}
16907
16908/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016909 * "strchars()" function
16910 */
16911 static void
16912f_strchars(argvars, rettv)
16913 typval_T *argvars;
16914 typval_T *rettv;
16915{
16916 char_u *s = get_tv_string(&argvars[0]);
16917#ifdef FEAT_MBYTE
16918 varnumber_T len = 0;
16919
16920 while (*s != NUL)
16921 {
16922 mb_cptr2char_adv(&s);
16923 ++len;
16924 }
16925 rettv->vval.v_number = len;
16926#else
16927 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16928#endif
16929}
16930
16931/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016932 * "strdisplaywidth()" function
16933 */
16934 static void
16935f_strdisplaywidth(argvars, rettv)
16936 typval_T *argvars;
16937 typval_T *rettv;
16938{
16939 char_u *s = get_tv_string(&argvars[0]);
16940 int col = 0;
16941
16942 if (argvars[1].v_type != VAR_UNKNOWN)
16943 col = get_tv_number(&argvars[1]);
16944
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016945 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016946}
16947
16948/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016949 * "strwidth()" function
16950 */
16951 static void
16952f_strwidth(argvars, rettv)
16953 typval_T *argvars;
16954 typval_T *rettv;
16955{
16956 char_u *s = get_tv_string(&argvars[0]);
16957
16958 rettv->vval.v_number = (varnumber_T)(
16959#ifdef FEAT_MBYTE
16960 mb_string2cells(s, -1)
16961#else
16962 STRLEN(s)
16963#endif
16964 );
16965}
16966
16967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 * "strpart()" function
16969 */
16970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016971f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016972 typval_T *argvars;
16973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974{
16975 char_u *p;
16976 int n;
16977 int len;
16978 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016979 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016981 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 slen = (int)STRLEN(p);
16983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 n = get_tv_number_chk(&argvars[1], &error);
16985 if (error)
16986 len = 0;
16987 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016988 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989 else
16990 len = slen - n; /* default len: all bytes that are available. */
16991
16992 /*
16993 * Only return the overlap between the specified part and the actual
16994 * string.
16995 */
16996 if (n < 0)
16997 {
16998 len += n;
16999 n = 0;
17000 }
17001 else if (n > slen)
17002 n = slen;
17003 if (len < 0)
17004 len = 0;
17005 else if (n + len > slen)
17006 len = slen - n;
17007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017008 rettv->v_type = VAR_STRING;
17009 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010}
17011
17012/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017013 * "strridx()" function
17014 */
17015 static void
17016f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017017 typval_T *argvars;
17018 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019{
17020 char_u buf[NUMBUFLEN];
17021 char_u *needle;
17022 char_u *haystack;
17023 char_u *rest;
17024 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017025 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017027 needle = get_tv_string_chk(&argvars[1]);
17028 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017029
17030 rettv->vval.v_number = -1;
17031 if (needle == NULL || haystack == NULL)
17032 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017033
17034 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017035 if (argvars[2].v_type != VAR_UNKNOWN)
17036 {
17037 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017038 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017039 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017040 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017041 }
17042 else
17043 end_idx = haystack_len;
17044
Bram Moolenaar0d660222005-01-07 21:51:51 +000017045 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017046 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017047 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017048 lastmatch = haystack + end_idx;
17049 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017050 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017051 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017052 for (rest = haystack; *rest != '\0'; ++rest)
17053 {
17054 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017055 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017056 break;
17057 lastmatch = rest;
17058 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017059 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060
17061 if (lastmatch == NULL)
17062 rettv->vval.v_number = -1;
17063 else
17064 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17065}
17066
17067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068 * "strtrans()" function
17069 */
17070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017071f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017072 typval_T *argvars;
17073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017075 rettv->v_type = VAR_STRING;
17076 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077}
17078
17079/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017080 * "submatch()" function
17081 */
17082 static void
17083f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017084 typval_T *argvars;
17085 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017086{
17087 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017088 rettv->vval.v_string =
17089 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017090}
17091
17092/*
17093 * "substitute()" function
17094 */
17095 static void
17096f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017097 typval_T *argvars;
17098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017099{
17100 char_u patbuf[NUMBUFLEN];
17101 char_u subbuf[NUMBUFLEN];
17102 char_u flagsbuf[NUMBUFLEN];
17103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017104 char_u *str = get_tv_string_chk(&argvars[0]);
17105 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17106 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17107 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17108
Bram Moolenaar0d660222005-01-07 21:51:51 +000017109 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017110 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17111 rettv->vval.v_string = NULL;
17112 else
17113 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017114}
17115
17116/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017117 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017120f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017121 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123{
17124 int id = 0;
17125#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017126 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 long col;
17128 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017129 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017131 lnum = get_tv_lnum(argvars); /* -1 on type error */
17132 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17133 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017135 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017136 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017137 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138#endif
17139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017140 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141}
17142
17143/*
17144 * "synIDattr(id, what [, mode])" function
17145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017147f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017148 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150{
17151 char_u *p = NULL;
17152#ifdef FEAT_SYN_HL
17153 int id;
17154 char_u *what;
17155 char_u *mode;
17156 char_u modebuf[NUMBUFLEN];
17157 int modec;
17158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017159 id = get_tv_number(&argvars[0]);
17160 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017161 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017163 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017165 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 modec = 0; /* replace invalid with current */
17167 }
17168 else
17169 {
17170#ifdef FEAT_GUI
17171 if (gui.in_use)
17172 modec = 'g';
17173 else
17174#endif
17175 if (t_colors > 1)
17176 modec = 'c';
17177 else
17178 modec = 't';
17179 }
17180
17181
17182 switch (TOLOWER_ASC(what[0]))
17183 {
17184 case 'b':
17185 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17186 p = highlight_color(id, what, modec);
17187 else /* bold */
17188 p = highlight_has_attr(id, HL_BOLD, modec);
17189 break;
17190
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017191 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 p = highlight_color(id, what, modec);
17193 break;
17194
17195 case 'i':
17196 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17197 p = highlight_has_attr(id, HL_INVERSE, modec);
17198 else /* italic */
17199 p = highlight_has_attr(id, HL_ITALIC, modec);
17200 break;
17201
17202 case 'n': /* name */
17203 p = get_highlight_name(NULL, id - 1);
17204 break;
17205
17206 case 'r': /* reverse */
17207 p = highlight_has_attr(id, HL_INVERSE, modec);
17208 break;
17209
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017210 case 's':
17211 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17212 p = highlight_color(id, what, modec);
17213 else /* standout */
17214 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 break;
17216
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017217 case 'u':
17218 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17219 /* underline */
17220 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17221 else
17222 /* undercurl */
17223 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 break;
17225 }
17226
17227 if (p != NULL)
17228 p = vim_strsave(p);
17229#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017230 rettv->v_type = VAR_STRING;
17231 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232}
17233
17234/*
17235 * "synIDtrans(id)" function
17236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017238f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017239 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241{
17242 int id;
17243
17244#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017245 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246
17247 if (id > 0)
17248 id = syn_get_final_id(id);
17249 else
17250#endif
17251 id = 0;
17252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017253 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254}
17255
17256/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017257 * "synconcealed(lnum, col)" function
17258 */
17259 static void
17260f_synconcealed(argvars, rettv)
17261 typval_T *argvars UNUSED;
17262 typval_T *rettv;
17263{
17264#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17265 long lnum;
17266 long col;
17267 int syntax_flags = 0;
17268 int cchar;
17269 int matchid = 0;
17270 char_u str[NUMBUFLEN];
17271#endif
17272
17273 rettv->v_type = VAR_LIST;
17274 rettv->vval.v_list = NULL;
17275
17276#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17277 lnum = get_tv_lnum(argvars); /* -1 on type error */
17278 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17279
17280 vim_memset(str, NUL, sizeof(str));
17281
17282 if (rettv_list_alloc(rettv) != FAIL)
17283 {
17284 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17285 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17286 && curwin->w_p_cole > 0)
17287 {
17288 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17289 syntax_flags = get_syntax_info(&matchid);
17290
17291 /* get the conceal character */
17292 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17293 {
17294 cchar = syn_get_sub_char();
17295 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17296 cchar = lcs_conceal;
17297 if (cchar != NUL)
17298 {
17299# ifdef FEAT_MBYTE
17300 if (has_mbyte)
17301 (*mb_char2bytes)(cchar, str);
17302 else
17303# endif
17304 str[0] = cchar;
17305 }
17306 }
17307 }
17308
17309 list_append_number(rettv->vval.v_list,
17310 (syntax_flags & HL_CONCEAL) != 0);
17311 /* -1 to auto-determine strlen */
17312 list_append_string(rettv->vval.v_list, str, -1);
17313 list_append_number(rettv->vval.v_list, matchid);
17314 }
17315#endif
17316}
17317
17318/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017319 * "synstack(lnum, col)" function
17320 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017321 static void
17322f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017323 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017324 typval_T *rettv;
17325{
17326#ifdef FEAT_SYN_HL
17327 long lnum;
17328 long col;
17329 int i;
17330 int id;
17331#endif
17332
17333 rettv->v_type = VAR_LIST;
17334 rettv->vval.v_list = NULL;
17335
17336#ifdef FEAT_SYN_HL
17337 lnum = get_tv_lnum(argvars); /* -1 on type error */
17338 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17339
17340 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017341 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017342 && rettv_list_alloc(rettv) != FAIL)
17343 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017344 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017345 for (i = 0; ; ++i)
17346 {
17347 id = syn_get_stack_item(i);
17348 if (id < 0)
17349 break;
17350 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17351 break;
17352 }
17353 }
17354#endif
17355}
17356
17357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358 * "system()" function
17359 */
17360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017361f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017362 typval_T *argvars;
17363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017365 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017367 char_u *infile = NULL;
17368 char_u buf[NUMBUFLEN];
17369 int err = FALSE;
17370 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017372 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017373 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017374
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017375 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017376 {
17377 /*
17378 * Write the string to a temp file, to be used for input of the shell
17379 * command.
17380 */
17381 if ((infile = vim_tempname('i')) == NULL)
17382 {
17383 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017384 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017385 }
17386
17387 fd = mch_fopen((char *)infile, WRITEBIN);
17388 if (fd == NULL)
17389 {
17390 EMSG2(_(e_notopen), infile);
17391 goto done;
17392 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017393 p = get_tv_string_buf_chk(&argvars[1], buf);
17394 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017395 {
17396 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017397 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017398 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017399 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17400 err = TRUE;
17401 if (fclose(fd) != 0)
17402 err = TRUE;
17403 if (err)
17404 {
17405 EMSG(_("E677: Error writing temp file"));
17406 goto done;
17407 }
17408 }
17409
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017410 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17411 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017412
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413#ifdef USE_CR
17414 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017415 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 {
17417 char_u *s;
17418
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017419 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420 {
17421 if (*s == CAR)
17422 *s = NL;
17423 }
17424 }
17425#else
17426# ifdef USE_CRNL
17427 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017428 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429 {
17430 char_u *s, *d;
17431
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017432 d = res;
17433 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434 {
17435 if (s[0] == CAR && s[1] == NL)
17436 ++s;
17437 *d++ = *s;
17438 }
17439 *d = NUL;
17440 }
17441# endif
17442#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017443
17444done:
17445 if (infile != NULL)
17446 {
17447 mch_remove(infile);
17448 vim_free(infile);
17449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017450 rettv->v_type = VAR_STRING;
17451 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452}
17453
17454/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017455 * "tabpagebuflist()" function
17456 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017457 static void
17458f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017459 typval_T *argvars UNUSED;
17460 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017461{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017462#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017463 tabpage_T *tp;
17464 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017465
17466 if (argvars[0].v_type == VAR_UNKNOWN)
17467 wp = firstwin;
17468 else
17469 {
17470 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17471 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017472 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017473 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017474 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017475 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017476 for (; wp != NULL; wp = wp->w_next)
17477 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017478 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017479 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017480 }
17481#endif
17482}
17483
17484
17485/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017486 * "tabpagenr()" function
17487 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017488 static void
17489f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017490 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017491 typval_T *rettv;
17492{
17493 int nr = 1;
17494#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017495 char_u *arg;
17496
17497 if (argvars[0].v_type != VAR_UNKNOWN)
17498 {
17499 arg = get_tv_string_chk(&argvars[0]);
17500 nr = 0;
17501 if (arg != NULL)
17502 {
17503 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017504 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017505 else
17506 EMSG2(_(e_invexpr2), arg);
17507 }
17508 }
17509 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017510 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017511#endif
17512 rettv->vval.v_number = nr;
17513}
17514
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017515
17516#ifdef FEAT_WINDOWS
17517static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17518
17519/*
17520 * Common code for tabpagewinnr() and winnr().
17521 */
17522 static int
17523get_winnr(tp, argvar)
17524 tabpage_T *tp;
17525 typval_T *argvar;
17526{
17527 win_T *twin;
17528 int nr = 1;
17529 win_T *wp;
17530 char_u *arg;
17531
17532 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17533 if (argvar->v_type != VAR_UNKNOWN)
17534 {
17535 arg = get_tv_string_chk(argvar);
17536 if (arg == NULL)
17537 nr = 0; /* type error; errmsg already given */
17538 else if (STRCMP(arg, "$") == 0)
17539 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17540 else if (STRCMP(arg, "#") == 0)
17541 {
17542 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17543 if (twin == NULL)
17544 nr = 0;
17545 }
17546 else
17547 {
17548 EMSG2(_(e_invexpr2), arg);
17549 nr = 0;
17550 }
17551 }
17552
17553 if (nr > 0)
17554 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17555 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017556 {
17557 if (wp == NULL)
17558 {
17559 /* didn't find it in this tabpage */
17560 nr = 0;
17561 break;
17562 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017563 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017564 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017565 return nr;
17566}
17567#endif
17568
17569/*
17570 * "tabpagewinnr()" function
17571 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017572 static void
17573f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017574 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017575 typval_T *rettv;
17576{
17577 int nr = 1;
17578#ifdef FEAT_WINDOWS
17579 tabpage_T *tp;
17580
17581 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17582 if (tp == NULL)
17583 nr = 0;
17584 else
17585 nr = get_winnr(tp, &argvars[1]);
17586#endif
17587 rettv->vval.v_number = nr;
17588}
17589
17590
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017591/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017592 * "tagfiles()" function
17593 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017594 static void
17595f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017596 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017597 typval_T *rettv;
17598{
17599 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017600 tagname_T tn;
17601 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017602
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017603 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017604 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017605
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017606 for (first = TRUE; ; first = FALSE)
17607 if (get_tagfname(&tn, first, fname) == FAIL
17608 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017609 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017610 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017611}
17612
17613/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017614 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017615 */
17616 static void
17617f_taglist(argvars, rettv)
17618 typval_T *argvars;
17619 typval_T *rettv;
17620{
17621 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017622
17623 tag_pattern = get_tv_string(&argvars[0]);
17624
17625 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017626 if (*tag_pattern == NUL)
17627 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017628
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017629 if (rettv_list_alloc(rettv) == OK)
17630 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017631}
17632
17633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 * "tempname()" function
17635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017637f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017638 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640{
17641 static int x = 'A';
17642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017643 rettv->v_type = VAR_STRING;
17644 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645
17646 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17647 * names. Skip 'I' and 'O', they are used for shell redirection. */
17648 do
17649 {
17650 if (x == 'Z')
17651 x = '0';
17652 else if (x == '9')
17653 x = 'A';
17654 else
17655 {
17656#ifdef EBCDIC
17657 if (x == 'I')
17658 x = 'J';
17659 else if (x == 'R')
17660 x = 'S';
17661 else
17662#endif
17663 ++x;
17664 }
17665 } while (x == 'I' || x == 'O');
17666}
17667
17668/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017669 * "test(list)" function: Just checking the walls...
17670 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017671 static void
17672f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017673 typval_T *argvars UNUSED;
17674 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017675{
17676 /* Used for unit testing. Change the code below to your liking. */
17677#if 0
17678 listitem_T *li;
17679 list_T *l;
17680 char_u *bad, *good;
17681
17682 if (argvars[0].v_type != VAR_LIST)
17683 return;
17684 l = argvars[0].vval.v_list;
17685 if (l == NULL)
17686 return;
17687 li = l->lv_first;
17688 if (li == NULL)
17689 return;
17690 bad = get_tv_string(&li->li_tv);
17691 li = li->li_next;
17692 if (li == NULL)
17693 return;
17694 good = get_tv_string(&li->li_tv);
17695 rettv->vval.v_number = test_edit_score(bad, good);
17696#endif
17697}
17698
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017699#ifdef FEAT_FLOAT
17700/*
17701 * "tan()" function
17702 */
17703 static void
17704f_tan(argvars, rettv)
17705 typval_T *argvars;
17706 typval_T *rettv;
17707{
17708 float_T f;
17709
17710 rettv->v_type = VAR_FLOAT;
17711 if (get_float_arg(argvars, &f) == OK)
17712 rettv->vval.v_float = tan(f);
17713 else
17714 rettv->vval.v_float = 0.0;
17715}
17716
17717/*
17718 * "tanh()" function
17719 */
17720 static void
17721f_tanh(argvars, rettv)
17722 typval_T *argvars;
17723 typval_T *rettv;
17724{
17725 float_T f;
17726
17727 rettv->v_type = VAR_FLOAT;
17728 if (get_float_arg(argvars, &f) == OK)
17729 rettv->vval.v_float = tanh(f);
17730 else
17731 rettv->vval.v_float = 0.0;
17732}
17733#endif
17734
Bram Moolenaard52d9742005-08-21 22:20:28 +000017735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 * "tolower(string)" function
17737 */
17738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017739f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017740 typval_T *argvars;
17741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742{
17743 char_u *p;
17744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017745 p = vim_strsave(get_tv_string(&argvars[0]));
17746 rettv->v_type = VAR_STRING;
17747 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748
17749 if (p != NULL)
17750 while (*p != NUL)
17751 {
17752#ifdef FEAT_MBYTE
17753 int l;
17754
17755 if (enc_utf8)
17756 {
17757 int c, lc;
17758
17759 c = utf_ptr2char(p);
17760 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017761 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 /* TODO: reallocate string when byte count changes. */
17763 if (utf_char2len(lc) == l)
17764 utf_char2bytes(lc, p);
17765 p += l;
17766 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017767 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768 p += l; /* skip multi-byte character */
17769 else
17770#endif
17771 {
17772 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17773 ++p;
17774 }
17775 }
17776}
17777
17778/*
17779 * "toupper(string)" function
17780 */
17781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017782f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017783 typval_T *argvars;
17784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017786 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017787 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788}
17789
17790/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017791 * "tr(string, fromstr, tostr)" function
17792 */
17793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017794f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017795 typval_T *argvars;
17796 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017797{
17798 char_u *instr;
17799 char_u *fromstr;
17800 char_u *tostr;
17801 char_u *p;
17802#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017803 int inlen;
17804 int fromlen;
17805 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017806 int idx;
17807 char_u *cpstr;
17808 int cplen;
17809 int first = TRUE;
17810#endif
17811 char_u buf[NUMBUFLEN];
17812 char_u buf2[NUMBUFLEN];
17813 garray_T ga;
17814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017815 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017816 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17817 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017818
17819 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017820 rettv->v_type = VAR_STRING;
17821 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017822 if (fromstr == NULL || tostr == NULL)
17823 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017824 ga_init2(&ga, (int)sizeof(char), 80);
17825
17826#ifdef FEAT_MBYTE
17827 if (!has_mbyte)
17828#endif
17829 /* not multi-byte: fromstr and tostr must be the same length */
17830 if (STRLEN(fromstr) != STRLEN(tostr))
17831 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017832#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017833error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017834#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017835 EMSG2(_(e_invarg2), fromstr);
17836 ga_clear(&ga);
17837 return;
17838 }
17839
17840 /* fromstr and tostr have to contain the same number of chars */
17841 while (*instr != NUL)
17842 {
17843#ifdef FEAT_MBYTE
17844 if (has_mbyte)
17845 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017846 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017847 cpstr = instr;
17848 cplen = inlen;
17849 idx = 0;
17850 for (p = fromstr; *p != NUL; p += fromlen)
17851 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017852 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017853 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17854 {
17855 for (p = tostr; *p != NUL; p += tolen)
17856 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017857 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017858 if (idx-- == 0)
17859 {
17860 cplen = tolen;
17861 cpstr = p;
17862 break;
17863 }
17864 }
17865 if (*p == NUL) /* tostr is shorter than fromstr */
17866 goto error;
17867 break;
17868 }
17869 ++idx;
17870 }
17871
17872 if (first && cpstr == instr)
17873 {
17874 /* Check that fromstr and tostr have the same number of
17875 * (multi-byte) characters. Done only once when a character
17876 * of instr doesn't appear in fromstr. */
17877 first = FALSE;
17878 for (p = tostr; *p != NUL; p += tolen)
17879 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017880 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017881 --idx;
17882 }
17883 if (idx != 0)
17884 goto error;
17885 }
17886
17887 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017888 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017889 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017890
17891 instr += inlen;
17892 }
17893 else
17894#endif
17895 {
17896 /* When not using multi-byte chars we can do it faster. */
17897 p = vim_strchr(fromstr, *instr);
17898 if (p != NULL)
17899 ga_append(&ga, tostr[p - fromstr]);
17900 else
17901 ga_append(&ga, *instr);
17902 ++instr;
17903 }
17904 }
17905
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017906 /* add a terminating NUL */
17907 ga_grow(&ga, 1);
17908 ga_append(&ga, NUL);
17909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017910 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017911}
17912
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017913#ifdef FEAT_FLOAT
17914/*
17915 * "trunc({float})" function
17916 */
17917 static void
17918f_trunc(argvars, rettv)
17919 typval_T *argvars;
17920 typval_T *rettv;
17921{
17922 float_T f;
17923
17924 rettv->v_type = VAR_FLOAT;
17925 if (get_float_arg(argvars, &f) == OK)
17926 /* trunc() is not in C90, use floor() or ceil() instead. */
17927 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17928 else
17929 rettv->vval.v_float = 0.0;
17930}
17931#endif
17932
Bram Moolenaar8299df92004-07-10 09:47:34 +000017933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 * "type(expr)" function
17935 */
17936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017937f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017938 typval_T *argvars;
17939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017941 int n;
17942
17943 switch (argvars[0].v_type)
17944 {
17945 case VAR_NUMBER: n = 0; break;
17946 case VAR_STRING: n = 1; break;
17947 case VAR_FUNC: n = 2; break;
17948 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017949 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017950#ifdef FEAT_FLOAT
17951 case VAR_FLOAT: n = 5; break;
17952#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017953 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17954 }
17955 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956}
17957
17958/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017959 * "undofile(name)" function
17960 */
17961 static void
17962f_undofile(argvars, rettv)
17963 typval_T *argvars;
17964 typval_T *rettv;
17965{
17966 rettv->v_type = VAR_STRING;
17967#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017968 {
17969 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17970
17971 if (ffname != NULL)
17972 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17973 vim_free(ffname);
17974 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017975#else
17976 rettv->vval.v_string = NULL;
17977#endif
17978}
17979
17980/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017981 * "undotree()" function
17982 */
17983 static void
17984f_undotree(argvars, rettv)
17985 typval_T *argvars UNUSED;
17986 typval_T *rettv;
17987{
17988 if (rettv_dict_alloc(rettv) == OK)
17989 {
17990 dict_T *dict = rettv->vval.v_dict;
17991 list_T *list;
17992
Bram Moolenaar730cde92010-06-27 05:18:54 +020017993 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017994 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017995 dict_add_nr_str(dict, "save_last",
17996 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017997 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17998 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017999 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018000
18001 list = list_alloc();
18002 if (list != NULL)
18003 {
18004 u_eval_tree(curbuf->b_u_oldhead, list);
18005 dict_add_list(dict, "entries", list);
18006 }
18007 }
18008}
18009
18010/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018011 * "values(dict)" function
18012 */
18013 static void
18014f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018015 typval_T *argvars;
18016 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018017{
18018 dict_list(argvars, rettv, 1);
18019}
18020
18021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 * "virtcol(string)" function
18023 */
18024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018025f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018026 typval_T *argvars;
18027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028{
18029 colnr_T vcol = 0;
18030 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018031 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018033 fp = var2fpos(&argvars[0], FALSE, &fnum);
18034 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18035 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 {
18037 getvvcol(curwin, fp, NULL, NULL, &vcol);
18038 ++vcol;
18039 }
18040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018041 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042}
18043
18044/*
18045 * "visualmode()" function
18046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018048f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018049 typval_T *argvars UNUSED;
18050 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051{
18052#ifdef FEAT_VISUAL
18053 char_u str[2];
18054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018055 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 str[0] = curbuf->b_visual_mode_eval;
18057 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018058 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059
18060 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018061 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063#endif
18064}
18065
18066/*
18067 * "winbufnr(nr)" function
18068 */
18069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018070f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018071 typval_T *argvars;
18072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073{
18074 win_T *wp;
18075
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018076 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018078 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018080 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081}
18082
18083/*
18084 * "wincol()" function
18085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018087f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018088 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090{
18091 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018092 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093}
18094
18095/*
18096 * "winheight(nr)" function
18097 */
18098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018099f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018100 typval_T *argvars;
18101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102{
18103 win_T *wp;
18104
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018105 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018109 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110}
18111
18112/*
18113 * "winline()" function
18114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018116f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018117 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119{
18120 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018121 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122}
18123
18124/*
18125 * "winnr()" function
18126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018128f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018129 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131{
18132 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018133
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018135 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018137 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138}
18139
18140/*
18141 * "winrestcmd()" function
18142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018144f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018145 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147{
18148#ifdef FEAT_WINDOWS
18149 win_T *wp;
18150 int winnr = 1;
18151 garray_T ga;
18152 char_u buf[50];
18153
18154 ga_init2(&ga, (int)sizeof(char), 70);
18155 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18156 {
18157 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18158 ga_concat(&ga, buf);
18159# ifdef FEAT_VERTSPLIT
18160 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18161 ga_concat(&ga, buf);
18162# endif
18163 ++winnr;
18164 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018165 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018167 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018169 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018171 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172}
18173
18174/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018175 * "winrestview()" function
18176 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018177 static void
18178f_winrestview(argvars, rettv)
18179 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018180 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018181{
18182 dict_T *dict;
18183
18184 if (argvars[0].v_type != VAR_DICT
18185 || (dict = argvars[0].vval.v_dict) == NULL)
18186 EMSG(_(e_invarg));
18187 else
18188 {
18189 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18190 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18191#ifdef FEAT_VIRTUALEDIT
18192 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18193#endif
18194 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018195 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018196
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018197 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018198#ifdef FEAT_DIFF
18199 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18200#endif
18201 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18202 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18203
18204 check_cursor();
18205 changed_cline_bef_curs();
18206 invalidate_botline();
18207 redraw_later(VALID);
18208
18209 if (curwin->w_topline == 0)
18210 curwin->w_topline = 1;
18211 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18212 curwin->w_topline = curbuf->b_ml.ml_line_count;
18213#ifdef FEAT_DIFF
18214 check_topfill(curwin, TRUE);
18215#endif
18216 }
18217}
18218
18219/*
18220 * "winsaveview()" function
18221 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018222 static void
18223f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018224 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018225 typval_T *rettv;
18226{
18227 dict_T *dict;
18228
Bram Moolenaara800b422010-06-27 01:15:55 +020018229 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018230 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018231 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018232
18233 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18234 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18235#ifdef FEAT_VIRTUALEDIT
18236 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18237#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018238 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018239 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18240
18241 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18242#ifdef FEAT_DIFF
18243 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18244#endif
18245 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18246 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18247}
18248
18249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 * "winwidth(nr)" function
18251 */
18252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018253f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018254 typval_T *argvars;
18255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256{
18257 win_T *wp;
18258
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018259 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018261 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 else
18263#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018264 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018266 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267#endif
18268}
18269
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018271 * "writefile()" function
18272 */
18273 static void
18274f_writefile(argvars, rettv)
18275 typval_T *argvars;
18276 typval_T *rettv;
18277{
18278 int binary = FALSE;
18279 char_u *fname;
18280 FILE *fd;
18281 listitem_T *li;
18282 char_u *s;
18283 int ret = 0;
18284 int c;
18285
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018286 if (check_restricted() || check_secure())
18287 return;
18288
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018289 if (argvars[0].v_type != VAR_LIST)
18290 {
18291 EMSG2(_(e_listarg), "writefile()");
18292 return;
18293 }
18294 if (argvars[0].vval.v_list == NULL)
18295 return;
18296
18297 if (argvars[2].v_type != VAR_UNKNOWN
18298 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18299 binary = TRUE;
18300
18301 /* Always open the file in binary mode, library functions have a mind of
18302 * their own about CR-LF conversion. */
18303 fname = get_tv_string(&argvars[1]);
18304 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18305 {
18306 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18307 ret = -1;
18308 }
18309 else
18310 {
18311 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18312 li = li->li_next)
18313 {
18314 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18315 {
18316 if (*s == '\n')
18317 c = putc(NUL, fd);
18318 else
18319 c = putc(*s, fd);
18320 if (c == EOF)
18321 {
18322 ret = -1;
18323 break;
18324 }
18325 }
18326 if (!binary || li->li_next != NULL)
18327 if (putc('\n', fd) == EOF)
18328 {
18329 ret = -1;
18330 break;
18331 }
18332 if (ret < 0)
18333 {
18334 EMSG(_(e_write));
18335 break;
18336 }
18337 }
18338 fclose(fd);
18339 }
18340
18341 rettv->vval.v_number = ret;
18342}
18343
18344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018346 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347 */
18348 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018349var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018350 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018351 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018352 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018354 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018356 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357
Bram Moolenaara5525202006-03-02 22:52:09 +000018358 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018359 if (varp->v_type == VAR_LIST)
18360 {
18361 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018362 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018363 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018364 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018365
18366 l = varp->vval.v_list;
18367 if (l == NULL)
18368 return NULL;
18369
18370 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018371 pos.lnum = list_find_nr(l, 0L, &error);
18372 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018373 return NULL; /* invalid line number */
18374
18375 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018376 pos.col = list_find_nr(l, 1L, &error);
18377 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018378 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018379 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018380
18381 /* We accept "$" for the column number: last column. */
18382 li = list_find(l, 1L);
18383 if (li != NULL && li->li_tv.v_type == VAR_STRING
18384 && li->li_tv.vval.v_string != NULL
18385 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18386 pos.col = len + 1;
18387
Bram Moolenaara5525202006-03-02 22:52:09 +000018388 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018389 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018390 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018391 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018392
Bram Moolenaara5525202006-03-02 22:52:09 +000018393#ifdef FEAT_VIRTUALEDIT
18394 /* Get the virtual offset. Defaults to zero. */
18395 pos.coladd = list_find_nr(l, 2L, &error);
18396 if (error)
18397 pos.coladd = 0;
18398#endif
18399
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018400 return &pos;
18401 }
18402
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018403 name = get_tv_string_chk(varp);
18404 if (name == NULL)
18405 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018406 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018408#ifdef FEAT_VISUAL
18409 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18410 {
18411 if (VIsual_active)
18412 return &VIsual;
18413 return &curwin->w_cursor;
18414 }
18415#endif
18416 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018418 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18420 return NULL;
18421 return pp;
18422 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018423
18424#ifdef FEAT_VIRTUALEDIT
18425 pos.coladd = 0;
18426#endif
18427
Bram Moolenaar477933c2007-07-17 14:32:23 +000018428 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018429 {
18430 pos.col = 0;
18431 if (name[1] == '0') /* "w0": first visible line */
18432 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018433 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018434 pos.lnum = curwin->w_topline;
18435 return &pos;
18436 }
18437 else if (name[1] == '$') /* "w$": last visible line */
18438 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018439 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018440 pos.lnum = curwin->w_botline - 1;
18441 return &pos;
18442 }
18443 }
18444 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018446 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 {
18448 pos.lnum = curbuf->b_ml.ml_line_count;
18449 pos.col = 0;
18450 }
18451 else
18452 {
18453 pos.lnum = curwin->w_cursor.lnum;
18454 pos.col = (colnr_T)STRLEN(ml_get_curline());
18455 }
18456 return &pos;
18457 }
18458 return NULL;
18459}
18460
18461/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018462 * Convert list in "arg" into a position and optional file number.
18463 * When "fnump" is NULL there is no file number, only 3 items.
18464 * Note that the column is passed on as-is, the caller may want to decrement
18465 * it to use 1 for the first column.
18466 * Return FAIL when conversion is not possible, doesn't check the position for
18467 * validity.
18468 */
18469 static int
18470list2fpos(arg, posp, fnump)
18471 typval_T *arg;
18472 pos_T *posp;
18473 int *fnump;
18474{
18475 list_T *l = arg->vval.v_list;
18476 long i = 0;
18477 long n;
18478
Bram Moolenaarbde35262006-07-23 20:12:24 +000018479 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18480 * when "fnump" isn't NULL and "coladd" is optional. */
18481 if (arg->v_type != VAR_LIST
18482 || l == NULL
18483 || l->lv_len < (fnump == NULL ? 2 : 3)
18484 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018485 return FAIL;
18486
18487 if (fnump != NULL)
18488 {
18489 n = list_find_nr(l, i++, NULL); /* fnum */
18490 if (n < 0)
18491 return FAIL;
18492 if (n == 0)
18493 n = curbuf->b_fnum; /* current buffer */
18494 *fnump = n;
18495 }
18496
18497 n = list_find_nr(l, i++, NULL); /* lnum */
18498 if (n < 0)
18499 return FAIL;
18500 posp->lnum = n;
18501
18502 n = list_find_nr(l, i++, NULL); /* col */
18503 if (n < 0)
18504 return FAIL;
18505 posp->col = n;
18506
18507#ifdef FEAT_VIRTUALEDIT
18508 n = list_find_nr(l, i, NULL);
18509 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018510 posp->coladd = 0;
18511 else
18512 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018513#endif
18514
18515 return OK;
18516}
18517
18518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 * Get the length of an environment variable name.
18520 * Advance "arg" to the first character after the name.
18521 * Return 0 for error.
18522 */
18523 static int
18524get_env_len(arg)
18525 char_u **arg;
18526{
18527 char_u *p;
18528 int len;
18529
18530 for (p = *arg; vim_isIDc(*p); ++p)
18531 ;
18532 if (p == *arg) /* no name found */
18533 return 0;
18534
18535 len = (int)(p - *arg);
18536 *arg = p;
18537 return len;
18538}
18539
18540/*
18541 * Get the length of the name of a function or internal variable.
18542 * "arg" is advanced to the first non-white character after the name.
18543 * Return 0 if something is wrong.
18544 */
18545 static int
18546get_id_len(arg)
18547 char_u **arg;
18548{
18549 char_u *p;
18550 int len;
18551
18552 /* Find the end of the name. */
18553 for (p = *arg; eval_isnamec(*p); ++p)
18554 ;
18555 if (p == *arg) /* no name found */
18556 return 0;
18557
18558 len = (int)(p - *arg);
18559 *arg = skipwhite(p);
18560
18561 return len;
18562}
18563
18564/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018565 * Get the length of the name of a variable or function.
18566 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018568 * Return -1 if curly braces expansion failed.
18569 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 * If the name contains 'magic' {}'s, expand them and return the
18571 * expanded name in an allocated string via 'alias' - caller must free.
18572 */
18573 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018574get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 char_u **arg;
18576 char_u **alias;
18577 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018578 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579{
18580 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581 char_u *p;
18582 char_u *expr_start;
18583 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584
18585 *alias = NULL; /* default to no alias */
18586
18587 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18588 && (*arg)[2] == (int)KE_SNR)
18589 {
18590 /* hard coded <SNR>, already translated */
18591 *arg += 3;
18592 return get_id_len(arg) + 3;
18593 }
18594 len = eval_fname_script(*arg);
18595 if (len > 0)
18596 {
18597 /* literal "<SID>", "s:" or "<SNR>" */
18598 *arg += len;
18599 }
18600
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018602 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018604 p = find_name_end(*arg, &expr_start, &expr_end,
18605 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 if (expr_start != NULL)
18607 {
18608 char_u *temp_string;
18609
18610 if (!evaluate)
18611 {
18612 len += (int)(p - *arg);
18613 *arg = skipwhite(p);
18614 return len;
18615 }
18616
18617 /*
18618 * Include any <SID> etc in the expanded string:
18619 * Thus the -len here.
18620 */
18621 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18622 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018623 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 *alias = temp_string;
18625 *arg = skipwhite(p);
18626 return (int)STRLEN(temp_string);
18627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628
18629 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018630 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631 EMSG2(_(e_invexpr2), *arg);
18632
18633 return len;
18634}
18635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018636/*
18637 * Find the end of a variable or function name, taking care of magic braces.
18638 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18639 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018640 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018641 * Return a pointer to just after the name. Equal to "arg" if there is no
18642 * valid name.
18643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018645find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 char_u *arg;
18647 char_u **expr_start;
18648 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018649 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018651 int mb_nest = 0;
18652 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 char_u *p;
18654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018655 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018657 *expr_start = NULL;
18658 *expr_end = NULL;
18659 }
18660
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018661 /* Quick check for valid starting character. */
18662 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18663 return arg;
18664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018665 for (p = arg; *p != NUL
18666 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018667 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018668 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018669 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018670 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018671 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018672 if (*p == '\'')
18673 {
18674 /* skip over 'string' to avoid counting [ and ] inside it. */
18675 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18676 ;
18677 if (*p == NUL)
18678 break;
18679 }
18680 else if (*p == '"')
18681 {
18682 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18683 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18684 if (*p == '\\' && p[1] != NUL)
18685 ++p;
18686 if (*p == NUL)
18687 break;
18688 }
18689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018690 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018692 if (*p == '[')
18693 ++br_nest;
18694 else if (*p == ']')
18695 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018698 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018700 if (*p == '{')
18701 {
18702 mb_nest++;
18703 if (expr_start != NULL && *expr_start == NULL)
18704 *expr_start = p;
18705 }
18706 else if (*p == '}')
18707 {
18708 mb_nest--;
18709 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18710 *expr_end = p;
18711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 }
18714
18715 return p;
18716}
18717
18718/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018719 * Expands out the 'magic' {}'s in a variable/function name.
18720 * Note that this can call itself recursively, to deal with
18721 * constructs like foo{bar}{baz}{bam}
18722 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18723 * "in_start" ^
18724 * "expr_start" ^
18725 * "expr_end" ^
18726 * "in_end" ^
18727 *
18728 * Returns a new allocated string, which the caller must free.
18729 * Returns NULL for failure.
18730 */
18731 static char_u *
18732make_expanded_name(in_start, expr_start, expr_end, in_end)
18733 char_u *in_start;
18734 char_u *expr_start;
18735 char_u *expr_end;
18736 char_u *in_end;
18737{
18738 char_u c1;
18739 char_u *retval = NULL;
18740 char_u *temp_result;
18741 char_u *nextcmd = NULL;
18742
18743 if (expr_end == NULL || in_end == NULL)
18744 return NULL;
18745 *expr_start = NUL;
18746 *expr_end = NUL;
18747 c1 = *in_end;
18748 *in_end = NUL;
18749
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018750 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018751 if (temp_result != NULL && nextcmd == NULL)
18752 {
18753 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18754 + (in_end - expr_end) + 1));
18755 if (retval != NULL)
18756 {
18757 STRCPY(retval, in_start);
18758 STRCAT(retval, temp_result);
18759 STRCAT(retval, expr_end + 1);
18760 }
18761 }
18762 vim_free(temp_result);
18763
18764 *in_end = c1; /* put char back for error messages */
18765 *expr_start = '{';
18766 *expr_end = '}';
18767
18768 if (retval != NULL)
18769 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018770 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018771 if (expr_start != NULL)
18772 {
18773 /* Further expansion! */
18774 temp_result = make_expanded_name(retval, expr_start,
18775 expr_end, temp_result);
18776 vim_free(retval);
18777 retval = temp_result;
18778 }
18779 }
18780
18781 return retval;
18782}
18783
18784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018786 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 */
18788 static int
18789eval_isnamec(c)
18790 int c;
18791{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018792 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18793}
18794
18795/*
18796 * Return TRUE if character "c" can be used as the first character in a
18797 * variable or function name (excluding '{' and '}').
18798 */
18799 static int
18800eval_isnamec1(c)
18801 int c;
18802{
18803 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804}
18805
18806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 * Set number v: variable to "val".
18808 */
18809 void
18810set_vim_var_nr(idx, val)
18811 int idx;
18812 long val;
18813{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018814 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815}
18816
18817/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018818 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 */
18820 long
18821get_vim_var_nr(idx)
18822 int idx;
18823{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018824 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825}
18826
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018827/*
18828 * Get string v: variable value. Uses a static buffer, can only be used once.
18829 */
18830 char_u *
18831get_vim_var_str(idx)
18832 int idx;
18833{
18834 return get_tv_string(&vimvars[idx].vv_tv);
18835}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018836
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018838 * Get List v: variable value. Caller must take care of reference count when
18839 * needed.
18840 */
18841 list_T *
18842get_vim_var_list(idx)
18843 int idx;
18844{
18845 return vimvars[idx].vv_list;
18846}
18847
18848/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018849 * Set v:char to character "c".
18850 */
18851 void
18852set_vim_var_char(c)
18853 int c;
18854{
18855#ifdef FEAT_MBYTE
18856 char_u buf[MB_MAXBYTES];
18857#else
18858 char_u buf[2];
18859#endif
18860
18861#ifdef FEAT_MBYTE
18862 if (has_mbyte)
18863 buf[(*mb_char2bytes)(c, buf)] = NUL;
18864 else
18865#endif
18866 {
18867 buf[0] = c;
18868 buf[1] = NUL;
18869 }
18870 set_vim_var_string(VV_CHAR, buf, -1);
18871}
18872
18873/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018874 * Set v:count to "count" and v:count1 to "count1".
18875 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876 */
18877 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018878set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 long count;
18880 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018881 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018883 if (set_prevcount)
18884 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018885 vimvars[VV_COUNT].vv_nr = count;
18886 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887}
18888
18889/*
18890 * Set string v: variable to a copy of "val".
18891 */
18892 void
18893set_vim_var_string(idx, val, len)
18894 int idx;
18895 char_u *val;
18896 int len; /* length of "val" to use or -1 (whole string) */
18897{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018898 /* Need to do this (at least) once, since we can't initialize a union.
18899 * Will always be invoked when "v:progname" is set. */
18900 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18901
Bram Moolenaare9a41262005-01-15 22:18:47 +000018902 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018904 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018906 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018908 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909}
18910
18911/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018912 * Set List v: variable to "val".
18913 */
18914 void
18915set_vim_var_list(idx, val)
18916 int idx;
18917 list_T *val;
18918{
18919 list_unref(vimvars[idx].vv_list);
18920 vimvars[idx].vv_list = val;
18921 if (val != NULL)
18922 ++val->lv_refcount;
18923}
18924
18925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 * Set v:register if needed.
18927 */
18928 void
18929set_reg_var(c)
18930 int c;
18931{
18932 char_u regname;
18933
18934 if (c == 0 || c == ' ')
18935 regname = '"';
18936 else
18937 regname = c;
18938 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018939 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940 set_vim_var_string(VV_REG, &regname, 1);
18941}
18942
18943/*
18944 * Get or set v:exception. If "oldval" == NULL, return the current value.
18945 * Otherwise, restore the value to "oldval" and return NULL.
18946 * Must always be called in pairs to save and restore v:exception! Does not
18947 * take care of memory allocations.
18948 */
18949 char_u *
18950v_exception(oldval)
18951 char_u *oldval;
18952{
18953 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018954 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955
Bram Moolenaare9a41262005-01-15 22:18:47 +000018956 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 return NULL;
18958}
18959
18960/*
18961 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18962 * Otherwise, restore the value to "oldval" and return NULL.
18963 * Must always be called in pairs to save and restore v:throwpoint! Does not
18964 * take care of memory allocations.
18965 */
18966 char_u *
18967v_throwpoint(oldval)
18968 char_u *oldval;
18969{
18970 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018971 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972
Bram Moolenaare9a41262005-01-15 22:18:47 +000018973 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 return NULL;
18975}
18976
18977#if defined(FEAT_AUTOCMD) || defined(PROTO)
18978/*
18979 * Set v:cmdarg.
18980 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18981 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18982 * Must always be called in pairs!
18983 */
18984 char_u *
18985set_cmdarg(eap, oldarg)
18986 exarg_T *eap;
18987 char_u *oldarg;
18988{
18989 char_u *oldval;
18990 char_u *newval;
18991 unsigned len;
18992
Bram Moolenaare9a41262005-01-15 22:18:47 +000018993 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018994 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018996 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018997 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018998 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999 }
19000
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019001 if (eap->force_bin == FORCE_BIN)
19002 len = 6;
19003 else if (eap->force_bin == FORCE_NOBIN)
19004 len = 8;
19005 else
19006 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019007
19008 if (eap->read_edit)
19009 len += 7;
19010
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019011 if (eap->force_ff != 0)
19012 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19013# ifdef FEAT_MBYTE
19014 if (eap->force_enc != 0)
19015 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019016 if (eap->bad_char != 0)
19017 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019018# endif
19019
19020 newval = alloc(len + 1);
19021 if (newval == NULL)
19022 return NULL;
19023
19024 if (eap->force_bin == FORCE_BIN)
19025 sprintf((char *)newval, " ++bin");
19026 else if (eap->force_bin == FORCE_NOBIN)
19027 sprintf((char *)newval, " ++nobin");
19028 else
19029 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019030
19031 if (eap->read_edit)
19032 STRCAT(newval, " ++edit");
19033
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019034 if (eap->force_ff != 0)
19035 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19036 eap->cmd + eap->force_ff);
19037# ifdef FEAT_MBYTE
19038 if (eap->force_enc != 0)
19039 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19040 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019041 if (eap->bad_char == BAD_KEEP)
19042 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19043 else if (eap->bad_char == BAD_DROP)
19044 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19045 else if (eap->bad_char != 0)
19046 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019047# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019048 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019049 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050}
19051#endif
19052
19053/*
19054 * Get the value of internal variable "name".
19055 * Return OK or FAIL.
19056 */
19057 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019058get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059 char_u *name;
19060 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019061 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019062 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063{
19064 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019065 typval_T *tv = NULL;
19066 typval_T atv;
19067 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069
19070 /* truncate the name, so that we can use strcmp() */
19071 cc = name[len];
19072 name[len] = NUL;
19073
19074 /*
19075 * Check for "b:changedtick".
19076 */
19077 if (STRCMP(name, "b:changedtick") == 0)
19078 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019079 atv.v_type = VAR_NUMBER;
19080 atv.vval.v_number = curbuf->b_changedtick;
19081 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 }
19083
19084 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 * Check for user-defined variables.
19086 */
19087 else
19088 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019089 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019091 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092 }
19093
Bram Moolenaare9a41262005-01-15 22:18:47 +000019094 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019096 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019097 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 ret = FAIL;
19099 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019100 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019101 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102
19103 name[len] = cc;
19104
19105 return ret;
19106}
19107
19108/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019109 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19110 * Also handle function call with Funcref variable: func(expr)
19111 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19112 */
19113 static int
19114handle_subscript(arg, rettv, evaluate, verbose)
19115 char_u **arg;
19116 typval_T *rettv;
19117 int evaluate; /* do more than finding the end */
19118 int verbose; /* give error messages */
19119{
19120 int ret = OK;
19121 dict_T *selfdict = NULL;
19122 char_u *s;
19123 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019124 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019125
19126 while (ret == OK
19127 && (**arg == '['
19128 || (**arg == '.' && rettv->v_type == VAR_DICT)
19129 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19130 && !vim_iswhite(*(*arg - 1)))
19131 {
19132 if (**arg == '(')
19133 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019134 /* need to copy the funcref so that we can clear rettv */
19135 functv = *rettv;
19136 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019137
19138 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019139 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019140 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019141 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19142 &len, evaluate, selfdict);
19143
19144 /* Clear the funcref afterwards, so that deleting it while
19145 * evaluating the arguments is possible (see test55). */
19146 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019147
19148 /* Stop the expression evaluation when immediately aborting on
19149 * error, or when an interrupt occurred or an exception was thrown
19150 * but not caught. */
19151 if (aborting())
19152 {
19153 if (ret == OK)
19154 clear_tv(rettv);
19155 ret = FAIL;
19156 }
19157 dict_unref(selfdict);
19158 selfdict = NULL;
19159 }
19160 else /* **arg == '[' || **arg == '.' */
19161 {
19162 dict_unref(selfdict);
19163 if (rettv->v_type == VAR_DICT)
19164 {
19165 selfdict = rettv->vval.v_dict;
19166 if (selfdict != NULL)
19167 ++selfdict->dv_refcount;
19168 }
19169 else
19170 selfdict = NULL;
19171 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19172 {
19173 clear_tv(rettv);
19174 ret = FAIL;
19175 }
19176 }
19177 }
19178 dict_unref(selfdict);
19179 return ret;
19180}
19181
19182/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019183 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019184 * value).
19185 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019186 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019187alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019188{
Bram Moolenaar33570922005-01-25 22:26:29 +000019189 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019190}
19191
19192/*
19193 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194 * The string "s" must have been allocated, it is consumed.
19195 * Return NULL for out of memory, the variable otherwise.
19196 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019197 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019198alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199 char_u *s;
19200{
Bram Moolenaar33570922005-01-25 22:26:29 +000019201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019203 rettv = alloc_tv();
19204 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019206 rettv->v_type = VAR_STRING;
19207 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208 }
19209 else
19210 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019211 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212}
19213
19214/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019215 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019217 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019218free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019219 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220{
19221 if (varp != NULL)
19222 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019223 switch (varp->v_type)
19224 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019225 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019226 func_unref(varp->vval.v_string);
19227 /*FALLTHROUGH*/
19228 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019229 vim_free(varp->vval.v_string);
19230 break;
19231 case VAR_LIST:
19232 list_unref(varp->vval.v_list);
19233 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019234 case VAR_DICT:
19235 dict_unref(varp->vval.v_dict);
19236 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019237 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019238#ifdef FEAT_FLOAT
19239 case VAR_FLOAT:
19240#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019241 case VAR_UNKNOWN:
19242 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019243 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019244 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019245 break;
19246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 vim_free(varp);
19248 }
19249}
19250
19251/*
19252 * Free the memory for a variable value and set the value to NULL or 0.
19253 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019254 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019255clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019256 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257{
19258 if (varp != NULL)
19259 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019260 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019262 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019263 func_unref(varp->vval.v_string);
19264 /*FALLTHROUGH*/
19265 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019266 vim_free(varp->vval.v_string);
19267 varp->vval.v_string = NULL;
19268 break;
19269 case VAR_LIST:
19270 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019271 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019272 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019273 case VAR_DICT:
19274 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019275 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019276 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019277 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019278 varp->vval.v_number = 0;
19279 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019280#ifdef FEAT_FLOAT
19281 case VAR_FLOAT:
19282 varp->vval.v_float = 0.0;
19283 break;
19284#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019285 case VAR_UNKNOWN:
19286 break;
19287 default:
19288 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019290 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291 }
19292}
19293
19294/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019295 * Set the value of a variable to NULL without freeing items.
19296 */
19297 static void
19298init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019299 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019300{
19301 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019302 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019303}
19304
19305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306 * Get the number value of a variable.
19307 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019308 * For incompatible types, return 0.
19309 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19310 * caller of incompatible types: it sets *denote to TRUE if "denote"
19311 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312 */
19313 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019314get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019315 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019317 int error = FALSE;
19318
19319 return get_tv_number_chk(varp, &error); /* return 0L on error */
19320}
19321
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019322 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019323get_tv_number_chk(varp, denote)
19324 typval_T *varp;
19325 int *denote;
19326{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019327 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019329 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019331 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019332 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019333#ifdef FEAT_FLOAT
19334 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019335 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019336 break;
19337#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019338 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019339 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019340 break;
19341 case VAR_STRING:
19342 if (varp->vval.v_string != NULL)
19343 vim_str2nr(varp->vval.v_string, NULL, NULL,
19344 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019345 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019346 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019347 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019348 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019349 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019350 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019351 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019352 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019353 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019354 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019356 if (denote == NULL) /* useful for values that must be unsigned */
19357 n = -1;
19358 else
19359 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019360 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361}
19362
19363/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019364 * Get the lnum from the first argument.
19365 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019366 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 */
19368 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019369get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019370 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371{
Bram Moolenaar33570922005-01-25 22:26:29 +000019372 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373 linenr_T lnum;
19374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019375 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 if (lnum == 0) /* no valid number, try using line() */
19377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019378 rettv.v_type = VAR_NUMBER;
19379 f_line(argvars, &rettv);
19380 lnum = rettv.vval.v_number;
19381 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 }
19383 return lnum;
19384}
19385
19386/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019387 * Get the lnum from the first argument.
19388 * Also accepts "$", then "buf" is used.
19389 * Returns 0 on error.
19390 */
19391 static linenr_T
19392get_tv_lnum_buf(argvars, buf)
19393 typval_T *argvars;
19394 buf_T *buf;
19395{
19396 if (argvars[0].v_type == VAR_STRING
19397 && argvars[0].vval.v_string != NULL
19398 && argvars[0].vval.v_string[0] == '$'
19399 && buf != NULL)
19400 return buf->b_ml.ml_line_count;
19401 return get_tv_number_chk(&argvars[0], NULL);
19402}
19403
19404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 * Get the string value of a variable.
19406 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019407 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19408 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 * If the String variable has never been set, return an empty string.
19410 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019411 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19412 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 */
19414 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019415get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019416 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417{
19418 static char_u mybuf[NUMBUFLEN];
19419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019420 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421}
19422
19423 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019424get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019425 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019426 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019428 char_u *res = get_tv_string_buf_chk(varp, buf);
19429
19430 return res != NULL ? res : (char_u *)"";
19431}
19432
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019433 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019434get_tv_string_chk(varp)
19435 typval_T *varp;
19436{
19437 static char_u mybuf[NUMBUFLEN];
19438
19439 return get_tv_string_buf_chk(varp, mybuf);
19440}
19441
19442 static char_u *
19443get_tv_string_buf_chk(varp, buf)
19444 typval_T *varp;
19445 char_u *buf;
19446{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019447 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019449 case VAR_NUMBER:
19450 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19451 return buf;
19452 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019453 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019454 break;
19455 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019456 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019457 break;
19458 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019459 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019460 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019461#ifdef FEAT_FLOAT
19462 case VAR_FLOAT:
19463 EMSG(_("E806: using Float as a String"));
19464 break;
19465#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019466 case VAR_STRING:
19467 if (varp->vval.v_string != NULL)
19468 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019469 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019470 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019471 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019472 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019474 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475}
19476
19477/*
19478 * Find variable "name" in the list of variables.
19479 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019480 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019481 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019482 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019484 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019485find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019487 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019490 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491
Bram Moolenaara7043832005-01-21 11:56:39 +000019492 ht = find_var_ht(name, &varname);
19493 if (htp != NULL)
19494 *htp = ht;
19495 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019497 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498}
19499
19500/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019501 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019502 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019504 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019505find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019506 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019507 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019508 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019509{
Bram Moolenaar33570922005-01-25 22:26:29 +000019510 hashitem_T *hi;
19511
19512 if (*varname == NUL)
19513 {
19514 /* Must be something like "s:", otherwise "ht" would be NULL. */
19515 switch (varname[-2])
19516 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019517 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019518 case 'g': return &globvars_var;
19519 case 'v': return &vimvars_var;
19520 case 'b': return &curbuf->b_bufvar;
19521 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019522#ifdef FEAT_WINDOWS
19523 case 't': return &curtab->tp_winvar;
19524#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019525 case 'l': return current_funccal == NULL
19526 ? NULL : &current_funccal->l_vars_var;
19527 case 'a': return current_funccal == NULL
19528 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019529 }
19530 return NULL;
19531 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019532
19533 hi = hash_find(ht, varname);
19534 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019535 {
19536 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019537 * worked find the variable again. Don't auto-load a script if it was
19538 * loaded already, otherwise it would be loaded every time when
19539 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019540 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019541 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019542 hi = hash_find(ht, varname);
19543 if (HASHITEM_EMPTY(hi))
19544 return NULL;
19545 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019546 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019547}
19548
19549/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019550 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019551 * Set "varname" to the start of name without ':'.
19552 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019553 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019554find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 char_u *name;
19556 char_u **varname;
19557{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019558 hashitem_T *hi;
19559
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 if (name[1] != ':')
19561 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019562 /* The name must not start with a colon or #. */
19563 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564 return NULL;
19565 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019566
19567 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019568 hi = hash_find(&compat_hashtab, name);
19569 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019570 return &compat_hashtab;
19571
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019573 return &globvarht; /* global variable */
19574 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575 }
19576 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019577 if (*name == 'g') /* global variable */
19578 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019579 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19580 */
19581 if (vim_strchr(name + 2, ':') != NULL
19582 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019583 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019585 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019587 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019588#ifdef FEAT_WINDOWS
19589 if (*name == 't') /* tab page variable */
19590 return &curtab->tp_vars.dv_hashtab;
19591#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019592 if (*name == 'v') /* v: variable */
19593 return &vimvarht;
19594 if (*name == 'a' && current_funccal != NULL) /* function argument */
19595 return &current_funccal->l_avars.dv_hashtab;
19596 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19597 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 if (*name == 's' /* script variable */
19599 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19600 return &SCRIPT_VARS(current_SID);
19601 return NULL;
19602}
19603
19604/*
19605 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019606 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 * Returns NULL when it doesn't exist.
19608 */
19609 char_u *
19610get_var_value(name)
19611 char_u *name;
19612{
Bram Moolenaar33570922005-01-25 22:26:29 +000019613 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614
Bram Moolenaara7043832005-01-21 11:56:39 +000019615 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 if (v == NULL)
19617 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019618 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619}
19620
19621/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019622 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 * sourcing this script and when executing functions defined in the script.
19624 */
19625 void
19626new_script_vars(id)
19627 scid_T id;
19628{
Bram Moolenaara7043832005-01-21 11:56:39 +000019629 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019630 hashtab_T *ht;
19631 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019632
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19634 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019635 /* Re-allocating ga_data means that an ht_array pointing to
19636 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019637 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019638 for (i = 1; i <= ga_scripts.ga_len; ++i)
19639 {
19640 ht = &SCRIPT_VARS(i);
19641 if (ht->ht_mask == HT_INIT_SIZE - 1)
19642 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019643 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019644 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019645 }
19646
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 while (ga_scripts.ga_len < id)
19648 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019649 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019650 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019651 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 }
19654 }
19655}
19656
19657/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019658 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19659 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 */
19661 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019662init_var_dict(dict, dict_var)
19663 dict_T *dict;
19664 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665{
Bram Moolenaar33570922005-01-25 22:26:29 +000019666 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019667 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019668 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019669 dict_var->di_tv.vval.v_dict = dict;
19670 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019671 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019672 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19673 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674}
19675
19676/*
19677 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019678 * Frees all allocated variables and the value they contain.
19679 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 */
19681 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019682vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019683 hashtab_T *ht;
19684{
19685 vars_clear_ext(ht, TRUE);
19686}
19687
19688/*
19689 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19690 */
19691 static void
19692vars_clear_ext(ht, free_val)
19693 hashtab_T *ht;
19694 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695{
Bram Moolenaara7043832005-01-21 11:56:39 +000019696 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019697 hashitem_T *hi;
19698 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699
Bram Moolenaar33570922005-01-25 22:26:29 +000019700 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019701 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019702 for (hi = ht->ht_array; todo > 0; ++hi)
19703 {
19704 if (!HASHITEM_EMPTY(hi))
19705 {
19706 --todo;
19707
Bram Moolenaar33570922005-01-25 22:26:29 +000019708 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019709 * ht_array might change then. hash_clear() takes care of it
19710 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019711 v = HI2DI(hi);
19712 if (free_val)
19713 clear_tv(&v->di_tv);
19714 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19715 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019716 }
19717 }
19718 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019719 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720}
19721
Bram Moolenaara7043832005-01-21 11:56:39 +000019722/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019723 * Delete a variable from hashtab "ht" at item "hi".
19724 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019727delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019728 hashtab_T *ht;
19729 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730{
Bram Moolenaar33570922005-01-25 22:26:29 +000019731 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019732
19733 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019734 clear_tv(&di->di_tv);
19735 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736}
19737
19738/*
19739 * List the value of one internal variable.
19740 */
19741 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019742list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019743 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019745 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019747 char_u *tofree;
19748 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019749 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019750
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019751 current_copyID += COPYID_INC;
19752 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019753 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019754 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019755 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756}
19757
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019759list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760 char_u *prefix;
19761 char_u *name;
19762 int type;
19763 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019764 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765{
Bram Moolenaar31859182007-08-14 20:41:13 +000019766 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19767 msg_start();
19768 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 if (name != NULL) /* "a:" vars don't have a name stored */
19770 msg_puts(name);
19771 msg_putchar(' ');
19772 msg_advance(22);
19773 if (type == VAR_NUMBER)
19774 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019775 else if (type == VAR_FUNC)
19776 msg_putchar('*');
19777 else if (type == VAR_LIST)
19778 {
19779 msg_putchar('[');
19780 if (*string == '[')
19781 ++string;
19782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019783 else if (type == VAR_DICT)
19784 {
19785 msg_putchar('{');
19786 if (*string == '{')
19787 ++string;
19788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 else
19790 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019791
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019793
19794 if (type == VAR_FUNC)
19795 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019796 if (*first)
19797 {
19798 msg_clr_eos();
19799 *first = FALSE;
19800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801}
19802
19803/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019804 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 * If the variable already exists, the value is updated.
19806 * Otherwise the variable is created.
19807 */
19808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019809set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019811 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019812 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813{
Bram Moolenaar33570922005-01-25 22:26:29 +000019814 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019816 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019818 ht = find_var_ht(name, &varname);
19819 if (ht == NULL || *varname == NUL)
19820 {
19821 EMSG2(_(e_illvar), name);
19822 return;
19823 }
19824 v = find_var_in_ht(ht, varname, TRUE);
19825
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019826 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
19827 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019828
Bram Moolenaar33570922005-01-25 22:26:29 +000019829 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019831 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019832 if (var_check_ro(v->di_flags, name)
19833 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019834 return;
19835 if (v->di_tv.v_type != tv->v_type
19836 && !((v->di_tv.v_type == VAR_STRING
19837 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019839 || tv->v_type == VAR_NUMBER))
19840#ifdef FEAT_FLOAT
19841 && !((v->di_tv.v_type == VAR_NUMBER
19842 || v->di_tv.v_type == VAR_FLOAT)
19843 && (tv->v_type == VAR_NUMBER
19844 || tv->v_type == VAR_FLOAT))
19845#endif
19846 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019847 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019848 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019849 return;
19850 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019851
19852 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019853 * Handle setting internal v: variables separately: we don't change
19854 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019855 */
19856 if (ht == &vimvarht)
19857 {
19858 if (v->di_tv.v_type == VAR_STRING)
19859 {
19860 vim_free(v->di_tv.vval.v_string);
19861 if (copy || tv->v_type != VAR_STRING)
19862 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19863 else
19864 {
19865 /* Take over the string to avoid an extra alloc/free. */
19866 v->di_tv.vval.v_string = tv->vval.v_string;
19867 tv->vval.v_string = NULL;
19868 }
19869 }
19870 else if (v->di_tv.v_type != VAR_NUMBER)
19871 EMSG2(_(e_intern2), "set_var()");
19872 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019873 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019874 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019875 if (STRCMP(varname, "searchforward") == 0)
19876 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19877 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019878 return;
19879 }
19880
19881 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882 }
19883 else /* add a new variable */
19884 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019885 /* Can't add "v:" variable. */
19886 if (ht == &vimvarht)
19887 {
19888 EMSG2(_(e_illvar), name);
19889 return;
19890 }
19891
Bram Moolenaar92124a32005-06-17 22:03:40 +000019892 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019893 if (!valid_varname(varname))
19894 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019895
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019896 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19897 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019898 if (v == NULL)
19899 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019900 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019901 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019903 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 return;
19905 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019906 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019908
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019909 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019910 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019911 else
19912 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019913 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019914 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019915 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917}
19918
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019919/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019920 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019921 * Also give an error message.
19922 */
19923 static int
19924var_check_ro(flags, name)
19925 int flags;
19926 char_u *name;
19927{
19928 if (flags & DI_FLAGS_RO)
19929 {
19930 EMSG2(_(e_readonlyvar), name);
19931 return TRUE;
19932 }
19933 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19934 {
19935 EMSG2(_(e_readonlysbx), name);
19936 return TRUE;
19937 }
19938 return FALSE;
19939}
19940
19941/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019942 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19943 * Also give an error message.
19944 */
19945 static int
19946var_check_fixed(flags, name)
19947 int flags;
19948 char_u *name;
19949{
19950 if (flags & DI_FLAGS_FIX)
19951 {
19952 EMSG2(_("E795: Cannot delete variable %s"), name);
19953 return TRUE;
19954 }
19955 return FALSE;
19956}
19957
19958/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019959 * Check if a funcref is assigned to a valid variable name.
19960 * Return TRUE and give an error if not.
19961 */
19962 static int
19963var_check_func_name(name, new_var)
19964 char_u *name; /* points to start of variable name */
19965 int new_var; /* TRUE when creating the variable */
19966{
19967 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19968 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19969 ? name[2] : name[0]))
19970 {
19971 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
19972 name);
19973 return TRUE;
19974 }
19975 /* Don't allow hiding a function. When "v" is not NULL we might be
19976 * assigning another function to the same var, the type is checked
19977 * below. */
19978 if (new_var && function_exists(name))
19979 {
19980 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
19981 name);
19982 return TRUE;
19983 }
19984 return FALSE;
19985}
19986
19987/*
19988 * Check if a variable name is valid.
19989 * Return FALSE and give an error if not.
19990 */
19991 static int
19992valid_varname(varname)
19993 char_u *varname;
19994{
19995 char_u *p;
19996
19997 for (p = varname; *p != NUL; ++p)
19998 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19999 && *p != AUTOLOAD_CHAR)
20000 {
20001 EMSG2(_(e_illvar), varname);
20002 return FALSE;
20003 }
20004 return TRUE;
20005}
20006
20007/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020008 * Return TRUE if typeval "tv" is set to be locked (immutable).
20009 * Also give an error message, using "name".
20010 */
20011 static int
20012tv_check_lock(lock, name)
20013 int lock;
20014 char_u *name;
20015{
20016 if (lock & VAR_LOCKED)
20017 {
20018 EMSG2(_("E741: Value is locked: %s"),
20019 name == NULL ? (char_u *)_("Unknown") : name);
20020 return TRUE;
20021 }
20022 if (lock & VAR_FIXED)
20023 {
20024 EMSG2(_("E742: Cannot change value of %s"),
20025 name == NULL ? (char_u *)_("Unknown") : name);
20026 return TRUE;
20027 }
20028 return FALSE;
20029}
20030
20031/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020032 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020033 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020034 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020035 * It is OK for "from" and "to" to point to the same item. This is used to
20036 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020037 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020038 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020039copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 typval_T *from;
20041 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020043 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020044 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020045 switch (from->v_type)
20046 {
20047 case VAR_NUMBER:
20048 to->vval.v_number = from->vval.v_number;
20049 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020050#ifdef FEAT_FLOAT
20051 case VAR_FLOAT:
20052 to->vval.v_float = from->vval.v_float;
20053 break;
20054#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020055 case VAR_STRING:
20056 case VAR_FUNC:
20057 if (from->vval.v_string == NULL)
20058 to->vval.v_string = NULL;
20059 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020060 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020061 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020062 if (from->v_type == VAR_FUNC)
20063 func_ref(to->vval.v_string);
20064 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020065 break;
20066 case VAR_LIST:
20067 if (from->vval.v_list == NULL)
20068 to->vval.v_list = NULL;
20069 else
20070 {
20071 to->vval.v_list = from->vval.v_list;
20072 ++to->vval.v_list->lv_refcount;
20073 }
20074 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020075 case VAR_DICT:
20076 if (from->vval.v_dict == NULL)
20077 to->vval.v_dict = NULL;
20078 else
20079 {
20080 to->vval.v_dict = from->vval.v_dict;
20081 ++to->vval.v_dict->dv_refcount;
20082 }
20083 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020084 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020085 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020086 break;
20087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088}
20089
20090/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020091 * Make a copy of an item.
20092 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020093 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20094 * reference to an already copied list/dict can be used.
20095 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020096 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020097 static int
20098item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020099 typval_T *from;
20100 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020101 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020102 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020103{
20104 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020105 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020106
Bram Moolenaar33570922005-01-25 22:26:29 +000020107 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020108 {
20109 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020110 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020111 }
20112 ++recurse;
20113
20114 switch (from->v_type)
20115 {
20116 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020117#ifdef FEAT_FLOAT
20118 case VAR_FLOAT:
20119#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020120 case VAR_STRING:
20121 case VAR_FUNC:
20122 copy_tv(from, to);
20123 break;
20124 case VAR_LIST:
20125 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020126 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020127 if (from->vval.v_list == NULL)
20128 to->vval.v_list = NULL;
20129 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20130 {
20131 /* use the copy made earlier */
20132 to->vval.v_list = from->vval.v_list->lv_copylist;
20133 ++to->vval.v_list->lv_refcount;
20134 }
20135 else
20136 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20137 if (to->vval.v_list == NULL)
20138 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020139 break;
20140 case VAR_DICT:
20141 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020142 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020143 if (from->vval.v_dict == NULL)
20144 to->vval.v_dict = NULL;
20145 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20146 {
20147 /* use the copy made earlier */
20148 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20149 ++to->vval.v_dict->dv_refcount;
20150 }
20151 else
20152 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20153 if (to->vval.v_dict == NULL)
20154 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020155 break;
20156 default:
20157 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020158 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020159 }
20160 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020161 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020162}
20163
20164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 * ":echo expr1 ..." print each argument separated with a space, add a
20166 * newline at the end.
20167 * ":echon expr1 ..." print each argument plain.
20168 */
20169 void
20170ex_echo(eap)
20171 exarg_T *eap;
20172{
20173 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020174 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020175 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 char_u *p;
20177 int needclr = TRUE;
20178 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020179 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180
20181 if (eap->skip)
20182 ++emsg_skip;
20183 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20184 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020185 /* If eval1() causes an error message the text from the command may
20186 * still need to be cleared. E.g., "echo 22,44". */
20187 need_clr_eos = needclr;
20188
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020190 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 {
20192 /*
20193 * Report the invalid expression unless the expression evaluation
20194 * has been cancelled due to an aborting error, an interrupt, or an
20195 * exception.
20196 */
20197 if (!aborting())
20198 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020199 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 break;
20201 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020202 need_clr_eos = FALSE;
20203
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 if (!eap->skip)
20205 {
20206 if (atstart)
20207 {
20208 atstart = FALSE;
20209 /* Call msg_start() after eval1(), evaluating the expression
20210 * may cause a message to appear. */
20211 if (eap->cmdidx == CMD_echo)
20212 msg_start();
20213 }
20214 else if (eap->cmdidx == CMD_echo)
20215 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020216 current_copyID += COPYID_INC;
20217 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020218 if (p != NULL)
20219 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020221 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020223 if (*p != TAB && needclr)
20224 {
20225 /* remove any text still there from the command */
20226 msg_clr_eos();
20227 needclr = FALSE;
20228 }
20229 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 }
20231 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020232 {
20233#ifdef FEAT_MBYTE
20234 if (has_mbyte)
20235 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020236 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020237
20238 (void)msg_outtrans_len_attr(p, i, echo_attr);
20239 p += i - 1;
20240 }
20241 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020243 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020246 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020248 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 arg = skipwhite(arg);
20250 }
20251 eap->nextcmd = check_nextcmd(arg);
20252
20253 if (eap->skip)
20254 --emsg_skip;
20255 else
20256 {
20257 /* remove text that may still be there from the command */
20258 if (needclr)
20259 msg_clr_eos();
20260 if (eap->cmdidx == CMD_echo)
20261 msg_end();
20262 }
20263}
20264
20265/*
20266 * ":echohl {name}".
20267 */
20268 void
20269ex_echohl(eap)
20270 exarg_T *eap;
20271{
20272 int id;
20273
20274 id = syn_name2id(eap->arg);
20275 if (id == 0)
20276 echo_attr = 0;
20277 else
20278 echo_attr = syn_id2attr(id);
20279}
20280
20281/*
20282 * ":execute expr1 ..." execute the result of an expression.
20283 * ":echomsg expr1 ..." Print a message
20284 * ":echoerr expr1 ..." Print an error
20285 * Each gets spaces around each argument and a newline at the end for
20286 * echo commands
20287 */
20288 void
20289ex_execute(eap)
20290 exarg_T *eap;
20291{
20292 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020293 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294 int ret = OK;
20295 char_u *p;
20296 garray_T ga;
20297 int len;
20298 int save_did_emsg;
20299
20300 ga_init2(&ga, 1, 80);
20301
20302 if (eap->skip)
20303 ++emsg_skip;
20304 while (*arg != NUL && *arg != '|' && *arg != '\n')
20305 {
20306 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020307 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 {
20309 /*
20310 * Report the invalid expression unless the expression evaluation
20311 * has been cancelled due to an aborting error, an interrupt, or an
20312 * exception.
20313 */
20314 if (!aborting())
20315 EMSG2(_(e_invexpr2), p);
20316 ret = FAIL;
20317 break;
20318 }
20319
20320 if (!eap->skip)
20321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020322 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 len = (int)STRLEN(p);
20324 if (ga_grow(&ga, len + 2) == FAIL)
20325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020326 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 ret = FAIL;
20328 break;
20329 }
20330 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 ga.ga_len += len;
20334 }
20335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020336 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337 arg = skipwhite(arg);
20338 }
20339
20340 if (ret != FAIL && ga.ga_data != NULL)
20341 {
20342 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020345 out_flush();
20346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 else if (eap->cmdidx == CMD_echoerr)
20348 {
20349 /* We don't want to abort following commands, restore did_emsg. */
20350 save_did_emsg = did_emsg;
20351 EMSG((char_u *)ga.ga_data);
20352 if (!force_abort)
20353 did_emsg = save_did_emsg;
20354 }
20355 else if (eap->cmdidx == CMD_execute)
20356 do_cmdline((char_u *)ga.ga_data,
20357 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20358 }
20359
20360 ga_clear(&ga);
20361
20362 if (eap->skip)
20363 --emsg_skip;
20364
20365 eap->nextcmd = check_nextcmd(arg);
20366}
20367
20368/*
20369 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20370 * "arg" points to the "&" or '+' when called, to "option" when returning.
20371 * Returns NULL when no option name found. Otherwise pointer to the char
20372 * after the option name.
20373 */
20374 static char_u *
20375find_option_end(arg, opt_flags)
20376 char_u **arg;
20377 int *opt_flags;
20378{
20379 char_u *p = *arg;
20380
20381 ++p;
20382 if (*p == 'g' && p[1] == ':')
20383 {
20384 *opt_flags = OPT_GLOBAL;
20385 p += 2;
20386 }
20387 else if (*p == 'l' && p[1] == ':')
20388 {
20389 *opt_flags = OPT_LOCAL;
20390 p += 2;
20391 }
20392 else
20393 *opt_flags = 0;
20394
20395 if (!ASCII_ISALPHA(*p))
20396 return NULL;
20397 *arg = p;
20398
20399 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20400 p += 4; /* termcap option */
20401 else
20402 while (ASCII_ISALPHA(*p))
20403 ++p;
20404 return p;
20405}
20406
20407/*
20408 * ":function"
20409 */
20410 void
20411ex_function(eap)
20412 exarg_T *eap;
20413{
20414 char_u *theline;
20415 int j;
20416 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 char_u *name = NULL;
20419 char_u *p;
20420 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020421 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 garray_T newargs;
20423 garray_T newlines;
20424 int varargs = FALSE;
20425 int mustend = FALSE;
20426 int flags = 0;
20427 ufunc_T *fp;
20428 int indent;
20429 int nesting;
20430 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020431 dictitem_T *v;
20432 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020433 static int func_nr = 0; /* number for nameless function */
20434 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020435 hashtab_T *ht;
20436 int todo;
20437 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020438 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439
20440 /*
20441 * ":function" without argument: list functions.
20442 */
20443 if (ends_excmd(*eap->arg))
20444 {
20445 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020446 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020447 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020448 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020449 {
20450 if (!HASHITEM_EMPTY(hi))
20451 {
20452 --todo;
20453 fp = HI2UF(hi);
20454 if (!isdigit(*fp->uf_name))
20455 list_func_head(fp, FALSE);
20456 }
20457 }
20458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459 eap->nextcmd = check_nextcmd(eap->arg);
20460 return;
20461 }
20462
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020463 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020464 * ":function /pat": list functions matching pattern.
20465 */
20466 if (*eap->arg == '/')
20467 {
20468 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20469 if (!eap->skip)
20470 {
20471 regmatch_T regmatch;
20472
20473 c = *p;
20474 *p = NUL;
20475 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20476 *p = c;
20477 if (regmatch.regprog != NULL)
20478 {
20479 regmatch.rm_ic = p_ic;
20480
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020481 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020482 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20483 {
20484 if (!HASHITEM_EMPTY(hi))
20485 {
20486 --todo;
20487 fp = HI2UF(hi);
20488 if (!isdigit(*fp->uf_name)
20489 && vim_regexec(&regmatch, fp->uf_name, 0))
20490 list_func_head(fp, FALSE);
20491 }
20492 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020493 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020494 }
20495 }
20496 if (*p == '/')
20497 ++p;
20498 eap->nextcmd = check_nextcmd(p);
20499 return;
20500 }
20501
20502 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020503 * Get the function name. There are these situations:
20504 * func normal function name
20505 * "name" == func, "fudi.fd_dict" == NULL
20506 * dict.func new dictionary entry
20507 * "name" == NULL, "fudi.fd_dict" set,
20508 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20509 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020510 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020511 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20512 * dict.func existing dict entry that's not a Funcref
20513 * "name" == NULL, "fudi.fd_dict" set,
20514 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020517 name = trans_function_name(&p, eap->skip, 0, &fudi);
20518 paren = (vim_strchr(p, '(') != NULL);
20519 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 {
20521 /*
20522 * Return on an invalid expression in braces, unless the expression
20523 * evaluation has been cancelled due to an aborting error, an
20524 * interrupt, or an exception.
20525 */
20526 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020527 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020528 if (!eap->skip && fudi.fd_newkey != NULL)
20529 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020530 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 else
20534 eap->skip = TRUE;
20535 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020536
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 /* An error in a function call during evaluation of an expression in magic
20538 * braces should not cause the function not to be defined. */
20539 saved_did_emsg = did_emsg;
20540 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541
20542 /*
20543 * ":function func" with only function name: list function.
20544 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020545 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 {
20547 if (!ends_excmd(*skipwhite(p)))
20548 {
20549 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020550 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 }
20552 eap->nextcmd = check_nextcmd(p);
20553 if (eap->nextcmd != NULL)
20554 *p = NUL;
20555 if (!eap->skip && !got_int)
20556 {
20557 fp = find_func(name);
20558 if (fp != NULL)
20559 {
20560 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020561 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020563 if (FUNCLINE(fp, j) == NULL)
20564 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 msg_putchar('\n');
20566 msg_outnum((long)(j + 1));
20567 if (j < 9)
20568 msg_putchar(' ');
20569 if (j < 99)
20570 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020571 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572 out_flush(); /* show a line at a time */
20573 ui_breakcheck();
20574 }
20575 if (!got_int)
20576 {
20577 msg_putchar('\n');
20578 msg_puts((char_u *)" endfunction");
20579 }
20580 }
20581 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020582 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020584 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 }
20586
20587 /*
20588 * ":function name(arg1, arg2)" Define function.
20589 */
20590 p = skipwhite(p);
20591 if (*p != '(')
20592 {
20593 if (!eap->skip)
20594 {
20595 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020596 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597 }
20598 /* attempt to continue by skipping some text */
20599 if (vim_strchr(p, '(') != NULL)
20600 p = vim_strchr(p, '(');
20601 }
20602 p = skipwhite(p + 1);
20603
20604 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20605 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20606
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020607 if (!eap->skip)
20608 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020609 /* Check the name of the function. Unless it's a dictionary function
20610 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020611 if (name != NULL)
20612 arg = name;
20613 else
20614 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020615 if (arg != NULL && (fudi.fd_di == NULL
20616 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020617 {
20618 if (*arg == K_SPECIAL)
20619 j = 3;
20620 else
20621 j = 0;
20622 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20623 : eval_isnamec(arg[j])))
20624 ++j;
20625 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020626 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020627 }
20628 }
20629
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 /*
20631 * Isolate the arguments: "arg1, arg2, ...)"
20632 */
20633 while (*p != ')')
20634 {
20635 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20636 {
20637 varargs = TRUE;
20638 p += 3;
20639 mustend = TRUE;
20640 }
20641 else
20642 {
20643 arg = p;
20644 while (ASCII_ISALNUM(*p) || *p == '_')
20645 ++p;
20646 if (arg == p || isdigit(*arg)
20647 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20648 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20649 {
20650 if (!eap->skip)
20651 EMSG2(_("E125: Illegal argument: %s"), arg);
20652 break;
20653 }
20654 if (ga_grow(&newargs, 1) == FAIL)
20655 goto erret;
20656 c = *p;
20657 *p = NUL;
20658 arg = vim_strsave(arg);
20659 if (arg == NULL)
20660 goto erret;
20661 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20662 *p = c;
20663 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 if (*p == ',')
20665 ++p;
20666 else
20667 mustend = TRUE;
20668 }
20669 p = skipwhite(p);
20670 if (mustend && *p != ')')
20671 {
20672 if (!eap->skip)
20673 EMSG2(_(e_invarg2), eap->arg);
20674 break;
20675 }
20676 }
20677 ++p; /* skip the ')' */
20678
Bram Moolenaare9a41262005-01-15 22:18:47 +000020679 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680 for (;;)
20681 {
20682 p = skipwhite(p);
20683 if (STRNCMP(p, "range", 5) == 0)
20684 {
20685 flags |= FC_RANGE;
20686 p += 5;
20687 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020688 else if (STRNCMP(p, "dict", 4) == 0)
20689 {
20690 flags |= FC_DICT;
20691 p += 4;
20692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693 else if (STRNCMP(p, "abort", 5) == 0)
20694 {
20695 flags |= FC_ABORT;
20696 p += 5;
20697 }
20698 else
20699 break;
20700 }
20701
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020702 /* When there is a line break use what follows for the function body.
20703 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20704 if (*p == '\n')
20705 line_arg = p + 1;
20706 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707 EMSG(_(e_trailing));
20708
20709 /*
20710 * Read the body of the function, until ":endfunction" is found.
20711 */
20712 if (KeyTyped)
20713 {
20714 /* Check if the function already exists, don't let the user type the
20715 * whole function before telling him it doesn't work! For a script we
20716 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020717 if (!eap->skip && !eap->forceit)
20718 {
20719 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20720 EMSG(_(e_funcdict));
20721 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020722 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020725 if (!eap->skip && did_emsg)
20726 goto erret;
20727
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 msg_putchar('\n'); /* don't overwrite the function name */
20729 cmdline_row = msg_row;
20730 }
20731
20732 indent = 2;
20733 nesting = 0;
20734 for (;;)
20735 {
20736 msg_scroll = TRUE;
20737 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020738 sourcing_lnum_off = sourcing_lnum;
20739
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020740 if (line_arg != NULL)
20741 {
20742 /* Use eap->arg, split up in parts by line breaks. */
20743 theline = line_arg;
20744 p = vim_strchr(theline, '\n');
20745 if (p == NULL)
20746 line_arg += STRLEN(line_arg);
20747 else
20748 {
20749 *p = NUL;
20750 line_arg = p + 1;
20751 }
20752 }
20753 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 theline = getcmdline(':', 0L, indent);
20755 else
20756 theline = eap->getline(':', eap->cookie, indent);
20757 if (KeyTyped)
20758 lines_left = Rows - 1;
20759 if (theline == NULL)
20760 {
20761 EMSG(_("E126: Missing :endfunction"));
20762 goto erret;
20763 }
20764
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020765 /* Detect line continuation: sourcing_lnum increased more than one. */
20766 if (sourcing_lnum > sourcing_lnum_off + 1)
20767 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20768 else
20769 sourcing_lnum_off = 0;
20770
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 if (skip_until != NULL)
20772 {
20773 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20774 * don't check for ":endfunc". */
20775 if (STRCMP(theline, skip_until) == 0)
20776 {
20777 vim_free(skip_until);
20778 skip_until = NULL;
20779 }
20780 }
20781 else
20782 {
20783 /* skip ':' and blanks*/
20784 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20785 ;
20786
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020787 /* Check for "endfunction". */
20788 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020790 if (line_arg == NULL)
20791 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 break;
20793 }
20794
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020795 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 * at "end". */
20797 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20798 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020799 else if (STRNCMP(p, "if", 2) == 0
20800 || STRNCMP(p, "wh", 2) == 0
20801 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802 || STRNCMP(p, "try", 3) == 0)
20803 indent += 2;
20804
20805 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020806 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020808 if (*p == '!')
20809 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 p += eval_fname_script(p);
20811 if (ASCII_ISALPHA(*p))
20812 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020813 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814 if (*skipwhite(p) == '(')
20815 {
20816 ++nesting;
20817 indent += 2;
20818 }
20819 }
20820 }
20821
20822 /* Check for ":append" or ":insert". */
20823 p = skip_range(p, NULL);
20824 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20825 || (p[0] == 'i'
20826 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20827 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20828 skip_until = vim_strsave((char_u *)".");
20829
20830 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20831 arg = skipwhite(skiptowhite(p));
20832 if (arg[0] == '<' && arg[1] =='<'
20833 && ((p[0] == 'p' && p[1] == 'y'
20834 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20835 || (p[0] == 'p' && p[1] == 'e'
20836 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20837 || (p[0] == 't' && p[1] == 'c'
20838 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20839 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20840 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020841 || (p[0] == 'm' && p[1] == 'z'
20842 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843 ))
20844 {
20845 /* ":python <<" continues until a dot, like ":append" */
20846 p = skipwhite(arg + 2);
20847 if (*p == NUL)
20848 skip_until = vim_strsave((char_u *)".");
20849 else
20850 skip_until = vim_strsave(p);
20851 }
20852 }
20853
20854 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020855 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020856 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020857 if (line_arg == NULL)
20858 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020860 }
20861
20862 /* Copy the line to newly allocated memory. get_one_sourceline()
20863 * allocates 250 bytes per line, this saves 80% on average. The cost
20864 * is an extra alloc/free. */
20865 p = vim_strsave(theline);
20866 if (p != NULL)
20867 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020868 if (line_arg == NULL)
20869 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020870 theline = p;
20871 }
20872
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020873 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20874
20875 /* Add NULL lines for continuation lines, so that the line count is
20876 * equal to the index in the growarray. */
20877 while (sourcing_lnum_off-- > 0)
20878 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020879
20880 /* Check for end of eap->arg. */
20881 if (line_arg != NULL && *line_arg == NUL)
20882 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 }
20884
20885 /* Don't define the function when skipping commands or when an error was
20886 * detected. */
20887 if (eap->skip || did_emsg)
20888 goto erret;
20889
20890 /*
20891 * If there are no errors, add the function
20892 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020893 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020894 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020895 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020896 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020897 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020898 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020899 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020900 goto erret;
20901 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020902
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020903 fp = find_func(name);
20904 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020906 if (!eap->forceit)
20907 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020908 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020909 goto erret;
20910 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020911 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020912 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020913 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020914 name);
20915 goto erret;
20916 }
20917 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020918 ga_clear_strings(&(fp->uf_args));
20919 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020920 vim_free(name);
20921 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923 }
20924 else
20925 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020926 char numbuf[20];
20927
20928 fp = NULL;
20929 if (fudi.fd_newkey == NULL && !eap->forceit)
20930 {
20931 EMSG(_(e_funcdict));
20932 goto erret;
20933 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020934 if (fudi.fd_di == NULL)
20935 {
20936 /* Can't add a function to a locked dictionary */
20937 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20938 goto erret;
20939 }
20940 /* Can't change an existing function if it is locked */
20941 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20942 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020943
20944 /* Give the function a sequential number. Can only be used with a
20945 * Funcref! */
20946 vim_free(name);
20947 sprintf(numbuf, "%d", ++func_nr);
20948 name = vim_strsave((char_u *)numbuf);
20949 if (name == NULL)
20950 goto erret;
20951 }
20952
20953 if (fp == NULL)
20954 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020955 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020956 {
20957 int slen, plen;
20958 char_u *scriptname;
20959
20960 /* Check that the autoload name matches the script name. */
20961 j = FAIL;
20962 if (sourcing_name != NULL)
20963 {
20964 scriptname = autoload_name(name);
20965 if (scriptname != NULL)
20966 {
20967 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020968 plen = (int)STRLEN(p);
20969 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020970 if (slen > plen && fnamecmp(p,
20971 sourcing_name + slen - plen) == 0)
20972 j = OK;
20973 vim_free(scriptname);
20974 }
20975 }
20976 if (j == FAIL)
20977 {
20978 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20979 goto erret;
20980 }
20981 }
20982
20983 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 if (fp == NULL)
20985 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020986
20987 if (fudi.fd_dict != NULL)
20988 {
20989 if (fudi.fd_di == NULL)
20990 {
20991 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020992 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020993 if (fudi.fd_di == NULL)
20994 {
20995 vim_free(fp);
20996 goto erret;
20997 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020998 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20999 {
21000 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021001 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021002 goto erret;
21003 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021004 }
21005 else
21006 /* overwrite existing dict entry */
21007 clear_tv(&fudi.fd_di->di_tv);
21008 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021009 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021010 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021011 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021012
21013 /* behave like "dict" was used */
21014 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021015 }
21016
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021018 STRCPY(fp->uf_name, name);
21019 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021021 fp->uf_args = newargs;
21022 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021023#ifdef FEAT_PROFILE
21024 fp->uf_tml_count = NULL;
21025 fp->uf_tml_total = NULL;
21026 fp->uf_tml_self = NULL;
21027 fp->uf_profiling = FALSE;
21028 if (prof_def_func())
21029 func_do_profile(fp);
21030#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021031 fp->uf_varargs = varargs;
21032 fp->uf_flags = flags;
21033 fp->uf_calls = 0;
21034 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021035 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036
21037erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038 ga_clear_strings(&newargs);
21039 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021040ret_free:
21041 vim_free(skip_until);
21042 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045}
21046
21047/*
21048 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021049 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021051 * flags:
21052 * TFN_INT: internal function name OK
21053 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054 * Advances "pp" to just after the function name (if no error).
21055 */
21056 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021057trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058 char_u **pp;
21059 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021060 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021061 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021063 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021064 char_u *start;
21065 char_u *end;
21066 int lead;
21067 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021069 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021070
21071 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021072 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021074
21075 /* Check for hard coded <SNR>: already translated function ID (from a user
21076 * command). */
21077 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21078 && (*pp)[2] == (int)KE_SNR)
21079 {
21080 *pp += 3;
21081 len = get_id_len(pp) + 3;
21082 return vim_strnsave(start, len);
21083 }
21084
21085 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21086 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021088 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021090
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021091 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21092 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021093 if (end == start)
21094 {
21095 if (!skip)
21096 EMSG(_("E129: Function name required"));
21097 goto theend;
21098 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021099 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021100 {
21101 /*
21102 * Report an invalid expression in braces, unless the expression
21103 * evaluation has been cancelled due to an aborting error, an
21104 * interrupt, or an exception.
21105 */
21106 if (!aborting())
21107 {
21108 if (end != NULL)
21109 EMSG2(_(e_invarg2), start);
21110 }
21111 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021112 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021113 goto theend;
21114 }
21115
21116 if (lv.ll_tv != NULL)
21117 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021118 if (fdp != NULL)
21119 {
21120 fdp->fd_dict = lv.ll_dict;
21121 fdp->fd_newkey = lv.ll_newkey;
21122 lv.ll_newkey = NULL;
21123 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021124 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021125 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21126 {
21127 name = vim_strsave(lv.ll_tv->vval.v_string);
21128 *pp = end;
21129 }
21130 else
21131 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021132 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21133 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021134 EMSG(_(e_funcref));
21135 else
21136 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021137 name = NULL;
21138 }
21139 goto theend;
21140 }
21141
21142 if (lv.ll_name == NULL)
21143 {
21144 /* Error found, but continue after the function name. */
21145 *pp = end;
21146 goto theend;
21147 }
21148
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021149 /* Check if the name is a Funcref. If so, use the value. */
21150 if (lv.ll_exp_name != NULL)
21151 {
21152 len = (int)STRLEN(lv.ll_exp_name);
21153 name = deref_func_name(lv.ll_exp_name, &len);
21154 if (name == lv.ll_exp_name)
21155 name = NULL;
21156 }
21157 else
21158 {
21159 len = (int)(end - *pp);
21160 name = deref_func_name(*pp, &len);
21161 if (name == *pp)
21162 name = NULL;
21163 }
21164 if (name != NULL)
21165 {
21166 name = vim_strsave(name);
21167 *pp = end;
21168 goto theend;
21169 }
21170
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021171 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021172 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021173 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021174 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21175 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21176 {
21177 /* When there was "s:" already or the name expanded to get a
21178 * leading "s:" then remove it. */
21179 lv.ll_name += 2;
21180 len -= 2;
21181 lead = 2;
21182 }
21183 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021184 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021185 {
21186 if (lead == 2) /* skip over "s:" */
21187 lv.ll_name += 2;
21188 len = (int)(end - lv.ll_name);
21189 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021190
21191 /*
21192 * Copy the function name to allocated memory.
21193 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21194 * Accept <SNR>123_name() outside a script.
21195 */
21196 if (skip)
21197 lead = 0; /* do nothing */
21198 else if (lead > 0)
21199 {
21200 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021201 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21202 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021203 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021204 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021205 if (current_SID <= 0)
21206 {
21207 EMSG(_(e_usingsid));
21208 goto theend;
21209 }
21210 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21211 lead += (int)STRLEN(sid_buf);
21212 }
21213 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021214 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021215 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021216 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021217 goto theend;
21218 }
21219 name = alloc((unsigned)(len + lead + 1));
21220 if (name != NULL)
21221 {
21222 if (lead > 0)
21223 {
21224 name[0] = K_SPECIAL;
21225 name[1] = KS_EXTRA;
21226 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021227 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021228 STRCPY(name + 3, sid_buf);
21229 }
21230 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21231 name[len + lead] = NUL;
21232 }
21233 *pp = end;
21234
21235theend:
21236 clear_lval(&lv);
21237 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238}
21239
21240/*
21241 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21242 * Return 2 if "p" starts with "s:".
21243 * Return 0 otherwise.
21244 */
21245 static int
21246eval_fname_script(p)
21247 char_u *p;
21248{
21249 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21250 || STRNICMP(p + 1, "SNR>", 4) == 0))
21251 return 5;
21252 if (p[0] == 's' && p[1] == ':')
21253 return 2;
21254 return 0;
21255}
21256
21257/*
21258 * Return TRUE if "p" starts with "<SID>" or "s:".
21259 * Only works if eval_fname_script() returned non-zero for "p"!
21260 */
21261 static int
21262eval_fname_sid(p)
21263 char_u *p;
21264{
21265 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21266}
21267
21268/*
21269 * List the head of the function: "name(arg1, arg2)".
21270 */
21271 static void
21272list_func_head(fp, indent)
21273 ufunc_T *fp;
21274 int indent;
21275{
21276 int j;
21277
21278 msg_start();
21279 if (indent)
21280 MSG_PUTS(" ");
21281 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021282 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 {
21284 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021285 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286 }
21287 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021288 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021290 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 {
21292 if (j)
21293 MSG_PUTS(", ");
21294 msg_puts(FUNCARG(fp, j));
21295 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021296 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 {
21298 if (j)
21299 MSG_PUTS(", ");
21300 MSG_PUTS("...");
21301 }
21302 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021303 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021304 if (p_verbose > 0)
21305 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306}
21307
21308/*
21309 * Find a function by name, return pointer to it in ufuncs.
21310 * Return NULL for unknown function.
21311 */
21312 static ufunc_T *
21313find_func(name)
21314 char_u *name;
21315{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021316 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021318 hi = hash_find(&func_hashtab, name);
21319 if (!HASHITEM_EMPTY(hi))
21320 return HI2UF(hi);
21321 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322}
21323
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021324#if defined(EXITFREE) || defined(PROTO)
21325 void
21326free_all_functions()
21327{
21328 hashitem_T *hi;
21329
21330 /* Need to start all over every time, because func_free() may change the
21331 * hash table. */
21332 while (func_hashtab.ht_used > 0)
21333 for (hi = func_hashtab.ht_array; ; ++hi)
21334 if (!HASHITEM_EMPTY(hi))
21335 {
21336 func_free(HI2UF(hi));
21337 break;
21338 }
21339}
21340#endif
21341
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021342/*
21343 * Return TRUE if a function "name" exists.
21344 */
21345 static int
21346function_exists(name)
21347 char_u *name;
21348{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021349 char_u *nm = name;
21350 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021351 int n = FALSE;
21352
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021353 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021354 nm = skipwhite(nm);
21355
21356 /* Only accept "funcname", "funcname ", "funcname (..." and
21357 * "funcname(...", not "funcname!...". */
21358 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021359 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021360 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021361 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021362 else
21363 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021364 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021365 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021366 return n;
21367}
21368
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021369/*
21370 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021371 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021372 */
21373 static int
21374builtin_function(name)
21375 char_u *name;
21376{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021377 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21378 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021379}
21380
Bram Moolenaar05159a02005-02-26 23:04:13 +000021381#if defined(FEAT_PROFILE) || defined(PROTO)
21382/*
21383 * Start profiling function "fp".
21384 */
21385 static void
21386func_do_profile(fp)
21387 ufunc_T *fp;
21388{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021389 int len = fp->uf_lines.ga_len;
21390
21391 if (len == 0)
21392 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021393 fp->uf_tm_count = 0;
21394 profile_zero(&fp->uf_tm_self);
21395 profile_zero(&fp->uf_tm_total);
21396 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021397 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021398 if (fp->uf_tml_total == NULL)
21399 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021400 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021401 if (fp->uf_tml_self == NULL)
21402 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021403 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021404 fp->uf_tml_idx = -1;
21405 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21406 || fp->uf_tml_self == NULL)
21407 return; /* out of memory */
21408
21409 fp->uf_profiling = TRUE;
21410}
21411
21412/*
21413 * Dump the profiling results for all functions in file "fd".
21414 */
21415 void
21416func_dump_profile(fd)
21417 FILE *fd;
21418{
21419 hashitem_T *hi;
21420 int todo;
21421 ufunc_T *fp;
21422 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021423 ufunc_T **sorttab;
21424 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021425
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021426 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021427 if (todo == 0)
21428 return; /* nothing to dump */
21429
Bram Moolenaar73830342005-02-28 22:48:19 +000021430 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21431
Bram Moolenaar05159a02005-02-26 23:04:13 +000021432 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21433 {
21434 if (!HASHITEM_EMPTY(hi))
21435 {
21436 --todo;
21437 fp = HI2UF(hi);
21438 if (fp->uf_profiling)
21439 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021440 if (sorttab != NULL)
21441 sorttab[st_len++] = fp;
21442
Bram Moolenaar05159a02005-02-26 23:04:13 +000021443 if (fp->uf_name[0] == K_SPECIAL)
21444 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21445 else
21446 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21447 if (fp->uf_tm_count == 1)
21448 fprintf(fd, "Called 1 time\n");
21449 else
21450 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21451 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21452 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21453 fprintf(fd, "\n");
21454 fprintf(fd, "count total (s) self (s)\n");
21455
21456 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21457 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021458 if (FUNCLINE(fp, i) == NULL)
21459 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021460 prof_func_line(fd, fp->uf_tml_count[i],
21461 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021462 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21463 }
21464 fprintf(fd, "\n");
21465 }
21466 }
21467 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021468
21469 if (sorttab != NULL && st_len > 0)
21470 {
21471 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21472 prof_total_cmp);
21473 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21474 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21475 prof_self_cmp);
21476 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21477 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021478
21479 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021480}
Bram Moolenaar73830342005-02-28 22:48:19 +000021481
21482 static void
21483prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21484 FILE *fd;
21485 ufunc_T **sorttab;
21486 int st_len;
21487 char *title;
21488 int prefer_self; /* when equal print only self time */
21489{
21490 int i;
21491 ufunc_T *fp;
21492
21493 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21494 fprintf(fd, "count total (s) self (s) function\n");
21495 for (i = 0; i < 20 && i < st_len; ++i)
21496 {
21497 fp = sorttab[i];
21498 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21499 prefer_self);
21500 if (fp->uf_name[0] == K_SPECIAL)
21501 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21502 else
21503 fprintf(fd, " %s()\n", fp->uf_name);
21504 }
21505 fprintf(fd, "\n");
21506}
21507
21508/*
21509 * Print the count and times for one function or function line.
21510 */
21511 static void
21512prof_func_line(fd, count, total, self, prefer_self)
21513 FILE *fd;
21514 int count;
21515 proftime_T *total;
21516 proftime_T *self;
21517 int prefer_self; /* when equal print only self time */
21518{
21519 if (count > 0)
21520 {
21521 fprintf(fd, "%5d ", count);
21522 if (prefer_self && profile_equal(total, self))
21523 fprintf(fd, " ");
21524 else
21525 fprintf(fd, "%s ", profile_msg(total));
21526 if (!prefer_self && profile_equal(total, self))
21527 fprintf(fd, " ");
21528 else
21529 fprintf(fd, "%s ", profile_msg(self));
21530 }
21531 else
21532 fprintf(fd, " ");
21533}
21534
21535/*
21536 * Compare function for total time sorting.
21537 */
21538 static int
21539#ifdef __BORLANDC__
21540_RTLENTRYF
21541#endif
21542prof_total_cmp(s1, s2)
21543 const void *s1;
21544 const void *s2;
21545{
21546 ufunc_T *p1, *p2;
21547
21548 p1 = *(ufunc_T **)s1;
21549 p2 = *(ufunc_T **)s2;
21550 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21551}
21552
21553/*
21554 * Compare function for self time sorting.
21555 */
21556 static int
21557#ifdef __BORLANDC__
21558_RTLENTRYF
21559#endif
21560prof_self_cmp(s1, s2)
21561 const void *s1;
21562 const void *s2;
21563{
21564 ufunc_T *p1, *p2;
21565
21566 p1 = *(ufunc_T **)s1;
21567 p2 = *(ufunc_T **)s2;
21568 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21569}
21570
Bram Moolenaar05159a02005-02-26 23:04:13 +000021571#endif
21572
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021573/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021574 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021575 * Return TRUE if a package was loaded.
21576 */
21577 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021578script_autoload(name, reload)
21579 char_u *name;
21580 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021581{
21582 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021583 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021584 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021585 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021586
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021587 /* Return quickly when autoload disabled. */
21588 if (no_autoload)
21589 return FALSE;
21590
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021591 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021592 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021593 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021594 return FALSE;
21595
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021596 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021597
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021598 /* Find the name in the list of previously loaded package names. Skip
21599 * "autoload/", it's always the same. */
21600 for (i = 0; i < ga_loaded.ga_len; ++i)
21601 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21602 break;
21603 if (!reload && i < ga_loaded.ga_len)
21604 ret = FALSE; /* was loaded already */
21605 else
21606 {
21607 /* Remember the name if it wasn't loaded already. */
21608 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21609 {
21610 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21611 tofree = NULL;
21612 }
21613
21614 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021615 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021616 ret = TRUE;
21617 }
21618
21619 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021620 return ret;
21621}
21622
21623/*
21624 * Return the autoload script name for a function or variable name.
21625 * Returns NULL when out of memory.
21626 */
21627 static char_u *
21628autoload_name(name)
21629 char_u *name;
21630{
21631 char_u *p;
21632 char_u *scriptname;
21633
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021634 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021635 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21636 if (scriptname == NULL)
21637 return FALSE;
21638 STRCPY(scriptname, "autoload/");
21639 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021640 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021641 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021642 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021643 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021644 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021645}
21646
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21648
21649/*
21650 * Function given to ExpandGeneric() to obtain the list of user defined
21651 * function names.
21652 */
21653 char_u *
21654get_user_func_name(xp, idx)
21655 expand_T *xp;
21656 int idx;
21657{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021658 static long_u done;
21659 static hashitem_T *hi;
21660 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661
21662 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021664 done = 0;
21665 hi = func_hashtab.ht_array;
21666 }
21667 if (done < func_hashtab.ht_used)
21668 {
21669 if (done++ > 0)
21670 ++hi;
21671 while (HASHITEM_EMPTY(hi))
21672 ++hi;
21673 fp = HI2UF(hi);
21674
21675 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21676 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677
21678 cat_func_name(IObuff, fp);
21679 if (xp->xp_context != EXPAND_USER_FUNC)
21680 {
21681 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021682 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683 STRCAT(IObuff, ")");
21684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 return IObuff;
21686 }
21687 return NULL;
21688}
21689
21690#endif /* FEAT_CMDL_COMPL */
21691
21692/*
21693 * Copy the function name of "fp" to buffer "buf".
21694 * "buf" must be able to hold the function name plus three bytes.
21695 * Takes care of script-local function names.
21696 */
21697 static void
21698cat_func_name(buf, fp)
21699 char_u *buf;
21700 ufunc_T *fp;
21701{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021702 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 {
21704 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021705 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 }
21707 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021708 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709}
21710
21711/*
21712 * ":delfunction {name}"
21713 */
21714 void
21715ex_delfunction(eap)
21716 exarg_T *eap;
21717{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021718 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 char_u *p;
21720 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021721 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722
21723 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021724 name = trans_function_name(&p, eap->skip, 0, &fudi);
21725 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021727 {
21728 if (fudi.fd_dict != NULL && !eap->skip)
21729 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 if (!ends_excmd(*skipwhite(p)))
21733 {
21734 vim_free(name);
21735 EMSG(_(e_trailing));
21736 return;
21737 }
21738 eap->nextcmd = check_nextcmd(p);
21739 if (eap->nextcmd != NULL)
21740 *p = NUL;
21741
21742 if (!eap->skip)
21743 fp = find_func(name);
21744 vim_free(name);
21745
21746 if (!eap->skip)
21747 {
21748 if (fp == NULL)
21749 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021750 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751 return;
21752 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021753 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 {
21755 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21756 return;
21757 }
21758
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021759 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021761 /* Delete the dict item that refers to the function, it will
21762 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021763 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021765 else
21766 func_free(fp);
21767 }
21768}
21769
21770/*
21771 * Free a function and remove it from the list of functions.
21772 */
21773 static void
21774func_free(fp)
21775 ufunc_T *fp;
21776{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021777 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021778
21779 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021780 ga_clear_strings(&(fp->uf_args));
21781 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021782#ifdef FEAT_PROFILE
21783 vim_free(fp->uf_tml_count);
21784 vim_free(fp->uf_tml_total);
21785 vim_free(fp->uf_tml_self);
21786#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021787
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021788 /* remove the function from the function hashtable */
21789 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21790 if (HASHITEM_EMPTY(hi))
21791 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021792 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021793 hash_remove(&func_hashtab, hi);
21794
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021795 vim_free(fp);
21796}
21797
21798/*
21799 * Unreference a Function: decrement the reference count and free it when it
21800 * becomes zero. Only for numbered functions.
21801 */
21802 static void
21803func_unref(name)
21804 char_u *name;
21805{
21806 ufunc_T *fp;
21807
21808 if (name != NULL && isdigit(*name))
21809 {
21810 fp = find_func(name);
21811 if (fp == NULL)
21812 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021813 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021814 {
21815 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021816 * when "uf_calls" becomes zero. */
21817 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021818 func_free(fp);
21819 }
21820 }
21821}
21822
21823/*
21824 * Count a reference to a Function.
21825 */
21826 static void
21827func_ref(name)
21828 char_u *name;
21829{
21830 ufunc_T *fp;
21831
21832 if (name != NULL && isdigit(*name))
21833 {
21834 fp = find_func(name);
21835 if (fp == NULL)
21836 EMSG2(_(e_intern2), "func_ref()");
21837 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021838 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 }
21840}
21841
21842/*
21843 * Call a user function.
21844 */
21845 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021846call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 ufunc_T *fp; /* pointer to function */
21848 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021849 typval_T *argvars; /* arguments */
21850 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 linenr_T firstline; /* first line of range */
21852 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021853 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854{
Bram Moolenaar33570922005-01-25 22:26:29 +000021855 char_u *save_sourcing_name;
21856 linenr_T save_sourcing_lnum;
21857 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021858 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021859 int save_did_emsg;
21860 static int depth = 0;
21861 dictitem_T *v;
21862 int fixvar_idx = 0; /* index in fixvar[] */
21863 int i;
21864 int ai;
21865 char_u numbuf[NUMBUFLEN];
21866 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021867#ifdef FEAT_PROFILE
21868 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021869 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871
21872 /* If depth of calling is getting too high, don't execute the function */
21873 if (depth >= p_mfd)
21874 {
21875 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021876 rettv->v_type = VAR_NUMBER;
21877 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 return;
21879 }
21880 ++depth;
21881
21882 line_breakcheck(); /* check for CTRL-C hit */
21883
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021884 fc = (funccall_T *)alloc(sizeof(funccall_T));
21885 fc->caller = current_funccal;
21886 current_funccal = fc;
21887 fc->func = fp;
21888 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021889 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021890 fc->linenr = 0;
21891 fc->returned = FALSE;
21892 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021894 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21895 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896
Bram Moolenaar33570922005-01-25 22:26:29 +000021897 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021898 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021899 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21900 * each argument variable and saves a lot of time.
21901 */
21902 /*
21903 * Init l: variables.
21904 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021905 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021906 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021907 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021908 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21909 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021910 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021911 name = v->di_key;
21912 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021913 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021914 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021915 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021916 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021917 v->di_tv.vval.v_dict = selfdict;
21918 ++selfdict->dv_refcount;
21919 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021920
Bram Moolenaar33570922005-01-25 22:26:29 +000021921 /*
21922 * Init a: variables.
21923 * Set a:0 to "argcount".
21924 * Set a:000 to a list with room for the "..." arguments.
21925 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021926 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21927 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021928 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021929 /* Use "name" to avoid a warning from some compiler that checks the
21930 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021931 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021932 name = v->di_key;
21933 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021934 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021935 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021936 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021937 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021938 v->di_tv.vval.v_list = &fc->l_varlist;
21939 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21940 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21941 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021942
21943 /*
21944 * Set a:firstline to "firstline" and a:lastline to "lastline".
21945 * Set a:name to named arguments.
21946 * Set a:N to the "..." arguments.
21947 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021948 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021949 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021950 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021951 (varnumber_T)lastline);
21952 for (i = 0; i < argcount; ++i)
21953 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021954 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021955 if (ai < 0)
21956 /* named argument a:name */
21957 name = FUNCARG(fp, i);
21958 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021959 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021960 /* "..." argument a:1, a:2, etc. */
21961 sprintf((char *)numbuf, "%d", ai + 1);
21962 name = numbuf;
21963 }
21964 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21965 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021966 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021967 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21968 }
21969 else
21970 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021971 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21972 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021973 if (v == NULL)
21974 break;
21975 v->di_flags = DI_FLAGS_RO;
21976 }
21977 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021978 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021979
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021980 /* Note: the values are copied directly to avoid alloc/free.
21981 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021982 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021983 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021984
21985 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21986 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021987 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21988 fc->l_listitems[ai].li_tv = argvars[i];
21989 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021990 }
21991 }
21992
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 /* Don't redraw while executing the function. */
21994 ++RedrawingDisabled;
21995 save_sourcing_name = sourcing_name;
21996 save_sourcing_lnum = sourcing_lnum;
21997 sourcing_lnum = 1;
21998 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021999 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 if (sourcing_name != NULL)
22001 {
22002 if (save_sourcing_name != NULL
22003 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22004 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22005 else
22006 STRCPY(sourcing_name, "function ");
22007 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22008
22009 if (p_verbose >= 12)
22010 {
22011 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022012 verbose_enter_scroll();
22013
Bram Moolenaar555b2802005-05-19 21:08:39 +000022014 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015 if (p_verbose >= 14)
22016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022018 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022019 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022020 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021
22022 msg_puts((char_u *)"(");
22023 for (i = 0; i < argcount; ++i)
22024 {
22025 if (i > 0)
22026 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022027 if (argvars[i].v_type == VAR_NUMBER)
22028 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 else
22030 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022031 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22032 if (s != NULL)
22033 {
22034 trunc_string(s, buf, MSG_BUF_CLEN);
22035 msg_puts(buf);
22036 vim_free(tofree);
22037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 }
22039 }
22040 msg_puts((char_u *)")");
22041 }
22042 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022043
22044 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 --no_wait_return;
22046 }
22047 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022048#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022049 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022050 {
22051 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22052 func_do_profile(fp);
22053 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022054 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022055 {
22056 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022057 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022058 profile_zero(&fp->uf_tm_children);
22059 }
22060 script_prof_save(&wait_start);
22061 }
22062#endif
22063
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022065 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 save_did_emsg = did_emsg;
22067 did_emsg = FALSE;
22068
22069 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022070 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22072
22073 --RedrawingDisabled;
22074
22075 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022076 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022078 clear_tv(rettv);
22079 rettv->v_type = VAR_NUMBER;
22080 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081 }
22082
Bram Moolenaar05159a02005-02-26 23:04:13 +000022083#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022084 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022085 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022086 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022087 profile_end(&call_start);
22088 profile_sub_wait(&wait_start, &call_start);
22089 profile_add(&fp->uf_tm_total, &call_start);
22090 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022091 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022092 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022093 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22094 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022095 }
22096 }
22097#endif
22098
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099 /* when being verbose, mention the return value */
22100 if (p_verbose >= 12)
22101 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022103 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022106 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022107 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022108 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022109 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022110 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022112 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022113 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022114 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022115 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022116
Bram Moolenaar555b2802005-05-19 21:08:39 +000022117 /* The value may be very long. Skip the middle part, so that we
22118 * have some idea how it starts and ends. smsg() would always
22119 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022120 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022121 if (s != NULL)
22122 {
22123 trunc_string(s, buf, MSG_BUF_CLEN);
22124 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22125 vim_free(tofree);
22126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 }
22128 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022129
22130 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 --no_wait_return;
22132 }
22133
22134 vim_free(sourcing_name);
22135 sourcing_name = save_sourcing_name;
22136 sourcing_lnum = save_sourcing_lnum;
22137 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022138#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022139 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022140 script_prof_restore(&wait_start);
22141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142
22143 if (p_verbose >= 12 && sourcing_name != NULL)
22144 {
22145 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022146 verbose_enter_scroll();
22147
Bram Moolenaar555b2802005-05-19 21:08:39 +000022148 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022150
22151 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 --no_wait_return;
22153 }
22154
22155 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022156 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022158
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022159 /* If the a:000 list and the l: and a: dicts are not referenced we can
22160 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022161 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22162 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22163 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22164 {
22165 free_funccal(fc, FALSE);
22166 }
22167 else
22168 {
22169 hashitem_T *hi;
22170 listitem_T *li;
22171 int todo;
22172
22173 /* "fc" is still in use. This can happen when returning "a:000" or
22174 * assigning "l:" to a global variable.
22175 * Link "fc" in the list for garbage collection later. */
22176 fc->caller = previous_funccal;
22177 previous_funccal = fc;
22178
22179 /* Make a copy of the a: variables, since we didn't do that above. */
22180 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22181 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22182 {
22183 if (!HASHITEM_EMPTY(hi))
22184 {
22185 --todo;
22186 v = HI2DI(hi);
22187 copy_tv(&v->di_tv, &v->di_tv);
22188 }
22189 }
22190
22191 /* Make a copy of the a:000 items, since we didn't do that above. */
22192 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22193 copy_tv(&li->li_tv, &li->li_tv);
22194 }
22195}
22196
22197/*
22198 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022199 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022200 */
22201 static int
22202can_free_funccal(fc, copyID)
22203 funccall_T *fc;
22204 int copyID;
22205{
22206 return (fc->l_varlist.lv_copyID != copyID
22207 && fc->l_vars.dv_copyID != copyID
22208 && fc->l_avars.dv_copyID != copyID);
22209}
22210
22211/*
22212 * Free "fc" and what it contains.
22213 */
22214 static void
22215free_funccal(fc, free_val)
22216 funccall_T *fc;
22217 int free_val; /* a: vars were allocated */
22218{
22219 listitem_T *li;
22220
22221 /* The a: variables typevals may not have been allocated, only free the
22222 * allocated variables. */
22223 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22224
22225 /* free all l: variables */
22226 vars_clear(&fc->l_vars.dv_hashtab);
22227
22228 /* Free the a:000 variables if they were allocated. */
22229 if (free_val)
22230 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22231 clear_tv(&li->li_tv);
22232
22233 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234}
22235
22236/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022237 * Add a number variable "name" to dict "dp" with value "nr".
22238 */
22239 static void
22240add_nr_var(dp, v, name, nr)
22241 dict_T *dp;
22242 dictitem_T *v;
22243 char *name;
22244 varnumber_T nr;
22245{
22246 STRCPY(v->di_key, name);
22247 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22248 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22249 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022250 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022251 v->di_tv.vval.v_number = nr;
22252}
22253
22254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 * ":return [expr]"
22256 */
22257 void
22258ex_return(eap)
22259 exarg_T *eap;
22260{
22261 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022262 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263 int returning = FALSE;
22264
22265 if (current_funccal == NULL)
22266 {
22267 EMSG(_("E133: :return not inside a function"));
22268 return;
22269 }
22270
22271 if (eap->skip)
22272 ++emsg_skip;
22273
22274 eap->nextcmd = NULL;
22275 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022276 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277 {
22278 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022279 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022281 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 }
22283 /* It's safer to return also on error. */
22284 else if (!eap->skip)
22285 {
22286 /*
22287 * Return unless the expression evaluation has been cancelled due to an
22288 * aborting error, an interrupt, or an exception.
22289 */
22290 if (!aborting())
22291 returning = do_return(eap, FALSE, TRUE, NULL);
22292 }
22293
22294 /* When skipping or the return gets pending, advance to the next command
22295 * in this line (!returning). Otherwise, ignore the rest of the line.
22296 * Following lines will be ignored by get_func_line(). */
22297 if (returning)
22298 eap->nextcmd = NULL;
22299 else if (eap->nextcmd == NULL) /* no argument */
22300 eap->nextcmd = check_nextcmd(arg);
22301
22302 if (eap->skip)
22303 --emsg_skip;
22304}
22305
22306/*
22307 * Return from a function. Possibly makes the return pending. Also called
22308 * for a pending return at the ":endtry" or after returning from an extra
22309 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022310 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022311 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022312 * FALSE when the return gets pending.
22313 */
22314 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022315do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 exarg_T *eap;
22317 int reanimate;
22318 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022319 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320{
22321 int idx;
22322 struct condstack *cstack = eap->cstack;
22323
22324 if (reanimate)
22325 /* Undo the return. */
22326 current_funccal->returned = FALSE;
22327
22328 /*
22329 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22330 * not in its finally clause (which then is to be executed next) is found.
22331 * In this case, make the ":return" pending for execution at the ":endtry".
22332 * Otherwise, return normally.
22333 */
22334 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22335 if (idx >= 0)
22336 {
22337 cstack->cs_pending[idx] = CSTP_RETURN;
22338
22339 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022340 /* A pending return again gets pending. "rettv" points to an
22341 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022343 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344 else
22345 {
22346 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022347 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022349 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022351 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352 {
22353 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022354 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022355 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 else
22357 EMSG(_(e_outofmem));
22358 }
22359 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022360 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361
22362 if (reanimate)
22363 {
22364 /* The pending return value could be overwritten by a ":return"
22365 * without argument in a finally clause; reset the default
22366 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022367 current_funccal->rettv->v_type = VAR_NUMBER;
22368 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 }
22370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022371 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 }
22373 else
22374 {
22375 current_funccal->returned = TRUE;
22376
22377 /* If the return is carried out now, store the return value. For
22378 * a return immediately after reanimation, the value is already
22379 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022380 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022382 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022383 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022385 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 }
22387 }
22388
22389 return idx < 0;
22390}
22391
22392/*
22393 * Free the variable with a pending return value.
22394 */
22395 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022396discard_pending_return(rettv)
22397 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398{
Bram Moolenaar33570922005-01-25 22:26:29 +000022399 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400}
22401
22402/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022403 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022404 * is an allocated string. Used by report_pending() for verbose messages.
22405 */
22406 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022407get_return_cmd(rettv)
22408 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022410 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022411 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022412 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022414 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022415 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022416 if (s == NULL)
22417 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022418
22419 STRCPY(IObuff, ":return ");
22420 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22421 if (STRLEN(s) + 8 >= IOSIZE)
22422 STRCPY(IObuff + IOSIZE - 4, "...");
22423 vim_free(tofree);
22424 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425}
22426
22427/*
22428 * Get next function line.
22429 * Called by do_cmdline() to get the next line.
22430 * Returns allocated string, or NULL for end of function.
22431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 char_u *
22433get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022434 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022436 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437{
Bram Moolenaar33570922005-01-25 22:26:29 +000022438 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022439 ufunc_T *fp = fcp->func;
22440 char_u *retval;
22441 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442
22443 /* If breakpoints have been added/deleted need to check for it. */
22444 if (fcp->dbg_tick != debug_tick)
22445 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022446 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 sourcing_lnum);
22448 fcp->dbg_tick = debug_tick;
22449 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022450#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022451 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022452 func_line_end(cookie);
22453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454
Bram Moolenaar05159a02005-02-26 23:04:13 +000022455 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022456 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22457 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 retval = NULL;
22459 else
22460 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022461 /* Skip NULL lines (continuation lines). */
22462 while (fcp->linenr < gap->ga_len
22463 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22464 ++fcp->linenr;
22465 if (fcp->linenr >= gap->ga_len)
22466 retval = NULL;
22467 else
22468 {
22469 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22470 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022471#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022472 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022473 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022474#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476 }
22477
22478 /* Did we encounter a breakpoint? */
22479 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22480 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022481 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022483 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 sourcing_lnum);
22485 fcp->dbg_tick = debug_tick;
22486 }
22487
22488 return retval;
22489}
22490
Bram Moolenaar05159a02005-02-26 23:04:13 +000022491#if defined(FEAT_PROFILE) || defined(PROTO)
22492/*
22493 * Called when starting to read a function line.
22494 * "sourcing_lnum" must be correct!
22495 * When skipping lines it may not actually be executed, but we won't find out
22496 * until later and we need to store the time now.
22497 */
22498 void
22499func_line_start(cookie)
22500 void *cookie;
22501{
22502 funccall_T *fcp = (funccall_T *)cookie;
22503 ufunc_T *fp = fcp->func;
22504
22505 if (fp->uf_profiling && sourcing_lnum >= 1
22506 && sourcing_lnum <= fp->uf_lines.ga_len)
22507 {
22508 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022509 /* Skip continuation lines. */
22510 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22511 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022512 fp->uf_tml_execed = FALSE;
22513 profile_start(&fp->uf_tml_start);
22514 profile_zero(&fp->uf_tml_children);
22515 profile_get_wait(&fp->uf_tml_wait);
22516 }
22517}
22518
22519/*
22520 * Called when actually executing a function line.
22521 */
22522 void
22523func_line_exec(cookie)
22524 void *cookie;
22525{
22526 funccall_T *fcp = (funccall_T *)cookie;
22527 ufunc_T *fp = fcp->func;
22528
22529 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22530 fp->uf_tml_execed = TRUE;
22531}
22532
22533/*
22534 * Called when done with a function line.
22535 */
22536 void
22537func_line_end(cookie)
22538 void *cookie;
22539{
22540 funccall_T *fcp = (funccall_T *)cookie;
22541 ufunc_T *fp = fcp->func;
22542
22543 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22544 {
22545 if (fp->uf_tml_execed)
22546 {
22547 ++fp->uf_tml_count[fp->uf_tml_idx];
22548 profile_end(&fp->uf_tml_start);
22549 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022550 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022551 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22552 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022553 }
22554 fp->uf_tml_idx = -1;
22555 }
22556}
22557#endif
22558
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559/*
22560 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022561 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 */
22563 int
22564func_has_ended(cookie)
22565 void *cookie;
22566{
Bram Moolenaar33570922005-01-25 22:26:29 +000022567 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568
22569 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22570 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022571 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 || fcp->returned);
22573}
22574
22575/*
22576 * return TRUE if cookie indicates a function which "abort"s on errors.
22577 */
22578 int
22579func_has_abort(cookie)
22580 void *cookie;
22581{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022582 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583}
22584
22585#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22586typedef enum
22587{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022588 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22589 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22590 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591} var_flavour_T;
22592
22593static var_flavour_T var_flavour __ARGS((char_u *varname));
22594
22595 static var_flavour_T
22596var_flavour(varname)
22597 char_u *varname;
22598{
22599 char_u *p = varname;
22600
22601 if (ASCII_ISUPPER(*p))
22602 {
22603 while (*(++p))
22604 if (ASCII_ISLOWER(*p))
22605 return VAR_FLAVOUR_SESSION;
22606 return VAR_FLAVOUR_VIMINFO;
22607 }
22608 else
22609 return VAR_FLAVOUR_DEFAULT;
22610}
22611#endif
22612
22613#if defined(FEAT_VIMINFO) || defined(PROTO)
22614/*
22615 * Restore global vars that start with a capital from the viminfo file
22616 */
22617 int
22618read_viminfo_varlist(virp, writing)
22619 vir_T *virp;
22620 int writing;
22621{
22622 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022623 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022624 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625
22626 if (!writing && (find_viminfo_parameter('!') != NULL))
22627 {
22628 tab = vim_strchr(virp->vir_line + 1, '\t');
22629 if (tab != NULL)
22630 {
22631 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022632 switch (*tab)
22633 {
22634 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022635#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022636 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022637#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022638 case 'D': type = VAR_DICT; break;
22639 case 'L': type = VAR_LIST; break;
22640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641
22642 tab = vim_strchr(tab, '\t');
22643 if (tab != NULL)
22644 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022645 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022646 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022647 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022648 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022649#ifdef FEAT_FLOAT
22650 else if (type == VAR_FLOAT)
22651 (void)string2float(tab + 1, &tv.vval.v_float);
22652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022654 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022655 if (type == VAR_DICT || type == VAR_LIST)
22656 {
22657 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22658
22659 if (etv == NULL)
22660 /* Failed to parse back the dict or list, use it as a
22661 * string. */
22662 tv.v_type = VAR_STRING;
22663 else
22664 {
22665 vim_free(tv.vval.v_string);
22666 tv = *etv;
22667 }
22668 }
22669
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022670 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022671
22672 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022673 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022674 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22675 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 }
22677 }
22678 }
22679
22680 return viminfo_readline(virp);
22681}
22682
22683/*
22684 * Write global vars that start with a capital to the viminfo file
22685 */
22686 void
22687write_viminfo_varlist(fp)
22688 FILE *fp;
22689{
Bram Moolenaar33570922005-01-25 22:26:29 +000022690 hashitem_T *hi;
22691 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022692 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022693 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022694 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022695 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022696 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697
22698 if (find_viminfo_parameter('!') == NULL)
22699 return;
22700
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022701 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022702
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022703 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022704 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022706 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022708 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022709 this_var = HI2DI(hi);
22710 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022711 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022712 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022713 {
22714 case VAR_STRING: s = "STR"; break;
22715 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022716#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022717 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022718#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022719 case VAR_DICT: s = "DIC"; break;
22720 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022721 default: continue;
22722 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022723 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022724 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022725 if (p != NULL)
22726 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022727 vim_free(tofree);
22728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 }
22730 }
22731}
22732#endif
22733
22734#if defined(FEAT_SESSION) || defined(PROTO)
22735 int
22736store_session_globals(fd)
22737 FILE *fd;
22738{
Bram Moolenaar33570922005-01-25 22:26:29 +000022739 hashitem_T *hi;
22740 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022741 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742 char_u *p, *t;
22743
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022744 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022745 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022747 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022749 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022750 this_var = HI2DI(hi);
22751 if ((this_var->di_tv.v_type == VAR_NUMBER
22752 || this_var->di_tv.v_type == VAR_STRING)
22753 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022754 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022755 /* Escape special characters with a backslash. Turn a LF and
22756 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022757 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022758 (char_u *)"\\\"\n\r");
22759 if (p == NULL) /* out of memory */
22760 break;
22761 for (t = p; *t != NUL; ++t)
22762 if (*t == '\n')
22763 *t = 'n';
22764 else if (*t == '\r')
22765 *t = 'r';
22766 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022767 this_var->di_key,
22768 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22769 : ' ',
22770 p,
22771 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22772 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022773 || put_eol(fd) == FAIL)
22774 {
22775 vim_free(p);
22776 return FAIL;
22777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778 vim_free(p);
22779 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022780#ifdef FEAT_FLOAT
22781 else if (this_var->di_tv.v_type == VAR_FLOAT
22782 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22783 {
22784 float_T f = this_var->di_tv.vval.v_float;
22785 int sign = ' ';
22786
22787 if (f < 0)
22788 {
22789 f = -f;
22790 sign = '-';
22791 }
22792 if ((fprintf(fd, "let %s = %c&%f",
22793 this_var->di_key, sign, f) < 0)
22794 || put_eol(fd) == FAIL)
22795 return FAIL;
22796 }
22797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798 }
22799 }
22800 return OK;
22801}
22802#endif
22803
Bram Moolenaar661b1822005-07-28 22:36:45 +000022804/*
22805 * Display script name where an item was last set.
22806 * Should only be invoked when 'verbose' is non-zero.
22807 */
22808 void
22809last_set_msg(scriptID)
22810 scid_T scriptID;
22811{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022812 char_u *p;
22813
Bram Moolenaar661b1822005-07-28 22:36:45 +000022814 if (scriptID != 0)
22815 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022816 p = home_replace_save(NULL, get_scriptname(scriptID));
22817 if (p != NULL)
22818 {
22819 verbose_enter();
22820 MSG_PUTS(_("\n\tLast set from "));
22821 MSG_PUTS(p);
22822 vim_free(p);
22823 verbose_leave();
22824 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022825 }
22826}
22827
Bram Moolenaard812df62008-11-09 12:46:09 +000022828/*
22829 * List v:oldfiles in a nice way.
22830 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022831 void
22832ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022833 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022834{
22835 list_T *l = vimvars[VV_OLDFILES].vv_list;
22836 listitem_T *li;
22837 int nr = 0;
22838
22839 if (l == NULL)
22840 msg((char_u *)_("No old files"));
22841 else
22842 {
22843 msg_start();
22844 msg_scroll = TRUE;
22845 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22846 {
22847 msg_outnum((long)++nr);
22848 MSG_PUTS(": ");
22849 msg_outtrans(get_tv_string(&li->li_tv));
22850 msg_putchar('\n');
22851 out_flush(); /* output one line at a time */
22852 ui_breakcheck();
22853 }
22854 /* Assume "got_int" was set to truncate the listing. */
22855 got_int = FALSE;
22856
22857#ifdef FEAT_BROWSE_CMD
22858 if (cmdmod.browse)
22859 {
22860 quit_more = FALSE;
22861 nr = prompt_for_number(FALSE);
22862 msg_starthere();
22863 if (nr > 0)
22864 {
22865 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22866 (long)nr);
22867
22868 if (p != NULL)
22869 {
22870 p = expand_env_save(p);
22871 eap->arg = p;
22872 eap->cmdidx = CMD_edit;
22873 cmdmod.browse = FALSE;
22874 do_exedit(eap, NULL);
22875 vim_free(p);
22876 }
22877 }
22878 }
22879#endif
22880 }
22881}
22882
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883#endif /* FEAT_EVAL */
22884
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022886#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887
22888#ifdef WIN3264
22889/*
22890 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22891 */
22892static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22893static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22894static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22895
22896/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022897 * Get the short path (8.3) for the filename in "fnamep".
22898 * Only works for a valid file name.
22899 * When the path gets longer "fnamep" is changed and the allocated buffer
22900 * is put in "bufp".
22901 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22902 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 */
22904 static int
22905get_short_pathname(fnamep, bufp, fnamelen)
22906 char_u **fnamep;
22907 char_u **bufp;
22908 int *fnamelen;
22909{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022910 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 char_u *newbuf;
22912
22913 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 l = GetShortPathName(*fnamep, *fnamep, len);
22915 if (l > len - 1)
22916 {
22917 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022918 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 newbuf = vim_strnsave(*fnamep, l);
22920 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022921 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922
22923 vim_free(*bufp);
22924 *fnamep = *bufp = newbuf;
22925
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022926 /* Really should always succeed, as the buffer is big enough. */
22927 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 }
22929
22930 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022931 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932}
22933
22934/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022935 * Get the short path (8.3) for the filename in "fname". The converted
22936 * path is returned in "bufp".
22937 *
22938 * Some of the directories specified in "fname" may not exist. This function
22939 * will shorten the existing directories at the beginning of the path and then
22940 * append the remaining non-existing path.
22941 *
22942 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022943 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022944 * bufp - Pointer to an allocated buffer for the filename.
22945 * fnamelen - Length of the filename pointed to by fname
22946 *
22947 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 */
22949 static int
22950shortpath_for_invalid_fname(fname, bufp, fnamelen)
22951 char_u **fname;
22952 char_u **bufp;
22953 int *fnamelen;
22954{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022955 char_u *short_fname, *save_fname, *pbuf_unused;
22956 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022958 int old_len, len;
22959 int new_len, sfx_len;
22960 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961
22962 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022963 old_len = *fnamelen;
22964 save_fname = vim_strnsave(*fname, old_len);
22965 pbuf_unused = NULL;
22966 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022968 endp = save_fname + old_len - 1; /* Find the end of the copy */
22969 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022971 /*
22972 * Try shortening the supplied path till it succeeds by removing one
22973 * directory at a time from the tail of the path.
22974 */
22975 len = 0;
22976 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022978 /* go back one path-separator */
22979 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22980 --endp;
22981 if (endp <= save_fname)
22982 break; /* processed the complete path */
22983
22984 /*
22985 * Replace the path separator with a NUL and try to shorten the
22986 * resulting path.
22987 */
22988 ch = *endp;
22989 *endp = 0;
22990 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022991 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022992 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22993 {
22994 retval = FAIL;
22995 goto theend;
22996 }
22997 *endp = ch; /* preserve the string */
22998
22999 if (len > 0)
23000 break; /* successfully shortened the path */
23001
23002 /* failed to shorten the path. Skip the path separator */
23003 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 }
23005
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023006 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023008 /*
23009 * Succeeded in shortening the path. Now concatenate the shortened
23010 * path with the remaining path at the tail.
23011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023013 /* Compute the length of the new path. */
23014 sfx_len = (int)(save_endp - endp) + 1;
23015 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023017 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023019 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023021 /* There is not enough space in the currently allocated string,
23022 * copy it to a buffer big enough. */
23023 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023025 {
23026 retval = FAIL;
23027 goto theend;
23028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 }
23030 else
23031 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023032 /* Transfer short_fname to the main buffer (it's big enough),
23033 * unless get_short_pathname() did its work in-place. */
23034 *fname = *bufp = save_fname;
23035 if (short_fname != save_fname)
23036 vim_strncpy(save_fname, short_fname, len);
23037 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023039
23040 /* concat the not-shortened part of the path */
23041 vim_strncpy(*fname + len, endp, sfx_len);
23042 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023044
23045theend:
23046 vim_free(pbuf_unused);
23047 vim_free(save_fname);
23048
23049 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050}
23051
23052/*
23053 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023054 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 */
23056 static int
23057shortpath_for_partial(fnamep, bufp, fnamelen)
23058 char_u **fnamep;
23059 char_u **bufp;
23060 int *fnamelen;
23061{
23062 int sepcount, len, tflen;
23063 char_u *p;
23064 char_u *pbuf, *tfname;
23065 int hasTilde;
23066
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023067 /* Count up the path separators from the RHS.. so we know which part
23068 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023070 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 if (vim_ispathsep(*p))
23072 ++sepcount;
23073
23074 /* Need full path first (use expand_env() to remove a "~/") */
23075 hasTilde = (**fnamep == '~');
23076 if (hasTilde)
23077 pbuf = tfname = expand_env_save(*fnamep);
23078 else
23079 pbuf = tfname = FullName_save(*fnamep, FALSE);
23080
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023081 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023083 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23084 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085
23086 if (len == 0)
23087 {
23088 /* Don't have a valid filename, so shorten the rest of the
23089 * path if we can. This CAN give us invalid 8.3 filenames, but
23090 * there's not a lot of point in guessing what it might be.
23091 */
23092 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023093 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23094 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023095 }
23096
23097 /* Count the paths backward to find the beginning of the desired string. */
23098 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023099 {
23100#ifdef FEAT_MBYTE
23101 if (has_mbyte)
23102 p -= mb_head_off(tfname, p);
23103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 if (vim_ispathsep(*p))
23105 {
23106 if (sepcount == 0 || (hasTilde && sepcount == 1))
23107 break;
23108 else
23109 sepcount --;
23110 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112 if (hasTilde)
23113 {
23114 --p;
23115 if (p >= tfname)
23116 *p = '~';
23117 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023118 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119 }
23120 else
23121 ++p;
23122
23123 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23124 vim_free(*bufp);
23125 *fnamelen = (int)STRLEN(p);
23126 *bufp = pbuf;
23127 *fnamep = p;
23128
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023129 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023130}
23131#endif /* WIN3264 */
23132
23133/*
23134 * Adjust a filename, according to a string of modifiers.
23135 * *fnamep must be NUL terminated when called. When returning, the length is
23136 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023137 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138 * When there is an error, *fnamep is set to NULL.
23139 */
23140 int
23141modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23142 char_u *src; /* string with modifiers */
23143 int *usedlen; /* characters after src that are used */
23144 char_u **fnamep; /* file name so far */
23145 char_u **bufp; /* buffer for allocated file name or NULL */
23146 int *fnamelen; /* length of fnamep */
23147{
23148 int valid = 0;
23149 char_u *tail;
23150 char_u *s, *p, *pbuf;
23151 char_u dirname[MAXPATHL];
23152 int c;
23153 int has_fullname = 0;
23154#ifdef WIN3264
23155 int has_shortname = 0;
23156#endif
23157
23158repeat:
23159 /* ":p" - full path/file_name */
23160 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23161 {
23162 has_fullname = 1;
23163
23164 valid |= VALID_PATH;
23165 *usedlen += 2;
23166
23167 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23168 if ((*fnamep)[0] == '~'
23169#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23170 && ((*fnamep)[1] == '/'
23171# ifdef BACKSLASH_IN_FILENAME
23172 || (*fnamep)[1] == '\\'
23173# endif
23174 || (*fnamep)[1] == NUL)
23175
23176#endif
23177 )
23178 {
23179 *fnamep = expand_env_save(*fnamep);
23180 vim_free(*bufp); /* free any allocated file name */
23181 *bufp = *fnamep;
23182 if (*fnamep == NULL)
23183 return -1;
23184 }
23185
23186 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023187 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 {
23189 if (vim_ispathsep(*p)
23190 && p[1] == '.'
23191 && (p[2] == NUL
23192 || vim_ispathsep(p[2])
23193 || (p[2] == '.'
23194 && (p[3] == NUL || vim_ispathsep(p[3])))))
23195 break;
23196 }
23197
23198 /* FullName_save() is slow, don't use it when not needed. */
23199 if (*p != NUL || !vim_isAbsName(*fnamep))
23200 {
23201 *fnamep = FullName_save(*fnamep, *p != NUL);
23202 vim_free(*bufp); /* free any allocated file name */
23203 *bufp = *fnamep;
23204 if (*fnamep == NULL)
23205 return -1;
23206 }
23207
23208 /* Append a path separator to a directory. */
23209 if (mch_isdir(*fnamep))
23210 {
23211 /* Make room for one or two extra characters. */
23212 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23213 vim_free(*bufp); /* free any allocated file name */
23214 *bufp = *fnamep;
23215 if (*fnamep == NULL)
23216 return -1;
23217 add_pathsep(*fnamep);
23218 }
23219 }
23220
23221 /* ":." - path relative to the current directory */
23222 /* ":~" - path relative to the home directory */
23223 /* ":8" - shortname path - postponed till after */
23224 while (src[*usedlen] == ':'
23225 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23226 {
23227 *usedlen += 2;
23228 if (c == '8')
23229 {
23230#ifdef WIN3264
23231 has_shortname = 1; /* Postpone this. */
23232#endif
23233 continue;
23234 }
23235 pbuf = NULL;
23236 /* Need full path first (use expand_env() to remove a "~/") */
23237 if (!has_fullname)
23238 {
23239 if (c == '.' && **fnamep == '~')
23240 p = pbuf = expand_env_save(*fnamep);
23241 else
23242 p = pbuf = FullName_save(*fnamep, FALSE);
23243 }
23244 else
23245 p = *fnamep;
23246
23247 has_fullname = 0;
23248
23249 if (p != NULL)
23250 {
23251 if (c == '.')
23252 {
23253 mch_dirname(dirname, MAXPATHL);
23254 s = shorten_fname(p, dirname);
23255 if (s != NULL)
23256 {
23257 *fnamep = s;
23258 if (pbuf != NULL)
23259 {
23260 vim_free(*bufp); /* free any allocated file name */
23261 *bufp = pbuf;
23262 pbuf = NULL;
23263 }
23264 }
23265 }
23266 else
23267 {
23268 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23269 /* Only replace it when it starts with '~' */
23270 if (*dirname == '~')
23271 {
23272 s = vim_strsave(dirname);
23273 if (s != NULL)
23274 {
23275 *fnamep = s;
23276 vim_free(*bufp);
23277 *bufp = s;
23278 }
23279 }
23280 }
23281 vim_free(pbuf);
23282 }
23283 }
23284
23285 tail = gettail(*fnamep);
23286 *fnamelen = (int)STRLEN(*fnamep);
23287
23288 /* ":h" - head, remove "/file_name", can be repeated */
23289 /* Don't remove the first "/" or "c:\" */
23290 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23291 {
23292 valid |= VALID_HEAD;
23293 *usedlen += 2;
23294 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023295 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023296 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297 *fnamelen = (int)(tail - *fnamep);
23298#ifdef VMS
23299 if (*fnamelen > 0)
23300 *fnamelen += 1; /* the path separator is part of the path */
23301#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023302 if (*fnamelen == 0)
23303 {
23304 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23305 p = vim_strsave((char_u *)".");
23306 if (p == NULL)
23307 return -1;
23308 vim_free(*bufp);
23309 *bufp = *fnamep = tail = p;
23310 *fnamelen = 1;
23311 }
23312 else
23313 {
23314 while (tail > s && !after_pathsep(s, tail))
23315 mb_ptr_back(*fnamep, tail);
23316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 }
23318
23319 /* ":8" - shortname */
23320 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23321 {
23322 *usedlen += 2;
23323#ifdef WIN3264
23324 has_shortname = 1;
23325#endif
23326 }
23327
23328#ifdef WIN3264
23329 /* Check shortname after we have done 'heads' and before we do 'tails'
23330 */
23331 if (has_shortname)
23332 {
23333 pbuf = NULL;
23334 /* Copy the string if it is shortened by :h */
23335 if (*fnamelen < (int)STRLEN(*fnamep))
23336 {
23337 p = vim_strnsave(*fnamep, *fnamelen);
23338 if (p == 0)
23339 return -1;
23340 vim_free(*bufp);
23341 *bufp = *fnamep = p;
23342 }
23343
23344 /* Split into two implementations - makes it easier. First is where
23345 * there isn't a full name already, second is where there is.
23346 */
23347 if (!has_fullname && !vim_isAbsName(*fnamep))
23348 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023349 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350 return -1;
23351 }
23352 else
23353 {
23354 int l;
23355
23356 /* Simple case, already have the full-name
23357 * Nearly always shorter, so try first time. */
23358 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023359 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023360 return -1;
23361
23362 if (l == 0)
23363 {
23364 /* Couldn't find the filename.. search the paths.
23365 */
23366 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023367 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023368 return -1;
23369 }
23370 *fnamelen = l;
23371 }
23372 }
23373#endif /* WIN3264 */
23374
23375 /* ":t" - tail, just the basename */
23376 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23377 {
23378 *usedlen += 2;
23379 *fnamelen -= (int)(tail - *fnamep);
23380 *fnamep = tail;
23381 }
23382
23383 /* ":e" - extension, can be repeated */
23384 /* ":r" - root, without extension, can be repeated */
23385 while (src[*usedlen] == ':'
23386 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23387 {
23388 /* find a '.' in the tail:
23389 * - for second :e: before the current fname
23390 * - otherwise: The last '.'
23391 */
23392 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23393 s = *fnamep - 2;
23394 else
23395 s = *fnamep + *fnamelen - 1;
23396 for ( ; s > tail; --s)
23397 if (s[0] == '.')
23398 break;
23399 if (src[*usedlen + 1] == 'e') /* :e */
23400 {
23401 if (s > tail)
23402 {
23403 *fnamelen += (int)(*fnamep - (s + 1));
23404 *fnamep = s + 1;
23405#ifdef VMS
23406 /* cut version from the extension */
23407 s = *fnamep + *fnamelen - 1;
23408 for ( ; s > *fnamep; --s)
23409 if (s[0] == ';')
23410 break;
23411 if (s > *fnamep)
23412 *fnamelen = s - *fnamep;
23413#endif
23414 }
23415 else if (*fnamep <= tail)
23416 *fnamelen = 0;
23417 }
23418 else /* :r */
23419 {
23420 if (s > tail) /* remove one extension */
23421 *fnamelen = (int)(s - *fnamep);
23422 }
23423 *usedlen += 2;
23424 }
23425
23426 /* ":s?pat?foo?" - substitute */
23427 /* ":gs?pat?foo?" - global substitute */
23428 if (src[*usedlen] == ':'
23429 && (src[*usedlen + 1] == 's'
23430 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23431 {
23432 char_u *str;
23433 char_u *pat;
23434 char_u *sub;
23435 int sep;
23436 char_u *flags;
23437 int didit = FALSE;
23438
23439 flags = (char_u *)"";
23440 s = src + *usedlen + 2;
23441 if (src[*usedlen + 1] == 'g')
23442 {
23443 flags = (char_u *)"g";
23444 ++s;
23445 }
23446
23447 sep = *s++;
23448 if (sep)
23449 {
23450 /* find end of pattern */
23451 p = vim_strchr(s, sep);
23452 if (p != NULL)
23453 {
23454 pat = vim_strnsave(s, (int)(p - s));
23455 if (pat != NULL)
23456 {
23457 s = p + 1;
23458 /* find end of substitution */
23459 p = vim_strchr(s, sep);
23460 if (p != NULL)
23461 {
23462 sub = vim_strnsave(s, (int)(p - s));
23463 str = vim_strnsave(*fnamep, *fnamelen);
23464 if (sub != NULL && str != NULL)
23465 {
23466 *usedlen = (int)(p + 1 - src);
23467 s = do_string_sub(str, pat, sub, flags);
23468 if (s != NULL)
23469 {
23470 *fnamep = s;
23471 *fnamelen = (int)STRLEN(s);
23472 vim_free(*bufp);
23473 *bufp = s;
23474 didit = TRUE;
23475 }
23476 }
23477 vim_free(sub);
23478 vim_free(str);
23479 }
23480 vim_free(pat);
23481 }
23482 }
23483 /* after using ":s", repeat all the modifiers */
23484 if (didit)
23485 goto repeat;
23486 }
23487 }
23488
23489 return valid;
23490}
23491
23492/*
23493 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23494 * "flags" can be "g" to do a global substitute.
23495 * Returns an allocated string, NULL for error.
23496 */
23497 char_u *
23498do_string_sub(str, pat, sub, flags)
23499 char_u *str;
23500 char_u *pat;
23501 char_u *sub;
23502 char_u *flags;
23503{
23504 int sublen;
23505 regmatch_T regmatch;
23506 int i;
23507 int do_all;
23508 char_u *tail;
23509 garray_T ga;
23510 char_u *ret;
23511 char_u *save_cpo;
23512
23513 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23514 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023515 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516
23517 ga_init2(&ga, 1, 200);
23518
23519 do_all = (flags[0] == 'g');
23520
23521 regmatch.rm_ic = p_ic;
23522 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23523 if (regmatch.regprog != NULL)
23524 {
23525 tail = str;
23526 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23527 {
23528 /*
23529 * Get some space for a temporary buffer to do the substitution
23530 * into. It will contain:
23531 * - The text up to where the match is.
23532 * - The substituted text.
23533 * - The text after the match.
23534 */
23535 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23536 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23537 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23538 {
23539 ga_clear(&ga);
23540 break;
23541 }
23542
23543 /* copy the text up to where the match is */
23544 i = (int)(regmatch.startp[0] - tail);
23545 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23546 /* add the substituted text */
23547 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23548 + ga.ga_len + i, TRUE, TRUE, FALSE);
23549 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550 /* avoid getting stuck on a match with an empty string */
23551 if (tail == regmatch.endp[0])
23552 {
23553 if (*tail == NUL)
23554 break;
23555 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23556 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 }
23558 else
23559 {
23560 tail = regmatch.endp[0];
23561 if (*tail == NUL)
23562 break;
23563 }
23564 if (!do_all)
23565 break;
23566 }
23567
23568 if (ga.ga_data != NULL)
23569 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23570
23571 vim_free(regmatch.regprog);
23572 }
23573
23574 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23575 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023576 if (p_cpo == empty_option)
23577 p_cpo = save_cpo;
23578 else
23579 /* Darn, evaluating {sub} expression changed the value. */
23580 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581
23582 return ret;
23583}
23584
23585#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */