blob: 9f63d4525d6d427d8613bae3e7e74850e89ca7ca [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000117 * All user-defined global variables are stored in dictionary "globvardict".
118 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120static dict_T globvardict;
121static dictitem_T globvars_var;
122#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
124/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125 * Old Vim variables such as "v:version" are also available without the "v:".
126 * Also in functions. We need a special hashtable for them.
127 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000128static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200130/* When using exists() don't auto-load a script. */
131static int no_autoload = FALSE;
132
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134 * When recursively copying lists and dicts we need to remember which ones we
135 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000136 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 */
138static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139#define COPYID_INC 2
140#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141
142/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000143 * Array to hold the hashtab with variables local to each sourced script.
144 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000146typedef struct
147{
148 dictitem_T sv_var;
149 dict_T sv_dict;
150} scriptvar_T;
151
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200152static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
153#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
154#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156static int echo_attr = 0; /* attributes used for ":echo" */
157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000158/* Values for trans_function_name() argument: */
159#define TFN_INT 1 /* internal function name OK */
160#define TFN_QUIET 2 /* no error messages */
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
373/*
374 * The v: variables are stored in dictionary "vimvardict".
375 * "vimvars_var" is the variable that is used for the "l:" scope.
376 */
377static dict_T vimvardict;
378static dictitem_T vimvars_var;
379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
403static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
404static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
405static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
406static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
407static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
408static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
409static void item_lock __ARGS((typval_T *tv, int deep, int lock));
410static int tv_islocked __ARGS((typval_T *tv));
411
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
413static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000418static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
419static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000420
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000421static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000426static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void listitem_free __ARGS((listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100429static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
430static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
431static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000432static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000434static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
436static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000437static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100439static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000441static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200442static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000443static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static int string2float __ARGS((char_u *text, float_T *value));
455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
457static int find_internal_func __ARGS((char_u *name));
458static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
459static 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 +0200460static 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 +0000461static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000462static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100469static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200475static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200477static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
491static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
492#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000493static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000496static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000499static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000500static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
506static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200507static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
512static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523#ifdef FEAT_FLOAT
524static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
525#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000528static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#ifdef FEAT_FLOAT
535static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000539static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000548static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000550static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000566static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000567static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200570static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000571static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000579static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000593static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100598static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000600static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000612#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200613static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
615#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200616#ifdef FEAT_LUA
617static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
618#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000623static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000624static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000627static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000631#ifdef vim_mkdir
632static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100635#ifdef FEAT_MZSCHEME
636static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
637#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100640static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000641static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000642#ifdef FEAT_FLOAT
643static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000646static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000647static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200648#ifdef FEAT_PYTHON3
649static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
651#ifdef FEAT_PYTHON
652static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
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 Moolenaar9750bb12012-12-05 16:10:42 +0100671static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000674static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000676static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000683static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000684static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000685static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000686static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200688static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000689static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100691#ifdef FEAT_CRYPT
692static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
693#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000694static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200695static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#ifdef FEAT_FLOAT
698static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200699static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000702static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000703static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#ifdef FEAT_FLOAT
707static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
709#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000710static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200711static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712#ifdef HAVE_STRFTIME
713static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
714#endif
715static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200721static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200722static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000728static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200729static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000731static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000732static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000733static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000734static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000735static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000737static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200738#ifdef FEAT_FLOAT
739static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000745#ifdef FEAT_FLOAT
746static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200749static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200750static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000760static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000763static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100764static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000766static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000767static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static int get_env_len __ARGS((char_u **arg));
769static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000770static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000771static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
772#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
773#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
774 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000775static 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 +0000776static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000777static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
779static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static typval_T *alloc_tv __ARGS((void));
781static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static void init_tv __ARGS((typval_T *varp));
783static long get_tv_number __ARGS((typval_T *varp));
784static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000785static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static char_u *get_tv_string __ARGS((typval_T *varp));
787static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000788static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000790static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000791static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
792static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
793static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000794static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
795static 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 +0000796static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
797static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000798static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200799static int var_check_func_name __ARGS((char_u *name, int new_var));
800static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000801static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000802static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000803static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
804static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
805static int eval_fname_script __ARGS((char_u *p));
806static int eval_fname_sid __ARGS((char_u *p));
807static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000808static ufunc_T *find_func __ARGS((char_u *name));
809static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000810static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811#ifdef FEAT_PROFILE
812static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000813static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
814static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
815static int
816# ifdef __BORLANDC__
817 _RTLENTRYF
818# endif
819 prof_total_cmp __ARGS((const void *s1, const void *s2));
820static int
821# ifdef __BORLANDC__
822 _RTLENTRYF
823# endif
824 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000826static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000827static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000828static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static 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 +0000831static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
832static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000833static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000834static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
835static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000836static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000837static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000838static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000839
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200840
841#ifdef EBCDIC
842static int compare_func_name __ARGS((const void *s1, const void *s2));
843static void sortFunctions __ARGS(());
844#endif
845
846
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000847/* Character used as separated in autoload function/variable names. */
848#define AUTOLOAD_CHAR '#'
849
Bram Moolenaar33570922005-01-25 22:26:29 +0000850/*
851 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000852 */
853 void
854eval_init()
855{
Bram Moolenaar33570922005-01-25 22:26:29 +0000856 int i;
857 struct vimvar *p;
858
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200859 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
860 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200861 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000862 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000863 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000864
865 for (i = 0; i < VV_LEN; ++i)
866 {
867 p = &vimvars[i];
868 STRCPY(p->vv_di.di_key, p->vv_name);
869 if (p->vv_flags & VV_RO)
870 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
871 else if (p->vv_flags & VV_RO_SBX)
872 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
873 else
874 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000875
876 /* add to v: scope dict, unless the value is not always available */
877 if (p->vv_type != VAR_UNKNOWN)
878 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000879 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000880 /* add to compat scope dict */
881 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000882 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000883 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200884 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200885
886#ifdef EBCDIC
887 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100888 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200889 */
890 sortFunctions();
891#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000892}
893
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000894#if defined(EXITFREE) || defined(PROTO)
895 void
896eval_clear()
897{
898 int i;
899 struct vimvar *p;
900
901 for (i = 0; i < VV_LEN; ++i)
902 {
903 p = &vimvars[i];
904 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000905 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000906 vim_free(p->vv_str);
907 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000908 }
909 else if (p->vv_di.di_tv.v_type == VAR_LIST)
910 {
911 list_unref(p->vv_list);
912 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914 }
915 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000916 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917 hash_clear(&compat_hashtab);
918
Bram Moolenaard9fba312005-06-26 22:34:35 +0000919 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200920 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000921
922 /* global variables */
923 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000924
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000925 /* autoloaded script names */
926 ga_clear_strings(&ga_loaded);
927
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200928 /* script-local variables */
929 for (i = 1; i <= ga_scripts.ga_len; ++i)
930 {
931 vars_clear(&SCRIPT_VARS(i));
932 vim_free(SCRIPT_SV(i));
933 }
934 ga_clear(&ga_scripts);
935
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000936 /* unreferenced lists and dicts */
937 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000938
939 /* functions */
940 free_all_functions();
941 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000942}
943#endif
944
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 * Return the name of the executed function.
947 */
948 char_u *
949func_name(cookie)
950 void *cookie;
951{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000952 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the next breakpoint line for a funccall cookie.
957 */
958 linenr_T *
959func_breakpoint(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the address holding the debug tick for a funccall cookie.
967 */
968 int *
969func_dbg_tick(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Return the nesting level for a funccall cookie.
977 */
978 int
979func_level(cookie)
980 void *cookie;
981{
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000986funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000988/* pointer to list of previously used funccal, still around because some
989 * item in it is still being used. */
990funccall_T *previous_funccal = NULL;
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992/*
993 * Return TRUE when a function was ended by a ":return" command.
994 */
995 int
996current_func_returned()
997{
998 return current_funccal->returned;
999}
1000
1001
1002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 * Set an internal variable to a string value. Creates the variable if it does
1004 * not already exist.
1005 */
1006 void
1007set_internal_string_var(name, value)
1008 char_u *name;
1009 char_u *value;
1010{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001012 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014 val = vim_strsave(value);
1015 if (val != NULL)
1016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001017 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001018 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001020 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001021 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023 }
1024}
1025
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001027static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001028static char_u *redir_endp = NULL;
1029static char_u *redir_varname = NULL;
1030
1031/*
1032 * Start recording command output to a variable
1033 * Returns OK if successfully completed the setup. FAIL otherwise.
1034 */
1035 int
1036var_redir_start(name, append)
1037 char_u *name;
1038 int append; /* append to an existing variable */
1039{
1040 int save_emsg;
1041 int err;
1042 typval_T tv;
1043
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001044 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001045 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 {
1047 EMSG(_(e_invarg));
1048 return FAIL;
1049 }
1050
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001051 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052 redir_varname = vim_strsave(name);
1053 if (redir_varname == NULL)
1054 return FAIL;
1055
1056 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1057 if (redir_lval == NULL)
1058 {
1059 var_redir_stop();
1060 return FAIL;
1061 }
1062
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001063 /* The output is stored in growarray "redir_ga" until redirection ends. */
1064 ga_init2(&redir_ga, (int)sizeof(char), 500);
1065
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001067 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1068 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1070 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001071 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp != NULL && *redir_endp != NUL)
1073 /* Trailing characters are present after the variable name */
1074 EMSG(_(e_trailing));
1075 else
1076 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001077 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 var_redir_stop();
1079 return FAIL;
1080 }
1081
1082 /* check if we can write to the variable: set it to or append an empty
1083 * string */
1084 save_emsg = did_emsg;
1085 did_emsg = FALSE;
1086 tv.v_type = VAR_STRING;
1087 tv.vval.v_string = (char_u *)"";
1088 if (append)
1089 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1090 else
1091 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001092 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001094 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 if (err)
1096 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001097 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 var_redir_stop();
1099 return FAIL;
1100 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101
1102 return OK;
1103}
1104
1105/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001106 * Append "value[value_len]" to the variable set by var_redir_start().
1107 * The actual appending is postponed until redirection ends, because the value
1108 * appended may in fact be the string we write to, changing it may cause freed
1109 * memory to be used:
1110 * :redir => foo
1111 * :let foo
1112 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 */
1114 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120
1121 if (redir_lval == NULL)
1122 return;
1123
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001125 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001127 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 {
1131 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 }
1134 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136}
1137
1138/*
1139 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001140 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 */
1142 void
1143var_redir_stop()
1144{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001145 typval_T tv;
1146
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 if (redir_lval != NULL)
1148 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 /* If there was no error: assign the text to the variable. */
1150 if (redir_endp != NULL)
1151 {
1152 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1153 tv.v_type = VAR_STRING;
1154 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001155 /* Call get_lval() again, if it's inside a Dict or List it may
1156 * have changed. */
1157 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1158 FALSE, FALSE, FALSE, FNE_CHECK_START);
1159 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1160 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1161 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001162 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001163
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001164 /* free the collected output */
1165 vim_free(redir_ga.ga_data);
1166 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 vim_free(redir_lval);
1169 redir_lval = NULL;
1170 }
1171 vim_free(redir_varname);
1172 redir_varname = NULL;
1173}
1174
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175# if defined(FEAT_MBYTE) || defined(PROTO)
1176 int
1177eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1178 char_u *enc_from;
1179 char_u *enc_to;
1180 char_u *fname_from;
1181 char_u *fname_to;
1182{
1183 int err = FALSE;
1184
1185 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1186 set_vim_var_string(VV_CC_TO, enc_to, -1);
1187 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1188 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1189 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1190 err = TRUE;
1191 set_vim_var_string(VV_CC_FROM, NULL, -1);
1192 set_vim_var_string(VV_CC_TO, NULL, -1);
1193 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1194 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1195
1196 if (err)
1197 return FAIL;
1198 return OK;
1199}
1200# endif
1201
1202# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1203 int
1204eval_printexpr(fname, args)
1205 char_u *fname;
1206 char_u *args;
1207{
1208 int err = FALSE;
1209
1210 set_vim_var_string(VV_FNAME_IN, fname, -1);
1211 set_vim_var_string(VV_CMDARG, args, -1);
1212 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1213 err = TRUE;
1214 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1215 set_vim_var_string(VV_CMDARG, NULL, -1);
1216
1217 if (err)
1218 {
1219 mch_remove(fname);
1220 return FAIL;
1221 }
1222 return OK;
1223}
1224# endif
1225
1226# if defined(FEAT_DIFF) || defined(PROTO)
1227 void
1228eval_diff(origfile, newfile, outfile)
1229 char_u *origfile;
1230 char_u *newfile;
1231 char_u *outfile;
1232{
1233 int err = FALSE;
1234
1235 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1236 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1237 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1238 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1239 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1240 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1241 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1242}
1243
1244 void
1245eval_patch(origfile, difffile, outfile)
1246 char_u *origfile;
1247 char_u *difffile;
1248 char_u *outfile;
1249{
1250 int err;
1251
1252 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1253 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1254 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1255 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1256 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1257 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1258 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1259}
1260# endif
1261
1262/*
1263 * Top level evaluation function, returning a boolean.
1264 * Sets "error" to TRUE if there was an error.
1265 * Return TRUE or FALSE.
1266 */
1267 int
1268eval_to_bool(arg, error, nextcmd, skip)
1269 char_u *arg;
1270 int *error;
1271 char_u **nextcmd;
1272 int skip; /* only parse, don't execute */
1273{
Bram Moolenaar33570922005-01-25 22:26:29 +00001274 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 int retval = FALSE;
1276
1277 if (skip)
1278 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001279 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 else
1282 {
1283 *error = FALSE;
1284 if (!skip)
1285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001286 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001287 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289 }
1290 if (skip)
1291 --emsg_skip;
1292
1293 return retval;
1294}
1295
1296/*
1297 * Top level evaluation function, returning a string. If "skip" is TRUE,
1298 * only parsing to "nextcmd" is done, without reporting errors. Return
1299 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1300 */
1301 char_u *
1302eval_to_string_skip(arg, nextcmd, skip)
1303 char_u *arg;
1304 char_u **nextcmd;
1305 int skip; /* only parse, don't execute */
1306{
Bram Moolenaar33570922005-01-25 22:26:29 +00001307 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 char_u *retval;
1309
1310 if (skip)
1311 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001312 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 retval = NULL;
1314 else
1315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001316 retval = vim_strsave(get_tv_string(&tv));
1317 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319 if (skip)
1320 --emsg_skip;
1321
1322 return retval;
1323}
1324
1325/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001326 * Skip over an expression at "*pp".
1327 * Return FAIL for an error, OK otherwise.
1328 */
1329 int
1330skip_expr(pp)
1331 char_u **pp;
1332{
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001334
1335 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337}
1338
1339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341 * When "convert" is TRUE convert a List into a sequence of lines and convert
1342 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Return pointer to allocated memory, or NULL for failure.
1344 */
1345 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 char_u *arg;
1348 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
Bram Moolenaar33570922005-01-25 22:26:29 +00001351 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001354#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 retval = NULL;
1360 else
1361 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001362 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 {
1364 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001365 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001366 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001367 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001368 if (tv.vval.v_list->lv_len > 0)
1369 ga_append(&ga, NL);
1370 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 ga_append(&ga, NUL);
1372 retval = (char_u *)ga.ga_data;
1373 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001374#ifdef FEAT_FLOAT
1375 else if (convert && tv.v_type == VAR_FLOAT)
1376 {
1377 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1378 retval = vim_strsave(numbuf);
1379 }
1380#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 else
1382 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385
1386 return retval;
1387}
1388
1389/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001390 * Call eval_to_string() without using current local variables and using
1391 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 */
1393 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 char_u *arg;
1396 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399 char_u *retval;
1400 void *save_funccalp;
1401
1402 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001403 if (use_sandbox)
1404 ++sandbox;
1405 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001406 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 if (use_sandbox)
1408 --sandbox;
1409 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 restore_funccal(save_funccalp);
1411 return retval;
1412}
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414/*
1415 * Top level evaluation function, returning a number.
1416 * Evaluates "expr" silently.
1417 * Returns -1 for an error.
1418 */
1419 int
1420eval_to_number(expr)
1421 char_u *expr;
1422{
Bram Moolenaar33570922005-01-25 22:26:29 +00001423 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001425 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
1427 ++emsg_off;
1428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001429 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 retval = -1;
1431 else
1432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001433 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 }
1436 --emsg_off;
1437
1438 return retval;
1439}
1440
Bram Moolenaara40058a2005-07-11 22:42:07 +00001441/*
1442 * Prepare v: variable "idx" to be used.
1443 * Save the current typeval in "save_tv".
1444 * When not used yet add the variable to the v: hashtable.
1445 */
1446 static void
1447prepare_vimvar(idx, save_tv)
1448 int idx;
1449 typval_T *save_tv;
1450{
1451 *save_tv = vimvars[idx].vv_tv;
1452 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1453 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1454}
1455
1456/*
1457 * Restore v: variable "idx" to typeval "save_tv".
1458 * When no longer defined, remove the variable from the v: hashtable.
1459 */
1460 static void
1461restore_vimvar(idx, save_tv)
1462 int idx;
1463 typval_T *save_tv;
1464{
1465 hashitem_T *hi;
1466
Bram Moolenaara40058a2005-07-11 22:42:07 +00001467 vimvars[idx].vv_tv = *save_tv;
1468 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1469 {
1470 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1471 if (HASHITEM_EMPTY(hi))
1472 EMSG2(_(e_intern2), "restore_vimvar()");
1473 else
1474 hash_remove(&vimvarht, hi);
1475 }
1476}
1477
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001478#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001479/*
1480 * Evaluate an expression to a list with suggestions.
1481 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001482 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001483 */
1484 list_T *
1485eval_spell_expr(badword, expr)
1486 char_u *badword;
1487 char_u *expr;
1488{
1489 typval_T save_val;
1490 typval_T rettv;
1491 list_T *list = NULL;
1492 char_u *p = skipwhite(expr);
1493
1494 /* Set "v:val" to the bad word. */
1495 prepare_vimvar(VV_VAL, &save_val);
1496 vimvars[VV_VAL].vv_type = VAR_STRING;
1497 vimvars[VV_VAL].vv_str = badword;
1498 if (p_verbose == 0)
1499 ++emsg_off;
1500
1501 if (eval1(&p, &rettv, TRUE) == OK)
1502 {
1503 if (rettv.v_type != VAR_LIST)
1504 clear_tv(&rettv);
1505 else
1506 list = rettv.vval.v_list;
1507 }
1508
1509 if (p_verbose == 0)
1510 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001511 restore_vimvar(VV_VAL, &save_val);
1512
1513 return list;
1514}
1515
1516/*
1517 * "list" is supposed to contain two items: a word and a number. Return the
1518 * word in "pp" and the number as the return value.
1519 * Return -1 if anything isn't right.
1520 * Used to get the good word and score from the eval_spell_expr() result.
1521 */
1522 int
1523get_spellword(list, pp)
1524 list_T *list;
1525 char_u **pp;
1526{
1527 listitem_T *li;
1528
1529 li = list->lv_first;
1530 if (li == NULL)
1531 return -1;
1532 *pp = get_tv_string(&li->li_tv);
1533
1534 li = li->li_next;
1535 if (li == NULL)
1536 return -1;
1537 return get_tv_number(&li->li_tv);
1538}
1539#endif
1540
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001541/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001542 * Top level evaluation function.
1543 * Returns an allocated typval_T with the result.
1544 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 */
1546 typval_T *
1547eval_expr(arg, nextcmd)
1548 char_u *arg;
1549 char_u **nextcmd;
1550{
1551 typval_T *tv;
1552
1553 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001554 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 {
1556 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 }
1559
1560 return tv;
1561}
1562
1563
Bram Moolenaar4f688582007-07-24 12:34:30 +00001564#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1565 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001568 * Uses argv[argc] for the function arguments. Only Number and String
1569 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001570 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001572 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001573call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 char_u *func;
1575 int argc;
1576 char_u **argv;
1577 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001578 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
Bram Moolenaar33570922005-01-25 22:26:29 +00001581 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 long n;
1583 int len;
1584 int i;
1585 int doesrange;
1586 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001589 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001591 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
1593 for (i = 0; i < argc; i++)
1594 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001595 /* Pass a NULL or empty argument as an empty string */
1596 if (argv[i] == NULL || *argv[i] == NUL)
1597 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001598 argvars[i].v_type = VAR_STRING;
1599 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001600 continue;
1601 }
1602
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001603 if (str_arg_only)
1604 len = 0;
1605 else
1606 /* Recognize a number argument, the others must be strings. */
1607 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (len != 0 && len == (int)STRLEN(argv[i]))
1609 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001610 argvars[i].v_type = VAR_NUMBER;
1611 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 else
1614 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001615 argvars[i].v_type = VAR_STRING;
1616 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 }
1618 }
1619
1620 if (safe)
1621 {
1622 save_funccalp = save_funccal();
1623 ++sandbox;
1624 }
1625
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1627 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (safe)
1631 {
1632 --sandbox;
1633 restore_funccal(save_funccalp);
1634 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001635 vim_free(argvars);
1636
1637 if (ret == FAIL)
1638 clear_tv(rettv);
1639
1640 return ret;
1641}
1642
Bram Moolenaar4f688582007-07-24 12:34:30 +00001643# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001645 * Call vimL function "func" and return the result as a string.
1646 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001647 * Uses argv[argc] for the function arguments.
1648 */
1649 void *
1650call_func_retstr(func, argc, argv, safe)
1651 char_u *func;
1652 int argc;
1653 char_u **argv;
1654 int safe; /* use the sandbox */
1655{
1656 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001657 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001659 /* All arguments are passed as strings, no conversion to number. */
1660 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001661 return NULL;
1662
1663 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001664 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 return retval;
1666}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001667# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001668
Bram Moolenaar4f688582007-07-24 12:34:30 +00001669# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001670/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001671 * Call vimL function "func" and return the result as a number.
1672 * Returns -1 when calling the function fails.
1673 * Uses argv[argc] for the function arguments.
1674 */
1675 long
1676call_func_retnr(func, argc, argv, safe)
1677 char_u *func;
1678 int argc;
1679 char_u **argv;
1680 int safe; /* use the sandbox */
1681{
1682 typval_T rettv;
1683 long retval;
1684
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001685 /* All arguments are passed as strings, no conversion to number. */
1686 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001687 return -1;
1688
1689 retval = get_tv_number_chk(&rettv, NULL);
1690 clear_tv(&rettv);
1691 return retval;
1692}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001693# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001694
1695/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001696 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001698 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 */
1700 void *
1701call_func_retlist(func, argc, argv, safe)
1702 char_u *func;
1703 int argc;
1704 char_u **argv;
1705 int safe; /* use the sandbox */
1706{
1707 typval_T rettv;
1708
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001709 /* All arguments are passed as strings, no conversion to number. */
1710 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 return NULL;
1712
1713 if (rettv.v_type != VAR_LIST)
1714 {
1715 clear_tv(&rettv);
1716 return NULL;
1717 }
1718
1719 return rettv.vval.v_list;
1720}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#endif
1722
Bram Moolenaar4f688582007-07-24 12:34:30 +00001723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724/*
1725 * Save the current function call pointer, and set it to NULL.
1726 * Used when executing autocommands and for ":source".
1727 */
1728 void *
1729save_funccal()
1730{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001731 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 current_funccal = NULL;
1734 return (void *)fc;
1735}
1736
1737 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001738restore_funccal(vfc)
1739 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001741 funccall_T *fc = (funccall_T *)vfc;
1742
1743 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744}
1745
Bram Moolenaar05159a02005-02-26 23:04:13 +00001746#if defined(FEAT_PROFILE) || defined(PROTO)
1747/*
1748 * Prepare profiling for entering a child or something else that is not
1749 * counted for the script/function itself.
1750 * Should always be called in pair with prof_child_exit().
1751 */
1752 void
1753prof_child_enter(tm)
1754 proftime_T *tm; /* place to store waittime */
1755{
1756 funccall_T *fc = current_funccal;
1757
1758 if (fc != NULL && fc->func->uf_profiling)
1759 profile_start(&fc->prof_child);
1760 script_prof_save(tm);
1761}
1762
1763/*
1764 * Take care of time spent in a child.
1765 * Should always be called after prof_child_enter().
1766 */
1767 void
1768prof_child_exit(tm)
1769 proftime_T *tm; /* where waittime was stored */
1770{
1771 funccall_T *fc = current_funccal;
1772
1773 if (fc != NULL && fc->func->uf_profiling)
1774 {
1775 profile_end(&fc->prof_child);
1776 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1777 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1778 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1779 }
1780 script_prof_restore(tm);
1781}
1782#endif
1783
1784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785#ifdef FEAT_FOLDING
1786/*
1787 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1788 * it in "*cp". Doesn't give error messages.
1789 */
1790 int
1791eval_foldexpr(arg, cp)
1792 char_u *arg;
1793 int *cp;
1794{
Bram Moolenaar33570922005-01-25 22:26:29 +00001795 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 int retval;
1797 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001798 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1799 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
1801 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001802 if (use_sandbox)
1803 ++sandbox;
1804 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 retval = 0;
1808 else
1809 {
1810 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 if (tv.v_type == VAR_NUMBER)
1812 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001813 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 retval = 0;
1815 else
1816 {
1817 /* If the result is a string, check if there is a non-digit before
1818 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 if (!VIM_ISDIGIT(*s) && *s != '-')
1821 *cp = *s++;
1822 retval = atol((char *)s);
1823 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 }
1826 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001827 if (use_sandbox)
1828 --sandbox;
1829 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
1831 return retval;
1832}
1833#endif
1834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 * ":let" list all variable values
1837 * ":let var1 var2" list variable values
1838 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001839 * ":let var += expr" assignment command.
1840 * ":let var -= expr" assignment command.
1841 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 */
1844 void
1845ex_let(eap)
1846 exarg_T *eap;
1847{
1848 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001850 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 int var_count = 0;
1853 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001854 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001855 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001856 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
Bram Moolenaardb552d602006-03-23 22:59:57 +00001858 argend = skip_var_list(arg, &var_count, &semicolon);
1859 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001861 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1862 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001863 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001866 /*
1867 * ":let" without "=": list variables
1868 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 if (*arg == '[')
1870 EMSG(_(e_invarg));
1871 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001873 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001877 list_glob_vars(&first);
1878 list_buf_vars(&first);
1879 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001880#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001881 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001882#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001883 list_script_vars(&first);
1884 list_func_vars(&first);
1885 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 eap->nextcmd = check_nextcmd(arg);
1888 }
1889 else
1890 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 op[0] = '=';
1892 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001893 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001894 {
1895 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1896 op[0] = expr[-1]; /* +=, -= or .= */
1897 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (eap->skip)
1901 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (eap->skip)
1904 {
1905 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 --emsg_skip;
1908 }
1909 else if (i != FAIL)
1910 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 }
1915 }
1916}
1917
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918/*
1919 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1920 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 * When "nextchars" is not NULL it points to a string with characters that
1922 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1923 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 * Returns OK or FAIL;
1925 */
1926 static int
1927ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1928 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001929 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 int copy; /* copy values from "tv", don't move */
1931 int semicolon; /* from skip_var_list() */
1932 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934{
1935 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001936 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001938 listitem_T *item;
1939 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940
1941 if (*arg != '[')
1942 {
1943 /*
1944 * ":let var = expr" or ":for var in list"
1945 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001946 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 return FAIL;
1948 return OK;
1949 }
1950
1951 /*
1952 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1953 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001954 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 {
1956 EMSG(_(e_listreq));
1957 return FAIL;
1958 }
1959
1960 i = list_len(l);
1961 if (semicolon == 0 && var_count < i)
1962 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001963 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 return FAIL;
1965 }
1966 if (var_count - semicolon > i)
1967 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001968 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 return FAIL;
1970 }
1971
1972 item = l->lv_first;
1973 while (*arg != ']')
1974 {
1975 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 item = item->li_next;
1978 if (arg == NULL)
1979 return FAIL;
1980
1981 arg = skipwhite(arg);
1982 if (*arg == ';')
1983 {
1984 /* Put the rest of the list (may be empty) in the var after ';'.
1985 * Create a new list for this. */
1986 l = list_alloc();
1987 if (l == NULL)
1988 return FAIL;
1989 while (item != NULL)
1990 {
1991 list_append_tv(l, &item->li_tv);
1992 item = item->li_next;
1993 }
1994
1995 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001996 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 ltv.vval.v_list = l;
1998 l->lv_refcount = 1;
1999
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002000 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2001 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 clear_tv(&ltv);
2003 if (arg == NULL)
2004 return FAIL;
2005 break;
2006 }
2007 else if (*arg != ',' && *arg != ']')
2008 {
2009 EMSG2(_(e_intern2), "ex_let_vars()");
2010 return FAIL;
2011 }
2012 }
2013
2014 return OK;
2015}
2016
2017/*
2018 * Skip over assignable variable "var" or list of variables "[var, var]".
2019 * Used for ":let varvar = expr" and ":for varvar in expr".
2020 * For "[var, var]" increment "*var_count" for each variable.
2021 * for "[var, var; var]" set "semicolon".
2022 * Return NULL for an error.
2023 */
2024 static char_u *
2025skip_var_list(arg, var_count, semicolon)
2026 char_u *arg;
2027 int *var_count;
2028 int *semicolon;
2029{
2030 char_u *p, *s;
2031
2032 if (*arg == '[')
2033 {
2034 /* "[var, var]": find the matching ']'. */
2035 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002036 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037 {
2038 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2039 s = skip_var_one(p);
2040 if (s == p)
2041 {
2042 EMSG2(_(e_invarg2), p);
2043 return NULL;
2044 }
2045 ++*var_count;
2046
2047 p = skipwhite(s);
2048 if (*p == ']')
2049 break;
2050 else if (*p == ';')
2051 {
2052 if (*semicolon == 1)
2053 {
2054 EMSG(_("Double ; in list of variables"));
2055 return NULL;
2056 }
2057 *semicolon = 1;
2058 }
2059 else if (*p != ',')
2060 {
2061 EMSG2(_(e_invarg2), p);
2062 return NULL;
2063 }
2064 }
2065 return p + 1;
2066 }
2067 else
2068 return skip_var_one(arg);
2069}
2070
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002071/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002072 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002073 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002075 static char_u *
2076skip_var_one(arg)
2077 char_u *arg;
2078{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002079 if (*arg == '@' && arg[1] != NUL)
2080 return arg + 2;
2081 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2082 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002083}
2084
Bram Moolenaara7043832005-01-21 11:56:39 +00002085/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 * List variables for hashtab "ht" with prefix "prefix".
2087 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002088 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002089 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002090list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002093 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002095{
Bram Moolenaar33570922005-01-25 22:26:29 +00002096 hashitem_T *hi;
2097 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 int todo;
2099
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002100 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002101 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2102 {
2103 if (!HASHITEM_EMPTY(hi))
2104 {
2105 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002106 di = HI2DI(hi);
2107 if (empty || di->di_tv.v_type != VAR_STRING
2108 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002110 }
2111 }
2112}
2113
2114/*
2115 * List global variables.
2116 */
2117 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118list_glob_vars(first)
2119 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002120{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002121 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002122}
2123
2124/*
2125 * List buffer variables.
2126 */
2127 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128list_buf_vars(first)
2129 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002130{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002131 char_u numbuf[NUMBUFLEN];
2132
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2134 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002135
2136 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2138 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002139}
2140
2141/*
2142 * List window variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_win_vars(first)
2146 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2149 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002150}
2151
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002152#ifdef FEAT_WINDOWS
2153/*
2154 * List tab page variables.
2155 */
2156 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157list_tab_vars(first)
2158 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002159{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2161 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002162}
2163#endif
2164
Bram Moolenaara7043832005-01-21 11:56:39 +00002165/*
2166 * List Vim variables.
2167 */
2168 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169list_vim_vars(first)
2170 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002171{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173}
2174
2175/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176 * List script-local variables, if there is a script.
2177 */
2178 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179list_script_vars(first)
2180 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002181{
2182 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2184 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185}
2186
2187/*
2188 * List function variables, if there is a function.
2189 */
2190 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191list_func_vars(first)
2192 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002193{
2194 if (current_funccal != NULL)
2195 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002197}
2198
2199/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200 * List variables in "arg".
2201 */
2202 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 exarg_T *eap;
2205 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207{
2208 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 char_u *name_start;
2212 char_u *arg_subsc;
2213 char_u *tofree;
2214 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215
2216 while (!ends_excmd(*arg) && !got_int)
2217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002220 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2222 {
2223 emsg_severe = TRUE;
2224 EMSG(_(e_trailing));
2225 break;
2226 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 /* get_name_len() takes care of expanding curly braces */
2231 name_start = name = arg;
2232 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2233 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 /* This is mainly to keep test 49 working: when expanding
2236 * curly braces fails overrule the exception error message. */
2237 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 emsg_severe = TRUE;
2240 EMSG2(_(e_invarg2), arg);
2241 break;
2242 }
2243 error = TRUE;
2244 }
2245 else
2246 {
2247 if (tofree != NULL)
2248 name = tofree;
2249 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 else
2252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 /* handle d.key, l[idx], f(expr) */
2254 arg_subsc = arg;
2255 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002256 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002260 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002263 case 'g': list_glob_vars(first); break;
2264 case 'b': list_buf_vars(first); break;
2265 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002266#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002268#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002269 case 'v': list_vim_vars(first); break;
2270 case 's': list_script_vars(first); break;
2271 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 default:
2273 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002274 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 }
2276 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 {
2278 char_u numbuf[NUMBUFLEN];
2279 char_u *tf;
2280 int c;
2281 char_u *s;
2282
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002283 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 c = *arg;
2285 *arg = NUL;
2286 list_one_var_a((char_u *)"",
2287 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002288 tv.v_type,
2289 s == NULL ? (char_u *)"" : s,
2290 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 *arg = c;
2292 vim_free(tf);
2293 }
2294 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 }
2297 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298
2299 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301
2302 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 }
2304
2305 return arg;
2306}
2307
2308/*
2309 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2310 * Returns a pointer to the char just after the var name.
2311 * Returns NULL if there is an error.
2312 */
2313 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002316 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002318 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320{
2321 int c1;
2322 char_u *name;
2323 char_u *p;
2324 char_u *arg_end = NULL;
2325 int len;
2326 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328
2329 /*
2330 * ":let $VAR = expr": Set environment variable.
2331 */
2332 if (*arg == '$')
2333 {
2334 /* Find the end of the name. */
2335 ++arg;
2336 name = arg;
2337 len = get_env_len(&arg);
2338 if (len == 0)
2339 EMSG2(_(e_invarg2), name - 1);
2340 else
2341 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 if (op != NULL && (*op == '+' || *op == '-'))
2343 EMSG2(_(e_letwrong), op);
2344 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002345 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002347 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002348 {
2349 c1 = name[len];
2350 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002351 p = get_tv_string_chk(tv);
2352 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 {
2354 int mustfree = FALSE;
2355 char_u *s = vim_getenv(name, &mustfree);
2356
2357 if (s != NULL)
2358 {
2359 p = tofree = concat_str(s, p);
2360 if (mustfree)
2361 vim_free(s);
2362 }
2363 }
2364 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002365 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002367 if (STRICMP(name, "HOME") == 0)
2368 init_homedir();
2369 else if (didset_vim && STRICMP(name, "VIM") == 0)
2370 didset_vim = FALSE;
2371 else if (didset_vimruntime
2372 && STRICMP(name, "VIMRUNTIME") == 0)
2373 didset_vimruntime = FALSE;
2374 arg_end = arg;
2375 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 }
2379 }
2380 }
2381
2382 /*
2383 * ":let &option = expr": Set option value.
2384 * ":let &l:option = expr": Set local option value.
2385 * ":let &g:option = expr": Set global option value.
2386 */
2387 else if (*arg == '&')
2388 {
2389 /* Find the end of the name. */
2390 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002391 if (p == NULL || (endchars != NULL
2392 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 EMSG(_(e_letunexp));
2394 else
2395 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 long n;
2397 int opt_type;
2398 long numval;
2399 char_u *stringval = NULL;
2400 char_u *s;
2401
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 c1 = *p;
2403 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404
2405 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002406 s = get_tv_string_chk(tv); /* != NULL if number or string */
2407 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408 {
2409 opt_type = get_option_value(arg, &numval,
2410 &stringval, opt_flags);
2411 if ((opt_type == 1 && *op == '.')
2412 || (opt_type == 0 && *op != '.'))
2413 EMSG2(_(e_letwrong), op);
2414 else
2415 {
2416 if (opt_type == 1) /* number */
2417 {
2418 if (*op == '+')
2419 n = numval + n;
2420 else
2421 n = numval - n;
2422 }
2423 else if (opt_type == 0 && stringval != NULL) /* string */
2424 {
2425 s = concat_str(stringval, s);
2426 vim_free(stringval);
2427 stringval = s;
2428 }
2429 }
2430 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002431 if (s != NULL)
2432 {
2433 set_option_value(arg, n, s, opt_flags);
2434 arg_end = p;
2435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 }
2439 }
2440
2441 /*
2442 * ":let @r = expr": Set register contents.
2443 */
2444 else if (*arg == '@')
2445 {
2446 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 if (op != NULL && (*op == '+' || *op == '-'))
2448 EMSG2(_(e_letwrong), op);
2449 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002450 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 EMSG(_(e_letunexp));
2452 else
2453 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 char_u *s;
2456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 p = get_tv_string_chk(tv);
2458 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002460 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 if (s != NULL)
2462 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002463 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 vim_free(s);
2465 }
2466 }
2467 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002470 arg_end = arg + 1;
2471 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002472 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
2474 }
2475
2476 /*
2477 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002480 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002482 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002484 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2488 EMSG(_(e_letunexp));
2489 else
2490 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 arg_end = p;
2493 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 }
2497
2498 else
2499 EMSG2(_(e_invarg2), arg);
2500
2501 return arg_end;
2502}
2503
2504/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2506 */
2507 static int
2508check_changedtick(arg)
2509 char_u *arg;
2510{
2511 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2512 {
2513 EMSG2(_(e_readonlyvar), arg);
2514 return TRUE;
2515 }
2516 return FALSE;
2517}
2518
2519/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 * Get an lval: variable, Dict item or List item that can be assigned a value
2521 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2522 * "name.key", "name.key[expr]" etc.
2523 * Indexing only works if "name" is an existing List or Dictionary.
2524 * "name" points to the start of the name.
2525 * If "rettv" is not NULL it points to the value to be assigned.
2526 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2527 * wrong; must end in space or cmd separator.
2528 *
2529 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002530 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 */
2533 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002534get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 typval_T *rettv;
2537 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 int unlet;
2539 int skip;
2540 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002541 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 char_u *expr_start, *expr_end;
2545 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002546 dictitem_T *v;
2547 typval_T var1;
2548 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002553 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557
2558 if (skip)
2559 {
2560 /* When skipping just find the end of the name. */
2561 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002562 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 }
2564
2565 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002566 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (expr_start != NULL)
2568 {
2569 /* Don't expand the name when we already know there is an error. */
2570 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2571 && *p != '[' && *p != '.')
2572 {
2573 EMSG(_(e_trailing));
2574 return NULL;
2575 }
2576
2577 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2578 if (lp->ll_exp_name == NULL)
2579 {
2580 /* Report an invalid expression in braces, unless the
2581 * expression evaluation has been cancelled due to an
2582 * aborting error, an interrupt, or an exception. */
2583 if (!aborting() && !quiet)
2584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002585 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 EMSG2(_(e_invarg2), name);
2587 return NULL;
2588 }
2589 }
2590 lp->ll_name = lp->ll_exp_name;
2591 }
2592 else
2593 lp->ll_name = name;
2594
2595 /* Without [idx] or .key we are done. */
2596 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2597 return p;
2598
2599 cc = *p;
2600 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002601 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (v == NULL && !quiet)
2603 EMSG2(_(e_undefvar), lp->ll_name);
2604 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 if (v == NULL)
2606 return NULL;
2607
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 /*
2609 * Loop until no more [idx] or .key is following.
2610 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002611 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2615 && !(lp->ll_tv->v_type == VAR_DICT
2616 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (!quiet)
2619 EMSG(_("E689: Can only index a List or Dictionary"));
2620 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_("E708: [:] must come last"));
2626 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 len = -1;
2630 if (*p == '.')
2631 {
2632 key = p + 1;
2633 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2634 ;
2635 if (len == 0)
2636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (!quiet)
2638 EMSG(_(e_emptykey));
2639 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 }
2641 p = key + len;
2642 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643 else
2644 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 if (*p == ':')
2648 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 else
2650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 empty1 = FALSE;
2652 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002654 if (get_tv_string_chk(&var1) == NULL)
2655 {
2656 /* not a number or string */
2657 clear_tv(&var1);
2658 return NULL;
2659 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 }
2661
2662 /* Optionally get the second index [ :expr]. */
2663 if (*p == ':')
2664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002668 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669 if (!empty1)
2670 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (rettv != NULL && (rettv->v_type != VAR_LIST
2674 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
2677 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
2682 p = skipwhite(p + 1);
2683 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 else
2686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (!empty1)
2691 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 if (get_tv_string_chk(&var2) == NULL)
2695 {
2696 /* not a number or string */
2697 if (!empty1)
2698 clear_tv(&var1);
2699 clear_tv(&var2);
2700 return NULL;
2701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (!quiet)
2711 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (!empty1)
2713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718
2719 /* Skip to past ']'. */
2720 ++p;
2721 }
2722
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 {
2725 if (len == -1)
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002728 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (*key == NUL)
2730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (!quiet)
2732 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
2736 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 lp->ll_list = NULL;
2738 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002739 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002740
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002741 /* When assigning to a scope dictionary check that a function and
2742 * variable name is valid (only variable name unless it is l: or
2743 * g: dictionary). Disallow overwriting a builtin function. */
2744 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002745 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002746 int prevval;
2747 int wrong;
2748
2749 if (len != -1)
2750 {
2751 prevval = key[len];
2752 key[len] = NUL;
2753 }
2754 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2755 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002757 || !valid_varname(key);
2758 if (len != -1)
2759 key[len] = prevval;
2760 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002761 return NULL;
2762 }
2763
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002766 /* Can't add "v:" variable. */
2767 if (lp->ll_dict == &vimvardict)
2768 {
2769 EMSG2(_(e_illvar), name);
2770 return NULL;
2771 }
2772
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002773 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002777 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 if (len == -1)
2779 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 }
2782 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 if (len == -1)
2787 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 p = NULL;
2790 break;
2791 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002792 /* existing variable, need to check if it can be changed */
2793 else if (var_check_ro(lp->ll_di->di_flags, name))
2794 return NULL;
2795
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 if (len == -1)
2797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 }
2800 else
2801 {
2802 /*
2803 * Get the number and item for the only or first index of the List.
2804 */
2805 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 else
2808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002809 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 clear_tv(&var1);
2811 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 lp->ll_list = lp->ll_tv->vval.v_list;
2814 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2815 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002817 if (lp->ll_n1 < 0)
2818 {
2819 lp->ll_n1 = 0;
2820 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2821 }
2822 }
2823 if (lp->ll_li == NULL)
2824 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002827 if (!quiet)
2828 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 }
2831
2832 /*
2833 * May need to find the item or absolute index for the second
2834 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 * When no index given: "lp->ll_empty2" is TRUE.
2836 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002840 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002846 {
2847 if (!quiet)
2848 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002850 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 }
2853
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2855 if (lp->ll_n1 < 0)
2856 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2857 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002858 {
2859 if (!quiet)
2860 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002862 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002863 }
2864
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002867 }
2868
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 return p;
2870}
2871
2872/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002873 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 */
2875 static void
2876clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002877 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878{
2879 vim_free(lp->ll_exp_name);
2880 vim_free(lp->ll_newkey);
2881}
2882
2883/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002884 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002886 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 */
2888 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002890 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002892 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895{
2896 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 listitem_T *ri;
2898 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899
2900 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002901 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 cc = *endp;
2905 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002906 if (op != NULL && *op != '=')
2907 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002909
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002910 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002911 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002912 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002913 {
2914 if (tv_op(&tv, rettv, op) == OK)
2915 set_var(lp->ll_name, &tv, FALSE);
2916 clear_tv(&tv);
2917 }
2918 }
2919 else
2920 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002922 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002924 else if (tv_check_lock(lp->ll_newkey == NULL
2925 ? lp->ll_tv->v_lock
2926 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2927 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 else if (lp->ll_range)
2929 {
2930 /*
2931 * Assign the List values to the list items.
2932 */
2933 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002934 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935 if (op != NULL && *op != '=')
2936 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2937 else
2938 {
2939 clear_tv(&lp->ll_li->li_tv);
2940 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2941 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 ri = ri->li_next;
2943 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2944 break;
2945 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002946 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002948 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002949 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 ri = NULL;
2951 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002952 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002953 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 lp->ll_li = lp->ll_li->li_next;
2955 ++lp->ll_n1;
2956 }
2957 if (ri != NULL)
2958 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002959 else if (lp->ll_empty2
2960 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 : lp->ll_n1 != lp->ll_n2)
2962 EMSG(_("E711: List value has not enough items"));
2963 }
2964 else
2965 {
2966 /*
2967 * Assign to a List or Dictionary item.
2968 */
2969 if (lp->ll_newkey != NULL)
2970 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002971 if (op != NULL && *op != '=')
2972 {
2973 EMSG2(_(e_letwrong), op);
2974 return;
2975 }
2976
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002978 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 if (di == NULL)
2980 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002981 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2982 {
2983 vim_free(di);
2984 return;
2985 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002988 else if (op != NULL && *op != '=')
2989 {
2990 tv_op(lp->ll_tv, rettv, op);
2991 return;
2992 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002993 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002995
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 /*
2997 * Assign the value to the variable or list item.
2998 */
2999 if (copy)
3000 copy_tv(rettv, lp->ll_tv);
3001 else
3002 {
3003 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003004 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003006 }
3007 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003008}
3009
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003010/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3012 * Returns OK or FAIL.
3013 */
3014 static int
3015tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003016 typval_T *tv1;
3017 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003018 char_u *op;
3019{
3020 long n;
3021 char_u numbuf[NUMBUFLEN];
3022 char_u *s;
3023
3024 /* Can't do anything with a Funcref or a Dict on the right. */
3025 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3026 {
3027 switch (tv1->v_type)
3028 {
3029 case VAR_DICT:
3030 case VAR_FUNC:
3031 break;
3032
3033 case VAR_LIST:
3034 if (*op != '+' || tv2->v_type != VAR_LIST)
3035 break;
3036 /* List += List */
3037 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3038 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3039 return OK;
3040
3041 case VAR_NUMBER:
3042 case VAR_STRING:
3043 if (tv2->v_type == VAR_LIST)
3044 break;
3045 if (*op == '+' || *op == '-')
3046 {
3047 /* nr += nr or nr -= nr*/
3048 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003049#ifdef FEAT_FLOAT
3050 if (tv2->v_type == VAR_FLOAT)
3051 {
3052 float_T f = n;
3053
3054 if (*op == '+')
3055 f += tv2->vval.v_float;
3056 else
3057 f -= tv2->vval.v_float;
3058 clear_tv(tv1);
3059 tv1->v_type = VAR_FLOAT;
3060 tv1->vval.v_float = f;
3061 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003062 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063#endif
3064 {
3065 if (*op == '+')
3066 n += get_tv_number(tv2);
3067 else
3068 n -= get_tv_number(tv2);
3069 clear_tv(tv1);
3070 tv1->v_type = VAR_NUMBER;
3071 tv1->vval.v_number = n;
3072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 }
3074 else
3075 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003076 if (tv2->v_type == VAR_FLOAT)
3077 break;
3078
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079 /* str .= str */
3080 s = get_tv_string(tv1);
3081 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3082 clear_tv(tv1);
3083 tv1->v_type = VAR_STRING;
3084 tv1->vval.v_string = s;
3085 }
3086 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003087
3088#ifdef FEAT_FLOAT
3089 case VAR_FLOAT:
3090 {
3091 float_T f;
3092
3093 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3094 && tv2->v_type != VAR_NUMBER
3095 && tv2->v_type != VAR_STRING))
3096 break;
3097 if (tv2->v_type == VAR_FLOAT)
3098 f = tv2->vval.v_float;
3099 else
3100 f = get_tv_number(tv2);
3101 if (*op == '+')
3102 tv1->vval.v_float += f;
3103 else
3104 tv1->vval.v_float -= f;
3105 }
3106 return OK;
3107#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003108 }
3109 }
3110
3111 EMSG2(_(e_letwrong), op);
3112 return FAIL;
3113}
3114
3115/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003116 * Add a watcher to a list.
3117 */
3118 static void
3119list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003120 list_T *l;
3121 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122{
3123 lw->lw_next = l->lv_watch;
3124 l->lv_watch = lw;
3125}
3126
3127/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003128 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003129 * No warning when it isn't found...
3130 */
3131 static void
3132list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003133 list_T *l;
3134 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003135{
Bram Moolenaar33570922005-01-25 22:26:29 +00003136 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137
3138 lwp = &l->lv_watch;
3139 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3140 {
3141 if (lw == lwrem)
3142 {
3143 *lwp = lw->lw_next;
3144 break;
3145 }
3146 lwp = &lw->lw_next;
3147 }
3148}
3149
3150/*
3151 * Just before removing an item from a list: advance watchers to the next
3152 * item.
3153 */
3154 static void
3155list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 list_T *l;
3157 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003158{
Bram Moolenaar33570922005-01-25 22:26:29 +00003159 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003160
3161 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3162 if (lw->lw_item == item)
3163 lw->lw_item = item->li_next;
3164}
3165
3166/*
3167 * Evaluate the expression used in a ":for var in expr" command.
3168 * "arg" points to "var".
3169 * Set "*errp" to TRUE for an error, FALSE otherwise;
3170 * Return a pointer that holds the info. Null when there is an error.
3171 */
3172 void *
3173eval_for_line(arg, errp, nextcmdp, skip)
3174 char_u *arg;
3175 int *errp;
3176 char_u **nextcmdp;
3177 int skip;
3178{
Bram Moolenaar33570922005-01-25 22:26:29 +00003179 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 typval_T tv;
3182 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183
3184 *errp = TRUE; /* default: there is an error */
3185
Bram Moolenaar33570922005-01-25 22:26:29 +00003186 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187 if (fi == NULL)
3188 return NULL;
3189
3190 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3191 if (expr == NULL)
3192 return fi;
3193
3194 expr = skipwhite(expr);
3195 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3196 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003197 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 return fi;
3199 }
3200
3201 if (skip)
3202 ++emsg_skip;
3203 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3204 {
3205 *errp = FALSE;
3206 if (!skip)
3207 {
3208 l = tv.vval.v_list;
3209 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003210 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003212 clear_tv(&tv);
3213 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214 else
3215 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003216 /* No need to increment the refcount, it's already set for the
3217 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 fi->fi_list = l;
3219 list_add_watch(l, &fi->fi_lw);
3220 fi->fi_lw.lw_item = l->lv_first;
3221 }
3222 }
3223 }
3224 if (skip)
3225 --emsg_skip;
3226
3227 return fi;
3228}
3229
3230/*
3231 * Use the first item in a ":for" list. Advance to the next.
3232 * Assign the values to the variable (list). "arg" points to the first one.
3233 * Return TRUE when a valid item was found, FALSE when at end of list or
3234 * something wrong.
3235 */
3236 int
3237next_for_item(fi_void, arg)
3238 void *fi_void;
3239 char_u *arg;
3240{
Bram Moolenaar33570922005-01-25 22:26:29 +00003241 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003243 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244
3245 item = fi->fi_lw.lw_item;
3246 if (item == NULL)
3247 result = FALSE;
3248 else
3249 {
3250 fi->fi_lw.lw_item = item->li_next;
3251 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3252 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3253 }
3254 return result;
3255}
3256
3257/*
3258 * Free the structure used to store info used by ":for".
3259 */
3260 void
3261free_for_info(fi_void)
3262 void *fi_void;
3263{
Bram Moolenaar33570922005-01-25 22:26:29 +00003264 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003266 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003267 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003269 list_unref(fi->fi_list);
3270 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 vim_free(fi);
3272}
3273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3275
3276 void
3277set_context_for_expression(xp, arg, cmdidx)
3278 expand_T *xp;
3279 char_u *arg;
3280 cmdidx_T cmdidx;
3281{
3282 int got_eq = FALSE;
3283 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 if (cmdidx == CMD_let)
3287 {
3288 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003289 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290 {
3291 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003292 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 {
3294 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 if (vim_iswhite(*p))
3297 break;
3298 }
3299 return;
3300 }
3301 }
3302 else
3303 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3304 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 while ((xp->xp_pattern = vim_strpbrk(arg,
3306 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3307 {
3308 c = *xp->xp_pattern;
3309 if (c == '&')
3310 {
3311 c = xp->xp_pattern[1];
3312 if (c == '&')
3313 {
3314 ++xp->xp_pattern;
3315 xp->xp_context = cmdidx != CMD_let || got_eq
3316 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3317 }
3318 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003321 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3322 xp->xp_pattern += 2;
3323
3324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
3326 else if (c == '$')
3327 {
3328 /* environment variable */
3329 xp->xp_context = EXPAND_ENV_VARS;
3330 }
3331 else if (c == '=')
3332 {
3333 got_eq = TRUE;
3334 xp->xp_context = EXPAND_EXPRESSION;
3335 }
3336 else if (c == '<'
3337 && xp->xp_context == EXPAND_FUNCTIONS
3338 && vim_strchr(xp->xp_pattern, '(') == NULL)
3339 {
3340 /* Function name can start with "<SNR>" */
3341 break;
3342 }
3343 else if (cmdidx != CMD_let || got_eq)
3344 {
3345 if (c == '"') /* string */
3346 {
3347 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3348 if (c == '\\' && xp->xp_pattern[1] != NUL)
3349 ++xp->xp_pattern;
3350 xp->xp_context = EXPAND_NOTHING;
3351 }
3352 else if (c == '\'') /* literal string */
3353 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003354 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3356 /* skip */ ;
3357 xp->xp_context = EXPAND_NOTHING;
3358 }
3359 else if (c == '|')
3360 {
3361 if (xp->xp_pattern[1] == '|')
3362 {
3363 ++xp->xp_pattern;
3364 xp->xp_context = EXPAND_EXPRESSION;
3365 }
3366 else
3367 xp->xp_context = EXPAND_COMMANDS;
3368 }
3369 else
3370 xp->xp_context = EXPAND_EXPRESSION;
3371 }
3372 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003373 /* Doesn't look like something valid, expand as an expression
3374 * anyway. */
3375 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 arg = xp->xp_pattern;
3377 if (*arg != NUL)
3378 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3379 /* skip */ ;
3380 }
3381 xp->xp_pattern = arg;
3382}
3383
3384#endif /* FEAT_CMDL_COMPL */
3385
3386/*
3387 * ":1,25call func(arg1, arg2)" function call.
3388 */
3389 void
3390ex_call(eap)
3391 exarg_T *eap;
3392{
3393 char_u *arg = eap->arg;
3394 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003396 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003398 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 linenr_T lnum;
3400 int doesrange;
3401 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003402 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003404 if (eap->skip)
3405 {
3406 /* trans_function_name() doesn't work well when skipping, use eval0()
3407 * instead to skip to any following command, e.g. for:
3408 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003409 ++emsg_skip;
3410 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3411 clear_tv(&rettv);
3412 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003413 return;
3414 }
3415
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003416 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003417 if (fudi.fd_newkey != NULL)
3418 {
3419 /* Still need to give an error message for missing key. */
3420 EMSG2(_(e_dictkey), fudi.fd_newkey);
3421 vim_free(fudi.fd_newkey);
3422 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003423 if (tofree == NULL)
3424 return;
3425
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003426 /* Increase refcount on dictionary, it could get deleted when evaluating
3427 * the arguments. */
3428 if (fudi.fd_dict != NULL)
3429 ++fudi.fd_dict->dv_refcount;
3430
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003431 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003432 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003433 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
Bram Moolenaar532c7802005-01-27 14:44:31 +00003435 /* Skip white space to allow ":call func ()". Not good, but required for
3436 * backward compatibility. */
3437 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003438 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 if (*startarg != '(')
3441 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003442 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 goto end;
3444 }
3445
3446 /*
3447 * When skipping, evaluate the function once, to find the end of the
3448 * arguments.
3449 * When the function takes a range, this is discovered after the first
3450 * call, and the loop is broken.
3451 */
3452 if (eap->skip)
3453 {
3454 ++emsg_skip;
3455 lnum = eap->line2; /* do it once, also with an invalid range */
3456 }
3457 else
3458 lnum = eap->line1;
3459 for ( ; lnum <= eap->line2; ++lnum)
3460 {
3461 if (!eap->skip && eap->addr_count > 0)
3462 {
3463 curwin->w_cursor.lnum = lnum;
3464 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003465#ifdef FEAT_VIRTUALEDIT
3466 curwin->w_cursor.coladd = 0;
3467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003470 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003471 eap->line1, eap->line2, &doesrange,
3472 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
3474 failed = TRUE;
3475 break;
3476 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003477
3478 /* Handle a function returning a Funcref, Dictionary or List. */
3479 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3480 {
3481 failed = TRUE;
3482 break;
3483 }
3484
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003485 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 if (doesrange || eap->skip)
3487 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003488
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003490 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003491 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003492 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 if (aborting())
3494 break;
3495 }
3496 if (eap->skip)
3497 --emsg_skip;
3498
3499 if (!failed)
3500 {
3501 /* Check for trailing illegal characters and a following command. */
3502 if (!ends_excmd(*arg))
3503 {
3504 emsg_severe = TRUE;
3505 EMSG(_(e_trailing));
3506 }
3507 else
3508 eap->nextcmd = check_nextcmd(arg);
3509 }
3510
3511end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003512 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003513 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514}
3515
3516/*
3517 * ":unlet[!] var1 ... " command.
3518 */
3519 void
3520ex_unlet(eap)
3521 exarg_T *eap;
3522{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003523 ex_unletlock(eap, eap->arg, 0);
3524}
3525
3526/*
3527 * ":lockvar" and ":unlockvar" commands
3528 */
3529 void
3530ex_lockvar(eap)
3531 exarg_T *eap;
3532{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534 int deep = 2;
3535
3536 if (eap->forceit)
3537 deep = -1;
3538 else if (vim_isdigit(*arg))
3539 {
3540 deep = getdigits(&arg);
3541 arg = skipwhite(arg);
3542 }
3543
3544 ex_unletlock(eap, arg, deep);
3545}
3546
3547/*
3548 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3549 */
3550 static void
3551ex_unletlock(eap, argstart, deep)
3552 exarg_T *eap;
3553 char_u *argstart;
3554 int deep;
3555{
3556 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003559 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561 do
3562 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003563 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003564 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3565 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003566 if (lv.ll_name == NULL)
3567 error = TRUE; /* error but continue parsing */
3568 if (name_end == NULL || (!vim_iswhite(*name_end)
3569 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571 if (name_end != NULL)
3572 {
3573 emsg_severe = TRUE;
3574 EMSG(_(e_trailing));
3575 }
3576 if (!(eap->skip || error))
3577 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 break;
3579 }
3580
3581 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582 {
3583 if (eap->cmdidx == CMD_unlet)
3584 {
3585 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3586 error = TRUE;
3587 }
3588 else
3589 {
3590 if (do_lock_var(&lv, name_end, deep,
3591 eap->cmdidx == CMD_lockvar) == FAIL)
3592 error = TRUE;
3593 }
3594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596 if (!eap->skip)
3597 clear_lval(&lv);
3598
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 arg = skipwhite(name_end);
3600 } while (!ends_excmd(*arg));
3601
3602 eap->nextcmd = check_nextcmd(arg);
3603}
3604
Bram Moolenaar8c711452005-01-14 21:53:12 +00003605 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003607 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003609 int forceit;
3610{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 int ret = OK;
3612 int cc;
3613
3614 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 cc = *name_end;
3617 *name_end = NUL;
3618
3619 /* Normal name or expanded name. */
3620 if (check_changedtick(lp->ll_name))
3621 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003622 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003625 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003626 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3627 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003628 else if (lp->ll_range)
3629 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003630 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631
3632 /* Delete a range of List items. */
3633 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3634 {
3635 li = lp->ll_li->li_next;
3636 listitem_remove(lp->ll_list, lp->ll_li);
3637 lp->ll_li = li;
3638 ++lp->ll_n1;
3639 }
3640 }
3641 else
3642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003643 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 /* unlet a List item. */
3645 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003647 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003648 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 }
3650
3651 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003652}
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654/*
3655 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003656 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 */
3658 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003659do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
Bram Moolenaar33570922005-01-25 22:26:29 +00003663 hashtab_T *ht;
3664 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003665 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003666 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
Bram Moolenaar33570922005-01-25 22:26:29 +00003668 ht = find_var_ht(name, &varname);
3669 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003671 hi = hash_find(ht, varname);
3672 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003673 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003674 di = HI2DI(hi);
3675 if (var_check_fixed(di->di_flags, name)
3676 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677 return FAIL;
3678 delete_var(ht, hi);
3679 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003682 if (forceit)
3683 return OK;
3684 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 return FAIL;
3686}
3687
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003688/*
3689 * Lock or unlock variable indicated by "lp".
3690 * "deep" is the levels to go (-1 for unlimited);
3691 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3692 */
3693 static int
3694do_lock_var(lp, name_end, deep, lock)
3695 lval_T *lp;
3696 char_u *name_end;
3697 int deep;
3698 int lock;
3699{
3700 int ret = OK;
3701 int cc;
3702 dictitem_T *di;
3703
3704 if (deep == 0) /* nothing to do */
3705 return OK;
3706
3707 if (lp->ll_tv == NULL)
3708 {
3709 cc = *name_end;
3710 *name_end = NUL;
3711
3712 /* Normal name or expanded name. */
3713 if (check_changedtick(lp->ll_name))
3714 ret = FAIL;
3715 else
3716 {
3717 di = find_var(lp->ll_name, NULL);
3718 if (di == NULL)
3719 ret = FAIL;
3720 else
3721 {
3722 if (lock)
3723 di->di_flags |= DI_FLAGS_LOCK;
3724 else
3725 di->di_flags &= ~DI_FLAGS_LOCK;
3726 item_lock(&di->di_tv, deep, lock);
3727 }
3728 }
3729 *name_end = cc;
3730 }
3731 else if (lp->ll_range)
3732 {
3733 listitem_T *li = lp->ll_li;
3734
3735 /* (un)lock a range of List items. */
3736 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3737 {
3738 item_lock(&li->li_tv, deep, lock);
3739 li = li->li_next;
3740 ++lp->ll_n1;
3741 }
3742 }
3743 else if (lp->ll_list != NULL)
3744 /* (un)lock a List item. */
3745 item_lock(&lp->ll_li->li_tv, deep, lock);
3746 else
3747 /* un(lock) a Dictionary item. */
3748 item_lock(&lp->ll_di->di_tv, deep, lock);
3749
3750 return ret;
3751}
3752
3753/*
3754 * Lock or unlock an item. "deep" is nr of levels to go.
3755 */
3756 static void
3757item_lock(tv, deep, lock)
3758 typval_T *tv;
3759 int deep;
3760 int lock;
3761{
3762 static int recurse = 0;
3763 list_T *l;
3764 listitem_T *li;
3765 dict_T *d;
3766 hashitem_T *hi;
3767 int todo;
3768
3769 if (recurse >= DICT_MAXNEST)
3770 {
3771 EMSG(_("E743: variable nested too deep for (un)lock"));
3772 return;
3773 }
3774 if (deep == 0)
3775 return;
3776 ++recurse;
3777
3778 /* lock/unlock the item itself */
3779 if (lock)
3780 tv->v_lock |= VAR_LOCKED;
3781 else
3782 tv->v_lock &= ~VAR_LOCKED;
3783
3784 switch (tv->v_type)
3785 {
3786 case VAR_LIST:
3787 if ((l = tv->vval.v_list) != NULL)
3788 {
3789 if (lock)
3790 l->lv_lock |= VAR_LOCKED;
3791 else
3792 l->lv_lock &= ~VAR_LOCKED;
3793 if (deep < 0 || deep > 1)
3794 /* recursive: lock/unlock the items the List contains */
3795 for (li = l->lv_first; li != NULL; li = li->li_next)
3796 item_lock(&li->li_tv, deep - 1, lock);
3797 }
3798 break;
3799 case VAR_DICT:
3800 if ((d = tv->vval.v_dict) != NULL)
3801 {
3802 if (lock)
3803 d->dv_lock |= VAR_LOCKED;
3804 else
3805 d->dv_lock &= ~VAR_LOCKED;
3806 if (deep < 0 || deep > 1)
3807 {
3808 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003809 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003810 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3811 {
3812 if (!HASHITEM_EMPTY(hi))
3813 {
3814 --todo;
3815 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3816 }
3817 }
3818 }
3819 }
3820 }
3821 --recurse;
3822}
3823
Bram Moolenaara40058a2005-07-11 22:42:07 +00003824/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003825 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3826 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003827 */
3828 static int
3829tv_islocked(tv)
3830 typval_T *tv;
3831{
3832 return (tv->v_lock & VAR_LOCKED)
3833 || (tv->v_type == VAR_LIST
3834 && tv->vval.v_list != NULL
3835 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3836 || (tv->v_type == VAR_DICT
3837 && tv->vval.v_dict != NULL
3838 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3839}
3840
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3842/*
3843 * Delete all "menutrans_" variables.
3844 */
3845 void
3846del_menutrans_vars()
3847{
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003849 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
Bram Moolenaar33570922005-01-25 22:26:29 +00003851 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003852 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003854 {
3855 if (!HASHITEM_EMPTY(hi))
3856 {
3857 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003858 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3859 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003860 }
3861 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863}
3864#endif
3865
3866#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3867
3868/*
3869 * Local string buffer for the next two functions to store a variable name
3870 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3871 * get_user_var_name().
3872 */
3873
3874static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3875
3876static char_u *varnamebuf = NULL;
3877static int varnamebuflen = 0;
3878
3879/*
3880 * Function to concatenate a prefix and a variable name.
3881 */
3882 static char_u *
3883cat_prefix_varname(prefix, name)
3884 int prefix;
3885 char_u *name;
3886{
3887 int len;
3888
3889 len = (int)STRLEN(name) + 3;
3890 if (len > varnamebuflen)
3891 {
3892 vim_free(varnamebuf);
3893 len += 10; /* some additional space */
3894 varnamebuf = alloc(len);
3895 if (varnamebuf == NULL)
3896 {
3897 varnamebuflen = 0;
3898 return NULL;
3899 }
3900 varnamebuflen = len;
3901 }
3902 *varnamebuf = prefix;
3903 varnamebuf[1] = ':';
3904 STRCPY(varnamebuf + 2, name);
3905 return varnamebuf;
3906}
3907
3908/*
3909 * Function given to ExpandGeneric() to obtain the list of user defined
3910 * (global/buffer/window/built-in) variable names.
3911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 char_u *
3913get_user_var_name(xp, idx)
3914 expand_T *xp;
3915 int idx;
3916{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003917 static long_u gdone;
3918 static long_u bdone;
3919 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003920#ifdef FEAT_WINDOWS
3921 static long_u tdone;
3922#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003923 static int vidx;
3924 static hashitem_T *hi;
3925 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003928 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003930#ifdef FEAT_WINDOWS
3931 tdone = 0;
3932#endif
3933 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003934
3935 /* Global variables */
3936 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003938 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003940 else
3941 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 while (HASHITEM_EMPTY(hi))
3943 ++hi;
3944 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3945 return cat_prefix_varname('g', hi->hi_key);
3946 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003948
3949 /* b: variables */
3950 ht = &curbuf->b_vars.dv_hashtab;
3951 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003954 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003955 else
3956 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003957 while (HASHITEM_EMPTY(hi))
3958 ++hi;
3959 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003961 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003963 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 return (char_u *)"b:changedtick";
3965 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003966
3967 /* w: variables */
3968 ht = &curwin->w_vars.dv_hashtab;
3969 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003971 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003972 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003973 else
3974 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003975 while (HASHITEM_EMPTY(hi))
3976 ++hi;
3977 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003979
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003980#ifdef FEAT_WINDOWS
3981 /* t: variables */
3982 ht = &curtab->tp_vars.dv_hashtab;
3983 if (tdone < ht->ht_used)
3984 {
3985 if (tdone++ == 0)
3986 hi = ht->ht_array;
3987 else
3988 ++hi;
3989 while (HASHITEM_EMPTY(hi))
3990 ++hi;
3991 return cat_prefix_varname('t', hi->hi_key);
3992 }
3993#endif
3994
Bram Moolenaar33570922005-01-25 22:26:29 +00003995 /* v: variables */
3996 if (vidx < VV_LEN)
3997 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999 vim_free(varnamebuf);
4000 varnamebuf = NULL;
4001 varnamebuflen = 0;
4002 return NULL;
4003}
4004
4005#endif /* FEAT_CMDL_COMPL */
4006
4007/*
4008 * types for expressions.
4009 */
4010typedef enum
4011{
4012 TYPE_UNKNOWN = 0
4013 , TYPE_EQUAL /* == */
4014 , TYPE_NEQUAL /* != */
4015 , TYPE_GREATER /* > */
4016 , TYPE_GEQUAL /* >= */
4017 , TYPE_SMALLER /* < */
4018 , TYPE_SEQUAL /* <= */
4019 , TYPE_MATCH /* =~ */
4020 , TYPE_NOMATCH /* !~ */
4021} exptype_T;
4022
4023/*
4024 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004025 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4027 */
4028
4029/*
4030 * Handle zero level expression.
4031 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004032 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004033 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 * Return OK or FAIL.
4035 */
4036 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 char_u **nextcmd;
4041 int evaluate;
4042{
4043 int ret;
4044 char_u *p;
4045
4046 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 if (ret == FAIL || !ends_excmd(*p))
4049 {
4050 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004051 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 /*
4053 * Report the invalid expression unless the expression evaluation has
4054 * been cancelled due to an aborting error, an interrupt, or an
4055 * exception.
4056 */
4057 if (!aborting())
4058 EMSG2(_(e_invexpr2), arg);
4059 ret = FAIL;
4060 }
4061 if (nextcmd != NULL)
4062 *nextcmd = check_nextcmd(p);
4063
4064 return ret;
4065}
4066
4067/*
4068 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004069 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 *
4071 * "arg" must point to the first non-white of the expression.
4072 * "arg" is advanced to the next non-white after the recognized expression.
4073 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004074 * Note: "rettv.v_lock" is not set.
4075 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 * Return OK or FAIL.
4077 */
4078 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004079eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 int evaluate;
4083{
4084 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
4087 /*
4088 * Get the first variable.
4089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 return FAIL;
4092
4093 if ((*arg)[0] == '?')
4094 {
4095 result = FALSE;
4096 if (evaluate)
4097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004098 int error = FALSE;
4099
4100 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004103 if (error)
4104 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106
4107 /*
4108 * Get the second variable.
4109 */
4110 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 return FAIL;
4113
4114 /*
4115 * Check for the ":".
4116 */
4117 if ((*arg)[0] != ':')
4118 {
4119 EMSG(_("E109: Missing ':' after '?'"));
4120 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 return FAIL;
4123 }
4124
4125 /*
4126 * Get the third variable.
4127 */
4128 *arg = skipwhite(*arg + 1);
4129 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4130 {
4131 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 return FAIL;
4134 }
4135 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
4138
4139 return OK;
4140}
4141
4142/*
4143 * Handle first level expression:
4144 * expr2 || expr2 || expr2 logical OR
4145 *
4146 * "arg" must point to the first non-white of the expression.
4147 * "arg" is advanced to the next non-white after the recognized expression.
4148 *
4149 * Return OK or FAIL.
4150 */
4151 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 int evaluate;
4156{
Bram Moolenaar33570922005-01-25 22:26:29 +00004157 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 long result;
4159 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004160 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
4162 /*
4163 * Get the first variable.
4164 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004165 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 return FAIL;
4167
4168 /*
4169 * Repeat until there is no following "||".
4170 */
4171 first = TRUE;
4172 result = FALSE;
4173 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4174 {
4175 if (evaluate && first)
4176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004180 if (error)
4181 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 first = FALSE;
4183 }
4184
4185 /*
4186 * Get the second variable.
4187 */
4188 *arg = skipwhite(*arg + 2);
4189 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4190 return FAIL;
4191
4192 /*
4193 * Compute the result.
4194 */
4195 if (evaluate && !result)
4196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004197 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004200 if (error)
4201 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203 if (evaluate)
4204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004205 rettv->v_type = VAR_NUMBER;
4206 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
4208 }
4209
4210 return OK;
4211}
4212
4213/*
4214 * Handle second level expression:
4215 * expr3 && expr3 && expr3 logical AND
4216 *
4217 * "arg" must point to the first non-white of the expression.
4218 * "arg" is advanced to the next non-white after the recognized expression.
4219 *
4220 * Return OK or FAIL.
4221 */
4222 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 int evaluate;
4227{
Bram Moolenaar33570922005-01-25 22:26:29 +00004228 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 long result;
4230 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004231 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
4233 /*
4234 * Get the first variable.
4235 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 return FAIL;
4238
4239 /*
4240 * Repeat until there is no following "&&".
4241 */
4242 first = TRUE;
4243 result = TRUE;
4244 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4245 {
4246 if (evaluate && first)
4247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004248 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004250 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 if (error)
4252 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 first = FALSE;
4254 }
4255
4256 /*
4257 * Get the second variable.
4258 */
4259 *arg = skipwhite(*arg + 2);
4260 if (eval4(arg, &var2, evaluate && result) == FAIL)
4261 return FAIL;
4262
4263 /*
4264 * Compute the result.
4265 */
4266 if (evaluate && result)
4267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 if (error)
4272 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 if (evaluate)
4275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 rettv->v_type = VAR_NUMBER;
4277 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 }
4280
4281 return OK;
4282}
4283
4284/*
4285 * Handle third level expression:
4286 * var1 == var2
4287 * var1 =~ var2
4288 * var1 != var2
4289 * var1 !~ var2
4290 * var1 > var2
4291 * var1 >= var2
4292 * var1 < var2
4293 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004294 * var1 is var2
4295 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 *
4297 * "arg" must point to the first non-white of the expression.
4298 * "arg" is advanced to the next non-white after the recognized expression.
4299 *
4300 * Return OK or FAIL.
4301 */
4302 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004303eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 int evaluate;
4307{
Bram Moolenaar33570922005-01-25 22:26:29 +00004308 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 char_u *p;
4310 int i;
4311 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004312 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 int len = 2;
4314 long n1, n2;
4315 char_u *s1, *s2;
4316 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4317 regmatch_T regmatch;
4318 int ic;
4319 char_u *save_cpo;
4320
4321 /*
4322 * Get the first variable.
4323 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 return FAIL;
4326
4327 p = *arg;
4328 switch (p[0])
4329 {
4330 case '=': if (p[1] == '=')
4331 type = TYPE_EQUAL;
4332 else if (p[1] == '~')
4333 type = TYPE_MATCH;
4334 break;
4335 case '!': if (p[1] == '=')
4336 type = TYPE_NEQUAL;
4337 else if (p[1] == '~')
4338 type = TYPE_NOMATCH;
4339 break;
4340 case '>': if (p[1] != '=')
4341 {
4342 type = TYPE_GREATER;
4343 len = 1;
4344 }
4345 else
4346 type = TYPE_GEQUAL;
4347 break;
4348 case '<': if (p[1] != '=')
4349 {
4350 type = TYPE_SMALLER;
4351 len = 1;
4352 }
4353 else
4354 type = TYPE_SEQUAL;
4355 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004356 case 'i': if (p[1] == 's')
4357 {
4358 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4359 len = 5;
4360 if (!vim_isIDc(p[len]))
4361 {
4362 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4363 type_is = TRUE;
4364 }
4365 }
4366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368
4369 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004370 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 */
4372 if (type != TYPE_UNKNOWN)
4373 {
4374 /* extra question mark appended: ignore case */
4375 if (p[len] == '?')
4376 {
4377 ic = TRUE;
4378 ++len;
4379 }
4380 /* extra '#' appended: match case */
4381 else if (p[len] == '#')
4382 {
4383 ic = FALSE;
4384 ++len;
4385 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004386 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 else
4388 ic = p_ic;
4389
4390 /*
4391 * Get the second variable.
4392 */
4393 *arg = skipwhite(p + len);
4394 if (eval5(arg, &var2, evaluate) == FAIL)
4395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return FAIL;
4398 }
4399
4400 if (evaluate)
4401 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004402 if (type_is && rettv->v_type != var2.v_type)
4403 {
4404 /* For "is" a different type always means FALSE, for "notis"
4405 * it means TRUE. */
4406 n1 = (type == TYPE_NEQUAL);
4407 }
4408 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4409 {
4410 if (type_is)
4411 {
4412 n1 = (rettv->v_type == var2.v_type
4413 && rettv->vval.v_list == var2.vval.v_list);
4414 if (type == TYPE_NEQUAL)
4415 n1 = !n1;
4416 }
4417 else if (rettv->v_type != var2.v_type
4418 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4419 {
4420 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004421 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004422 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004423 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 clear_tv(rettv);
4425 clear_tv(&var2);
4426 return FAIL;
4427 }
4428 else
4429 {
4430 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004431 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4432 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 if (type == TYPE_NEQUAL)
4434 n1 = !n1;
4435 }
4436 }
4437
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004438 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4439 {
4440 if (type_is)
4441 {
4442 n1 = (rettv->v_type == var2.v_type
4443 && rettv->vval.v_dict == var2.vval.v_dict);
4444 if (type == TYPE_NEQUAL)
4445 n1 = !n1;
4446 }
4447 else if (rettv->v_type != var2.v_type
4448 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4449 {
4450 if (rettv->v_type != var2.v_type)
4451 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4452 else
4453 EMSG(_("E736: Invalid operation for Dictionary"));
4454 clear_tv(rettv);
4455 clear_tv(&var2);
4456 return FAIL;
4457 }
4458 else
4459 {
4460 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004461 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4462 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004463 if (type == TYPE_NEQUAL)
4464 n1 = !n1;
4465 }
4466 }
4467
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4469 {
4470 if (rettv->v_type != var2.v_type
4471 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4472 {
4473 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004474 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004475 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004476 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004477 clear_tv(rettv);
4478 clear_tv(&var2);
4479 return FAIL;
4480 }
4481 else
4482 {
4483 /* Compare two Funcrefs for being equal or unequal. */
4484 if (rettv->vval.v_string == NULL
4485 || var2.vval.v_string == NULL)
4486 n1 = FALSE;
4487 else
4488 n1 = STRCMP(rettv->vval.v_string,
4489 var2.vval.v_string) == 0;
4490 if (type == TYPE_NEQUAL)
4491 n1 = !n1;
4492 }
4493 }
4494
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004495#ifdef FEAT_FLOAT
4496 /*
4497 * If one of the two variables is a float, compare as a float.
4498 * When using "=~" or "!~", always compare as string.
4499 */
4500 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4501 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4502 {
4503 float_T f1, f2;
4504
4505 if (rettv->v_type == VAR_FLOAT)
4506 f1 = rettv->vval.v_float;
4507 else
4508 f1 = get_tv_number(rettv);
4509 if (var2.v_type == VAR_FLOAT)
4510 f2 = var2.vval.v_float;
4511 else
4512 f2 = get_tv_number(&var2);
4513 n1 = FALSE;
4514 switch (type)
4515 {
4516 case TYPE_EQUAL: n1 = (f1 == f2); break;
4517 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4518 case TYPE_GREATER: n1 = (f1 > f2); break;
4519 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4520 case TYPE_SMALLER: n1 = (f1 < f2); break;
4521 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4522 case TYPE_UNKNOWN:
4523 case TYPE_MATCH:
4524 case TYPE_NOMATCH: break; /* avoid gcc warning */
4525 }
4526 }
4527#endif
4528
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 /*
4530 * If one of the two variables is a number, compare as a number.
4531 * When using "=~" or "!~", always compare as string.
4532 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004533 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4535 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004536 n1 = get_tv_number(rettv);
4537 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 switch (type)
4539 {
4540 case TYPE_EQUAL: n1 = (n1 == n2); break;
4541 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4542 case TYPE_GREATER: n1 = (n1 > n2); break;
4543 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4544 case TYPE_SMALLER: n1 = (n1 < n2); break;
4545 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4546 case TYPE_UNKNOWN:
4547 case TYPE_MATCH:
4548 case TYPE_NOMATCH: break; /* avoid gcc warning */
4549 }
4550 }
4551 else
4552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004553 s1 = get_tv_string_buf(rettv, buf1);
4554 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4556 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4557 else
4558 i = 0;
4559 n1 = FALSE;
4560 switch (type)
4561 {
4562 case TYPE_EQUAL: n1 = (i == 0); break;
4563 case TYPE_NEQUAL: n1 = (i != 0); break;
4564 case TYPE_GREATER: n1 = (i > 0); break;
4565 case TYPE_GEQUAL: n1 = (i >= 0); break;
4566 case TYPE_SMALLER: n1 = (i < 0); break;
4567 case TYPE_SEQUAL: n1 = (i <= 0); break;
4568
4569 case TYPE_MATCH:
4570 case TYPE_NOMATCH:
4571 /* avoid 'l' flag in 'cpoptions' */
4572 save_cpo = p_cpo;
4573 p_cpo = (char_u *)"";
4574 regmatch.regprog = vim_regcomp(s2,
4575 RE_MAGIC + RE_STRING);
4576 regmatch.rm_ic = ic;
4577 if (regmatch.regprog != NULL)
4578 {
4579 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4580 vim_free(regmatch.regprog);
4581 if (type == TYPE_NOMATCH)
4582 n1 = !n1;
4583 }
4584 p_cpo = save_cpo;
4585 break;
4586
4587 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4588 }
4589 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004590 clear_tv(rettv);
4591 clear_tv(&var2);
4592 rettv->v_type = VAR_NUMBER;
4593 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
4595 }
4596
4597 return OK;
4598}
4599
4600/*
4601 * Handle fourth level expression:
4602 * + number addition
4603 * - number subtraction
4604 * . string concatenation
4605 *
4606 * "arg" must point to the first non-white of the expression.
4607 * "arg" is advanced to the next non-white after the recognized expression.
4608 *
4609 * Return OK or FAIL.
4610 */
4611 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 int evaluate;
4616{
Bram Moolenaar33570922005-01-25 22:26:29 +00004617 typval_T var2;
4618 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 int op;
4620 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004621#ifdef FEAT_FLOAT
4622 float_T f1 = 0, f2 = 0;
4623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 char_u *s1, *s2;
4625 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4626 char_u *p;
4627
4628 /*
4629 * Get the first variable.
4630 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004631 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 return FAIL;
4633
4634 /*
4635 * Repeat computing, until no '+', '-' or '.' is following.
4636 */
4637 for (;;)
4638 {
4639 op = **arg;
4640 if (op != '+' && op != '-' && op != '.')
4641 break;
4642
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004643 if ((op != '+' || rettv->v_type != VAR_LIST)
4644#ifdef FEAT_FLOAT
4645 && (op == '.' || rettv->v_type != VAR_FLOAT)
4646#endif
4647 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004648 {
4649 /* For "list + ...", an illegal use of the first operand as
4650 * a number cannot be determined before evaluating the 2nd
4651 * operand: if this is also a list, all is ok.
4652 * For "something . ...", "something - ..." or "non-list + ...",
4653 * we know that the first operand needs to be a string or number
4654 * without evaluating the 2nd operand. So check before to avoid
4655 * side effects after an error. */
4656 if (evaluate && get_tv_string_chk(rettv) == NULL)
4657 {
4658 clear_tv(rettv);
4659 return FAIL;
4660 }
4661 }
4662
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 /*
4664 * Get the second variable.
4665 */
4666 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004667 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 return FAIL;
4671 }
4672
4673 if (evaluate)
4674 {
4675 /*
4676 * Compute the result.
4677 */
4678 if (op == '.')
4679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004680 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4681 s2 = get_tv_string_buf_chk(&var2, buf2);
4682 if (s2 == NULL) /* type error ? */
4683 {
4684 clear_tv(rettv);
4685 clear_tv(&var2);
4686 return FAIL;
4687 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004688 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004689 clear_tv(rettv);
4690 rettv->v_type = VAR_STRING;
4691 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004693 else if (op == '+' && rettv->v_type == VAR_LIST
4694 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004695 {
4696 /* concatenate Lists */
4697 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4698 &var3) == FAIL)
4699 {
4700 clear_tv(rettv);
4701 clear_tv(&var2);
4702 return FAIL;
4703 }
4704 clear_tv(rettv);
4705 *rettv = var3;
4706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 else
4708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 int error = FALSE;
4710
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711#ifdef FEAT_FLOAT
4712 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004713 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004714 f1 = rettv->vval.v_float;
4715 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004717 else
4718#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004719 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004720 n1 = get_tv_number_chk(rettv, &error);
4721 if (error)
4722 {
4723 /* This can only happen for "list + non-list". For
4724 * "non-list + ..." or "something - ...", we returned
4725 * before evaluating the 2nd operand. */
4726 clear_tv(rettv);
4727 return FAIL;
4728 }
4729#ifdef FEAT_FLOAT
4730 if (var2.v_type == VAR_FLOAT)
4731 f1 = n1;
4732#endif
4733 }
4734#ifdef FEAT_FLOAT
4735 if (var2.v_type == VAR_FLOAT)
4736 {
4737 f2 = var2.vval.v_float;
4738 n2 = 0;
4739 }
4740 else
4741#endif
4742 {
4743 n2 = get_tv_number_chk(&var2, &error);
4744 if (error)
4745 {
4746 clear_tv(rettv);
4747 clear_tv(&var2);
4748 return FAIL;
4749 }
4750#ifdef FEAT_FLOAT
4751 if (rettv->v_type == VAR_FLOAT)
4752 f2 = n2;
4753#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004755 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004756
4757#ifdef FEAT_FLOAT
4758 /* If there is a float on either side the result is a float. */
4759 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4760 {
4761 if (op == '+')
4762 f1 = f1 + f2;
4763 else
4764 f1 = f1 - f2;
4765 rettv->v_type = VAR_FLOAT;
4766 rettv->vval.v_float = f1;
4767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004769#endif
4770 {
4771 if (op == '+')
4772 n1 = n1 + n2;
4773 else
4774 n1 = n1 - n2;
4775 rettv->v_type = VAR_NUMBER;
4776 rettv->vval.v_number = n1;
4777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
4781 }
4782 return OK;
4783}
4784
4785/*
4786 * Handle fifth level expression:
4787 * * number multiplication
4788 * / number division
4789 * % number modulo
4790 *
4791 * "arg" must point to the first non-white of the expression.
4792 * "arg" is advanced to the next non-white after the recognized expression.
4793 *
4794 * Return OK or FAIL.
4795 */
4796 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004797eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004801 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802{
Bram Moolenaar33570922005-01-25 22:26:29 +00004803 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 int op;
4805 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004806#ifdef FEAT_FLOAT
4807 int use_float = FALSE;
4808 float_T f1 = 0, f2;
4809#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812 /*
4813 * Get the first variable.
4814 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004815 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 return FAIL;
4817
4818 /*
4819 * Repeat computing, until no '*', '/' or '%' is following.
4820 */
4821 for (;;)
4822 {
4823 op = **arg;
4824 if (op != '*' && op != '/' && op != '%')
4825 break;
4826
4827 if (evaluate)
4828 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004829#ifdef FEAT_FLOAT
4830 if (rettv->v_type == VAR_FLOAT)
4831 {
4832 f1 = rettv->vval.v_float;
4833 use_float = TRUE;
4834 n1 = 0;
4835 }
4836 else
4837#endif
4838 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004840 if (error)
4841 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 }
4843 else
4844 n1 = 0;
4845
4846 /*
4847 * Get the second variable.
4848 */
4849 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004850 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 return FAIL;
4852
4853 if (evaluate)
4854 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004855#ifdef FEAT_FLOAT
4856 if (var2.v_type == VAR_FLOAT)
4857 {
4858 if (!use_float)
4859 {
4860 f1 = n1;
4861 use_float = TRUE;
4862 }
4863 f2 = var2.vval.v_float;
4864 n2 = 0;
4865 }
4866 else
4867#endif
4868 {
4869 n2 = get_tv_number_chk(&var2, &error);
4870 clear_tv(&var2);
4871 if (error)
4872 return FAIL;
4873#ifdef FEAT_FLOAT
4874 if (use_float)
4875 f2 = n2;
4876#endif
4877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
4879 /*
4880 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883#ifdef FEAT_FLOAT
4884 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004886 if (op == '*')
4887 f1 = f1 * f2;
4888 else if (op == '/')
4889 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004890# ifdef VMS
4891 /* VMS crashes on divide by zero, work around it */
4892 if (f2 == 0.0)
4893 {
4894 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004895 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004896 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004897 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004898 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004899 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004900 }
4901 else
4902 f1 = f1 / f2;
4903# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904 /* We rely on the floating point library to handle divide
4905 * by zero to result in "inf" and not a crash. */
4906 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004907# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004911 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 return FAIL;
4913 }
4914 rettv->v_type = VAR_FLOAT;
4915 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920 if (op == '*')
4921 n1 = n1 * n2;
4922 else if (op == '/')
4923 {
4924 if (n2 == 0) /* give an error message? */
4925 {
4926 if (n1 == 0)
4927 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4928 else if (n1 < 0)
4929 n1 = -0x7fffffffL;
4930 else
4931 n1 = 0x7fffffffL;
4932 }
4933 else
4934 n1 = n1 / n2;
4935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004937 {
4938 if (n2 == 0) /* give an error message? */
4939 n1 = 0;
4940 else
4941 n1 = n1 % n2;
4942 }
4943 rettv->v_type = VAR_NUMBER;
4944 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947 }
4948
4949 return OK;
4950}
4951
4952/*
4953 * Handle sixth level expression:
4954 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004955 * "string" string constant
4956 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 * &option-name option value
4958 * @r register contents
4959 * identifier variable value
4960 * function() function call
4961 * $VAR environment variable
4962 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004963 * [expr, expr] List
4964 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 *
4966 * Also handle:
4967 * ! in front logical NOT
4968 * - in front unary minus
4969 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970 * trailing [] subscript in String or List
4971 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 *
4973 * "arg" must point to the first non-white of the expression.
4974 * "arg" is advanced to the next non-white after the recognized expression.
4975 *
4976 * Return OK or FAIL.
4977 */
4978 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004979eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004983 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 long n;
4986 int len;
4987 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 char_u *start_leader, *end_leader;
4989 int ret = OK;
4990 char_u *alias;
4991
4992 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004993 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004994 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004996 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997
4998 /*
4999 * Skip '!' and '-' characters. They are handled later.
5000 */
5001 start_leader = *arg;
5002 while (**arg == '!' || **arg == '-' || **arg == '+')
5003 *arg = skipwhite(*arg + 1);
5004 end_leader = *arg;
5005
5006 switch (**arg)
5007 {
5008 /*
5009 * Number constant.
5010 */
5011 case '0':
5012 case '1':
5013 case '2':
5014 case '3':
5015 case '4':
5016 case '5':
5017 case '6':
5018 case '7':
5019 case '8':
5020 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005021 {
5022#ifdef FEAT_FLOAT
5023 char_u *p = skipdigits(*arg + 1);
5024 int get_float = FALSE;
5025
5026 /* We accept a float when the format matches
5027 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005028 * strict to avoid backwards compatibility problems.
5029 * Don't look for a float after the "." operator, so that
5030 * ":let vers = 1.2.3" doesn't fail. */
5031 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005033 get_float = TRUE;
5034 p = skipdigits(p + 2);
5035 if (*p == 'e' || *p == 'E')
5036 {
5037 ++p;
5038 if (*p == '-' || *p == '+')
5039 ++p;
5040 if (!vim_isdigit(*p))
5041 get_float = FALSE;
5042 else
5043 p = skipdigits(p + 1);
5044 }
5045 if (ASCII_ISALPHA(*p) || *p == '.')
5046 get_float = FALSE;
5047 }
5048 if (get_float)
5049 {
5050 float_T f;
5051
5052 *arg += string2float(*arg, &f);
5053 if (evaluate)
5054 {
5055 rettv->v_type = VAR_FLOAT;
5056 rettv->vval.v_float = f;
5057 }
5058 }
5059 else
5060#endif
5061 {
5062 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5063 *arg += len;
5064 if (evaluate)
5065 {
5066 rettv->v_type = VAR_NUMBER;
5067 rettv->vval.v_number = n;
5068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 }
5070 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 /*
5074 * String constant: "string".
5075 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 break;
5078
5079 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005083 break;
5084
5085 /*
5086 * List: [expr, expr]
5087 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005088 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 break;
5090
5091 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 * Dictionary: {key: val, key: val}
5093 */
5094 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5095 break;
5096
5097 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005098 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005100 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 break;
5102
5103 /*
5104 * Environment variable: $VAR.
5105 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005106 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 break;
5108
5109 /*
5110 * Register contents: @r.
5111 */
5112 case '@': ++*arg;
5113 if (evaluate)
5114 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005115 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005116 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
5118 if (**arg != NUL)
5119 ++*arg;
5120 break;
5121
5122 /*
5123 * nested expression: (expression).
5124 */
5125 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005126 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (**arg == ')')
5128 ++*arg;
5129 else if (ret == OK)
5130 {
5131 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005132 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 ret = FAIL;
5134 }
5135 break;
5136
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 break;
5139 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140
5141 if (ret == NOTDONE)
5142 {
5143 /*
5144 * Must be a variable or function name.
5145 * Can also be a curly-braces kind of name: {expr}.
5146 */
5147 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005148 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 if (alias != NULL)
5150 s = alias;
5151
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005152 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 ret = FAIL;
5154 else
5155 {
5156 if (**arg == '(') /* recursive! */
5157 {
5158 /* If "s" is the name of a variable of type VAR_FUNC
5159 * use its contents. */
5160 s = deref_func_name(s, &len);
5161
5162 /* Invoke the function. */
5163 ret = get_func_tv(s, len, rettv, arg,
5164 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005165 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 /* Stop the expression evaluation when immediately
5167 * aborting on error, or when an interrupt occurred or
5168 * an exception was thrown but not caught. */
5169 if (aborting())
5170 {
5171 if (ret == OK)
5172 clear_tv(rettv);
5173 ret = FAIL;
5174 }
5175 }
5176 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005177 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005178 else
5179 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005181 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 }
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 *arg = skipwhite(*arg);
5185
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005186 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5187 * expr(expr). */
5188 if (ret == OK)
5189 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190
5191 /*
5192 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5193 */
5194 if (ret == OK && evaluate && end_leader > start_leader)
5195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005196 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005197 int val = 0;
5198#ifdef FEAT_FLOAT
5199 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005200
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005201 if (rettv->v_type == VAR_FLOAT)
5202 f = rettv->vval.v_float;
5203 else
5204#endif
5205 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 clear_tv(rettv);
5209 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211 else
5212 {
5213 while (end_leader > start_leader)
5214 {
5215 --end_leader;
5216 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005217 {
5218#ifdef FEAT_FLOAT
5219 if (rettv->v_type == VAR_FLOAT)
5220 f = !f;
5221 else
5222#endif
5223 val = !val;
5224 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005226 {
5227#ifdef FEAT_FLOAT
5228 if (rettv->v_type == VAR_FLOAT)
5229 f = -f;
5230 else
5231#endif
5232 val = -val;
5233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005234 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005235#ifdef FEAT_FLOAT
5236 if (rettv->v_type == VAR_FLOAT)
5237 {
5238 clear_tv(rettv);
5239 rettv->vval.v_float = f;
5240 }
5241 else
5242#endif
5243 {
5244 clear_tv(rettv);
5245 rettv->v_type = VAR_NUMBER;
5246 rettv->vval.v_number = val;
5247 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250
5251 return ret;
5252}
5253
5254/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005255 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5256 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5258 */
5259 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005260eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005262 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005264 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265{
5266 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 long len = -1;
5270 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005274 if (rettv->v_type == VAR_FUNC
5275#ifdef FEAT_FLOAT
5276 || rettv->v_type == VAR_FLOAT
5277#endif
5278 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005280 if (verbose)
5281 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 return FAIL;
5283 }
5284
Bram Moolenaar8c711452005-01-14 21:53:12 +00005285 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 /*
5288 * dict.name
5289 */
5290 key = *arg + 1;
5291 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5292 ;
5293 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 }
5297 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 /*
5300 * something[idx]
5301 *
5302 * Get the (first) variable from inside the [].
5303 */
5304 *arg = skipwhite(*arg + 1);
5305 if (**arg == ':')
5306 empty1 = TRUE;
5307 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5308 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005309 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5310 {
5311 /* not a number or string */
5312 clear_tv(&var1);
5313 return FAIL;
5314 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315
5316 /*
5317 * Get the second variable from inside the [:].
5318 */
5319 if (**arg == ':')
5320 {
5321 range = TRUE;
5322 *arg = skipwhite(*arg + 1);
5323 if (**arg == ']')
5324 empty2 = TRUE;
5325 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 if (!empty1)
5328 clear_tv(&var1);
5329 return FAIL;
5330 }
5331 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5332 {
5333 /* not a number or string */
5334 if (!empty1)
5335 clear_tv(&var1);
5336 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005337 return FAIL;
5338 }
5339 }
5340
5341 /* Check for the ']'. */
5342 if (**arg != ']')
5343 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005344 if (verbose)
5345 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 clear_tv(&var1);
5347 if (range)
5348 clear_tv(&var2);
5349 return FAIL;
5350 }
5351 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 }
5353
5354 if (evaluate)
5355 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 n1 = 0;
5357 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005359 n1 = get_tv_number(&var1);
5360 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 }
5362 if (range)
5363 {
5364 if (empty2)
5365 n2 = -1;
5366 else
5367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005368 n2 = get_tv_number(&var2);
5369 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 }
5371 }
5372
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005373 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 {
5375 case VAR_NUMBER:
5376 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005377 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 len = (long)STRLEN(s);
5379 if (range)
5380 {
5381 /* The resulting variable is a substring. If the indexes
5382 * are out of range the result is empty. */
5383 if (n1 < 0)
5384 {
5385 n1 = len + n1;
5386 if (n1 < 0)
5387 n1 = 0;
5388 }
5389 if (n2 < 0)
5390 n2 = len + n2;
5391 else if (n2 >= len)
5392 n2 = len;
5393 if (n1 >= len || n2 < 0 || n1 > n2)
5394 s = NULL;
5395 else
5396 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5397 }
5398 else
5399 {
5400 /* The resulting variable is a string of a single
5401 * character. If the index is too big or negative the
5402 * result is empty. */
5403 if (n1 >= len || n1 < 0)
5404 s = NULL;
5405 else
5406 s = vim_strnsave(s + n1, 1);
5407 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005408 clear_tv(rettv);
5409 rettv->v_type = VAR_STRING;
5410 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 break;
5412
5413 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005414 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 if (n1 < 0)
5416 n1 = len + n1;
5417 if (!empty1 && (n1 < 0 || n1 >= len))
5418 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005419 /* For a range we allow invalid values and return an empty
5420 * list. A list index out of range is an error. */
5421 if (!range)
5422 {
5423 if (verbose)
5424 EMSGN(_(e_listidx), n1);
5425 return FAIL;
5426 }
5427 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 }
5429 if (range)
5430 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005431 list_T *l;
5432 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433
5434 if (n2 < 0)
5435 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005436 else if (n2 >= len)
5437 n2 = len - 1;
5438 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005439 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440 l = list_alloc();
5441 if (l == NULL)
5442 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005443 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 n1 <= n2; ++n1)
5445 {
5446 if (list_append_tv(l, &item->li_tv) == FAIL)
5447 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005448 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 return FAIL;
5450 }
5451 item = item->li_next;
5452 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005453 clear_tv(rettv);
5454 rettv->v_type = VAR_LIST;
5455 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005456 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 }
5458 else
5459 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005460 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005461 clear_tv(rettv);
5462 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 }
5464 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005465
5466 case VAR_DICT:
5467 if (range)
5468 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005469 if (verbose)
5470 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005471 if (len == -1)
5472 clear_tv(&var1);
5473 return FAIL;
5474 }
5475 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005476 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005477
5478 if (len == -1)
5479 {
5480 key = get_tv_string(&var1);
5481 if (*key == NUL)
5482 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005483 if (verbose)
5484 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005485 clear_tv(&var1);
5486 return FAIL;
5487 }
5488 }
5489
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005490 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005492 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005493 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005494 if (len == -1)
5495 clear_tv(&var1);
5496 if (item == NULL)
5497 return FAIL;
5498
5499 copy_tv(&item->di_tv, &var1);
5500 clear_tv(rettv);
5501 *rettv = var1;
5502 }
5503 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 }
5505 }
5506
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 return OK;
5508}
5509
5510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 * Get an option value.
5512 * "arg" points to the '&' or '+' before the option name.
5513 * "arg" is advanced to character after the option name.
5514 * Return OK or FAIL.
5515 */
5516 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005519 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 int evaluate;
5521{
5522 char_u *option_end;
5523 long numval;
5524 char_u *stringval;
5525 int opt_type;
5526 int c;
5527 int working = (**arg == '+'); /* has("+option") */
5528 int ret = OK;
5529 int opt_flags;
5530
5531 /*
5532 * Isolate the option name and find its value.
5533 */
5534 option_end = find_option_end(arg, &opt_flags);
5535 if (option_end == NULL)
5536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 EMSG2(_("E112: Option name missing: %s"), *arg);
5539 return FAIL;
5540 }
5541
5542 if (!evaluate)
5543 {
5544 *arg = option_end;
5545 return OK;
5546 }
5547
5548 c = *option_end;
5549 *option_end = NUL;
5550 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
5553 if (opt_type == -3) /* invalid name */
5554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 EMSG2(_("E113: Unknown option: %s"), *arg);
5557 ret = FAIL;
5558 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {
5561 if (opt_type == -2) /* hidden string option */
5562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 rettv->v_type = VAR_STRING;
5564 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566 else if (opt_type == -1) /* hidden number option */
5567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 rettv->v_type = VAR_NUMBER;
5569 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571 else if (opt_type == 1) /* number option */
5572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 rettv->v_type = VAR_NUMBER;
5574 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 }
5576 else /* string option */
5577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578 rettv->v_type = VAR_STRING;
5579 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 }
5581 }
5582 else if (working && (opt_type == -2 || opt_type == -1))
5583 ret = FAIL;
5584
5585 *option_end = c; /* put back for error messages */
5586 *arg = option_end;
5587
5588 return ret;
5589}
5590
5591/*
5592 * Allocate a variable for a string constant.
5593 * Return OK or FAIL.
5594 */
5595 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005596get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 int evaluate;
5600{
5601 char_u *p;
5602 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 int extra = 0;
5604
5605 /*
5606 * Find the end of the string, skipping backslashed characters.
5607 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005608 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
5610 if (*p == '\\' && p[1] != NUL)
5611 {
5612 ++p;
5613 /* A "\<x>" form occupies at least 4 characters, and produces up
5614 * to 6 characters: reserve space for 2 extra */
5615 if (*p == '<')
5616 extra += 2;
5617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619
5620 if (*p != '"')
5621 {
5622 EMSG2(_("E114: Missing quote: %s"), *arg);
5623 return FAIL;
5624 }
5625
5626 /* If only parsing, set *arg and return here */
5627 if (!evaluate)
5628 {
5629 *arg = p + 1;
5630 return OK;
5631 }
5632
5633 /*
5634 * Copy the string into allocated memory, handling backslashed
5635 * characters.
5636 */
5637 name = alloc((unsigned)(p - *arg + extra));
5638 if (name == NULL)
5639 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 rettv->v_type = VAR_STRING;
5641 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
Bram Moolenaar8c711452005-01-14 21:53:12 +00005643 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {
5645 if (*p == '\\')
5646 {
5647 switch (*++p)
5648 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649 case 'b': *name++ = BS; ++p; break;
5650 case 'e': *name++ = ESC; ++p; break;
5651 case 'f': *name++ = FF; ++p; break;
5652 case 'n': *name++ = NL; ++p; break;
5653 case 'r': *name++ = CAR; ++p; break;
5654 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655
5656 case 'X': /* hex: "\x1", "\x12" */
5657 case 'x':
5658 case 'u': /* Unicode: "\u0023" */
5659 case 'U':
5660 if (vim_isxdigit(p[1]))
5661 {
5662 int n, nr;
5663 int c = toupper(*p);
5664
5665 if (c == 'X')
5666 n = 2;
5667 else
5668 n = 4;
5669 nr = 0;
5670 while (--n >= 0 && vim_isxdigit(p[1]))
5671 {
5672 ++p;
5673 nr = (nr << 4) + hex2nr(*p);
5674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676#ifdef FEAT_MBYTE
5677 /* For "\u" store the number according to
5678 * 'encoding'. */
5679 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 else
5682#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 break;
5686
5687 /* octal: "\1", "\12", "\123" */
5688 case '0':
5689 case '1':
5690 case '2':
5691 case '3':
5692 case '4':
5693 case '5':
5694 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 case '7': *name = *p++ - '0';
5696 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 *name = (*name << 3) + *p++ - '0';
5699 if (*p >= '0' && *p <= '7')
5700 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 break;
5704
5705 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 if (extra != 0)
5708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 break;
5711 }
5712 /* FALLTHROUGH */
5713
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 break;
5716 }
5717 }
5718 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 *arg = p + 1;
5724
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 return OK;
5726}
5727
5728/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 * Return OK or FAIL.
5731 */
5732 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005733get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 int evaluate;
5737{
5738 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005739 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005740 int reduce = 0;
5741
5742 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 */
5745 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5746 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005748 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 break;
5751 ++reduce;
5752 ++p;
5753 }
5754 }
5755
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 return FAIL;
5760 }
5761
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 if (!evaluate)
5764 {
5765 *arg = p + 1;
5766 return OK;
5767 }
5768
5769 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005770 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005771 */
5772 str = alloc((unsigned)((p - *arg) - reduce));
5773 if (str == NULL)
5774 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 rettv->v_type = VAR_STRING;
5776 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005777
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 break;
5784 ++p;
5785 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005789 *arg = p + 1;
5790
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005791 return OK;
5792}
5793
5794/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 * Allocate a variable for a List and fill it from "*arg".
5796 * Return OK or FAIL.
5797 */
5798 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005799get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005800 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802 int evaluate;
5803{
Bram Moolenaar33570922005-01-25 22:26:29 +00005804 list_T *l = NULL;
5805 typval_T tv;
5806 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807
5808 if (evaluate)
5809 {
5810 l = list_alloc();
5811 if (l == NULL)
5812 return FAIL;
5813 }
5814
5815 *arg = skipwhite(*arg + 1);
5816 while (**arg != ']' && **arg != NUL)
5817 {
5818 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5819 goto failret;
5820 if (evaluate)
5821 {
5822 item = listitem_alloc();
5823 if (item != NULL)
5824 {
5825 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005826 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827 list_append(l, item);
5828 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005829 else
5830 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831 }
5832
5833 if (**arg == ']')
5834 break;
5835 if (**arg != ',')
5836 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838 goto failret;
5839 }
5840 *arg = skipwhite(*arg + 1);
5841 }
5842
5843 if (**arg != ']')
5844 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005845 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846failret:
5847 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005848 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005849 return FAIL;
5850 }
5851
5852 *arg = skipwhite(*arg + 1);
5853 if (evaluate)
5854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005855 rettv->v_type = VAR_LIST;
5856 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 ++l->lv_refcount;
5858 }
5859
5860 return OK;
5861}
5862
5863/*
5864 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005865 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005866 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005867 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868list_alloc()
5869{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005870 list_T *l;
5871
5872 l = (list_T *)alloc_clear(sizeof(list_T));
5873 if (l != NULL)
5874 {
5875 /* Prepend the list to the list of lists for garbage collection. */
5876 if (first_list != NULL)
5877 first_list->lv_used_prev = l;
5878 l->lv_used_prev = NULL;
5879 l->lv_used_next = first_list;
5880 first_list = l;
5881 }
5882 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883}
5884
5885/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005886 * Allocate an empty list for a return value.
5887 * Returns OK or FAIL.
5888 */
5889 static int
5890rettv_list_alloc(rettv)
5891 typval_T *rettv;
5892{
5893 list_T *l = list_alloc();
5894
5895 if (l == NULL)
5896 return FAIL;
5897
5898 rettv->vval.v_list = l;
5899 rettv->v_type = VAR_LIST;
5900 ++l->lv_refcount;
5901 return OK;
5902}
5903
5904/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905 * Unreference a list: decrement the reference count and free it when it
5906 * becomes zero.
5907 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005908 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005910 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005912 if (l != NULL && --l->lv_refcount <= 0)
5913 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914}
5915
5916/*
5917 * Free a list, including all items it points to.
5918 * Ignores the reference count.
5919 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005920 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005921list_free(l, recurse)
5922 list_T *l;
5923 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924{
Bram Moolenaar33570922005-01-25 22:26:29 +00005925 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005927 /* Remove the list from the list of lists for garbage collection. */
5928 if (l->lv_used_prev == NULL)
5929 first_list = l->lv_used_next;
5930 else
5931 l->lv_used_prev->lv_used_next = l->lv_used_next;
5932 if (l->lv_used_next != NULL)
5933 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5934
Bram Moolenaard9fba312005-06-26 22:34:35 +00005935 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005937 /* Remove the item before deleting it. */
5938 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005939 if (recurse || (item->li_tv.v_type != VAR_LIST
5940 && item->li_tv.v_type != VAR_DICT))
5941 clear_tv(&item->li_tv);
5942 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 }
5944 vim_free(l);
5945}
5946
5947/*
5948 * Allocate a list item.
5949 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005950 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951listitem_alloc()
5952{
Bram Moolenaar33570922005-01-25 22:26:29 +00005953 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954}
5955
5956/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005957 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 */
5959 static void
5960listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005961 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005963 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964 vim_free(item);
5965}
5966
5967/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005968 * Remove a list item from a List and free it. Also clears the value.
5969 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005970 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005971listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 list_T *l;
5973 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005974{
5975 list_remove(l, item, item);
5976 listitem_free(item);
5977}
5978
5979/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 * Get the number of items in a list.
5981 */
5982 static long
5983list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005984 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986 if (l == NULL)
5987 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005988 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989}
5990
5991/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005992 * Return TRUE when two lists have exactly the same values.
5993 */
5994 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005995list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005996 list_T *l1;
5997 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005998 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005999 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006000{
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006003 if (l1 == NULL || l2 == NULL)
6004 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006005 if (l1 == l2)
6006 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006007 if (list_len(l1) != list_len(l2))
6008 return FALSE;
6009
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006010 for (item1 = l1->lv_first, item2 = l2->lv_first;
6011 item1 != NULL && item2 != NULL;
6012 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006013 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 return FALSE;
6015 return item1 == NULL && item2 == NULL;
6016}
6017
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006018#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6019 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006020/*
6021 * Return the dictitem that an entry in a hashtable points to.
6022 */
6023 dictitem_T *
6024dict_lookup(hi)
6025 hashitem_T *hi;
6026{
6027 return HI2DI(hi);
6028}
6029#endif
6030
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006031/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006032 * Return TRUE when two dictionaries have exactly the same key/values.
6033 */
6034 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006035dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 dict_T *d1;
6037 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006038 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006039 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006040{
Bram Moolenaar33570922005-01-25 22:26:29 +00006041 hashitem_T *hi;
6042 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006043 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006044
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006045 if (d1 == NULL || d2 == NULL)
6046 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006047 if (d1 == d2)
6048 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006049 if (dict_len(d1) != dict_len(d2))
6050 return FALSE;
6051
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006052 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006054 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006055 if (!HASHITEM_EMPTY(hi))
6056 {
6057 item2 = dict_find(d2, hi->hi_key, -1);
6058 if (item2 == NULL)
6059 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006060 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006061 return FALSE;
6062 --todo;
6063 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006064 }
6065 return TRUE;
6066}
6067
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006068static int tv_equal_recurse_limit;
6069
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006071 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006072 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006073 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006074 */
6075 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006077 typval_T *tv1;
6078 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006079 int ic; /* ignore case */
6080 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006081{
6082 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006083 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006085 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006086
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006087 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006088 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006089
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006090 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006091 * recursiveness to a limit. We guess they are equal then.
6092 * A fixed limit has the problem of still taking an awful long time.
6093 * Reduce the limit every time running into it. That should work fine for
6094 * deeply linked structures that are not recursively linked and catch
6095 * recursiveness quickly. */
6096 if (!recursive)
6097 tv_equal_recurse_limit = 1000;
6098 if (recursive_cnt >= tv_equal_recurse_limit)
6099 {
6100 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006101 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006103
6104 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006105 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006106 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107 ++recursive_cnt;
6108 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6109 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006110 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006111
6112 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113 ++recursive_cnt;
6114 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6115 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006116 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006117
6118 case VAR_FUNC:
6119 return (tv1->vval.v_string != NULL
6120 && tv2->vval.v_string != NULL
6121 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6122
6123 case VAR_NUMBER:
6124 return tv1->vval.v_number == tv2->vval.v_number;
6125
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006126#ifdef FEAT_FLOAT
6127 case VAR_FLOAT:
6128 return tv1->vval.v_float == tv2->vval.v_float;
6129#endif
6130
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006131 case VAR_STRING:
6132 s1 = get_tv_string_buf(tv1, buf1);
6133 s2 = get_tv_string_buf(tv2, buf2);
6134 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006136
6137 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006138 return TRUE;
6139}
6140
6141/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142 * Locate item with index "n" in list "l" and return it.
6143 * A negative index is counted from the end; -1 is the last item.
6144 * Returns NULL when "n" is out of range.
6145 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006146 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006147list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 long n;
6150{
Bram Moolenaar33570922005-01-25 22:26:29 +00006151 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006152 long idx;
6153
6154 if (l == NULL)
6155 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006156
6157 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006159 n = l->lv_len + n;
6160
6161 /* Check for index out of range. */
6162 if (n < 0 || n >= l->lv_len)
6163 return NULL;
6164
6165 /* When there is a cached index may start search from there. */
6166 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006167 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006168 if (n < l->lv_idx / 2)
6169 {
6170 /* closest to the start of the list */
6171 item = l->lv_first;
6172 idx = 0;
6173 }
6174 else if (n > (l->lv_idx + l->lv_len) / 2)
6175 {
6176 /* closest to the end of the list */
6177 item = l->lv_last;
6178 idx = l->lv_len - 1;
6179 }
6180 else
6181 {
6182 /* closest to the cached index */
6183 item = l->lv_idx_item;
6184 idx = l->lv_idx;
6185 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006186 }
6187 else
6188 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006189 if (n < l->lv_len / 2)
6190 {
6191 /* closest to the start of the list */
6192 item = l->lv_first;
6193 idx = 0;
6194 }
6195 else
6196 {
6197 /* closest to the end of the list */
6198 item = l->lv_last;
6199 idx = l->lv_len - 1;
6200 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006202
6203 while (n > idx)
6204 {
6205 /* search forward */
6206 item = item->li_next;
6207 ++idx;
6208 }
6209 while (n < idx)
6210 {
6211 /* search backward */
6212 item = item->li_prev;
6213 --idx;
6214 }
6215
6216 /* cache the used index */
6217 l->lv_idx = idx;
6218 l->lv_idx_item = item;
6219
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220 return item;
6221}
6222
6223/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006224 * Get list item "l[idx]" as a number.
6225 */
6226 static long
6227list_find_nr(l, idx, errorp)
6228 list_T *l;
6229 long idx;
6230 int *errorp; /* set to TRUE when something wrong */
6231{
6232 listitem_T *li;
6233
6234 li = list_find(l, idx);
6235 if (li == NULL)
6236 {
6237 if (errorp != NULL)
6238 *errorp = TRUE;
6239 return -1L;
6240 }
6241 return get_tv_number_chk(&li->li_tv, errorp);
6242}
6243
6244/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006245 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6246 */
6247 char_u *
6248list_find_str(l, idx)
6249 list_T *l;
6250 long idx;
6251{
6252 listitem_T *li;
6253
6254 li = list_find(l, idx - 1);
6255 if (li == NULL)
6256 {
6257 EMSGN(_(e_listidx), idx);
6258 return NULL;
6259 }
6260 return get_tv_string(&li->li_tv);
6261}
6262
6263/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006264 * Locate "item" list "l" and return its index.
6265 * Returns -1 when "item" is not in the list.
6266 */
6267 static long
6268list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006269 list_T *l;
6270 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006271{
6272 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006273 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006274
6275 if (l == NULL)
6276 return -1;
6277 idx = 0;
6278 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6279 ++idx;
6280 if (li == NULL)
6281 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006282 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006283}
6284
6285/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 * Append item "item" to the end of list "l".
6287 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006288 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006290 list_T *l;
6291 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292{
6293 if (l->lv_last == NULL)
6294 {
6295 /* empty list */
6296 l->lv_first = item;
6297 l->lv_last = item;
6298 item->li_prev = NULL;
6299 }
6300 else
6301 {
6302 l->lv_last->li_next = item;
6303 item->li_prev = l->lv_last;
6304 l->lv_last = item;
6305 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006306 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307 item->li_next = NULL;
6308}
6309
6310/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006312 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006314 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 list_T *l;
6317 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006319 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320
Bram Moolenaar05159a02005-02-26 23:04:13 +00006321 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006323 copy_tv(tv, &li->li_tv);
6324 list_append(l, li);
6325 return OK;
6326}
6327
6328/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006329 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006330 * Return FAIL when out of memory.
6331 */
6332 int
6333list_append_dict(list, dict)
6334 list_T *list;
6335 dict_T *dict;
6336{
6337 listitem_T *li = listitem_alloc();
6338
6339 if (li == NULL)
6340 return FAIL;
6341 li->li_tv.v_type = VAR_DICT;
6342 li->li_tv.v_lock = 0;
6343 li->li_tv.vval.v_dict = dict;
6344 list_append(list, li);
6345 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346 return OK;
6347}
6348
6349/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006350 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006351 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006352 * Returns FAIL when out of memory.
6353 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006354 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006355list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006356 list_T *l;
6357 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006358 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006359{
6360 listitem_T *li = listitem_alloc();
6361
6362 if (li == NULL)
6363 return FAIL;
6364 list_append(l, li);
6365 li->li_tv.v_type = VAR_STRING;
6366 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006367 if (str == NULL)
6368 li->li_tv.vval.v_string = NULL;
6369 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006370 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006371 return FAIL;
6372 return OK;
6373}
6374
6375/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006376 * Append "n" to list "l".
6377 * Returns FAIL when out of memory.
6378 */
6379 static int
6380list_append_number(l, n)
6381 list_T *l;
6382 varnumber_T n;
6383{
6384 listitem_T *li;
6385
6386 li = listitem_alloc();
6387 if (li == NULL)
6388 return FAIL;
6389 li->li_tv.v_type = VAR_NUMBER;
6390 li->li_tv.v_lock = 0;
6391 li->li_tv.vval.v_number = n;
6392 list_append(l, li);
6393 return OK;
6394}
6395
6396/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006398 * If "item" is NULL append at the end.
6399 * Return FAIL when out of memory.
6400 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006401 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 list_T *l;
6404 typval_T *tv;
6405 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406{
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006408
6409 if (ni == NULL)
6410 return FAIL;
6411 copy_tv(tv, &ni->li_tv);
6412 if (item == NULL)
6413 /* Append new item at end of list. */
6414 list_append(l, ni);
6415 else
6416 {
6417 /* Insert new item before existing item. */
6418 ni->li_prev = item->li_prev;
6419 ni->li_next = item;
6420 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006421 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006423 ++l->lv_idx;
6424 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006426 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006428 l->lv_idx_item = NULL;
6429 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006430 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006431 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432 }
6433 return OK;
6434}
6435
6436/*
6437 * Extend "l1" with "l2".
6438 * If "bef" is NULL append at the end, otherwise insert before this item.
6439 * Returns FAIL when out of memory.
6440 */
6441 static int
6442list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 list_T *l1;
6444 list_T *l2;
6445 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446{
Bram Moolenaar33570922005-01-25 22:26:29 +00006447 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006448 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006449
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006450 /* We also quit the loop when we have inserted the original item count of
6451 * the list, avoid a hang when we extend a list with itself. */
6452 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6454 return FAIL;
6455 return OK;
6456}
6457
6458/*
6459 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6460 * Return FAIL when out of memory.
6461 */
6462 static int
6463list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006464 list_T *l1;
6465 list_T *l2;
6466 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006467{
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006470 if (l1 == NULL || l2 == NULL)
6471 return FAIL;
6472
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006473 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006474 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475 if (l == NULL)
6476 return FAIL;
6477 tv->v_type = VAR_LIST;
6478 tv->vval.v_list = l;
6479
6480 /* append all items from the second list */
6481 return list_extend(l, l2, NULL);
6482}
6483
6484/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006486 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006487 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488 * Returns NULL when out of memory.
6489 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006490 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006491list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006492 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006494 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495{
Bram Moolenaar33570922005-01-25 22:26:29 +00006496 list_T *copy;
6497 listitem_T *item;
6498 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499
6500 if (orig == NULL)
6501 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502
6503 copy = list_alloc();
6504 if (copy != NULL)
6505 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006506 if (copyID != 0)
6507 {
6508 /* Do this before adding the items, because one of the items may
6509 * refer back to this list. */
6510 orig->lv_copyID = copyID;
6511 orig->lv_copylist = copy;
6512 }
6513 for (item = orig->lv_first; item != NULL && !got_int;
6514 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515 {
6516 ni = listitem_alloc();
6517 if (ni == NULL)
6518 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006519 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006520 {
6521 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6522 {
6523 vim_free(ni);
6524 break;
6525 }
6526 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006528 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006529 list_append(copy, ni);
6530 }
6531 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006532 if (item != NULL)
6533 {
6534 list_unref(copy);
6535 copy = NULL;
6536 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 }
6538
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006539 return copy;
6540}
6541
6542/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006544 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006545 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006546 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006547list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006548 list_T *l;
6549 listitem_T *item;
6550 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551{
Bram Moolenaar33570922005-01-25 22:26:29 +00006552 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006553
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 /* notify watchers */
6555 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006557 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558 list_fix_watch(l, ip);
6559 if (ip == item2)
6560 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006561 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562
6563 if (item2->li_next == NULL)
6564 l->lv_last = item->li_prev;
6565 else
6566 item2->li_next->li_prev = item->li_prev;
6567 if (item->li_prev == NULL)
6568 l->lv_first = item2->li_next;
6569 else
6570 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006571 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572}
6573
6574/*
6575 * Return an allocated string with the string representation of a list.
6576 * May return NULL.
6577 */
6578 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006579list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006581 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582{
6583 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584
6585 if (tv->vval.v_list == NULL)
6586 return NULL;
6587 ga_init2(&ga, (int)sizeof(char), 80);
6588 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006589 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006590 {
6591 vim_free(ga.ga_data);
6592 return NULL;
6593 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594 ga_append(&ga, ']');
6595 ga_append(&ga, NUL);
6596 return (char_u *)ga.ga_data;
6597}
6598
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006599typedef struct join_S {
6600 char_u *s;
6601 char_u *tofree;
6602} join_T;
6603
6604 static int
6605list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6606 garray_T *gap; /* to store the result in */
6607 list_T *l;
6608 char_u *sep;
6609 int echo_style;
6610 int copyID;
6611 garray_T *join_gap; /* to keep each list item string */
6612{
6613 int i;
6614 join_T *p;
6615 int len;
6616 int sumlen = 0;
6617 int first = TRUE;
6618 char_u *tofree;
6619 char_u numbuf[NUMBUFLEN];
6620 listitem_T *item;
6621 char_u *s;
6622
6623 /* Stringify each item in the list. */
6624 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6625 {
6626 if (echo_style)
6627 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6628 else
6629 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6630 if (s == NULL)
6631 return FAIL;
6632
6633 len = (int)STRLEN(s);
6634 sumlen += len;
6635
6636 ga_grow(join_gap, 1);
6637 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6638 if (tofree != NULL || s != numbuf)
6639 {
6640 p->s = s;
6641 p->tofree = tofree;
6642 }
6643 else
6644 {
6645 p->s = vim_strnsave(s, len);
6646 p->tofree = p->s;
6647 }
6648
6649 line_breakcheck();
6650 }
6651
6652 /* Allocate result buffer with its total size, avoid re-allocation and
6653 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6654 if (join_gap->ga_len >= 2)
6655 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6656 if (ga_grow(gap, sumlen + 2) == FAIL)
6657 return FAIL;
6658
6659 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6660 {
6661 if (first)
6662 first = FALSE;
6663 else
6664 ga_concat(gap, sep);
6665 p = ((join_T *)join_gap->ga_data) + i;
6666
6667 if (p->s != NULL)
6668 ga_concat(gap, p->s);
6669 line_breakcheck();
6670 }
6671
6672 return OK;
6673}
6674
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006676 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006677 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006678 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006679 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006680 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006681list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006682 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006683 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006684 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006685 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006686 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006687{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006688 garray_T join_ga;
6689 int retval;
6690 join_T *p;
6691 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006692
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006693 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6694 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6695
6696 /* Dispose each item in join_ga. */
6697 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006698 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006699 p = (join_T *)join_ga.ga_data;
6700 for (i = 0; i < join_ga.ga_len; ++i)
6701 {
6702 vim_free(p->tofree);
6703 ++p;
6704 }
6705 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006706 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006707
6708 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709}
6710
6711/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006712 * Garbage collection for lists and dictionaries.
6713 *
6714 * We use reference counts to be able to free most items right away when they
6715 * are no longer used. But for composite items it's possible that it becomes
6716 * unused while the reference count is > 0: When there is a recursive
6717 * reference. Example:
6718 * :let l = [1, 2, 3]
6719 * :let d = {9: l}
6720 * :let l[1] = d
6721 *
6722 * Since this is quite unusual we handle this with garbage collection: every
6723 * once in a while find out which lists and dicts are not referenced from any
6724 * variable.
6725 *
6726 * Here is a good reference text about garbage collection (refers to Python
6727 * but it applies to all reference-counting mechanisms):
6728 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006729 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006730
6731/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006732 * Do garbage collection for lists and dicts.
6733 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006735 int
6736garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006737{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006738 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006739 buf_T *buf;
6740 win_T *wp;
6741 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006742 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006743 int did_free;
6744 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006745#ifdef FEAT_WINDOWS
6746 tabpage_T *tp;
6747#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006748
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006749 /* Only do this once. */
6750 want_garbage_collect = FALSE;
6751 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006752 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006753
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006754 /* We advance by two because we add one for items referenced through
6755 * previous_funccal. */
6756 current_copyID += COPYID_INC;
6757 copyID = current_copyID;
6758
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006759 /*
6760 * 1. Go through all accessible variables and mark all lists and dicts
6761 * with copyID.
6762 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006763
6764 /* Don't free variables in the previous_funccal list unless they are only
6765 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006766 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006767 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6768 {
6769 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6770 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6771 }
6772
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006773 /* script-local variables */
6774 for (i = 1; i <= ga_scripts.ga_len; ++i)
6775 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6776
6777 /* buffer-local variables */
6778 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6779 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6780
6781 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006782 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006783 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6784
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006785#ifdef FEAT_WINDOWS
6786 /* tabpage-local variables */
6787 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6788 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6789#endif
6790
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006791 /* global variables */
6792 set_ref_in_ht(&globvarht, copyID);
6793
6794 /* function-local variables */
6795 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6796 {
6797 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6798 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6799 }
6800
Bram Moolenaard812df62008-11-09 12:46:09 +00006801 /* v: vars */
6802 set_ref_in_ht(&vimvarht, copyID);
6803
Bram Moolenaar1dced572012-04-05 16:54:08 +02006804#ifdef FEAT_LUA
6805 set_ref_in_lua(copyID);
6806#endif
6807
Bram Moolenaardb913952012-06-29 12:54:53 +02006808#ifdef FEAT_PYTHON
6809 set_ref_in_python(copyID);
6810#endif
6811
6812#ifdef FEAT_PYTHON3
6813 set_ref_in_python3(copyID);
6814#endif
6815
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006816 /*
6817 * 2. Free lists and dictionaries that are not referenced.
6818 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006819 did_free = free_unref_items(copyID);
6820
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006821 /*
6822 * 3. Check if any funccal can be freed now.
6823 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006824 for (pfc = &previous_funccal; *pfc != NULL; )
6825 {
6826 if (can_free_funccal(*pfc, copyID))
6827 {
6828 fc = *pfc;
6829 *pfc = fc->caller;
6830 free_funccal(fc, TRUE);
6831 did_free = TRUE;
6832 did_free_funccal = TRUE;
6833 }
6834 else
6835 pfc = &(*pfc)->caller;
6836 }
6837 if (did_free_funccal)
6838 /* When a funccal was freed some more items might be garbage
6839 * collected, so run again. */
6840 (void)garbage_collect();
6841
6842 return did_free;
6843}
6844
6845/*
6846 * Free lists and dictionaries that are no longer referenced.
6847 */
6848 static int
6849free_unref_items(copyID)
6850 int copyID;
6851{
6852 dict_T *dd;
6853 list_T *ll;
6854 int did_free = FALSE;
6855
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 */
6859 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006862 /* Free the Dictionary and ordinary items it contains, but don't
6863 * recurse into Lists and Dictionaries, they will be in the list
6864 * of dicts or list of lists. */
6865 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 did_free = TRUE;
6867
6868 /* restart, next dict may also have been freed */
6869 dd = first_dict;
6870 }
6871 else
6872 dd = dd->dv_used_next;
6873
6874 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 * Go through the list of lists and free items without the copyID.
6876 * But don't free a list that has a watcher (used in a for loop), these
6877 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878 */
6879 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006880 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6881 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006882 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006883 /* Free the List and ordinary items it contains, but don't recurse
6884 * into Lists and Dictionaries, they will be in the list of dicts
6885 * or list of lists. */
6886 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 did_free = TRUE;
6888
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006889 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 ll = first_list;
6891 }
6892 else
6893 ll = ll->lv_used_next;
6894
6895 return did_free;
6896}
6897
6898/*
6899 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6900 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006901 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902set_ref_in_ht(ht, copyID)
6903 hashtab_T *ht;
6904 int copyID;
6905{
6906 int todo;
6907 hashitem_T *hi;
6908
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006909 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910 for (hi = ht->ht_array; todo > 0; ++hi)
6911 if (!HASHITEM_EMPTY(hi))
6912 {
6913 --todo;
6914 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6915 }
6916}
6917
6918/*
6919 * Mark all lists and dicts referenced through list "l" with "copyID".
6920 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006921 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922set_ref_in_list(l, copyID)
6923 list_T *l;
6924 int copyID;
6925{
6926 listitem_T *li;
6927
6928 for (li = l->lv_first; li != NULL; li = li->li_next)
6929 set_ref_in_item(&li->li_tv, copyID);
6930}
6931
6932/*
6933 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6934 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006935 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936set_ref_in_item(tv, copyID)
6937 typval_T *tv;
6938 int copyID;
6939{
6940 dict_T *dd;
6941 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006942
6943 switch (tv->v_type)
6944 {
6945 case VAR_DICT:
6946 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006947 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006948 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006949 /* Didn't see this dict yet. */
6950 dd->dv_copyID = copyID;
6951 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006952 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006953 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006954
6955 case VAR_LIST:
6956 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006957 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006958 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959 /* Didn't see this list yet. */
6960 ll->lv_copyID = copyID;
6961 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006962 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006964 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006965 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006966}
6967
6968/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969 * Allocate an empty header for a dictionary.
6970 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006971 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006972dict_alloc()
6973{
Bram Moolenaar33570922005-01-25 22:26:29 +00006974 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006975
Bram Moolenaar33570922005-01-25 22:26:29 +00006976 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006977 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006978 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006979 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 if (first_dict != NULL)
6981 first_dict->dv_used_prev = d;
6982 d->dv_used_next = first_dict;
6983 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006984 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985
Bram Moolenaar33570922005-01-25 22:26:29 +00006986 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006987 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006988 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006989 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006990 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006991 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006992 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006993}
6994
6995/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006996 * Allocate an empty dict for a return value.
6997 * Returns OK or FAIL.
6998 */
6999 static int
7000rettv_dict_alloc(rettv)
7001 typval_T *rettv;
7002{
7003 dict_T *d = dict_alloc();
7004
7005 if (d == NULL)
7006 return FAIL;
7007
7008 rettv->vval.v_dict = d;
7009 rettv->v_type = VAR_DICT;
7010 ++d->dv_refcount;
7011 return OK;
7012}
7013
7014
7015/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007016 * Unreference a Dictionary: decrement the reference count and free it when it
7017 * becomes zero.
7018 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007019 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007020dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007021 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007023 if (d != NULL && --d->dv_refcount <= 0)
7024 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007025}
7026
7027/*
7028 * Free a Dictionary, including all items it contains.
7029 * Ignores the reference count.
7030 */
7031 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007032dict_free(d, recurse)
7033 dict_T *d;
7034 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007035{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007036 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007037 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007038 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007039
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040 /* Remove the dict from the list of dicts for garbage collection. */
7041 if (d->dv_used_prev == NULL)
7042 first_dict = d->dv_used_next;
7043 else
7044 d->dv_used_prev->dv_used_next = d->dv_used_next;
7045 if (d->dv_used_next != NULL)
7046 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7047
7048 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007049 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007050 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007051 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007053 if (!HASHITEM_EMPTY(hi))
7054 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007055 /* Remove the item before deleting it, just in case there is
7056 * something recursive causing trouble. */
7057 di = HI2DI(hi);
7058 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007059 if (recurse || (di->di_tv.v_type != VAR_LIST
7060 && di->di_tv.v_type != VAR_DICT))
7061 clear_tv(&di->di_tv);
7062 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007063 --todo;
7064 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007065 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007066 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007067 vim_free(d);
7068}
7069
7070/*
7071 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007072 * The "key" is copied to the new item.
7073 * Note that the value of the item "di_tv" still needs to be initialized!
7074 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007075 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007076 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007077dictitem_alloc(key)
7078 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007079{
Bram Moolenaar33570922005-01-25 22:26:29 +00007080 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007081
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007082 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007083 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007084 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007086 di->di_flags = 0;
7087 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007088 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007089}
7090
7091/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007092 * Make a copy of a Dictionary item.
7093 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007094 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007095dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007096 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007097{
Bram Moolenaar33570922005-01-25 22:26:29 +00007098 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007099
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007100 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7101 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007102 if (di != NULL)
7103 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007104 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007105 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007106 copy_tv(&org->di_tv, &di->di_tv);
7107 }
7108 return di;
7109}
7110
7111/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 * Remove item "item" from Dictionary "dict" and free it.
7113 */
7114 static void
7115dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 dict_T *dict;
7117 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007118{
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122 if (HASHITEM_EMPTY(hi))
7123 EMSG2(_(e_intern2), "dictitem_remove()");
7124 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007126 dictitem_free(item);
7127}
7128
7129/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130 * Free a dict item. Also clears the value.
7131 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007132 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007134 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007135{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007136 clear_tv(&item->di_tv);
7137 vim_free(item);
7138}
7139
7140/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7142 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007143 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144 * Returns NULL when out of memory.
7145 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007146 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007147dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007149 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007150 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007151{
Bram Moolenaar33570922005-01-25 22:26:29 +00007152 dict_T *copy;
7153 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007154 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007156
7157 if (orig == NULL)
7158 return NULL;
7159
7160 copy = dict_alloc();
7161 if (copy != NULL)
7162 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007163 if (copyID != 0)
7164 {
7165 orig->dv_copyID = copyID;
7166 orig->dv_copydict = copy;
7167 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007168 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007169 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007171 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007172 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007173 --todo;
7174
7175 di = dictitem_alloc(hi->hi_key);
7176 if (di == NULL)
7177 break;
7178 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007179 {
7180 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7181 copyID) == FAIL)
7182 {
7183 vim_free(di);
7184 break;
7185 }
7186 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007187 else
7188 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7189 if (dict_add(copy, di) == FAIL)
7190 {
7191 dictitem_free(di);
7192 break;
7193 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007195 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196
Bram Moolenaare9a41262005-01-15 22:18:47 +00007197 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 if (todo > 0)
7199 {
7200 dict_unref(copy);
7201 copy = NULL;
7202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 }
7204
7205 return copy;
7206}
7207
7208/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007210 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007212 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 dict_T *d;
7215 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007216{
Bram Moolenaar33570922005-01-25 22:26:29 +00007217 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218}
7219
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007221 * Add a number or string entry to dictionary "d".
7222 * When "str" is NULL use number "nr", otherwise use "str".
7223 * Returns FAIL when out of memory and when key already exists.
7224 */
7225 int
7226dict_add_nr_str(d, key, nr, str)
7227 dict_T *d;
7228 char *key;
7229 long nr;
7230 char_u *str;
7231{
7232 dictitem_T *item;
7233
7234 item = dictitem_alloc((char_u *)key);
7235 if (item == NULL)
7236 return FAIL;
7237 item->di_tv.v_lock = 0;
7238 if (str == NULL)
7239 {
7240 item->di_tv.v_type = VAR_NUMBER;
7241 item->di_tv.vval.v_number = nr;
7242 }
7243 else
7244 {
7245 item->di_tv.v_type = VAR_STRING;
7246 item->di_tv.vval.v_string = vim_strsave(str);
7247 }
7248 if (dict_add(d, item) == FAIL)
7249 {
7250 dictitem_free(item);
7251 return FAIL;
7252 }
7253 return OK;
7254}
7255
7256/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007257 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007258 * Returns FAIL when out of memory and when key already exists.
7259 */
7260 int
7261dict_add_list(d, key, list)
7262 dict_T *d;
7263 char *key;
7264 list_T *list;
7265{
7266 dictitem_T *item;
7267
7268 item = dictitem_alloc((char_u *)key);
7269 if (item == NULL)
7270 return FAIL;
7271 item->di_tv.v_lock = 0;
7272 item->di_tv.v_type = VAR_LIST;
7273 item->di_tv.vval.v_list = list;
7274 if (dict_add(d, item) == FAIL)
7275 {
7276 dictitem_free(item);
7277 return FAIL;
7278 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007279 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007280 return OK;
7281}
7282
7283/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007284 * Get the number of items in a Dictionary.
7285 */
7286 static long
7287dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007289{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290 if (d == NULL)
7291 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007292 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293}
7294
7295/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296 * Find item "key[len]" in Dictionary "d".
7297 * If "len" is negative use strlen(key).
7298 * Returns NULL when not found.
7299 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007300 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007303 char_u *key;
7304 int len;
7305{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306#define AKEYLEN 200
7307 char_u buf[AKEYLEN];
7308 char_u *akey;
7309 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312 if (len < 0)
7313 akey = key;
7314 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007315 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 tofree = akey = vim_strnsave(key, len);
7317 if (akey == NULL)
7318 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007319 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 else
7321 {
7322 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007323 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 akey = buf;
7325 }
7326
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 vim_free(tofree);
7329 if (HASHITEM_EMPTY(hi))
7330 return NULL;
7331 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332}
7333
7334/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007335 * Get a string item from a dictionary.
7336 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007337 * Returns NULL if the entry doesn't exist or out of memory.
7338 */
7339 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007340get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007341 dict_T *d;
7342 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007343 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007344{
7345 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007346 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007347
7348 di = dict_find(d, key, -1);
7349 if (di == NULL)
7350 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007351 s = get_tv_string(&di->di_tv);
7352 if (save && s != NULL)
7353 s = vim_strsave(s);
7354 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007355}
7356
7357/*
7358 * Get a number item from a dictionary.
7359 * Returns 0 if the entry doesn't exist or out of memory.
7360 */
7361 long
7362get_dict_number(d, key)
7363 dict_T *d;
7364 char_u *key;
7365{
7366 dictitem_T *di;
7367
7368 di = dict_find(d, key, -1);
7369 if (di == NULL)
7370 return 0;
7371 return get_tv_number(&di->di_tv);
7372}
7373
7374/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375 * Return an allocated string with the string representation of a Dictionary.
7376 * May return NULL.
7377 */
7378 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007379dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007381 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382{
7383 garray_T ga;
7384 int first = TRUE;
7385 char_u *tofree;
7386 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007390 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393 return NULL;
7394 ga_init2(&ga, (int)sizeof(char), 80);
7395 ga_append(&ga, '{');
7396
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007397 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007398 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402 --todo;
7403
7404 if (first)
7405 first = FALSE;
7406 else
7407 ga_concat(&ga, (char_u *)", ");
7408
7409 tofree = string_quote(hi->hi_key, FALSE);
7410 if (tofree != NULL)
7411 {
7412 ga_concat(&ga, tofree);
7413 vim_free(tofree);
7414 }
7415 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007416 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007417 if (s != NULL)
7418 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007419 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007420 if (s == NULL)
7421 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007424 if (todo > 0)
7425 {
7426 vim_free(ga.ga_data);
7427 return NULL;
7428 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429
7430 ga_append(&ga, '}');
7431 ga_append(&ga, NUL);
7432 return (char_u *)ga.ga_data;
7433}
7434
7435/*
7436 * Allocate a variable for a Dictionary and fill it from "*arg".
7437 * Return OK or FAIL. Returns NOTDONE for {expr}.
7438 */
7439 static int
7440get_dict_tv(arg, rettv, evaluate)
7441 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443 int evaluate;
7444{
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 dict_T *d = NULL;
7446 typval_T tvkey;
7447 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007448 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007450 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007451 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452
7453 /*
7454 * First check if it's not a curly-braces thing: {expr}.
7455 * Must do this without evaluating, otherwise a function may be called
7456 * twice. Unfortunately this means we need to call eval1() twice for the
7457 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007458 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007459 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007460 if (*start != '}')
7461 {
7462 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7463 return FAIL;
7464 if (*start == '}')
7465 return NOTDONE;
7466 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467
7468 if (evaluate)
7469 {
7470 d = dict_alloc();
7471 if (d == NULL)
7472 return FAIL;
7473 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007474 tvkey.v_type = VAR_UNKNOWN;
7475 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476
7477 *arg = skipwhite(*arg + 1);
7478 while (**arg != '}' && **arg != NUL)
7479 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007480 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481 goto failret;
7482 if (**arg != ':')
7483 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007484 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007485 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 goto failret;
7487 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007488 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007490 key = get_tv_string_buf_chk(&tvkey, buf);
7491 if (key == NULL || *key == NUL)
7492 {
7493 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7494 if (key != NULL)
7495 EMSG(_(e_emptykey));
7496 clear_tv(&tvkey);
7497 goto failret;
7498 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500
7501 *arg = skipwhite(*arg + 1);
7502 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7503 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007504 if (evaluate)
7505 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506 goto failret;
7507 }
7508 if (evaluate)
7509 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511 if (item != NULL)
7512 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007513 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 clear_tv(&tv);
7516 goto failret;
7517 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 item = dictitem_alloc(key);
7519 clear_tv(&tvkey);
7520 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007523 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007524 if (dict_add(d, item) == FAIL)
7525 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007526 }
7527 }
7528
7529 if (**arg == '}')
7530 break;
7531 if (**arg != ',')
7532 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007533 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 goto failret;
7535 }
7536 *arg = skipwhite(*arg + 1);
7537 }
7538
7539 if (**arg != '}')
7540 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007541 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542failret:
7543 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007544 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 return FAIL;
7546 }
7547
7548 *arg = skipwhite(*arg + 1);
7549 if (evaluate)
7550 {
7551 rettv->v_type = VAR_DICT;
7552 rettv->vval.v_dict = d;
7553 ++d->dv_refcount;
7554 }
7555
7556 return OK;
7557}
7558
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007560 * Return a string with the string representation of a variable.
7561 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007562 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007563 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007564 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007565 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007566 */
7567 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007568echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007570 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007571 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007572 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007573{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574 static int recurse = 0;
7575 char_u *r = NULL;
7576
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007578 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007579 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007580 *tofree = NULL;
7581 return NULL;
7582 }
7583 ++recurse;
7584
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007585 switch (tv->v_type)
7586 {
7587 case VAR_FUNC:
7588 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 r = tv->vval.v_string;
7590 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007591
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007592 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007593 if (tv->vval.v_list == NULL)
7594 {
7595 *tofree = NULL;
7596 r = NULL;
7597 }
7598 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7599 {
7600 *tofree = NULL;
7601 r = (char_u *)"[...]";
7602 }
7603 else
7604 {
7605 tv->vval.v_list->lv_copyID = copyID;
7606 *tofree = list2string(tv, copyID);
7607 r = *tofree;
7608 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007609 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007610
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007612 if (tv->vval.v_dict == NULL)
7613 {
7614 *tofree = NULL;
7615 r = NULL;
7616 }
7617 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7618 {
7619 *tofree = NULL;
7620 r = (char_u *)"{...}";
7621 }
7622 else
7623 {
7624 tv->vval.v_dict->dv_copyID = copyID;
7625 *tofree = dict2string(tv, copyID);
7626 r = *tofree;
7627 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007628 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007629
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007630 case VAR_STRING:
7631 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007632 *tofree = NULL;
7633 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007634 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007635
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007636#ifdef FEAT_FLOAT
7637 case VAR_FLOAT:
7638 *tofree = NULL;
7639 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7640 r = numbuf;
7641 break;
7642#endif
7643
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007644 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007645 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007646 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007647 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648
7649 --recurse;
7650 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007651}
7652
7653/*
7654 * Return a string with the string representation of a variable.
7655 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7656 * "numbuf" is used for a number.
7657 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007658 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007659 */
7660 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007661tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007663 char_u **tofree;
7664 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007665 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007666{
7667 switch (tv->v_type)
7668 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007669 case VAR_FUNC:
7670 *tofree = string_quote(tv->vval.v_string, TRUE);
7671 return *tofree;
7672 case VAR_STRING:
7673 *tofree = string_quote(tv->vval.v_string, FALSE);
7674 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007675#ifdef FEAT_FLOAT
7676 case VAR_FLOAT:
7677 *tofree = NULL;
7678 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7679 return numbuf;
7680#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007681 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007682 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007684 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007685 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007687 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007688 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007689}
7690
7691/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 * Return string "str" in ' quotes, doubling ' characters.
7693 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007695 */
7696 static char_u *
7697string_quote(str, function)
7698 char_u *str;
7699 int function;
7700{
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007702 char_u *p, *r, *s;
7703
Bram Moolenaar33570922005-01-25 22:26:29 +00007704 len = (function ? 13 : 3);
7705 if (str != NULL)
7706 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007707 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007708 for (p = str; *p != NUL; mb_ptr_adv(p))
7709 if (*p == '\'')
7710 ++len;
7711 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007712 s = r = alloc(len);
7713 if (r != NULL)
7714 {
7715 if (function)
7716 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007718 r += 10;
7719 }
7720 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007722 if (str != NULL)
7723 for (p = str; *p != NUL; )
7724 {
7725 if (*p == '\'')
7726 *r++ = '\'';
7727 MB_COPY_CHAR(p, r);
7728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007730 if (function)
7731 *r++ = ')';
7732 *r++ = NUL;
7733 }
7734 return s;
7735}
7736
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007737#ifdef FEAT_FLOAT
7738/*
7739 * Convert the string "text" to a floating point number.
7740 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7741 * this always uses a decimal point.
7742 * Returns the length of the text that was consumed.
7743 */
7744 static int
7745string2float(text, value)
7746 char_u *text;
7747 float_T *value; /* result stored here */
7748{
7749 char *s = (char *)text;
7750 float_T f;
7751
7752 f = strtod(s, &s);
7753 *value = f;
7754 return (int)((char_u *)s - text);
7755}
7756#endif
7757
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 * Get the value of an environment variable.
7760 * "arg" is pointing to the '$'. It is advanced to after the name.
7761 * If the environment variable was not set, silently assume it is empty.
7762 * Always return OK.
7763 */
7764 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 int evaluate;
7769{
7770 char_u *string = NULL;
7771 int len;
7772 int cc;
7773 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007774 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775
7776 ++*arg;
7777 name = *arg;
7778 len = get_env_len(arg);
7779 if (evaluate)
7780 {
7781 if (len != 0)
7782 {
7783 cc = name[len];
7784 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007785 /* first try vim_getenv(), fast for normal environment vars */
7786 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007788 {
7789 if (!mustfree)
7790 string = vim_strsave(string);
7791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 else
7793 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007794 if (mustfree)
7795 vim_free(string);
7796
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 /* next try expanding things like $VIM and ${HOME} */
7798 string = expand_env_save(name - 1);
7799 if (string != NULL && *string == '$')
7800 {
7801 vim_free(string);
7802 string = NULL;
7803 }
7804 }
7805 name[len] = cc;
7806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807 rettv->v_type = VAR_STRING;
7808 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 }
7810
7811 return OK;
7812}
7813
7814/*
7815 * Array with names and number of arguments of all internal functions
7816 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7817 */
7818static struct fst
7819{
7820 char *f_name; /* function name */
7821 char f_min_argc; /* minimal number of arguments */
7822 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007823 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007824 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825} functions[] =
7826{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007827#ifdef FEAT_FLOAT
7828 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007829 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007830#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007831 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007832 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 {"append", 2, 2, f_append},
7834 {"argc", 0, 0, f_argc},
7835 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007836 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007837#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007838 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007839 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007840 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007843 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {"bufexists", 1, 1, f_bufexists},
7845 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7846 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7847 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7848 {"buflisted", 1, 1, f_buflisted},
7849 {"bufloaded", 1, 1, f_bufloaded},
7850 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007851 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {"bufwinnr", 1, 1, f_bufwinnr},
7853 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007854 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007855 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007856#ifdef FEAT_FLOAT
7857 {"ceil", 1, 1, f_ceil},
7858#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007859 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007860 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007862 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007864#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007865 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007866 {"complete_add", 1, 1, f_complete_add},
7867 {"complete_check", 0, 0, f_complete_check},
7868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007870 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#ifdef FEAT_FLOAT
7872 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007873 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007874#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007875 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007877 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007878 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {"delete", 1, 1, f_delete},
7880 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007881 {"diff_filler", 1, 1, f_diff_filler},
7882 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007883 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007885 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {"eventhandler", 0, 0, f_eventhandler},
7887 {"executable", 1, 1, f_executable},
7888 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007889#ifdef FEAT_FLOAT
7890 {"exp", 1, 1, f_exp},
7891#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007892 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007893 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007894 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7896 {"filereadable", 1, 1, f_filereadable},
7897 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007898 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007899 {"finddir", 1, 3, f_finddir},
7900 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007901#ifdef FEAT_FLOAT
7902 {"float2nr", 1, 1, f_float2nr},
7903 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007904 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007905#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007906 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {"fnamemodify", 2, 2, f_fnamemodify},
7908 {"foldclosed", 1, 1, f_foldclosed},
7909 {"foldclosedend", 1, 1, f_foldclosedend},
7910 {"foldlevel", 1, 1, f_foldlevel},
7911 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007912 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007914 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007915 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007916 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007917 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {"getbufvar", 2, 2, f_getbufvar},
7919 {"getchar", 0, 1, f_getchar},
7920 {"getcharmod", 0, 0, f_getcharmod},
7921 {"getcmdline", 0, 0, f_getcmdline},
7922 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007923 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007925 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007926 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"getfsize", 1, 1, f_getfsize},
7928 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007929 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007930 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007931 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007932 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007933 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007934 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007935 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007936 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007938 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007939 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {"getwinposx", 0, 0, f_getwinposx},
7941 {"getwinposy", 0, 0, f_getwinposy},
7942 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007943 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007944 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007946 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007947 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007948 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7950 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7951 {"histadd", 2, 2, f_histadd},
7952 {"histdel", 1, 2, f_histdel},
7953 {"histget", 1, 2, f_histget},
7954 {"histnr", 1, 1, f_histnr},
7955 {"hlID", 1, 1, f_hlID},
7956 {"hlexists", 1, 1, f_hlexists},
7957 {"hostname", 0, 0, f_hostname},
7958 {"iconv", 3, 3, f_iconv},
7959 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007961 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007963 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 {"inputrestore", 0, 0, f_inputrestore},
7965 {"inputsave", 0, 0, f_inputsave},
7966 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007967 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007968 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007970 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007973 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007975 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 {"libcall", 3, 3, f_libcall},
7977 {"libcallnr", 3, 3, f_libcallnr},
7978 {"line", 1, 1, f_line},
7979 {"line2byte", 1, 1, f_line2byte},
7980 {"lispindent", 1, 1, f_lispindent},
7981 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007982#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007983 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007984 {"log10", 1, 1, f_log10},
7985#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007986#ifdef FEAT_LUA
7987 {"luaeval", 1, 2, f_luaeval},
7988#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007989 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007990 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007991 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007992 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007993 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007994 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007995 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007996 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007997 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007998 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007999 {"max", 1, 1, f_max},
8000 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008001#ifdef vim_mkdir
8002 {"mkdir", 1, 3, f_mkdir},
8003#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008004 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008005#ifdef FEAT_MZSCHEME
8006 {"mzeval", 1, 1, f_mzeval},
8007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008009 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008010 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008011 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012#ifdef FEAT_FLOAT
8013 {"pow", 2, 2, f_pow},
8014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008016 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008017 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008018#ifdef FEAT_PYTHON3
8019 {"py3eval", 1, 1, f_py3eval},
8020#endif
8021#ifdef FEAT_PYTHON
8022 {"pyeval", 1, 1, f_pyeval},
8023#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008024 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008025 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008026 {"reltime", 0, 2, f_reltime},
8027 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 {"remote_expr", 2, 3, f_remote_expr},
8029 {"remote_foreground", 1, 1, f_remote_foreground},
8030 {"remote_peek", 1, 2, f_remote_peek},
8031 {"remote_read", 1, 1, f_remote_read},
8032 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008033 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008035 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008037 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008038#ifdef FEAT_FLOAT
8039 {"round", 1, 1, f_round},
8040#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008041 {"screencol", 0, 0, f_screencol},
8042 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008043 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008044 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008045 {"searchpair", 3, 7, f_searchpair},
8046 {"searchpairpos", 3, 7, f_searchpairpos},
8047 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {"server2client", 2, 2, f_server2client},
8049 {"serverlist", 0, 0, f_serverlist},
8050 {"setbufvar", 3, 3, f_setbufvar},
8051 {"setcmdpos", 1, 1, f_setcmdpos},
8052 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008053 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008054 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008055 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008056 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008058 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008059 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008061#ifdef FEAT_CRYPT
8062 {"sha256", 1, 1, f_sha256},
8063#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008064 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008065 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008067#ifdef FEAT_FLOAT
8068 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008069 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008071 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008072 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008073 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008074 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008075 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008076#ifdef FEAT_FLOAT
8077 {"sqrt", 1, 1, f_sqrt},
8078 {"str2float", 1, 1, f_str2float},
8079#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008080 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008081 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008082 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083#ifdef HAVE_STRFTIME
8084 {"strftime", 1, 2, f_strftime},
8085#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008086 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008087 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {"strlen", 1, 1, f_strlen},
8089 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008090 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008092 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"submatch", 1, 1, f_submatch},
8094 {"substitute", 4, 4, f_substitute},
8095 {"synID", 3, 3, f_synID},
8096 {"synIDattr", 2, 3, f_synIDattr},
8097 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008098 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008099 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008100 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008101 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008102 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008103 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008104 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008105 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008106#ifdef FEAT_FLOAT
8107 {"tan", 1, 1, f_tan},
8108 {"tanh", 1, 1, f_tanh},
8109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008111 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"tolower", 1, 1, f_tolower},
8113 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008114 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008115#ifdef FEAT_FLOAT
8116 {"trunc", 1, 1, f_trunc},
8117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008119 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008120 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008121 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"virtcol", 1, 1, f_virtcol},
8123 {"visualmode", 0, 1, f_visualmode},
8124 {"winbufnr", 1, 1, f_winbufnr},
8125 {"wincol", 0, 0, f_wincol},
8126 {"winheight", 1, 1, f_winheight},
8127 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008128 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008130 {"winrestview", 1, 1, f_winrestview},
8131 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008133 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008134 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135};
8136
8137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8138
8139/*
8140 * Function given to ExpandGeneric() to obtain the list of internal
8141 * or user defined function names.
8142 */
8143 char_u *
8144get_function_name(xp, idx)
8145 expand_T *xp;
8146 int idx;
8147{
8148 static int intidx = -1;
8149 char_u *name;
8150
8151 if (idx == 0)
8152 intidx = -1;
8153 if (intidx < 0)
8154 {
8155 name = get_user_func_name(xp, idx);
8156 if (name != NULL)
8157 return name;
8158 }
8159 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8160 {
8161 STRCPY(IObuff, functions[intidx].f_name);
8162 STRCAT(IObuff, "(");
8163 if (functions[intidx].f_max_argc == 0)
8164 STRCAT(IObuff, ")");
8165 return IObuff;
8166 }
8167
8168 return NULL;
8169}
8170
8171/*
8172 * Function given to ExpandGeneric() to obtain the list of internal or
8173 * user defined variable or function names.
8174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 char_u *
8176get_expr_name(xp, idx)
8177 expand_T *xp;
8178 int idx;
8179{
8180 static int intidx = -1;
8181 char_u *name;
8182
8183 if (idx == 0)
8184 intidx = -1;
8185 if (intidx < 0)
8186 {
8187 name = get_function_name(xp, idx);
8188 if (name != NULL)
8189 return name;
8190 }
8191 return get_user_var_name(xp, ++intidx);
8192}
8193
8194#endif /* FEAT_CMDL_COMPL */
8195
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008196#if defined(EBCDIC) || defined(PROTO)
8197/*
8198 * Compare struct fst by function name.
8199 */
8200 static int
8201compare_func_name(s1, s2)
8202 const void *s1;
8203 const void *s2;
8204{
8205 struct fst *p1 = (struct fst *)s1;
8206 struct fst *p2 = (struct fst *)s2;
8207
8208 return STRCMP(p1->f_name, p2->f_name);
8209}
8210
8211/*
8212 * Sort the function table by function name.
8213 * The sorting of the table above is ASCII dependant.
8214 * On machines using EBCDIC we have to sort it.
8215 */
8216 static void
8217sortFunctions()
8218{
8219 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8220
8221 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8222}
8223#endif
8224
8225
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226/*
8227 * Find internal function in table above.
8228 * Return index, or -1 if not found
8229 */
8230 static int
8231find_internal_func(name)
8232 char_u *name; /* name of the function */
8233{
8234 int first = 0;
8235 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8236 int cmp;
8237 int x;
8238
8239 /*
8240 * Find the function name in the table. Binary search.
8241 */
8242 while (first <= last)
8243 {
8244 x = first + ((unsigned)(last - first) >> 1);
8245 cmp = STRCMP(name, functions[x].f_name);
8246 if (cmp < 0)
8247 last = x - 1;
8248 else if (cmp > 0)
8249 first = x + 1;
8250 else
8251 return x;
8252 }
8253 return -1;
8254}
8255
8256/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008257 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8258 * name it contains, otherwise return "name".
8259 */
8260 static char_u *
8261deref_func_name(name, lenp)
8262 char_u *name;
8263 int *lenp;
8264{
Bram Moolenaar33570922005-01-25 22:26:29 +00008265 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008266 int cc;
8267
8268 cc = name[*lenp];
8269 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008270 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008271 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008272 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008273 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 {
8276 *lenp = 0;
8277 return (char_u *)""; /* just in case */
8278 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008279 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008280 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008281 }
8282
8283 return name;
8284}
8285
8286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 * Allocate a variable for the result of a function.
8288 * Return OK or FAIL.
8289 */
8290 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008291get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8292 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 char_u *name; /* name of the function */
8294 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 char_u **arg; /* argument, pointing to the '(' */
8297 linenr_T firstline; /* first line of range */
8298 linenr_T lastline; /* last line of range */
8299 int *doesrange; /* return: function handled range */
8300 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008301 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303 char_u *argp;
8304 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008305 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 int argcount = 0; /* number of arguments found */
8307
8308 /*
8309 * Get the arguments.
8310 */
8311 argp = *arg;
8312 while (argcount < MAX_FUNC_ARGS)
8313 {
8314 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8315 if (*argp == ')' || *argp == ',' || *argp == NUL)
8316 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8318 {
8319 ret = FAIL;
8320 break;
8321 }
8322 ++argcount;
8323 if (*argp != ',')
8324 break;
8325 }
8326 if (*argp == ')')
8327 ++argp;
8328 else
8329 ret = FAIL;
8330
8331 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008332 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008333 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008335 {
8336 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008337 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008339 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341
8342 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
8345 *arg = skipwhite(argp);
8346 return ret;
8347}
8348
8349
8350/*
8351 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008352 * Return OK when the function can't be called, FAIL otherwise.
8353 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 */
8355 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008356call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008357 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008358 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008360 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008362 typval_T *argvars; /* vars for arguments, must have "argcount"
8363 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 linenr_T firstline; /* first line of range */
8365 linenr_T lastline; /* last line of range */
8366 int *doesrange; /* return: function handled range */
8367 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008368 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369{
8370 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371#define ERROR_UNKNOWN 0
8372#define ERROR_TOOMANY 1
8373#define ERROR_TOOFEW 2
8374#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008375#define ERROR_DICT 4
8376#define ERROR_NONE 5
8377#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 int error = ERROR_NONE;
8379 int i;
8380 int llen;
8381 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382#define FLEN_FIXED 40
8383 char_u fname_buf[FLEN_FIXED + 1];
8384 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008385 char_u *name;
8386
8387 /* Make a copy of the name, if it comes from a funcref variable it could
8388 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008389 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008390 if (name == NULL)
8391 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392
8393 /*
8394 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8395 * Change <SNR>123_name() to K_SNR 123_name().
8396 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 llen = eval_fname_script(name);
8399 if (llen > 0)
8400 {
8401 fname_buf[0] = K_SPECIAL;
8402 fname_buf[1] = KS_EXTRA;
8403 fname_buf[2] = (int)KE_SNR;
8404 i = 3;
8405 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8406 {
8407 if (current_SID <= 0)
8408 error = ERROR_SCRIPT;
8409 else
8410 {
8411 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8412 i = (int)STRLEN(fname_buf);
8413 }
8414 }
8415 if (i + STRLEN(name + llen) < FLEN_FIXED)
8416 {
8417 STRCPY(fname_buf + i, name + llen);
8418 fname = fname_buf;
8419 }
8420 else
8421 {
8422 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8423 if (fname == NULL)
8424 error = ERROR_OTHER;
8425 else
8426 {
8427 mch_memmove(fname, fname_buf, (size_t)i);
8428 STRCPY(fname + i, name + llen);
8429 }
8430 }
8431 }
8432 else
8433 fname = name;
8434
8435 *doesrange = FALSE;
8436
8437
8438 /* execute the function if no errors detected and executing */
8439 if (evaluate && error == ERROR_NONE)
8440 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008441 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8442 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 error = ERROR_UNKNOWN;
8444
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008445 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {
8447 /*
8448 * User defined function.
8449 */
8450 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008451
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008453 /* Trigger FuncUndefined event, may load the function. */
8454 if (fp == NULL
8455 && apply_autocmds(EVENT_FUNCUNDEFINED,
8456 fname, fname, TRUE, NULL)
8457 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008459 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 fp = find_func(fname);
8461 }
8462#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008463 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008464 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008465 {
8466 /* loaded a package, search for the function again */
8467 fp = find_func(fname);
8468 }
8469
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 if (fp != NULL)
8471 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008472 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008474 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008476 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008478 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008479 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 else
8481 {
8482 /*
8483 * Call the user function.
8484 * Save and restore search patterns, script variables and
8485 * redo buffer.
8486 */
8487 save_search_patterns();
8488 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008489 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008490 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008491 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008492 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8493 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8494 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008495 /* Function was unreferenced while being used, free it
8496 * now. */
8497 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 restoreRedobuff();
8499 restore_search_patterns();
8500 error = ERROR_NONE;
8501 }
8502 }
8503 }
8504 else
8505 {
8506 /*
8507 * Find the function name in the table, call its implementation.
8508 */
8509 i = find_internal_func(fname);
8510 if (i >= 0)
8511 {
8512 if (argcount < functions[i].f_min_argc)
8513 error = ERROR_TOOFEW;
8514 else if (argcount > functions[i].f_max_argc)
8515 error = ERROR_TOOMANY;
8516 else
8517 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008518 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 error = ERROR_NONE;
8521 }
8522 }
8523 }
8524 /*
8525 * The function call (or "FuncUndefined" autocommand sequence) might
8526 * have been aborted by an error, an interrupt, or an explicitly thrown
8527 * exception that has not been caught so far. This situation can be
8528 * tested for by calling aborting(). For an error in an internal
8529 * function or for the "E132" error in call_user_func(), however, the
8530 * throw point at which the "force_abort" flag (temporarily reset by
8531 * emsg()) is normally updated has not been reached yet. We need to
8532 * update that flag first to make aborting() reliable.
8533 */
8534 update_force_abort();
8535 }
8536 if (error == ERROR_NONE)
8537 ret = OK;
8538
8539 /*
8540 * Report an error unless the argument evaluation or function call has been
8541 * cancelled due to an aborting error, an interrupt, or an exception.
8542 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008543 if (!aborting())
8544 {
8545 switch (error)
8546 {
8547 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008548 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008549 break;
8550 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008551 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008552 break;
8553 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008554 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008555 name);
8556 break;
8557 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008558 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008559 name);
8560 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008561 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008562 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008563 name);
8564 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008565 }
8566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 if (fname != name && fname != fname_buf)
8569 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008570 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571
8572 return ret;
8573}
8574
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008575/*
8576 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008577 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008578 */
8579 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008580emsg_funcname(ermsg, name)
8581 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008582 char_u *name;
8583{
8584 char_u *p;
8585
8586 if (*name == K_SPECIAL)
8587 p = concat_str((char_u *)"<SNR>", name + 3);
8588 else
8589 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008590 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008591 if (p != name)
8592 vim_free(p);
8593}
8594
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008595/*
8596 * Return TRUE for a non-zero Number and a non-empty String.
8597 */
8598 static int
8599non_zero_arg(argvars)
8600 typval_T *argvars;
8601{
8602 return ((argvars[0].v_type == VAR_NUMBER
8603 && argvars[0].vval.v_number != 0)
8604 || (argvars[0].v_type == VAR_STRING
8605 && argvars[0].vval.v_string != NULL
8606 && *argvars[0].vval.v_string != NUL));
8607}
8608
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609/*********************************************
8610 * Implementation of the built-in functions
8611 */
8612
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008613#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008614static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8615
8616/*
8617 * Get the float value of "argvars[0]" into "f".
8618 * Returns FAIL when the argument is not a Number or Float.
8619 */
8620 static int
8621get_float_arg(argvars, f)
8622 typval_T *argvars;
8623 float_T *f;
8624{
8625 if (argvars[0].v_type == VAR_FLOAT)
8626 {
8627 *f = argvars[0].vval.v_float;
8628 return OK;
8629 }
8630 if (argvars[0].v_type == VAR_NUMBER)
8631 {
8632 *f = (float_T)argvars[0].vval.v_number;
8633 return OK;
8634 }
8635 EMSG(_("E808: Number or Float required"));
8636 return FAIL;
8637}
8638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008639/*
8640 * "abs(expr)" function
8641 */
8642 static void
8643f_abs(argvars, rettv)
8644 typval_T *argvars;
8645 typval_T *rettv;
8646{
8647 if (argvars[0].v_type == VAR_FLOAT)
8648 {
8649 rettv->v_type = VAR_FLOAT;
8650 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8651 }
8652 else
8653 {
8654 varnumber_T n;
8655 int error = FALSE;
8656
8657 n = get_tv_number_chk(&argvars[0], &error);
8658 if (error)
8659 rettv->vval.v_number = -1;
8660 else if (n > 0)
8661 rettv->vval.v_number = n;
8662 else
8663 rettv->vval.v_number = -n;
8664 }
8665}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008666
8667/*
8668 * "acos()" function
8669 */
8670 static void
8671f_acos(argvars, rettv)
8672 typval_T *argvars;
8673 typval_T *rettv;
8674{
8675 float_T f;
8676
8677 rettv->v_type = VAR_FLOAT;
8678 if (get_float_arg(argvars, &f) == OK)
8679 rettv->vval.v_float = acos(f);
8680 else
8681 rettv->vval.v_float = 0.0;
8682}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008683#endif
8684
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008686 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 */
8688 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008689f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008690 typval_T *argvars;
8691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692{
Bram Moolenaar33570922005-01-25 22:26:29 +00008693 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008696 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008698 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008699 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008700 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008701 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008702 }
8703 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008704 EMSG(_(e_listreq));
8705}
8706
8707/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008708 * "and(expr, expr)" function
8709 */
8710 static void
8711f_and(argvars, rettv)
8712 typval_T *argvars;
8713 typval_T *rettv;
8714{
8715 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8716 & get_tv_number_chk(&argvars[1], NULL);
8717}
8718
8719/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720 * "append(lnum, string/list)" function
8721 */
8722 static void
8723f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008724 typval_T *argvars;
8725 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008726{
8727 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008728 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 list_T *l = NULL;
8730 listitem_T *li = NULL;
8731 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008732 long added = 0;
8733
Bram Moolenaar0d660222005-01-07 21:51:51 +00008734 lnum = get_tv_lnum(argvars);
8735 if (lnum >= 0
8736 && lnum <= curbuf->b_ml.ml_line_count
8737 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008738 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008739 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008740 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008741 l = argvars[1].vval.v_list;
8742 if (l == NULL)
8743 return;
8744 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008745 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008746 for (;;)
8747 {
8748 if (l == NULL)
8749 tv = &argvars[1]; /* append a string */
8750 else if (li == NULL)
8751 break; /* end of list */
8752 else
8753 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008754 line = get_tv_string_chk(tv);
8755 if (line == NULL) /* type error */
8756 {
8757 rettv->vval.v_number = 1; /* Failed */
8758 break;
8759 }
8760 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008761 ++added;
8762 if (l == NULL)
8763 break;
8764 li = li->li_next;
8765 }
8766
8767 appended_lines_mark(lnum, added);
8768 if (curwin->w_cursor.lnum > lnum)
8769 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008771 else
8772 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773}
8774
8775/*
8776 * "argc()" function
8777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008779f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784}
8785
8786/*
8787 * "argidx()" function
8788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008791 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795}
8796
8797/*
8798 * "argv(nr)" function
8799 */
8800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008802 typval_T *argvars;
8803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805 int idx;
8806
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008807 if (argvars[0].v_type != VAR_UNKNOWN)
8808 {
8809 idx = get_tv_number_chk(&argvars[0], NULL);
8810 if (idx >= 0 && idx < ARGCOUNT)
8811 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8812 else
8813 rettv->vval.v_string = NULL;
8814 rettv->v_type = VAR_STRING;
8815 }
8816 else if (rettv_list_alloc(rettv) == OK)
8817 for (idx = 0; idx < ARGCOUNT; ++idx)
8818 list_append_string(rettv->vval.v_list,
8819 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820}
8821
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008822#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008823/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008824 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008825 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008826 static void
8827f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008828 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008829 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008830{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008831 float_T f;
8832
8833 rettv->v_type = VAR_FLOAT;
8834 if (get_float_arg(argvars, &f) == OK)
8835 rettv->vval.v_float = asin(f);
8836 else
8837 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008838}
8839
8840/*
8841 * "atan()" function
8842 */
8843 static void
8844f_atan(argvars, rettv)
8845 typval_T *argvars;
8846 typval_T *rettv;
8847{
8848 float_T f;
8849
8850 rettv->v_type = VAR_FLOAT;
8851 if (get_float_arg(argvars, &f) == OK)
8852 rettv->vval.v_float = atan(f);
8853 else
8854 rettv->vval.v_float = 0.0;
8855}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008856
8857/*
8858 * "atan2()" function
8859 */
8860 static void
8861f_atan2(argvars, rettv)
8862 typval_T *argvars;
8863 typval_T *rettv;
8864{
8865 float_T fx, fy;
8866
8867 rettv->v_type = VAR_FLOAT;
8868 if (get_float_arg(argvars, &fx) == OK
8869 && get_float_arg(&argvars[1], &fy) == OK)
8870 rettv->vval.v_float = atan2(fx, fy);
8871 else
8872 rettv->vval.v_float = 0.0;
8873}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008874#endif
8875
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876/*
8877 * "browse(save, title, initdir, default)" function
8878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008881 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883{
8884#ifdef FEAT_BROWSE
8885 int save;
8886 char_u *title;
8887 char_u *initdir;
8888 char_u *defname;
8889 char_u buf[NUMBUFLEN];
8890 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008891 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008893 save = get_tv_number_chk(&argvars[0], &error);
8894 title = get_tv_string_chk(&argvars[1]);
8895 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8896 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008898 if (error || title == NULL || initdir == NULL || defname == NULL)
8899 rettv->vval.v_string = NULL;
8900 else
8901 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008902 do_browse(save ? BROWSE_SAVE : 0,
8903 title, defname, NULL, initdir, NULL, curbuf);
8904#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008905 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008906#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008908}
8909
8910/*
8911 * "browsedir(title, initdir)" function
8912 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008914f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008915 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008916 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008917{
8918#ifdef FEAT_BROWSE
8919 char_u *title;
8920 char_u *initdir;
8921 char_u buf[NUMBUFLEN];
8922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008923 title = get_tv_string_chk(&argvars[0]);
8924 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008926 if (title == NULL || initdir == NULL)
8927 rettv->vval.v_string = NULL;
8928 else
8929 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008930 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935}
8936
Bram Moolenaar33570922005-01-25 22:26:29 +00008937static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008938
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939/*
8940 * Find a buffer by number or exact name.
8941 */
8942 static buf_T *
8943find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945{
8946 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008948 if (avar->v_type == VAR_NUMBER)
8949 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008950 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008952 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008953 if (buf == NULL)
8954 {
8955 /* No full path name match, try a match with a URL or a "nofile"
8956 * buffer, these don't use the full path. */
8957 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8958 if (buf->b_fname != NULL
8959 && (path_with_url(buf->b_fname)
8960#ifdef FEAT_QUICKFIX
8961 || bt_nofile(buf)
8962#endif
8963 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008964 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008965 break;
8966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 }
8968 return buf;
8969}
8970
8971/*
8972 * "bufexists(expr)" function
8973 */
8974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 typval_T *argvars;
8977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980}
8981
8982/*
8983 * "buflisted(expr)" function
8984 */
8985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 typval_T *argvars;
8988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
8990 buf_T *buf;
8991
8992 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994}
8995
8996/*
8997 * "bufloaded(expr)" function
8998 */
8999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 typval_T *argvars;
9002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
9004 buf_T *buf;
9005
9006 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009007 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008}
9009
Bram Moolenaar33570922005-01-25 22:26:29 +00009010static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009011
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012/*
9013 * Get buffer by number or pattern.
9014 */
9015 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 int save_magic;
9021 char_u *save_cpo;
9022 buf_T *buf;
9023
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009024 if (tv->v_type == VAR_NUMBER)
9025 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009026 if (tv->v_type != VAR_STRING)
9027 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 if (name == NULL || *name == NUL)
9029 return curbuf;
9030 if (name[0] == '$' && name[1] == NUL)
9031 return lastbuf;
9032
9033 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9034 save_magic = p_magic;
9035 p_magic = TRUE;
9036 save_cpo = p_cpo;
9037 p_cpo = (char_u *)"";
9038
9039 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
9040 TRUE, FALSE));
9041
9042 p_magic = save_magic;
9043 p_cpo = save_cpo;
9044
9045 /* If not found, try expanding the name, like done for bufexists(). */
9046 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009047 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048
9049 return buf;
9050}
9051
9052/*
9053 * "bufname(expr)" function
9054 */
9055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009057 typval_T *argvars;
9058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
9060 buf_T *buf;
9061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009062 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064 buf = get_buf_tv(&argvars[0]);
9065 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 --emsg_off;
9071}
9072
9073/*
9074 * "bufnr(expr)" function
9075 */
9076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 typval_T *argvars;
9079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009082 int error = FALSE;
9083 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009085 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009088 --emsg_off;
9089
9090 /* If the buffer isn't found and the second argument is not zero create a
9091 * new buffer. */
9092 if (buf == NULL
9093 && argvars[1].v_type != VAR_UNKNOWN
9094 && get_tv_number_chk(&argvars[1], &error) != 0
9095 && !error
9096 && (name = get_tv_string_chk(&argvars[0])) != NULL
9097 && !error)
9098 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9099
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104}
9105
9106/*
9107 * "bufwinnr(nr)" function
9108 */
9109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009111 typval_T *argvars;
9112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113{
9114#ifdef FEAT_WINDOWS
9115 win_T *wp;
9116 int winnr = 0;
9117#endif
9118 buf_T *buf;
9119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009120 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123#ifdef FEAT_WINDOWS
9124 for (wp = firstwin; wp; wp = wp->w_next)
9125 {
9126 ++winnr;
9127 if (wp->w_buffer == buf)
9128 break;
9129 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009130 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133#endif
9134 --emsg_off;
9135}
9136
9137/*
9138 * "byte2line(byte)" function
9139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009141f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009142 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144{
9145#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147#else
9148 long boff = 0;
9149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 (linenr_T)0, &boff);
9156#endif
9157}
9158
9159/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009160 * "byteidx()" function
9161 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009163f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009164 typval_T *argvars;
9165 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009166{
9167#ifdef FEAT_MBYTE
9168 char_u *t;
9169#endif
9170 char_u *str;
9171 long idx;
9172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009173 str = get_tv_string_chk(&argvars[0]);
9174 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009177 return;
9178
9179#ifdef FEAT_MBYTE
9180 t = str;
9181 for ( ; idx > 0; idx--)
9182 {
9183 if (*t == NUL) /* EOL reached */
9184 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009185 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009186 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009187 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009188#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009189 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009190 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009191#endif
9192}
9193
Bram Moolenaardb913952012-06-29 12:54:53 +02009194 int
9195func_call(name, args, selfdict, rettv)
9196 char_u *name;
9197 typval_T *args;
9198 dict_T *selfdict;
9199 typval_T *rettv;
9200{
9201 listitem_T *item;
9202 typval_T argv[MAX_FUNC_ARGS + 1];
9203 int argc = 0;
9204 int dummy;
9205 int r = 0;
9206
9207 for (item = args->vval.v_list->lv_first; item != NULL;
9208 item = item->li_next)
9209 {
9210 if (argc == MAX_FUNC_ARGS)
9211 {
9212 EMSG(_("E699: Too many arguments"));
9213 break;
9214 }
9215 /* Make a copy of each argument. This is needed to be able to set
9216 * v_lock to VAR_FIXED in the copy without changing the original list.
9217 */
9218 copy_tv(&item->li_tv, &argv[argc++]);
9219 }
9220
9221 if (item == NULL)
9222 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9223 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9224 &dummy, TRUE, selfdict);
9225
9226 /* Free the arguments. */
9227 while (argc > 0)
9228 clear_tv(&argv[--argc]);
9229
9230 return r;
9231}
9232
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009233/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009234 * "call(func, arglist)" function
9235 */
9236 static void
9237f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009238 typval_T *argvars;
9239 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009240{
9241 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009242 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009243
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009244 if (argvars[1].v_type != VAR_LIST)
9245 {
9246 EMSG(_(e_listreq));
9247 return;
9248 }
9249 if (argvars[1].vval.v_list == NULL)
9250 return;
9251
9252 if (argvars[0].v_type == VAR_FUNC)
9253 func = argvars[0].vval.v_string;
9254 else
9255 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 if (*func == NUL)
9257 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009258
Bram Moolenaare9a41262005-01-15 22:18:47 +00009259 if (argvars[2].v_type != VAR_UNKNOWN)
9260 {
9261 if (argvars[2].v_type != VAR_DICT)
9262 {
9263 EMSG(_(e_dictreq));
9264 return;
9265 }
9266 selfdict = argvars[2].vval.v_dict;
9267 }
9268
Bram Moolenaardb913952012-06-29 12:54:53 +02009269 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009270}
9271
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009272#ifdef FEAT_FLOAT
9273/*
9274 * "ceil({float})" function
9275 */
9276 static void
9277f_ceil(argvars, rettv)
9278 typval_T *argvars;
9279 typval_T *rettv;
9280{
9281 float_T f;
9282
9283 rettv->v_type = VAR_FLOAT;
9284 if (get_float_arg(argvars, &f) == OK)
9285 rettv->vval.v_float = ceil(f);
9286 else
9287 rettv->vval.v_float = 0.0;
9288}
9289#endif
9290
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009291/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009292 * "changenr()" function
9293 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009294 static void
9295f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009296 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009297 typval_T *rettv;
9298{
9299 rettv->vval.v_number = curbuf->b_u_seq_cur;
9300}
9301
9302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 * "char2nr(string)" function
9304 */
9305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009306f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009307 typval_T *argvars;
9308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309{
9310#ifdef FEAT_MBYTE
9311 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009312 {
9313 int utf8 = 0;
9314
9315 if (argvars[1].v_type != VAR_UNKNOWN)
9316 utf8 = get_tv_number_chk(&argvars[1], NULL);
9317
9318 if (utf8)
9319 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9320 else
9321 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 else
9324#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326}
9327
9328/*
9329 * "cindent(lnum)" function
9330 */
9331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009333 typval_T *argvars;
9334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335{
9336#ifdef FEAT_CINDENT
9337 pos_T pos;
9338 linenr_T lnum;
9339
9340 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009341 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9343 {
9344 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009345 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 curwin->w_cursor = pos;
9347 }
9348 else
9349#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351}
9352
9353/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009354 * "clearmatches()" function
9355 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009356 static void
9357f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009358 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009359 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009360{
9361#ifdef FEAT_SEARCH_EXTRA
9362 clear_matches(curwin);
9363#endif
9364}
9365
9366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 * "col(string)" function
9368 */
9369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009371 typval_T *argvars;
9372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373{
9374 colnr_T col = 0;
9375 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009376 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009378 fp = var2fpos(&argvars[0], FALSE, &fnum);
9379 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 {
9381 if (fp->col == MAXCOL)
9382 {
9383 /* '> can be MAXCOL, get the length of the line then */
9384 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009385 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 else
9387 col = MAXCOL;
9388 }
9389 else
9390 {
9391 col = fp->col + 1;
9392#ifdef FEAT_VIRTUALEDIT
9393 /* col(".") when the cursor is on the NUL at the end of the line
9394 * because of "coladd" can be seen as an extra column. */
9395 if (virtual_active() && fp == &curwin->w_cursor)
9396 {
9397 char_u *p = ml_get_cursor();
9398
9399 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9400 curwin->w_virtcol - curwin->w_cursor.coladd))
9401 {
9402# ifdef FEAT_MBYTE
9403 int l;
9404
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009405 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 col += l;
9407# else
9408 if (*p != NUL && p[1] == NUL)
9409 ++col;
9410# endif
9411 }
9412 }
9413#endif
9414 }
9415 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417}
9418
Bram Moolenaar572cb562005-08-05 21:35:02 +00009419#if defined(FEAT_INS_EXPAND)
9420/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009421 * "complete()" function
9422 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009423 static void
9424f_complete(argvars, rettv)
9425 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009426 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009427{
9428 int startcol;
9429
9430 if ((State & INSERT) == 0)
9431 {
9432 EMSG(_("E785: complete() can only be used in Insert mode"));
9433 return;
9434 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009435
9436 /* Check for undo allowed here, because if something was already inserted
9437 * the line was already saved for undo and this check isn't done. */
9438 if (!undo_allowed())
9439 return;
9440
Bram Moolenaarade00832006-03-10 21:46:58 +00009441 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9442 {
9443 EMSG(_(e_invarg));
9444 return;
9445 }
9446
9447 startcol = get_tv_number_chk(&argvars[0], NULL);
9448 if (startcol <= 0)
9449 return;
9450
9451 set_completion(startcol - 1, argvars[1].vval.v_list);
9452}
9453
9454/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009455 * "complete_add()" function
9456 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009457 static void
9458f_complete_add(argvars, rettv)
9459 typval_T *argvars;
9460 typval_T *rettv;
9461{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009462 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009463}
9464
9465/*
9466 * "complete_check()" function
9467 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009468 static void
9469f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009470 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009471 typval_T *rettv;
9472{
9473 int saved = RedrawingDisabled;
9474
9475 RedrawingDisabled = 0;
9476 ins_compl_check_keys(0);
9477 rettv->vval.v_number = compl_interrupted;
9478 RedrawingDisabled = saved;
9479}
9480#endif
9481
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482/*
9483 * "confirm(message, buttons[, default [, type]])" function
9484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009487 typval_T *argvars UNUSED;
9488 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489{
9490#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9491 char_u *message;
9492 char_u *buttons = NULL;
9493 char_u buf[NUMBUFLEN];
9494 char_u buf2[NUMBUFLEN];
9495 int def = 1;
9496 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009497 char_u *typestr;
9498 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009500 message = get_tv_string_chk(&argvars[0]);
9501 if (message == NULL)
9502 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009503 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009505 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9506 if (buttons == NULL)
9507 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009508 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009510 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009511 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9514 if (typestr == NULL)
9515 error = TRUE;
9516 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009518 switch (TOUPPER_ASC(*typestr))
9519 {
9520 case 'E': type = VIM_ERROR; break;
9521 case 'Q': type = VIM_QUESTION; break;
9522 case 'I': type = VIM_INFO; break;
9523 case 'W': type = VIM_WARNING; break;
9524 case 'G': type = VIM_GENERIC; break;
9525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 }
9527 }
9528 }
9529 }
9530
9531 if (buttons == NULL || *buttons == NUL)
9532 buttons = (char_u *)_("&Ok");
9533
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009534 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009535 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009536 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537#endif
9538}
9539
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009540/*
9541 * "copy()" function
9542 */
9543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009544f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009545 typval_T *argvars;
9546 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009547{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009548 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009549}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009551#ifdef FEAT_FLOAT
9552/*
9553 * "cos()" function
9554 */
9555 static void
9556f_cos(argvars, rettv)
9557 typval_T *argvars;
9558 typval_T *rettv;
9559{
9560 float_T f;
9561
9562 rettv->v_type = VAR_FLOAT;
9563 if (get_float_arg(argvars, &f) == OK)
9564 rettv->vval.v_float = cos(f);
9565 else
9566 rettv->vval.v_float = 0.0;
9567}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009568
9569/*
9570 * "cosh()" function
9571 */
9572 static void
9573f_cosh(argvars, rettv)
9574 typval_T *argvars;
9575 typval_T *rettv;
9576{
9577 float_T f;
9578
9579 rettv->v_type = VAR_FLOAT;
9580 if (get_float_arg(argvars, &f) == OK)
9581 rettv->vval.v_float = cosh(f);
9582 else
9583 rettv->vval.v_float = 0.0;
9584}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009585#endif
9586
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009588 * "count()" function
9589 */
9590 static void
9591f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009592 typval_T *argvars;
9593 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009594{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009595 long n = 0;
9596 int ic = FALSE;
9597
Bram Moolenaare9a41262005-01-15 22:18:47 +00009598 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009599 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009600 listitem_T *li;
9601 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009602 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009603
Bram Moolenaare9a41262005-01-15 22:18:47 +00009604 if ((l = argvars[0].vval.v_list) != NULL)
9605 {
9606 li = l->lv_first;
9607 if (argvars[2].v_type != VAR_UNKNOWN)
9608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009609 int error = FALSE;
9610
9611 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009612 if (argvars[3].v_type != VAR_UNKNOWN)
9613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009614 idx = get_tv_number_chk(&argvars[3], &error);
9615 if (!error)
9616 {
9617 li = list_find(l, idx);
9618 if (li == NULL)
9619 EMSGN(_(e_listidx), idx);
9620 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009621 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 if (error)
9623 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 }
9625
9626 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009627 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009628 ++n;
9629 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009630 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009631 else if (argvars[0].v_type == VAR_DICT)
9632 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009633 int todo;
9634 dict_T *d;
9635 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009636
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009637 if ((d = argvars[0].vval.v_dict) != NULL)
9638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 int error = FALSE;
9640
Bram Moolenaare9a41262005-01-15 22:18:47 +00009641 if (argvars[2].v_type != VAR_UNKNOWN)
9642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009644 if (argvars[3].v_type != VAR_UNKNOWN)
9645 EMSG(_(e_invarg));
9646 }
9647
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009648 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009649 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009650 {
9651 if (!HASHITEM_EMPTY(hi))
9652 {
9653 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009654 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009655 ++n;
9656 }
9657 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009658 }
9659 }
9660 else
9661 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009662 rettv->vval.v_number = n;
9663}
9664
9665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9667 *
9668 * Checks the existence of a cscope connection.
9669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009672 typval_T *argvars UNUSED;
9673 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674{
9675#ifdef FEAT_CSCOPE
9676 int num = 0;
9677 char_u *dbpath = NULL;
9678 char_u *prepend = NULL;
9679 char_u buf[NUMBUFLEN];
9680
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009681 if (argvars[0].v_type != VAR_UNKNOWN
9682 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684 num = (int)get_tv_number(&argvars[0]);
9685 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009686 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009687 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 }
9689
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009690 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691#endif
9692}
9693
9694/*
9695 * "cursor(lnum, col)" function
9696 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009697 * Moves the cursor to the specified line and column.
9698 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009702 typval_T *argvars;
9703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704{
9705 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009706#ifdef FEAT_VIRTUALEDIT
9707 long coladd = 0;
9708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009710 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009711 if (argvars[1].v_type == VAR_UNKNOWN)
9712 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009713 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009714
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009715 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009716 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009717 line = pos.lnum;
9718 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009719#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009720 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009721#endif
9722 }
9723 else
9724 {
9725 line = get_tv_lnum(argvars);
9726 col = get_tv_number_chk(&argvars[1], NULL);
9727#ifdef FEAT_VIRTUALEDIT
9728 if (argvars[2].v_type != VAR_UNKNOWN)
9729 coladd = get_tv_number_chk(&argvars[2], NULL);
9730#endif
9731 }
9732 if (line < 0 || col < 0
9733#ifdef FEAT_VIRTUALEDIT
9734 || coladd < 0
9735#endif
9736 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009737 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 if (line > 0)
9739 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 if (col > 0)
9741 curwin->w_cursor.col = col - 1;
9742#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009743 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744#endif
9745
9746 /* Make sure the cursor is in a valid position. */
9747 check_cursor();
9748#ifdef FEAT_MBYTE
9749 /* Correct cursor for multi-byte character. */
9750 if (has_mbyte)
9751 mb_adjust_cursor();
9752#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009753
9754 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009755 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756}
9757
9758/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009759 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 */
9761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009763 typval_T *argvars;
9764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009766 int noref = 0;
9767
9768 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009769 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009770 if (noref < 0 || noref > 1)
9771 EMSG(_(e_invarg));
9772 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009773 {
9774 current_copyID += COPYID_INC;
9775 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777}
9778
9779/*
9780 * "delete()" function
9781 */
9782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009784 typval_T *argvars;
9785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786{
9787 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791}
9792
9793/*
9794 * "did_filetype()" function
9795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009798 typval_T *argvars UNUSED;
9799 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
9801#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803#endif
9804}
9805
9806/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009807 * "diff_filler()" function
9808 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009811 typval_T *argvars UNUSED;
9812 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009813{
9814#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009816#endif
9817}
9818
9819/*
9820 * "diff_hlID()" function
9821 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009824 typval_T *argvars UNUSED;
9825 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009826{
9827#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009829 static linenr_T prev_lnum = 0;
9830 static int changedtick = 0;
9831 static int fnum = 0;
9832 static int change_start = 0;
9833 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009834 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009835 int filler_lines;
9836 int col;
9837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009838 if (lnum < 0) /* ignore type error in {lnum} arg */
9839 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009840 if (lnum != prev_lnum
9841 || changedtick != curbuf->b_changedtick
9842 || fnum != curbuf->b_fnum)
9843 {
9844 /* New line, buffer, change: need to get the values. */
9845 filler_lines = diff_check(curwin, lnum);
9846 if (filler_lines < 0)
9847 {
9848 if (filler_lines == -1)
9849 {
9850 change_start = MAXCOL;
9851 change_end = -1;
9852 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9853 hlID = HLF_ADD; /* added line */
9854 else
9855 hlID = HLF_CHD; /* changed line */
9856 }
9857 else
9858 hlID = HLF_ADD; /* added line */
9859 }
9860 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009861 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009862 prev_lnum = lnum;
9863 changedtick = curbuf->b_changedtick;
9864 fnum = curbuf->b_fnum;
9865 }
9866
9867 if (hlID == HLF_CHD || hlID == HLF_TXD)
9868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009869 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009870 if (col >= change_start && col <= change_end)
9871 hlID = HLF_TXD; /* changed text */
9872 else
9873 hlID = HLF_CHD; /* changed line */
9874 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009875 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009876#endif
9877}
9878
9879/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009880 * "empty({expr})" function
9881 */
9882 static void
9883f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009884 typval_T *argvars;
9885 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009886{
9887 int n;
9888
9889 switch (argvars[0].v_type)
9890 {
9891 case VAR_STRING:
9892 case VAR_FUNC:
9893 n = argvars[0].vval.v_string == NULL
9894 || *argvars[0].vval.v_string == NUL;
9895 break;
9896 case VAR_NUMBER:
9897 n = argvars[0].vval.v_number == 0;
9898 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009899#ifdef FEAT_FLOAT
9900 case VAR_FLOAT:
9901 n = argvars[0].vval.v_float == 0.0;
9902 break;
9903#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009904 case VAR_LIST:
9905 n = argvars[0].vval.v_list == NULL
9906 || argvars[0].vval.v_list->lv_first == NULL;
9907 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009908 case VAR_DICT:
9909 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009910 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009911 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009912 default:
9913 EMSG2(_(e_intern2), "f_empty()");
9914 n = 0;
9915 }
9916
9917 rettv->vval.v_number = n;
9918}
9919
9920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 * "escape({string}, {chars})" function
9922 */
9923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009924f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009925 typval_T *argvars;
9926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927{
9928 char_u buf[NUMBUFLEN];
9929
Bram Moolenaar758711c2005-02-02 23:11:38 +00009930 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9931 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009932 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933}
9934
9935/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009936 * "eval()" function
9937 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009938 static void
9939f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009940 typval_T *argvars;
9941 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009942{
9943 char_u *s;
9944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 s = get_tv_string_chk(&argvars[0]);
9946 if (s != NULL)
9947 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009949 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9950 {
9951 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009952 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009953 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009954 else if (*s != NUL)
9955 EMSG(_(e_trailing));
9956}
9957
9958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 * "eventhandler()" function
9960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009962f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009963 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967}
9968
9969/*
9970 * "executable()" function
9971 */
9972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009974 typval_T *argvars;
9975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978}
9979
9980/*
9981 * "exists()" function
9982 */
9983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009984f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009985 typval_T *argvars;
9986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987{
9988 char_u *p;
9989 char_u *name;
9990 int n = FALSE;
9991 int len = 0;
9992
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009993 no_autoload = TRUE;
9994
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009995 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 if (*p == '$') /* environment variable */
9997 {
9998 /* first try "normal" environment variables (fast) */
9999 if (mch_getenv(p + 1) != NULL)
10000 n = TRUE;
10001 else
10002 {
10003 /* try expanding things like $VIM and ${HOME} */
10004 p = expand_env_save(p);
10005 if (p != NULL && *p != '$')
10006 n = TRUE;
10007 vim_free(p);
10008 }
10009 }
10010 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010013 if (*skipwhite(p) != NUL)
10014 n = FALSE; /* trailing garbage */
10015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 else if (*p == '*') /* internal or user defined function */
10017 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010018 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 }
10020 else if (*p == ':')
10021 {
10022 n = cmd_exists(p + 1);
10023 }
10024 else if (*p == '#')
10025 {
10026#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010027 if (p[1] == '#')
10028 n = autocmd_supported(p + 2);
10029 else
10030 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031#endif
10032 }
10033 else /* internal variable */
10034 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010035 char_u *tofree;
10036 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010038 /* get_name_len() takes care of expanding curly braces */
10039 name = p;
10040 len = get_name_len(&p, &tofree, TRUE, FALSE);
10041 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010043 if (tofree != NULL)
10044 name = tofree;
10045 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10046 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010048 /* handle d.key, l[idx], f(expr) */
10049 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10050 if (n)
10051 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 }
10053 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010054 if (*p != NUL)
10055 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010057 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 }
10059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010061
10062 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063}
10064
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010065#ifdef FEAT_FLOAT
10066/*
10067 * "exp()" function
10068 */
10069 static void
10070f_exp(argvars, rettv)
10071 typval_T *argvars;
10072 typval_T *rettv;
10073{
10074 float_T f;
10075
10076 rettv->v_type = VAR_FLOAT;
10077 if (get_float_arg(argvars, &f) == OK)
10078 rettv->vval.v_float = exp(f);
10079 else
10080 rettv->vval.v_float = 0.0;
10081}
10082#endif
10083
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084/*
10085 * "expand()" function
10086 */
10087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010089 typval_T *argvars;
10090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091{
10092 char_u *s;
10093 int len;
10094 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010095 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010097 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010098 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010101 if (argvars[1].v_type != VAR_UNKNOWN
10102 && argvars[2].v_type != VAR_UNKNOWN
10103 && get_tv_number_chk(&argvars[2], &error)
10104 && !error)
10105 {
10106 rettv->v_type = VAR_LIST;
10107 rettv->vval.v_list = NULL;
10108 }
10109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 if (*s == '%' || *s == '#' || *s == '<')
10112 {
10113 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010114 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010116 if (rettv->v_type == VAR_LIST)
10117 {
10118 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10119 list_append_string(rettv->vval.v_list, result, -1);
10120 else
10121 vim_free(result);
10122 }
10123 else
10124 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 }
10126 else
10127 {
10128 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010129 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010130 if (argvars[1].v_type != VAR_UNKNOWN
10131 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010132 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010133 if (!error)
10134 {
10135 ExpandInit(&xpc);
10136 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010137 if (p_wic)
10138 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010139 if (rettv->v_type == VAR_STRING)
10140 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10141 options, WILD_ALL);
10142 else if (rettv_list_alloc(rettv) != FAIL)
10143 {
10144 int i;
10145
10146 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10147 for (i = 0; i < xpc.xp_numfiles; i++)
10148 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10149 ExpandCleanup(&xpc);
10150 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010151 }
10152 else
10153 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 }
10155}
10156
10157/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010158 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010159 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010160 */
10161 static void
10162f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010163 typval_T *argvars;
10164 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010165{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010166 char *arg_errmsg = N_("extend() argument");
10167
Bram Moolenaare9a41262005-01-15 22:18:47 +000010168 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010169 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010170 list_T *l1, *l2;
10171 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010172 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010173 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010174
Bram Moolenaare9a41262005-01-15 22:18:47 +000010175 l1 = argvars[0].vval.v_list;
10176 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010177 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010178 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010179 {
10180 if (argvars[2].v_type != VAR_UNKNOWN)
10181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010182 before = get_tv_number_chk(&argvars[2], &error);
10183 if (error)
10184 return; /* type error; errmsg already given */
10185
Bram Moolenaar758711c2005-02-02 23:11:38 +000010186 if (before == l1->lv_len)
10187 item = NULL;
10188 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010189 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010190 item = list_find(l1, before);
10191 if (item == NULL)
10192 {
10193 EMSGN(_(e_listidx), before);
10194 return;
10195 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010196 }
10197 }
10198 else
10199 item = NULL;
10200 list_extend(l1, l2, item);
10201
Bram Moolenaare9a41262005-01-15 22:18:47 +000010202 copy_tv(&argvars[0], rettv);
10203 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010205 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10206 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010207 dict_T *d1, *d2;
10208 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010209 char_u *action;
10210 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010211 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010212 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010213
10214 d1 = argvars[0].vval.v_dict;
10215 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010216 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010217 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010218 {
10219 /* Check the third argument. */
10220 if (argvars[2].v_type != VAR_UNKNOWN)
10221 {
10222 static char *(av[]) = {"keep", "force", "error"};
10223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010224 action = get_tv_string_chk(&argvars[2]);
10225 if (action == NULL)
10226 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010227 for (i = 0; i < 3; ++i)
10228 if (STRCMP(action, av[i]) == 0)
10229 break;
10230 if (i == 3)
10231 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010232 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010233 return;
10234 }
10235 }
10236 else
10237 action = (char_u *)"force";
10238
10239 /* Go over all entries in the second dict and add them to the
10240 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010241 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010242 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010244 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010245 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010246 --todo;
10247 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010248 if (d1->dv_scope != 0)
10249 {
10250 /* Disallow replacing a builtin function in l: and g:.
10251 * Check the key to be valid when adding to any
10252 * scope. */
10253 if (d1->dv_scope == VAR_DEF_SCOPE
10254 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10255 && var_check_func_name(hi2->hi_key,
10256 di1 == NULL))
10257 break;
10258 if (!valid_varname(hi2->hi_key))
10259 break;
10260 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010261 if (di1 == NULL)
10262 {
10263 di1 = dictitem_copy(HI2DI(hi2));
10264 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10265 dictitem_free(di1);
10266 }
10267 else if (*action == 'e')
10268 {
10269 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10270 break;
10271 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010272 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010273 {
10274 clear_tv(&di1->di_tv);
10275 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10276 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 }
10278 }
10279
Bram Moolenaare9a41262005-01-15 22:18:47 +000010280 copy_tv(&argvars[0], rettv);
10281 }
10282 }
10283 else
10284 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010285}
10286
10287/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010288 * "feedkeys()" function
10289 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010290 static void
10291f_feedkeys(argvars, rettv)
10292 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010293 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010294{
10295 int remap = TRUE;
10296 char_u *keys, *flags;
10297 char_u nbuf[NUMBUFLEN];
10298 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010299 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010300
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010301 /* This is not allowed in the sandbox. If the commands would still be
10302 * executed in the sandbox it would be OK, but it probably happens later,
10303 * when "sandbox" is no longer set. */
10304 if (check_secure())
10305 return;
10306
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010307 keys = get_tv_string(&argvars[0]);
10308 if (*keys != NUL)
10309 {
10310 if (argvars[1].v_type != VAR_UNKNOWN)
10311 {
10312 flags = get_tv_string_buf(&argvars[1], nbuf);
10313 for ( ; *flags != NUL; ++flags)
10314 {
10315 switch (*flags)
10316 {
10317 case 'n': remap = FALSE; break;
10318 case 'm': remap = TRUE; break;
10319 case 't': typed = TRUE; break;
10320 }
10321 }
10322 }
10323
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010324 /* Need to escape K_SPECIAL and CSI before putting the string in the
10325 * typeahead buffer. */
10326 keys_esc = vim_strsave_escape_csi(keys);
10327 if (keys_esc != NULL)
10328 {
10329 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010330 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010331 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010332 if (vgetc_busy)
10333 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010334 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010335 }
10336}
10337
10338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 * "filereadable()" function
10340 */
10341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010342f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010343 typval_T *argvars;
10344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010346 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 char_u *p;
10348 int n;
10349
Bram Moolenaarc236c162008-07-13 17:41:49 +000010350#ifndef O_NONBLOCK
10351# define O_NONBLOCK 0
10352#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010353 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010354 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10355 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 {
10357 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010358 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 }
10360 else
10361 n = FALSE;
10362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010363 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364}
10365
10366/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010367 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 * rights to write into.
10369 */
10370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010371f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010372 typval_T *argvars;
10373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010375 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376}
10377
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010378static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010379
10380 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010381findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010382 typval_T *argvars;
10383 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010384 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010385{
10386#ifdef FEAT_SEARCHPATH
10387 char_u *fname;
10388 char_u *fresult = NULL;
10389 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10390 char_u *p;
10391 char_u pathbuf[NUMBUFLEN];
10392 int count = 1;
10393 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010394 int error = FALSE;
10395#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010396
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010397 rettv->vval.v_string = NULL;
10398 rettv->v_type = VAR_STRING;
10399
10400#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010401 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010402
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010403 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010405 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10406 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010407 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010408 else
10409 {
10410 if (*p != NUL)
10411 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010412
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010413 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010414 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010415 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010416 }
10417
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010418 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10419 error = TRUE;
10420
10421 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010423 do
10424 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010425 if (rettv->v_type == VAR_STRING)
10426 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010427 fresult = find_file_in_path_option(first ? fname : NULL,
10428 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010429 0, first, path,
10430 find_what,
10431 curbuf->b_ffname,
10432 find_what == FINDFILE_DIR
10433 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010434 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010435
10436 if (fresult != NULL && rettv->v_type == VAR_LIST)
10437 list_append_string(rettv->vval.v_list, fresult, -1);
10438
10439 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010441
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010442 if (rettv->v_type == VAR_STRING)
10443 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010444#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010445}
10446
Bram Moolenaar33570922005-01-25 22:26:29 +000010447static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10448static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010449
10450/*
10451 * Implementation of map() and filter().
10452 */
10453 static void
10454filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010455 typval_T *argvars;
10456 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010457 int map;
10458{
10459 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010460 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010461 listitem_T *li, *nli;
10462 list_T *l = NULL;
10463 dictitem_T *di;
10464 hashtab_T *ht;
10465 hashitem_T *hi;
10466 dict_T *d = NULL;
10467 typval_T save_val;
10468 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010469 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010470 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010471 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10472 char *arg_errmsg = (map ? N_("map() argument")
10473 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010474 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010475 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010476
Bram Moolenaare9a41262005-01-15 22:18:47 +000010477 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010478 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010479 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010480 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010481 return;
10482 }
10483 else if (argvars[0].v_type == VAR_DICT)
10484 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010485 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010486 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010487 return;
10488 }
10489 else
10490 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010491 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010492 return;
10493 }
10494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 expr = get_tv_string_buf_chk(&argvars[1], buf);
10496 /* On type errors, the preceding call has already displayed an error
10497 * message. Avoid a misleading error message for an empty string that
10498 * was not passed as argument. */
10499 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010501 prepare_vimvar(VV_VAL, &save_val);
10502 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010503
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010504 /* We reset "did_emsg" to be able to detect whether an error
10505 * occurred during evaluation of the expression. */
10506 save_did_emsg = did_emsg;
10507 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010508
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010509 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010510 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010512 vimvars[VV_KEY].vv_type = VAR_STRING;
10513
10514 ht = &d->dv_hashtab;
10515 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010516 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010517 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 if (!HASHITEM_EMPTY(hi))
10520 {
10521 --todo;
10522 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010523 if (tv_check_lock(di->di_tv.v_lock,
10524 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010525 break;
10526 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010527 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010528 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 break;
10530 if (!map && rem)
10531 dictitem_remove(d, di);
10532 clear_tv(&vimvars[VV_KEY].vv_tv);
10533 }
10534 }
10535 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 }
10537 else
10538 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010539 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10540
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010541 for (li = l->lv_first; li != NULL; li = nli)
10542 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010543 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010544 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010546 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010547 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010548 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010549 break;
10550 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010551 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010552 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010553 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010554 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010555
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010556 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010557 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010558
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010559 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010560 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010561
10562 copy_tv(&argvars[0], rettv);
10563}
10564
10565 static int
10566filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010567 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 char_u *expr;
10569 int map;
10570 int *remp;
10571{
Bram Moolenaar33570922005-01-25 22:26:29 +000010572 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010574 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010575
Bram Moolenaar33570922005-01-25 22:26:29 +000010576 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010577 s = expr;
10578 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010579 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 if (*s != NUL) /* check for trailing chars after expr */
10581 {
10582 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010583 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010584 }
10585 if (map)
10586 {
10587 /* map(): replace the list item value */
10588 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010589 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 *tv = rettv;
10591 }
10592 else
10593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010594 int error = FALSE;
10595
Bram Moolenaare9a41262005-01-15 22:18:47 +000010596 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010597 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010598 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010599 /* On type error, nothing has been removed; return FAIL to stop the
10600 * loop. The error message was given by get_tv_number_chk(). */
10601 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010602 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010604 retval = OK;
10605theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010606 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010607 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010608}
10609
10610/*
10611 * "filter()" function
10612 */
10613 static void
10614f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010615 typval_T *argvars;
10616 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010617{
10618 filter_map(argvars, rettv, FALSE);
10619}
10620
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010621/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010622 * "finddir({fname}[, {path}[, {count}]])" function
10623 */
10624 static void
10625f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010626 typval_T *argvars;
10627 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010628{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010629 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010630}
10631
10632/*
10633 * "findfile({fname}[, {path}[, {count}]])" function
10634 */
10635 static void
10636f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010637 typval_T *argvars;
10638 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010639{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010640 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010641}
10642
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010643#ifdef FEAT_FLOAT
10644/*
10645 * "float2nr({float})" function
10646 */
10647 static void
10648f_float2nr(argvars, rettv)
10649 typval_T *argvars;
10650 typval_T *rettv;
10651{
10652 float_T f;
10653
10654 if (get_float_arg(argvars, &f) == OK)
10655 {
10656 if (f < -0x7fffffff)
10657 rettv->vval.v_number = -0x7fffffff;
10658 else if (f > 0x7fffffff)
10659 rettv->vval.v_number = 0x7fffffff;
10660 else
10661 rettv->vval.v_number = (varnumber_T)f;
10662 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010663}
10664
10665/*
10666 * "floor({float})" function
10667 */
10668 static void
10669f_floor(argvars, rettv)
10670 typval_T *argvars;
10671 typval_T *rettv;
10672{
10673 float_T f;
10674
10675 rettv->v_type = VAR_FLOAT;
10676 if (get_float_arg(argvars, &f) == OK)
10677 rettv->vval.v_float = floor(f);
10678 else
10679 rettv->vval.v_float = 0.0;
10680}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010681
10682/*
10683 * "fmod()" function
10684 */
10685 static void
10686f_fmod(argvars, rettv)
10687 typval_T *argvars;
10688 typval_T *rettv;
10689{
10690 float_T fx, fy;
10691
10692 rettv->v_type = VAR_FLOAT;
10693 if (get_float_arg(argvars, &fx) == OK
10694 && get_float_arg(&argvars[1], &fy) == OK)
10695 rettv->vval.v_float = fmod(fx, fy);
10696 else
10697 rettv->vval.v_float = 0.0;
10698}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010699#endif
10700
Bram Moolenaar0d660222005-01-07 21:51:51 +000010701/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010702 * "fnameescape({string})" function
10703 */
10704 static void
10705f_fnameescape(argvars, rettv)
10706 typval_T *argvars;
10707 typval_T *rettv;
10708{
10709 rettv->vval.v_string = vim_strsave_fnameescape(
10710 get_tv_string(&argvars[0]), FALSE);
10711 rettv->v_type = VAR_STRING;
10712}
10713
10714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 * "fnamemodify({fname}, {mods})" function
10716 */
10717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010719 typval_T *argvars;
10720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721{
10722 char_u *fname;
10723 char_u *mods;
10724 int usedlen = 0;
10725 int len;
10726 char_u *fbuf = NULL;
10727 char_u buf[NUMBUFLEN];
10728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010729 fname = get_tv_string_chk(&argvars[0]);
10730 mods = get_tv_string_buf_chk(&argvars[1], buf);
10731 if (fname == NULL || mods == NULL)
10732 fname = NULL;
10733 else
10734 {
10735 len = (int)STRLEN(fname);
10736 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010739 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010743 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 vim_free(fbuf);
10745}
10746
Bram Moolenaar33570922005-01-25 22:26:29 +000010747static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748
10749/*
10750 * "foldclosed()" function
10751 */
10752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010753foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010754 typval_T *argvars;
10755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 int end;
10757{
10758#ifdef FEAT_FOLDING
10759 linenr_T lnum;
10760 linenr_T first, last;
10761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10764 {
10765 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10766 {
10767 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010768 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 return;
10772 }
10773 }
10774#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776}
10777
10778/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010779 * "foldclosed()" function
10780 */
10781 static void
10782f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010783 typval_T *argvars;
10784 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010785{
10786 foldclosed_both(argvars, rettv, FALSE);
10787}
10788
10789/*
10790 * "foldclosedend()" function
10791 */
10792 static void
10793f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010794 typval_T *argvars;
10795 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010796{
10797 foldclosed_both(argvars, rettv, TRUE);
10798}
10799
10800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 * "foldlevel()" function
10802 */
10803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010804f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010805 typval_T *argvars;
10806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807{
10808#ifdef FEAT_FOLDING
10809 linenr_T lnum;
10810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010811 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815}
10816
10817/*
10818 * "foldtext()" function
10819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010822 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824{
10825#ifdef FEAT_FOLDING
10826 linenr_T lnum;
10827 char_u *s;
10828 char_u *r;
10829 int len;
10830 char *txt;
10831#endif
10832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833 rettv->v_type = VAR_STRING;
10834 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010836 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10837 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10838 <= curbuf->b_ml.ml_line_count
10839 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 {
10841 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10843 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 {
10845 if (!linewhite(lnum))
10846 break;
10847 ++lnum;
10848 }
10849
10850 /* Find interesting text in this line. */
10851 s = skipwhite(ml_get(lnum));
10852 /* skip C comment-start */
10853 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010856 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010857 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010858 {
10859 s = skipwhite(ml_get(lnum + 1));
10860 if (*s == '*')
10861 s = skipwhite(s + 1);
10862 }
10863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 txt = _("+-%s%3ld lines: ");
10865 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010866 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 + 20 /* for %3ld */
10868 + STRLEN(s))); /* concatenated */
10869 if (r != NULL)
10870 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010871 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10872 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10873 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 len = (int)STRLEN(r);
10875 STRCAT(r, s);
10876 /* remove 'foldmarker' and 'commentstring' */
10877 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 }
10880 }
10881#endif
10882}
10883
10884/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010885 * "foldtextresult(lnum)" function
10886 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010888f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010889 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010890 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010891{
10892#ifdef FEAT_FOLDING
10893 linenr_T lnum;
10894 char_u *text;
10895 char_u buf[51];
10896 foldinfo_T foldinfo;
10897 int fold_count;
10898#endif
10899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010900 rettv->v_type = VAR_STRING;
10901 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010902#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010904 /* treat illegal types and illegal string values for {lnum} the same */
10905 if (lnum < 0)
10906 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010907 fold_count = foldedCount(curwin, lnum, &foldinfo);
10908 if (fold_count > 0)
10909 {
10910 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10911 &foldinfo, buf);
10912 if (text == buf)
10913 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010915 }
10916#endif
10917}
10918
10919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 * "foreground()" function
10921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010923f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010924 typval_T *argvars UNUSED;
10925 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927#ifdef FEAT_GUI
10928 if (gui.in_use)
10929 gui_mch_set_foreground();
10930#else
10931# ifdef WIN32
10932 win32_set_foreground();
10933# endif
10934#endif
10935}
10936
10937/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010938 * "function()" function
10939 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010941f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010942 typval_T *argvars;
10943 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010944{
10945 char_u *s;
10946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010948 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010949 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010950 /* Don't check an autoload name for existence here. */
10951 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010952 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010953 else
10954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010955 rettv->vval.v_string = vim_strsave(s);
10956 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010957 }
10958}
10959
10960/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010961 * "garbagecollect()" function
10962 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010963 static void
10964f_garbagecollect(argvars, rettv)
10965 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010966 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010967{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010968 /* This is postponed until we are back at the toplevel, because we may be
10969 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10970 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010971
10972 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10973 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010974}
10975
10976/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010977 * "get()" function
10978 */
10979 static void
10980f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010981 typval_T *argvars;
10982 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010983{
Bram Moolenaar33570922005-01-25 22:26:29 +000010984 listitem_T *li;
10985 list_T *l;
10986 dictitem_T *di;
10987 dict_T *d;
10988 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010989
Bram Moolenaare9a41262005-01-15 22:18:47 +000010990 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010991 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010992 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010993 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010994 int error = FALSE;
10995
10996 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10997 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010998 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010999 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011000 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001 else if (argvars[0].v_type == VAR_DICT)
11002 {
11003 if ((d = argvars[0].vval.v_dict) != NULL)
11004 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011005 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011006 if (di != NULL)
11007 tv = &di->di_tv;
11008 }
11009 }
11010 else
11011 EMSG2(_(e_listdictarg), "get()");
11012
11013 if (tv == NULL)
11014 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011016 copy_tv(&argvars[2], rettv);
11017 }
11018 else
11019 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011020}
11021
Bram Moolenaar342337a2005-07-21 21:11:17 +000011022static 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 +000011023
11024/*
11025 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011026 * Return a range (from start to end) of lines in rettv from the specified
11027 * buffer.
11028 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011029 */
11030 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011031get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011032 buf_T *buf;
11033 linenr_T start;
11034 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011035 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011036 typval_T *rettv;
11037{
11038 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011039
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011040 if (retlist && rettv_list_alloc(rettv) == FAIL)
11041 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011042
11043 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11044 return;
11045
11046 if (!retlist)
11047 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011048 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11049 p = ml_get_buf(buf, start, FALSE);
11050 else
11051 p = (char_u *)"";
11052
11053 rettv->v_type = VAR_STRING;
11054 rettv->vval.v_string = vim_strsave(p);
11055 }
11056 else
11057 {
11058 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011059 return;
11060
11061 if (start < 1)
11062 start = 1;
11063 if (end > buf->b_ml.ml_line_count)
11064 end = buf->b_ml.ml_line_count;
11065 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011066 if (list_append_string(rettv->vval.v_list,
11067 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011068 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011069 }
11070}
11071
11072/*
11073 * "getbufline()" function
11074 */
11075 static void
11076f_getbufline(argvars, rettv)
11077 typval_T *argvars;
11078 typval_T *rettv;
11079{
11080 linenr_T lnum;
11081 linenr_T end;
11082 buf_T *buf;
11083
11084 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11085 ++emsg_off;
11086 buf = get_buf_tv(&argvars[0]);
11087 --emsg_off;
11088
Bram Moolenaar661b1822005-07-28 22:36:45 +000011089 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011090 if (argvars[2].v_type == VAR_UNKNOWN)
11091 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011092 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011093 end = get_tv_lnum_buf(&argvars[2], buf);
11094
Bram Moolenaar342337a2005-07-21 21:11:17 +000011095 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011096}
11097
Bram Moolenaar0d660222005-01-07 21:51:51 +000011098/*
11099 * "getbufvar()" function
11100 */
11101 static void
11102f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 typval_T *argvars;
11104 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011105{
11106 buf_T *buf;
11107 buf_T *save_curbuf;
11108 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011109 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011111 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11112 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011113 ++emsg_off;
11114 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011115
11116 rettv->v_type = VAR_STRING;
11117 rettv->vval.v_string = NULL;
11118
11119 if (buf != NULL && varname != NULL)
11120 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011121 /* set curbuf to be our buf, temporarily */
11122 save_curbuf = curbuf;
11123 curbuf = buf;
11124
Bram Moolenaar0d660222005-01-07 21:51:51 +000011125 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011126 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011127 else if (STRCMP(varname, "changedtick") == 0)
11128 {
11129 rettv->v_type = VAR_NUMBER;
11130 rettv->vval.v_number = curbuf->b_changedtick;
11131 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011132 else
11133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011134 if (*varname == NUL)
11135 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11136 * scope prefix before the NUL byte is required by
11137 * find_var_in_ht(). */
11138 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011139 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011140 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011141 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011142 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011143 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011144
11145 /* restore previous notion of curbuf */
11146 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011147 }
11148
11149 --emsg_off;
11150}
11151
11152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 * "getchar()" function
11154 */
11155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011156f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011157 typval_T *argvars;
11158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159{
11160 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011161 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011163 /* Position the cursor. Needed after a message that ends in a space. */
11164 windgoto(msg_row, msg_col);
11165
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 ++no_mapping;
11167 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011168 for (;;)
11169 {
11170 if (argvars[0].v_type == VAR_UNKNOWN)
11171 /* getchar(): blocking wait. */
11172 n = safe_vgetc();
11173 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11174 /* getchar(1): only check if char avail */
11175 n = vpeekc();
11176 else if (error || vpeekc() == NUL)
11177 /* illegal argument or getchar(0) and no char avail: return zero */
11178 n = 0;
11179 else
11180 /* getchar(0) and char avail: return char */
11181 n = safe_vgetc();
11182 if (n == K_IGNORE)
11183 continue;
11184 break;
11185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 --no_mapping;
11187 --allow_keys;
11188
Bram Moolenaar219b8702006-11-01 14:32:36 +000011189 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11190 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11191 vimvars[VV_MOUSE_COL].vv_nr = 0;
11192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011193 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 if (IS_SPECIAL(n) || mod_mask != 0)
11195 {
11196 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11197 int i = 0;
11198
11199 /* Turn a special key into three bytes, plus modifier. */
11200 if (mod_mask != 0)
11201 {
11202 temp[i++] = K_SPECIAL;
11203 temp[i++] = KS_MODIFIER;
11204 temp[i++] = mod_mask;
11205 }
11206 if (IS_SPECIAL(n))
11207 {
11208 temp[i++] = K_SPECIAL;
11209 temp[i++] = K_SECOND(n);
11210 temp[i++] = K_THIRD(n);
11211 }
11212#ifdef FEAT_MBYTE
11213 else if (has_mbyte)
11214 i += (*mb_char2bytes)(n, temp + i);
11215#endif
11216 else
11217 temp[i++] = n;
11218 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219 rettv->v_type = VAR_STRING;
11220 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011221
11222#ifdef FEAT_MOUSE
11223 if (n == K_LEFTMOUSE
11224 || n == K_LEFTMOUSE_NM
11225 || n == K_LEFTDRAG
11226 || n == K_LEFTRELEASE
11227 || n == K_LEFTRELEASE_NM
11228 || n == K_MIDDLEMOUSE
11229 || n == K_MIDDLEDRAG
11230 || n == K_MIDDLERELEASE
11231 || n == K_RIGHTMOUSE
11232 || n == K_RIGHTDRAG
11233 || n == K_RIGHTRELEASE
11234 || n == K_X1MOUSE
11235 || n == K_X1DRAG
11236 || n == K_X1RELEASE
11237 || n == K_X2MOUSE
11238 || n == K_X2DRAG
11239 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011240 || n == K_MOUSELEFT
11241 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011242 || n == K_MOUSEDOWN
11243 || n == K_MOUSEUP)
11244 {
11245 int row = mouse_row;
11246 int col = mouse_col;
11247 win_T *win;
11248 linenr_T lnum;
11249# ifdef FEAT_WINDOWS
11250 win_T *wp;
11251# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011252 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011253
11254 if (row >= 0 && col >= 0)
11255 {
11256 /* Find the window at the mouse coordinates and compute the
11257 * text position. */
11258 win = mouse_find_win(&row, &col);
11259 (void)mouse_comp_pos(win, &row, &col, &lnum);
11260# ifdef FEAT_WINDOWS
11261 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011262 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011263# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011264 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011265 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11266 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11267 }
11268 }
11269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 }
11271}
11272
11273/*
11274 * "getcharmod()" function
11275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011278 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282}
11283
11284/*
11285 * "getcmdline()" function
11286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011289 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011292 rettv->v_type = VAR_STRING;
11293 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294}
11295
11296/*
11297 * "getcmdpos()" function
11298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011301 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011304 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305}
11306
11307/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011308 * "getcmdtype()" function
11309 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011310 static void
11311f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011312 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011313 typval_T *rettv;
11314{
11315 rettv->v_type = VAR_STRING;
11316 rettv->vval.v_string = alloc(2);
11317 if (rettv->vval.v_string != NULL)
11318 {
11319 rettv->vval.v_string[0] = get_cmdline_type();
11320 rettv->vval.v_string[1] = NUL;
11321 }
11322}
11323
11324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 * "getcwd()" function
11326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011329 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011332 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011335 rettv->vval.v_string = NULL;
11336 cwd = alloc(MAXPATHL);
11337 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011339 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11340 {
11341 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011343 if (rettv->vval.v_string != NULL)
11344 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011346 }
11347 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 }
11349}
11350
11351/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011352 * "getfontname()" function
11353 */
11354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011356 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011357 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011358{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359 rettv->v_type = VAR_STRING;
11360 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011361#ifdef FEAT_GUI
11362 if (gui.in_use)
11363 {
11364 GuiFont font;
11365 char_u *name = NULL;
11366
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011367 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011368 {
11369 /* Get the "Normal" font. Either the name saved by
11370 * hl_set_font_name() or from the font ID. */
11371 font = gui.norm_font;
11372 name = hl_get_font_name();
11373 }
11374 else
11375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011377 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11378 return;
11379 font = gui_mch_get_font(name, FALSE);
11380 if (font == NOFONT)
11381 return; /* Invalid font name, return empty string. */
11382 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011383 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011384 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011385 gui_mch_free_font(font);
11386 }
11387#endif
11388}
11389
11390/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011391 * "getfperm({fname})" function
11392 */
11393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011394f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011395 typval_T *argvars;
11396 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011397{
11398 char_u *fname;
11399 struct stat st;
11400 char_u *perm = NULL;
11401 char_u flags[] = "rwx";
11402 int i;
11403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011407 if (mch_stat((char *)fname, &st) >= 0)
11408 {
11409 perm = vim_strsave((char_u *)"---------");
11410 if (perm != NULL)
11411 {
11412 for (i = 0; i < 9; i++)
11413 {
11414 if (st.st_mode & (1 << (8 - i)))
11415 perm[i] = flags[i % 3];
11416 }
11417 }
11418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011420}
11421
11422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 * "getfsize({fname})" function
11424 */
11425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011427 typval_T *argvars;
11428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429{
11430 char_u *fname;
11431 struct stat st;
11432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436
11437 if (mch_stat((char *)fname, &st) >= 0)
11438 {
11439 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011442 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011444
11445 /* non-perfect check for overflow */
11446 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11447 rettv->vval.v_number = -2;
11448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 }
11450 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011451 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452}
11453
11454/*
11455 * "getftime({fname})" function
11456 */
11457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011458f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011459 typval_T *argvars;
11460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461{
11462 char_u *fname;
11463 struct stat st;
11464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466
11467 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471}
11472
11473/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011474 * "getftype({fname})" function
11475 */
11476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011478 typval_T *argvars;
11479 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011480{
11481 char_u *fname;
11482 struct stat st;
11483 char_u *type = NULL;
11484 char *t;
11485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011489 if (mch_lstat((char *)fname, &st) >= 0)
11490 {
11491#ifdef S_ISREG
11492 if (S_ISREG(st.st_mode))
11493 t = "file";
11494 else if (S_ISDIR(st.st_mode))
11495 t = "dir";
11496# ifdef S_ISLNK
11497 else if (S_ISLNK(st.st_mode))
11498 t = "link";
11499# endif
11500# ifdef S_ISBLK
11501 else if (S_ISBLK(st.st_mode))
11502 t = "bdev";
11503# endif
11504# ifdef S_ISCHR
11505 else if (S_ISCHR(st.st_mode))
11506 t = "cdev";
11507# endif
11508# ifdef S_ISFIFO
11509 else if (S_ISFIFO(st.st_mode))
11510 t = "fifo";
11511# endif
11512# ifdef S_ISSOCK
11513 else if (S_ISSOCK(st.st_mode))
11514 t = "fifo";
11515# endif
11516 else
11517 t = "other";
11518#else
11519# ifdef S_IFMT
11520 switch (st.st_mode & S_IFMT)
11521 {
11522 case S_IFREG: t = "file"; break;
11523 case S_IFDIR: t = "dir"; break;
11524# ifdef S_IFLNK
11525 case S_IFLNK: t = "link"; break;
11526# endif
11527# ifdef S_IFBLK
11528 case S_IFBLK: t = "bdev"; break;
11529# endif
11530# ifdef S_IFCHR
11531 case S_IFCHR: t = "cdev"; break;
11532# endif
11533# ifdef S_IFIFO
11534 case S_IFIFO: t = "fifo"; break;
11535# endif
11536# ifdef S_IFSOCK
11537 case S_IFSOCK: t = "socket"; break;
11538# endif
11539 default: t = "other";
11540 }
11541# else
11542 if (mch_isdir(fname))
11543 t = "dir";
11544 else
11545 t = "file";
11546# endif
11547#endif
11548 type = vim_strsave((char_u *)t);
11549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011550 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011551}
11552
11553/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011554 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011555 */
11556 static void
11557f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011558 typval_T *argvars;
11559 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011560{
11561 linenr_T lnum;
11562 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011563 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011564
11565 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011566 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011567 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011568 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011569 retlist = FALSE;
11570 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011571 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011572 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011573 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011574 retlist = TRUE;
11575 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011576
Bram Moolenaar342337a2005-07-21 21:11:17 +000011577 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011578}
11579
11580/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011581 * "getmatches()" function
11582 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011583 static void
11584f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011585 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011586 typval_T *rettv;
11587{
11588#ifdef FEAT_SEARCH_EXTRA
11589 dict_T *dict;
11590 matchitem_T *cur = curwin->w_match_head;
11591
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011592 if (rettv_list_alloc(rettv) == OK)
11593 {
11594 while (cur != NULL)
11595 {
11596 dict = dict_alloc();
11597 if (dict == NULL)
11598 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011599 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11600 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11601 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11602 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11603 list_append_dict(rettv->vval.v_list, dict);
11604 cur = cur->next;
11605 }
11606 }
11607#endif
11608}
11609
11610/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011611 * "getpid()" function
11612 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011613 static void
11614f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011615 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011616 typval_T *rettv;
11617{
11618 rettv->vval.v_number = mch_get_pid();
11619}
11620
11621/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011622 * "getpos(string)" function
11623 */
11624 static void
11625f_getpos(argvars, rettv)
11626 typval_T *argvars;
11627 typval_T *rettv;
11628{
11629 pos_T *fp;
11630 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011631 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011632
11633 if (rettv_list_alloc(rettv) == OK)
11634 {
11635 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011636 fp = var2fpos(&argvars[0], TRUE, &fnum);
11637 if (fnum != -1)
11638 list_append_number(l, (varnumber_T)fnum);
11639 else
11640 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011641 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11642 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011643 list_append_number(l, (fp != NULL)
11644 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011645 : (varnumber_T)0);
11646 list_append_number(l,
11647#ifdef FEAT_VIRTUALEDIT
11648 (fp != NULL) ? (varnumber_T)fp->coladd :
11649#endif
11650 (varnumber_T)0);
11651 }
11652 else
11653 rettv->vval.v_number = FALSE;
11654}
11655
11656/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011657 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011658 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011659 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011660f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011661 typval_T *argvars UNUSED;
11662 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011663{
11664#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011665 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011666#endif
11667
Bram Moolenaar2641f772005-03-25 21:58:17 +000011668#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011669 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011670 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011671 wp = NULL;
11672 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11673 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011674 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011675 if (wp == NULL)
11676 return;
11677 }
11678
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011679 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011680 }
11681#endif
11682}
11683
11684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685 * "getreg()" function
11686 */
11687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011688f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011689 typval_T *argvars;
11690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691{
11692 char_u *strregname;
11693 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011694 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011695 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011697 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011699 strregname = get_tv_string_chk(&argvars[0]);
11700 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011701 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011702 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011705 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 regname = (strregname == NULL ? '"' : *strregname);
11707 if (regname == 0)
11708 regname = '"';
11709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011710 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011711 rettv->vval.v_string = error ? NULL :
11712 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713}
11714
11715/*
11716 * "getregtype()" function
11717 */
11718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011719f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011720 typval_T *argvars;
11721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722{
11723 char_u *strregname;
11724 int regname;
11725 char_u buf[NUMBUFLEN + 2];
11726 long reglen = 0;
11727
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011728 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011729 {
11730 strregname = get_tv_string_chk(&argvars[0]);
11731 if (strregname == NULL) /* type error; errmsg already given */
11732 {
11733 rettv->v_type = VAR_STRING;
11734 rettv->vval.v_string = NULL;
11735 return;
11736 }
11737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 else
11739 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011740 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741
11742 regname = (strregname == NULL ? '"' : *strregname);
11743 if (regname == 0)
11744 regname = '"';
11745
11746 buf[0] = NUL;
11747 buf[1] = NUL;
11748 switch (get_reg_type(regname, &reglen))
11749 {
11750 case MLINE: buf[0] = 'V'; break;
11751 case MCHAR: buf[0] = 'v'; break;
11752#ifdef FEAT_VISUAL
11753 case MBLOCK:
11754 buf[0] = Ctrl_V;
11755 sprintf((char *)buf + 1, "%ld", reglen + 1);
11756 break;
11757#endif
11758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011759 rettv->v_type = VAR_STRING;
11760 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761}
11762
11763/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011764 * "gettabvar()" function
11765 */
11766 static void
11767f_gettabvar(argvars, rettv)
11768 typval_T *argvars;
11769 typval_T *rettv;
11770{
11771 tabpage_T *tp;
11772 dictitem_T *v;
11773 char_u *varname;
11774
11775 rettv->v_type = VAR_STRING;
11776 rettv->vval.v_string = NULL;
11777
11778 varname = get_tv_string_chk(&argvars[1]);
11779 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11780 if (tp != NULL && varname != NULL)
11781 {
11782 /* look up the variable */
11783 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11784 if (v != NULL)
11785 copy_tv(&v->di_tv, rettv);
11786 }
11787}
11788
11789/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011790 * "gettabwinvar()" function
11791 */
11792 static void
11793f_gettabwinvar(argvars, rettv)
11794 typval_T *argvars;
11795 typval_T *rettv;
11796{
11797 getwinvar(argvars, rettv, 1);
11798}
11799
11800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 * "getwinposx()" function
11802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011804f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011805 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809#ifdef FEAT_GUI
11810 if (gui.in_use)
11811 {
11812 int x, y;
11813
11814 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816 }
11817#endif
11818}
11819
11820/*
11821 * "getwinposy()" function
11822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011824f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011825 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829#ifdef FEAT_GUI
11830 if (gui.in_use)
11831 {
11832 int x, y;
11833
11834 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011835 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 }
11837#endif
11838}
11839
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011840/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011841 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011842 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011843 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011844find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011845 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011846 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011847{
11848#ifdef FEAT_WINDOWS
11849 win_T *wp;
11850#endif
11851 int nr;
11852
11853 nr = get_tv_number_chk(vp, NULL);
11854
11855#ifdef FEAT_WINDOWS
11856 if (nr < 0)
11857 return NULL;
11858 if (nr == 0)
11859 return curwin;
11860
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011861 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11862 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011863 if (--nr <= 0)
11864 break;
11865 return wp;
11866#else
11867 if (nr == 0 || nr == 1)
11868 return curwin;
11869 return NULL;
11870#endif
11871}
11872
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873/*
11874 * "getwinvar()" function
11875 */
11876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011878 typval_T *argvars;
11879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011881 getwinvar(argvars, rettv, 0);
11882}
11883
11884/*
11885 * getwinvar() and gettabwinvar()
11886 */
11887 static void
11888getwinvar(argvars, rettv, off)
11889 typval_T *argvars;
11890 typval_T *rettv;
11891 int off; /* 1 for gettabwinvar() */
11892{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 win_T *win, *oldcurwin;
11894 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011895 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011896 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011898#ifdef FEAT_WINDOWS
11899 if (off == 1)
11900 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11901 else
11902 tp = curtab;
11903#endif
11904 win = find_win_by_nr(&argvars[off], tp);
11905 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011906 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011908 rettv->v_type = VAR_STRING;
11909 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910
11911 if (win != NULL && varname != NULL)
11912 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011913 /* Set curwin to be our win, temporarily. Also set curbuf, so
11914 * that we can get buffer-local options. */
11915 oldcurwin = curwin;
11916 curwin = win;
11917 curbuf = win->w_buffer;
11918
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011920 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 else
11922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011923 if (*varname == NUL)
11924 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11925 * scope prefix before the NUL byte is required by
11926 * find_var_in_ht(). */
11927 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011929 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011931 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011933
11934 /* restore previous notion of curwin */
11935 curwin = oldcurwin;
11936 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937 }
11938
11939 --emsg_off;
11940}
11941
11942/*
11943 * "glob()" function
11944 */
11945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011947 typval_T *argvars;
11948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011950 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011952 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011954 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011955 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011957 if (argvars[1].v_type != VAR_UNKNOWN)
11958 {
11959 if (get_tv_number_chk(&argvars[1], &error))
11960 options |= WILD_KEEP_ALL;
11961 if (argvars[2].v_type != VAR_UNKNOWN
11962 && get_tv_number_chk(&argvars[2], &error))
11963 {
11964 rettv->v_type = VAR_LIST;
11965 rettv->vval.v_list = NULL;
11966 }
11967 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011968 if (!error)
11969 {
11970 ExpandInit(&xpc);
11971 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011972 if (p_wic)
11973 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011974 if (rettv->v_type == VAR_STRING)
11975 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011976 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011977 else if (rettv_list_alloc(rettv) != FAIL)
11978 {
11979 int i;
11980
11981 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11982 NULL, options, WILD_ALL_KEEP);
11983 for (i = 0; i < xpc.xp_numfiles; i++)
11984 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11985
11986 ExpandCleanup(&xpc);
11987 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011988 }
11989 else
11990 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991}
11992
11993/*
11994 * "globpath()" function
11995 */
11996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011998 typval_T *argvars;
11999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012001 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012003 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012004 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012006 /* When the optional second argument is non-zero, don't remove matches
12007 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12008 if (argvars[2].v_type != VAR_UNKNOWN
12009 && get_tv_number_chk(&argvars[2], &error))
12010 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012012 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012013 rettv->vval.v_string = NULL;
12014 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012015 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12016 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017}
12018
12019/*
12020 * "has()" function
12021 */
12022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012024 typval_T *argvars;
12025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026{
12027 int i;
12028 char_u *name;
12029 int n = FALSE;
12030 static char *(has_list[]) =
12031 {
12032#ifdef AMIGA
12033 "amiga",
12034# ifdef FEAT_ARP
12035 "arp",
12036# endif
12037#endif
12038#ifdef __BEOS__
12039 "beos",
12040#endif
12041#ifdef MSDOS
12042# ifdef DJGPP
12043 "dos32",
12044# else
12045 "dos16",
12046# endif
12047#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012048#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049 "mac",
12050#endif
12051#if defined(MACOS_X_UNIX)
12052 "macunix",
12053#endif
12054#ifdef OS2
12055 "os2",
12056#endif
12057#ifdef __QNX__
12058 "qnx",
12059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060#ifdef UNIX
12061 "unix",
12062#endif
12063#ifdef VMS
12064 "vms",
12065#endif
12066#ifdef WIN16
12067 "win16",
12068#endif
12069#ifdef WIN32
12070 "win32",
12071#endif
12072#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12073 "win32unix",
12074#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012075#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 "win64",
12077#endif
12078#ifdef EBCDIC
12079 "ebcdic",
12080#endif
12081#ifndef CASE_INSENSITIVE_FILENAME
12082 "fname_case",
12083#endif
12084#ifdef FEAT_ARABIC
12085 "arabic",
12086#endif
12087#ifdef FEAT_AUTOCMD
12088 "autocmd",
12089#endif
12090#ifdef FEAT_BEVAL
12091 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012092# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12093 "balloon_multiline",
12094# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095#endif
12096#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12097 "builtin_terms",
12098# ifdef ALL_BUILTIN_TCAPS
12099 "all_builtin_terms",
12100# endif
12101#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012102#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12103 || defined(FEAT_GUI_W32) \
12104 || defined(FEAT_GUI_MOTIF))
12105 "browsefilter",
12106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107#ifdef FEAT_BYTEOFF
12108 "byte_offset",
12109#endif
12110#ifdef FEAT_CINDENT
12111 "cindent",
12112#endif
12113#ifdef FEAT_CLIENTSERVER
12114 "clientserver",
12115#endif
12116#ifdef FEAT_CLIPBOARD
12117 "clipboard",
12118#endif
12119#ifdef FEAT_CMDL_COMPL
12120 "cmdline_compl",
12121#endif
12122#ifdef FEAT_CMDHIST
12123 "cmdline_hist",
12124#endif
12125#ifdef FEAT_COMMENTS
12126 "comments",
12127#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012128#ifdef FEAT_CONCEAL
12129 "conceal",
12130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131#ifdef FEAT_CRYPT
12132 "cryptv",
12133#endif
12134#ifdef FEAT_CSCOPE
12135 "cscope",
12136#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012137#ifdef FEAT_CURSORBIND
12138 "cursorbind",
12139#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012140#ifdef CURSOR_SHAPE
12141 "cursorshape",
12142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143#ifdef DEBUG
12144 "debug",
12145#endif
12146#ifdef FEAT_CON_DIALOG
12147 "dialog_con",
12148#endif
12149#ifdef FEAT_GUI_DIALOG
12150 "dialog_gui",
12151#endif
12152#ifdef FEAT_DIFF
12153 "diff",
12154#endif
12155#ifdef FEAT_DIGRAPHS
12156 "digraphs",
12157#endif
12158#ifdef FEAT_DND
12159 "dnd",
12160#endif
12161#ifdef FEAT_EMACS_TAGS
12162 "emacs_tags",
12163#endif
12164 "eval", /* always present, of course! */
12165#ifdef FEAT_EX_EXTRA
12166 "ex_extra",
12167#endif
12168#ifdef FEAT_SEARCH_EXTRA
12169 "extra_search",
12170#endif
12171#ifdef FEAT_FKMAP
12172 "farsi",
12173#endif
12174#ifdef FEAT_SEARCHPATH
12175 "file_in_path",
12176#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012177#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012178 "filterpipe",
12179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180#ifdef FEAT_FIND_ID
12181 "find_in_path",
12182#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012183#ifdef FEAT_FLOAT
12184 "float",
12185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186#ifdef FEAT_FOLDING
12187 "folding",
12188#endif
12189#ifdef FEAT_FOOTER
12190 "footer",
12191#endif
12192#if !defined(USE_SYSTEM) && defined(UNIX)
12193 "fork",
12194#endif
12195#ifdef FEAT_GETTEXT
12196 "gettext",
12197#endif
12198#ifdef FEAT_GUI
12199 "gui",
12200#endif
12201#ifdef FEAT_GUI_ATHENA
12202# ifdef FEAT_GUI_NEXTAW
12203 "gui_neXtaw",
12204# else
12205 "gui_athena",
12206# endif
12207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208#ifdef FEAT_GUI_GTK
12209 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012212#ifdef FEAT_GUI_GNOME
12213 "gui_gnome",
12214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215#ifdef FEAT_GUI_MAC
12216 "gui_mac",
12217#endif
12218#ifdef FEAT_GUI_MOTIF
12219 "gui_motif",
12220#endif
12221#ifdef FEAT_GUI_PHOTON
12222 "gui_photon",
12223#endif
12224#ifdef FEAT_GUI_W16
12225 "gui_win16",
12226#endif
12227#ifdef FEAT_GUI_W32
12228 "gui_win32",
12229#endif
12230#ifdef FEAT_HANGULIN
12231 "hangul_input",
12232#endif
12233#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12234 "iconv",
12235#endif
12236#ifdef FEAT_INS_EXPAND
12237 "insert_expand",
12238#endif
12239#ifdef FEAT_JUMPLIST
12240 "jumplist",
12241#endif
12242#ifdef FEAT_KEYMAP
12243 "keymap",
12244#endif
12245#ifdef FEAT_LANGMAP
12246 "langmap",
12247#endif
12248#ifdef FEAT_LIBCALL
12249 "libcall",
12250#endif
12251#ifdef FEAT_LINEBREAK
12252 "linebreak",
12253#endif
12254#ifdef FEAT_LISP
12255 "lispindent",
12256#endif
12257#ifdef FEAT_LISTCMDS
12258 "listcmds",
12259#endif
12260#ifdef FEAT_LOCALMAP
12261 "localmap",
12262#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012263#ifdef FEAT_LUA
12264# ifndef DYNAMIC_LUA
12265 "lua",
12266# endif
12267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268#ifdef FEAT_MENU
12269 "menu",
12270#endif
12271#ifdef FEAT_SESSION
12272 "mksession",
12273#endif
12274#ifdef FEAT_MODIFY_FNAME
12275 "modify_fname",
12276#endif
12277#ifdef FEAT_MOUSE
12278 "mouse",
12279#endif
12280#ifdef FEAT_MOUSESHAPE
12281 "mouseshape",
12282#endif
12283#if defined(UNIX) || defined(VMS)
12284# ifdef FEAT_MOUSE_DEC
12285 "mouse_dec",
12286# endif
12287# ifdef FEAT_MOUSE_GPM
12288 "mouse_gpm",
12289# endif
12290# ifdef FEAT_MOUSE_JSB
12291 "mouse_jsbterm",
12292# endif
12293# ifdef FEAT_MOUSE_NET
12294 "mouse_netterm",
12295# endif
12296# ifdef FEAT_MOUSE_PTERM
12297 "mouse_pterm",
12298# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012299# ifdef FEAT_MOUSE_SGR
12300 "mouse_sgr",
12301# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012302# ifdef FEAT_SYSMOUSE
12303 "mouse_sysmouse",
12304# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012305# ifdef FEAT_MOUSE_URXVT
12306 "mouse_urxvt",
12307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308# ifdef FEAT_MOUSE_XTERM
12309 "mouse_xterm",
12310# endif
12311#endif
12312#ifdef FEAT_MBYTE
12313 "multi_byte",
12314#endif
12315#ifdef FEAT_MBYTE_IME
12316 "multi_byte_ime",
12317#endif
12318#ifdef FEAT_MULTI_LANG
12319 "multi_lang",
12320#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012321#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012322#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012323 "mzscheme",
12324#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326#ifdef FEAT_OLE
12327 "ole",
12328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329#ifdef FEAT_PATH_EXTRA
12330 "path_extra",
12331#endif
12332#ifdef FEAT_PERL
12333#ifndef DYNAMIC_PERL
12334 "perl",
12335#endif
12336#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012337#ifdef FEAT_PERSISTENT_UNDO
12338 "persistent_undo",
12339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340#ifdef FEAT_PYTHON
12341#ifndef DYNAMIC_PYTHON
12342 "python",
12343#endif
12344#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012345#ifdef FEAT_PYTHON3
12346#ifndef DYNAMIC_PYTHON3
12347 "python3",
12348#endif
12349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350#ifdef FEAT_POSTSCRIPT
12351 "postscript",
12352#endif
12353#ifdef FEAT_PRINTER
12354 "printer",
12355#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012356#ifdef FEAT_PROFILE
12357 "profile",
12358#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012359#ifdef FEAT_RELTIME
12360 "reltime",
12361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362#ifdef FEAT_QUICKFIX
12363 "quickfix",
12364#endif
12365#ifdef FEAT_RIGHTLEFT
12366 "rightleft",
12367#endif
12368#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12369 "ruby",
12370#endif
12371#ifdef FEAT_SCROLLBIND
12372 "scrollbind",
12373#endif
12374#ifdef FEAT_CMDL_INFO
12375 "showcmd",
12376 "cmdline_info",
12377#endif
12378#ifdef FEAT_SIGNS
12379 "signs",
12380#endif
12381#ifdef FEAT_SMARTINDENT
12382 "smartindent",
12383#endif
12384#ifdef FEAT_SNIFF
12385 "sniff",
12386#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012387#ifdef STARTUPTIME
12388 "startuptime",
12389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390#ifdef FEAT_STL_OPT
12391 "statusline",
12392#endif
12393#ifdef FEAT_SUN_WORKSHOP
12394 "sun_workshop",
12395#endif
12396#ifdef FEAT_NETBEANS_INTG
12397 "netbeans_intg",
12398#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012399#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012400 "spell",
12401#endif
12402#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403 "syntax",
12404#endif
12405#if defined(USE_SYSTEM) || !defined(UNIX)
12406 "system",
12407#endif
12408#ifdef FEAT_TAG_BINS
12409 "tag_binary",
12410#endif
12411#ifdef FEAT_TAG_OLDSTATIC
12412 "tag_old_static",
12413#endif
12414#ifdef FEAT_TAG_ANYWHITE
12415 "tag_any_white",
12416#endif
12417#ifdef FEAT_TCL
12418# ifndef DYNAMIC_TCL
12419 "tcl",
12420# endif
12421#endif
12422#ifdef TERMINFO
12423 "terminfo",
12424#endif
12425#ifdef FEAT_TERMRESPONSE
12426 "termresponse",
12427#endif
12428#ifdef FEAT_TEXTOBJ
12429 "textobjects",
12430#endif
12431#ifdef HAVE_TGETENT
12432 "tgetent",
12433#endif
12434#ifdef FEAT_TITLE
12435 "title",
12436#endif
12437#ifdef FEAT_TOOLBAR
12438 "toolbar",
12439#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012440#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12441 "unnamedplus",
12442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443#ifdef FEAT_USR_CMDS
12444 "user-commands", /* was accidentally included in 5.4 */
12445 "user_commands",
12446#endif
12447#ifdef FEAT_VIMINFO
12448 "viminfo",
12449#endif
12450#ifdef FEAT_VERTSPLIT
12451 "vertsplit",
12452#endif
12453#ifdef FEAT_VIRTUALEDIT
12454 "virtualedit",
12455#endif
12456#ifdef FEAT_VISUAL
12457 "visual",
12458#endif
12459#ifdef FEAT_VISUALEXTRA
12460 "visualextra",
12461#endif
12462#ifdef FEAT_VREPLACE
12463 "vreplace",
12464#endif
12465#ifdef FEAT_WILDIGN
12466 "wildignore",
12467#endif
12468#ifdef FEAT_WILDMENU
12469 "wildmenu",
12470#endif
12471#ifdef FEAT_WINDOWS
12472 "windows",
12473#endif
12474#ifdef FEAT_WAK
12475 "winaltkeys",
12476#endif
12477#ifdef FEAT_WRITEBACKUP
12478 "writebackup",
12479#endif
12480#ifdef FEAT_XIM
12481 "xim",
12482#endif
12483#ifdef FEAT_XFONTSET
12484 "xfontset",
12485#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012486#ifdef FEAT_XPM_W32
12487 "xpm_w32",
12488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489#ifdef USE_XSMP
12490 "xsmp",
12491#endif
12492#ifdef USE_XSMP_INTERACT
12493 "xsmp_interact",
12494#endif
12495#ifdef FEAT_XCLIPBOARD
12496 "xterm_clipboard",
12497#endif
12498#ifdef FEAT_XTERM_SAVE
12499 "xterm_save",
12500#endif
12501#if defined(UNIX) && defined(FEAT_X11)
12502 "X11",
12503#endif
12504 NULL
12505 };
12506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012507 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 for (i = 0; has_list[i] != NULL; ++i)
12509 if (STRICMP(name, has_list[i]) == 0)
12510 {
12511 n = TRUE;
12512 break;
12513 }
12514
12515 if (n == FALSE)
12516 {
12517 if (STRNICMP(name, "patch", 5) == 0)
12518 n = has_patch(atoi((char *)name + 5));
12519 else if (STRICMP(name, "vim_starting") == 0)
12520 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012521#ifdef FEAT_MBYTE
12522 else if (STRICMP(name, "multi_byte_encoding") == 0)
12523 n = has_mbyte;
12524#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012525#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12526 else if (STRICMP(name, "balloon_multiline") == 0)
12527 n = multiline_balloon_available();
12528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529#ifdef DYNAMIC_TCL
12530 else if (STRICMP(name, "tcl") == 0)
12531 n = tcl_enabled(FALSE);
12532#endif
12533#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12534 else if (STRICMP(name, "iconv") == 0)
12535 n = iconv_enabled(FALSE);
12536#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012537#ifdef DYNAMIC_LUA
12538 else if (STRICMP(name, "lua") == 0)
12539 n = lua_enabled(FALSE);
12540#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012541#ifdef DYNAMIC_MZSCHEME
12542 else if (STRICMP(name, "mzscheme") == 0)
12543 n = mzscheme_enabled(FALSE);
12544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545#ifdef DYNAMIC_RUBY
12546 else if (STRICMP(name, "ruby") == 0)
12547 n = ruby_enabled(FALSE);
12548#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012549#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550#ifdef DYNAMIC_PYTHON
12551 else if (STRICMP(name, "python") == 0)
12552 n = python_enabled(FALSE);
12553#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012554#endif
12555#ifdef FEAT_PYTHON3
12556#ifdef DYNAMIC_PYTHON3
12557 else if (STRICMP(name, "python3") == 0)
12558 n = python3_enabled(FALSE);
12559#endif
12560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561#ifdef DYNAMIC_PERL
12562 else if (STRICMP(name, "perl") == 0)
12563 n = perl_enabled(FALSE);
12564#endif
12565#ifdef FEAT_GUI
12566 else if (STRICMP(name, "gui_running") == 0)
12567 n = (gui.in_use || gui.starting);
12568# ifdef FEAT_GUI_W32
12569 else if (STRICMP(name, "gui_win32s") == 0)
12570 n = gui_is_win32s();
12571# endif
12572# ifdef FEAT_BROWSE
12573 else if (STRICMP(name, "browse") == 0)
12574 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12575# endif
12576#endif
12577#ifdef FEAT_SYN_HL
12578 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012579 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580#endif
12581#if defined(WIN3264)
12582 else if (STRICMP(name, "win95") == 0)
12583 n = mch_windows95();
12584#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012585#ifdef FEAT_NETBEANS_INTG
12586 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012587 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589 }
12590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592}
12593
12594/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012595 * "has_key()" function
12596 */
12597 static void
12598f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012599 typval_T *argvars;
12600 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012601{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012602 if (argvars[0].v_type != VAR_DICT)
12603 {
12604 EMSG(_(e_dictreq));
12605 return;
12606 }
12607 if (argvars[0].vval.v_dict == NULL)
12608 return;
12609
12610 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012611 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012612}
12613
12614/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012615 * "haslocaldir()" function
12616 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012617 static void
12618f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012619 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012620 typval_T *rettv;
12621{
12622 rettv->vval.v_number = (curwin->w_localdir != NULL);
12623}
12624
12625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626 * "hasmapto()" function
12627 */
12628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012629f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012630 typval_T *argvars;
12631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632{
12633 char_u *name;
12634 char_u *mode;
12635 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012636 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012639 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640 mode = (char_u *)"nvo";
12641 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012644 if (argvars[2].v_type != VAR_UNKNOWN)
12645 abbr = get_tv_number(&argvars[2]);
12646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647
Bram Moolenaar2c932302006-03-18 21:42:09 +000012648 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012651 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652}
12653
12654/*
12655 * "histadd()" function
12656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012658f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012659 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661{
12662#ifdef FEAT_CMDHIST
12663 int histype;
12664 char_u *str;
12665 char_u buf[NUMBUFLEN];
12666#endif
12667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012668 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669 if (check_restricted() || check_secure())
12670 return;
12671#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12673 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 if (histype >= 0)
12675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 if (*str != NUL)
12678 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012679 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012681 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 return;
12683 }
12684 }
12685#endif
12686}
12687
12688/*
12689 * "histdel()" function
12690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012693 typval_T *argvars UNUSED;
12694 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695{
12696#ifdef FEAT_CMDHIST
12697 int n;
12698 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012699 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012701 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12702 if (str == NULL)
12703 n = 0;
12704 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012706 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012707 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012710 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 else
12712 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012713 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 get_tv_string_buf(&argvars[1], buf));
12715 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716#endif
12717}
12718
12719/*
12720 * "histget()" function
12721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012723f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012724 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726{
12727#ifdef FEAT_CMDHIST
12728 int type;
12729 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012730 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012732 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12733 if (str == NULL)
12734 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012736 {
12737 type = get_histtype(str);
12738 if (argvars[1].v_type == VAR_UNKNOWN)
12739 idx = get_history_idx(type);
12740 else
12741 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12742 /* -1 on type error */
12743 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012746 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749}
12750
12751/*
12752 * "histnr()" function
12753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758{
12759 int i;
12760
12761#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012762 char_u *history = get_tv_string_chk(&argvars[0]);
12763
12764 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765 if (i >= HIST_CMD && i < HIST_COUNT)
12766 i = get_history_idx(i);
12767 else
12768#endif
12769 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771}
12772
12773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 * "highlightID(name)" function
12775 */
12776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012778 typval_T *argvars;
12779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012781 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782}
12783
12784/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012785 * "highlight_exists()" function
12786 */
12787 static void
12788f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012789 typval_T *argvars;
12790 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012791{
12792 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12793}
12794
12795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 * "hostname()" function
12797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012800 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802{
12803 char_u hostname[256];
12804
12805 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806 rettv->v_type = VAR_STRING;
12807 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808}
12809
12810/*
12811 * iconv() function
12812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012815 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817{
12818#ifdef FEAT_MBYTE
12819 char_u buf1[NUMBUFLEN];
12820 char_u buf2[NUMBUFLEN];
12821 char_u *from, *to, *str;
12822 vimconv_T vimconv;
12823#endif
12824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825 rettv->v_type = VAR_STRING;
12826 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827
12828#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829 str = get_tv_string(&argvars[0]);
12830 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12831 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832 vimconv.vc_type = CONV_NONE;
12833 convert_setup(&vimconv, from, to);
12834
12835 /* If the encodings are equal, no conversion needed. */
12836 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840
12841 convert_setup(&vimconv, NULL, NULL);
12842 vim_free(from);
12843 vim_free(to);
12844#endif
12845}
12846
12847/*
12848 * "indent()" function
12849 */
12850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012852 typval_T *argvars;
12853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854{
12855 linenr_T lnum;
12856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862}
12863
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012864/*
12865 * "index()" function
12866 */
12867 static void
12868f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012869 typval_T *argvars;
12870 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012871{
Bram Moolenaar33570922005-01-25 22:26:29 +000012872 list_T *l;
12873 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012874 long idx = 0;
12875 int ic = FALSE;
12876
12877 rettv->vval.v_number = -1;
12878 if (argvars[0].v_type != VAR_LIST)
12879 {
12880 EMSG(_(e_listreq));
12881 return;
12882 }
12883 l = argvars[0].vval.v_list;
12884 if (l != NULL)
12885 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012886 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012887 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012889 int error = FALSE;
12890
Bram Moolenaar758711c2005-02-02 23:11:38 +000012891 /* Start at specified item. Use the cached index that list_find()
12892 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012893 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012894 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012895 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012896 ic = get_tv_number_chk(&argvars[3], &error);
12897 if (error)
12898 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012899 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012900
Bram Moolenaar758711c2005-02-02 23:11:38 +000012901 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012902 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012903 {
12904 rettv->vval.v_number = idx;
12905 break;
12906 }
12907 }
12908}
12909
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910static int inputsecret_flag = 0;
12911
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012912static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12913
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012915 * This function is used by f_input() and f_inputdialog() functions. The third
12916 * argument to f_input() specifies the type of completion to use at the
12917 * prompt. The third argument to f_inputdialog() specifies the value to return
12918 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 */
12920 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012921get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012922 typval_T *argvars;
12923 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012924 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012926 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 char_u *p = NULL;
12928 int c;
12929 char_u buf[NUMBUFLEN];
12930 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012931 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012932 int xp_type = EXPAND_NOTHING;
12933 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012935 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012936 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937
12938#ifdef NO_CONSOLE_INPUT
12939 /* While starting up, there is no place to enter text. */
12940 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942#endif
12943
12944 cmd_silent = FALSE; /* Want to see the prompt. */
12945 if (prompt != NULL)
12946 {
12947 /* Only the part of the message after the last NL is considered as
12948 * prompt for the command line */
12949 p = vim_strrchr(prompt, '\n');
12950 if (p == NULL)
12951 p = prompt;
12952 else
12953 {
12954 ++p;
12955 c = *p;
12956 *p = NUL;
12957 msg_start();
12958 msg_clr_eos();
12959 msg_puts_attr(prompt, echo_attr);
12960 msg_didout = FALSE;
12961 msg_starthere();
12962 *p = c;
12963 }
12964 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012966 if (argvars[1].v_type != VAR_UNKNOWN)
12967 {
12968 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12969 if (defstr != NULL)
12970 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012972 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012973 {
12974 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012975 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012976 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012977
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012978 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012979 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012980
Bram Moolenaar4463f292005-09-25 22:20:24 +000012981 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12982 if (xp_name == NULL)
12983 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012984
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012985 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012986
Bram Moolenaar4463f292005-09-25 22:20:24 +000012987 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12988 &xp_arg) == FAIL)
12989 return;
12990 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012991 }
12992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012993 if (defstr != NULL)
12994 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012995 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12996 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020012997 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012998 && argvars[1].v_type != VAR_UNKNOWN
12999 && argvars[2].v_type != VAR_UNKNOWN)
13000 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13001 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013002
13003 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013005 /* since the user typed this, no need to wait for return */
13006 need_wait_return = FALSE;
13007 msg_didout = FALSE;
13008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009 cmd_silent = cmd_silent_save;
13010}
13011
13012/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013013 * "input()" function
13014 * Also handles inputsecret() when inputsecret is set.
13015 */
13016 static void
13017f_input(argvars, rettv)
13018 typval_T *argvars;
13019 typval_T *rettv;
13020{
13021 get_user_input(argvars, rettv, FALSE);
13022}
13023
13024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 * "inputdialog()" function
13026 */
13027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013028f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013029 typval_T *argvars;
13030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031{
13032#if defined(FEAT_GUI_TEXTDIALOG)
13033 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13034 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13035 {
13036 char_u *message;
13037 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013038 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013040 message = get_tv_string_chk(&argvars[0]);
13041 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013042 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013043 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 else
13045 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013046 if (message != NULL && defstr != NULL
13047 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013048 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013049 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050 else
13051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013052 if (message != NULL && defstr != NULL
13053 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013054 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013055 rettv->vval.v_string = vim_strsave(
13056 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013058 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061 }
13062 else
13063#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013064 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065}
13066
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013067/*
13068 * "inputlist()" function
13069 */
13070 static void
13071f_inputlist(argvars, rettv)
13072 typval_T *argvars;
13073 typval_T *rettv;
13074{
13075 listitem_T *li;
13076 int selected;
13077 int mouse_used;
13078
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013079#ifdef NO_CONSOLE_INPUT
13080 /* While starting up, there is no place to enter text. */
13081 if (no_console_input())
13082 return;
13083#endif
13084 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13085 {
13086 EMSG2(_(e_listarg), "inputlist()");
13087 return;
13088 }
13089
13090 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013091 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013092 lines_left = Rows; /* avoid more prompt */
13093 msg_scroll = TRUE;
13094 msg_clr_eos();
13095
13096 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13097 {
13098 msg_puts(get_tv_string(&li->li_tv));
13099 msg_putchar('\n');
13100 }
13101
13102 /* Ask for choice. */
13103 selected = prompt_for_number(&mouse_used);
13104 if (mouse_used)
13105 selected -= lines_left;
13106
13107 rettv->vval.v_number = selected;
13108}
13109
13110
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13112
13113/*
13114 * "inputrestore()" function
13115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013117f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013118 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120{
13121 if (ga_userinput.ga_len > 0)
13122 {
13123 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13125 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013126 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 }
13128 else if (p_verbose > 1)
13129 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013130 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013131 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 }
13133}
13134
13135/*
13136 * "inputsave()" function
13137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013140 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013143 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 if (ga_grow(&ga_userinput, 1) == OK)
13145 {
13146 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13147 + ga_userinput.ga_len);
13148 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013149 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150 }
13151 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013152 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153}
13154
13155/*
13156 * "inputsecret()" function
13157 */
13158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013159f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013160 typval_T *argvars;
13161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162{
13163 ++cmdline_star;
13164 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013165 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 --cmdline_star;
13167 --inputsecret_flag;
13168}
13169
13170/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013171 * "insert()" function
13172 */
13173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013174f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013175 typval_T *argvars;
13176 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013177{
13178 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013179 listitem_T *item;
13180 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013181 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013182
13183 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013184 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013185 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013186 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013187 {
13188 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013189 before = get_tv_number_chk(&argvars[2], &error);
13190 if (error)
13191 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013192
Bram Moolenaar758711c2005-02-02 23:11:38 +000013193 if (before == l->lv_len)
13194 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013195 else
13196 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013197 item = list_find(l, before);
13198 if (item == NULL)
13199 {
13200 EMSGN(_(e_listidx), before);
13201 l = NULL;
13202 }
13203 }
13204 if (l != NULL)
13205 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013206 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013207 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013208 }
13209 }
13210}
13211
13212/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013213 * "invert(expr)" function
13214 */
13215 static void
13216f_invert(argvars, rettv)
13217 typval_T *argvars;
13218 typval_T *rettv;
13219{
13220 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13221}
13222
13223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 * "isdirectory()" function
13225 */
13226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013227f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013228 typval_T *argvars;
13229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232}
13233
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013234/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013235 * "islocked()" function
13236 */
13237 static void
13238f_islocked(argvars, rettv)
13239 typval_T *argvars;
13240 typval_T *rettv;
13241{
13242 lval_T lv;
13243 char_u *end;
13244 dictitem_T *di;
13245
13246 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013247 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13248 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013249 if (end != NULL && lv.ll_name != NULL)
13250 {
13251 if (*end != NUL)
13252 EMSG(_(e_trailing));
13253 else
13254 {
13255 if (lv.ll_tv == NULL)
13256 {
13257 if (check_changedtick(lv.ll_name))
13258 rettv->vval.v_number = 1; /* always locked */
13259 else
13260 {
13261 di = find_var(lv.ll_name, NULL);
13262 if (di != NULL)
13263 {
13264 /* Consider a variable locked when:
13265 * 1. the variable itself is locked
13266 * 2. the value of the variable is locked.
13267 * 3. the List or Dict value is locked.
13268 */
13269 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13270 || tv_islocked(&di->di_tv));
13271 }
13272 }
13273 }
13274 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013275 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013276 else if (lv.ll_newkey != NULL)
13277 EMSG2(_(e_dictkey), lv.ll_newkey);
13278 else if (lv.ll_list != NULL)
13279 /* List item. */
13280 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13281 else
13282 /* Dictionary item. */
13283 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13284 }
13285 }
13286
13287 clear_lval(&lv);
13288}
13289
Bram Moolenaar33570922005-01-25 22:26:29 +000013290static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013291
13292/*
13293 * Turn a dict into a list:
13294 * "what" == 0: list of keys
13295 * "what" == 1: list of values
13296 * "what" == 2: list of items
13297 */
13298 static void
13299dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013300 typval_T *argvars;
13301 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013302 int what;
13303{
Bram Moolenaar33570922005-01-25 22:26:29 +000013304 list_T *l2;
13305 dictitem_T *di;
13306 hashitem_T *hi;
13307 listitem_T *li;
13308 listitem_T *li2;
13309 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013310 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013311
Bram Moolenaar8c711452005-01-14 21:53:12 +000013312 if (argvars[0].v_type != VAR_DICT)
13313 {
13314 EMSG(_(e_dictreq));
13315 return;
13316 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013317 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013318 return;
13319
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013320 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013321 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013322
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013323 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013324 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013325 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013326 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013327 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013328 --todo;
13329 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013330
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013331 li = listitem_alloc();
13332 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013333 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013334 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013335
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013336 if (what == 0)
13337 {
13338 /* keys() */
13339 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013340 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013341 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13342 }
13343 else if (what == 1)
13344 {
13345 /* values() */
13346 copy_tv(&di->di_tv, &li->li_tv);
13347 }
13348 else
13349 {
13350 /* items() */
13351 l2 = list_alloc();
13352 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013353 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013354 li->li_tv.vval.v_list = l2;
13355 if (l2 == NULL)
13356 break;
13357 ++l2->lv_refcount;
13358
13359 li2 = listitem_alloc();
13360 if (li2 == NULL)
13361 break;
13362 list_append(l2, li2);
13363 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013364 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013365 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13366
13367 li2 = listitem_alloc();
13368 if (li2 == NULL)
13369 break;
13370 list_append(l2, li2);
13371 copy_tv(&di->di_tv, &li2->li_tv);
13372 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013373 }
13374 }
13375}
13376
13377/*
13378 * "items(dict)" function
13379 */
13380 static void
13381f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013382 typval_T *argvars;
13383 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013384{
13385 dict_list(argvars, rettv, 2);
13386}
13387
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013389 * "join()" function
13390 */
13391 static void
13392f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013393 typval_T *argvars;
13394 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013395{
13396 garray_T ga;
13397 char_u *sep;
13398
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013399 if (argvars[0].v_type != VAR_LIST)
13400 {
13401 EMSG(_(e_listreq));
13402 return;
13403 }
13404 if (argvars[0].vval.v_list == NULL)
13405 return;
13406 if (argvars[1].v_type == VAR_UNKNOWN)
13407 sep = (char_u *)" ";
13408 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013409 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013410
13411 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013412
13413 if (sep != NULL)
13414 {
13415 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013416 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013417 ga_append(&ga, NUL);
13418 rettv->vval.v_string = (char_u *)ga.ga_data;
13419 }
13420 else
13421 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013422}
13423
13424/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013425 * "keys()" function
13426 */
13427 static void
13428f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013429 typval_T *argvars;
13430 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013431{
13432 dict_list(argvars, rettv, 0);
13433}
13434
13435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 * "last_buffer_nr()" function.
13437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013440 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442{
13443 int n = 0;
13444 buf_T *buf;
13445
13446 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13447 if (n < buf->b_fnum)
13448 n = buf->b_fnum;
13449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013450 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013451}
13452
13453/*
13454 * "len()" function
13455 */
13456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013457f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013458 typval_T *argvars;
13459 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013460{
13461 switch (argvars[0].v_type)
13462 {
13463 case VAR_STRING:
13464 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013465 rettv->vval.v_number = (varnumber_T)STRLEN(
13466 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013467 break;
13468 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013469 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013470 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013471 case VAR_DICT:
13472 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13473 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013474 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013475 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013476 break;
13477 }
13478}
13479
Bram Moolenaar33570922005-01-25 22:26:29 +000013480static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013481
13482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013484 typval_T *argvars;
13485 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013486 int type;
13487{
13488#ifdef FEAT_LIBCALL
13489 char_u *string_in;
13490 char_u **string_result;
13491 int nr_result;
13492#endif
13493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013494 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013495 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013496 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013497
13498 if (check_restricted() || check_secure())
13499 return;
13500
13501#ifdef FEAT_LIBCALL
13502 /* The first two args must be strings, otherwise its meaningless */
13503 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13504 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013505 string_in = NULL;
13506 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013507 string_in = argvars[2].vval.v_string;
13508 if (type == VAR_NUMBER)
13509 string_result = NULL;
13510 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013511 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013512 if (mch_libcall(argvars[0].vval.v_string,
13513 argvars[1].vval.v_string,
13514 string_in,
13515 argvars[2].vval.v_number,
13516 string_result,
13517 &nr_result) == OK
13518 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013520 }
13521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522}
13523
13524/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013525 * "libcall()" function
13526 */
13527 static void
13528f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013529 typval_T *argvars;
13530 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013531{
13532 libcall_common(argvars, rettv, VAR_STRING);
13533}
13534
13535/*
13536 * "libcallnr()" function
13537 */
13538 static void
13539f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013540 typval_T *argvars;
13541 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013542{
13543 libcall_common(argvars, rettv, VAR_NUMBER);
13544}
13545
13546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 * "line(string)" function
13548 */
13549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013550f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013551 typval_T *argvars;
13552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553{
13554 linenr_T lnum = 0;
13555 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013556 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013558 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 if (fp != NULL)
13560 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013561 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562}
13563
13564/*
13565 * "line2byte(lnum)" function
13566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013568f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013569 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571{
13572#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574#else
13575 linenr_T lnum;
13576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013579 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013581 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13582 if (rettv->vval.v_number >= 0)
13583 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584#endif
13585}
13586
13587/*
13588 * "lispindent(lnum)" function
13589 */
13590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013591f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013592 typval_T *argvars;
13593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594{
13595#ifdef FEAT_LISP
13596 pos_T pos;
13597 linenr_T lnum;
13598
13599 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013600 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13602 {
13603 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013604 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 curwin->w_cursor = pos;
13606 }
13607 else
13608#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610}
13611
13612/*
13613 * "localtime()" function
13614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013616f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013617 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621}
13622
Bram Moolenaar33570922005-01-25 22:26:29 +000013623static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624
13625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013627 typval_T *argvars;
13628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 int exact;
13630{
13631 char_u *keys;
13632 char_u *which;
13633 char_u buf[NUMBUFLEN];
13634 char_u *keys_buf = NULL;
13635 char_u *rhs;
13636 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013637 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013638 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013639 mapblock_T *mp;
13640 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641
13642 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013643 rettv->v_type = VAR_STRING;
13644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 if (*keys == NUL)
13648 return;
13649
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013650 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013652 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013653 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013654 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013655 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013656 if (argvars[3].v_type != VAR_UNKNOWN)
13657 get_dict = get_tv_number(&argvars[3]);
13658 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 else
13661 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013662 if (which == NULL)
13663 return;
13664
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 mode = get_map_mode(&which, 0);
13666
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013667 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013668 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013670
13671 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013673 /* Return a string. */
13674 if (rhs != NULL)
13675 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676
Bram Moolenaarbd743252010-10-20 21:23:33 +020013677 }
13678 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13679 {
13680 /* Return a dictionary. */
13681 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13682 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13683 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684
Bram Moolenaarbd743252010-10-20 21:23:33 +020013685 dict_add_nr_str(dict, "lhs", 0L, lhs);
13686 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13687 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13688 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13689 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13690 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13691 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13692 dict_add_nr_str(dict, "mode", 0L, mapmode);
13693
13694 vim_free(lhs);
13695 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696 }
13697}
13698
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013699#ifdef FEAT_FLOAT
13700/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013701 * "log()" function
13702 */
13703 static void
13704f_log(argvars, rettv)
13705 typval_T *argvars;
13706 typval_T *rettv;
13707{
13708 float_T f;
13709
13710 rettv->v_type = VAR_FLOAT;
13711 if (get_float_arg(argvars, &f) == OK)
13712 rettv->vval.v_float = log(f);
13713 else
13714 rettv->vval.v_float = 0.0;
13715}
13716
13717/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013718 * "log10()" function
13719 */
13720 static void
13721f_log10(argvars, rettv)
13722 typval_T *argvars;
13723 typval_T *rettv;
13724{
13725 float_T f;
13726
13727 rettv->v_type = VAR_FLOAT;
13728 if (get_float_arg(argvars, &f) == OK)
13729 rettv->vval.v_float = log10(f);
13730 else
13731 rettv->vval.v_float = 0.0;
13732}
13733#endif
13734
Bram Moolenaar1dced572012-04-05 16:54:08 +020013735#ifdef FEAT_LUA
13736/*
13737 * "luaeval()" function
13738 */
13739 static void
13740f_luaeval(argvars, rettv)
13741 typval_T *argvars;
13742 typval_T *rettv;
13743{
13744 char_u *str;
13745 char_u buf[NUMBUFLEN];
13746
13747 str = get_tv_string_buf(&argvars[0], buf);
13748 do_luaeval(str, argvars + 1, rettv);
13749}
13750#endif
13751
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013753 * "map()" function
13754 */
13755 static void
13756f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013757 typval_T *argvars;
13758 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013759{
13760 filter_map(argvars, rettv, TRUE);
13761}
13762
13763/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013764 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765 */
13766 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013767f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013768 typval_T *argvars;
13769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013771 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772}
13773
13774/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013775 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 */
13777 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013778f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013779 typval_T *argvars;
13780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013782 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783}
13784
Bram Moolenaar33570922005-01-25 22:26:29 +000013785static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786
13787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013788find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013789 typval_T *argvars;
13790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791 int type;
13792{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013793 char_u *str = NULL;
13794 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 char_u *pat;
13796 regmatch_T regmatch;
13797 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013798 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799 char_u *save_cpo;
13800 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013801 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013802 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013803 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013804 list_T *l = NULL;
13805 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013806 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013807 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808
13809 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13810 save_cpo = p_cpo;
13811 p_cpo = (char_u *)"";
13812
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013813 rettv->vval.v_number = -1;
13814 if (type == 3)
13815 {
13816 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013817 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013818 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013819 }
13820 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013822 rettv->v_type = VAR_STRING;
13823 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013826 if (argvars[0].v_type == VAR_LIST)
13827 {
13828 if ((l = argvars[0].vval.v_list) == NULL)
13829 goto theend;
13830 li = l->lv_first;
13831 }
13832 else
13833 expr = str = get_tv_string(&argvars[0]);
13834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13836 if (pat == NULL)
13837 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013838
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013839 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013841 int error = FALSE;
13842
13843 start = get_tv_number_chk(&argvars[2], &error);
13844 if (error)
13845 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013846 if (l != NULL)
13847 {
13848 li = list_find(l, start);
13849 if (li == NULL)
13850 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013851 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013852 }
13853 else
13854 {
13855 if (start < 0)
13856 start = 0;
13857 if (start > (long)STRLEN(str))
13858 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013859 /* When "count" argument is there ignore matches before "start",
13860 * otherwise skip part of the string. Differs when pattern is "^"
13861 * or "\<". */
13862 if (argvars[3].v_type != VAR_UNKNOWN)
13863 startcol = start;
13864 else
13865 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013866 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013867
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013868 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013869 nth = get_tv_number_chk(&argvars[3], &error);
13870 if (error)
13871 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 }
13873
13874 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13875 if (regmatch.regprog != NULL)
13876 {
13877 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013878
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013879 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013880 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013881 if (l != NULL)
13882 {
13883 if (li == NULL)
13884 {
13885 match = FALSE;
13886 break;
13887 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013888 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013889 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013890 if (str == NULL)
13891 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013892 }
13893
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013894 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013895
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013896 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013897 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013898 if (l == NULL && !match)
13899 break;
13900
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013901 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013902 if (l != NULL)
13903 {
13904 li = li->li_next;
13905 ++idx;
13906 }
13907 else
13908 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013909#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013910 startcol = (colnr_T)(regmatch.startp[0]
13911 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013912#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013913 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013914#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013915 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013916 }
13917
13918 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013920 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013921 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013922 int i;
13923
13924 /* return list with matched string and submatches */
13925 for (i = 0; i < NSUBEXP; ++i)
13926 {
13927 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013928 {
13929 if (list_append_string(rettv->vval.v_list,
13930 (char_u *)"", 0) == FAIL)
13931 break;
13932 }
13933 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013934 regmatch.startp[i],
13935 (int)(regmatch.endp[i] - regmatch.startp[i]))
13936 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013937 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013938 }
13939 }
13940 else if (type == 2)
13941 {
13942 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013943 if (l != NULL)
13944 copy_tv(&li->li_tv, rettv);
13945 else
13946 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013948 }
13949 else if (l != NULL)
13950 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 else
13952 {
13953 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013954 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955 (varnumber_T)(regmatch.startp[0] - str);
13956 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013957 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013959 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960 }
13961 }
13962 vim_free(regmatch.regprog);
13963 }
13964
13965theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013966 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 p_cpo = save_cpo;
13968}
13969
13970/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013971 * "match()" function
13972 */
13973 static void
13974f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013975 typval_T *argvars;
13976 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013977{
13978 find_some_match(argvars, rettv, 1);
13979}
13980
13981/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013982 * "matchadd()" function
13983 */
13984 static void
13985f_matchadd(argvars, rettv)
13986 typval_T *argvars;
13987 typval_T *rettv;
13988{
13989#ifdef FEAT_SEARCH_EXTRA
13990 char_u buf[NUMBUFLEN];
13991 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13992 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13993 int prio = 10; /* default priority */
13994 int id = -1;
13995 int error = FALSE;
13996
13997 rettv->vval.v_number = -1;
13998
13999 if (grp == NULL || pat == NULL)
14000 return;
14001 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014002 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014003 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014004 if (argvars[3].v_type != VAR_UNKNOWN)
14005 id = get_tv_number_chk(&argvars[3], &error);
14006 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014007 if (error == TRUE)
14008 return;
14009 if (id >= 1 && id <= 3)
14010 {
14011 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14012 return;
14013 }
14014
14015 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14016#endif
14017}
14018
14019/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014020 * "matcharg()" function
14021 */
14022 static void
14023f_matcharg(argvars, rettv)
14024 typval_T *argvars;
14025 typval_T *rettv;
14026{
14027 if (rettv_list_alloc(rettv) == OK)
14028 {
14029#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014030 int id = get_tv_number(&argvars[0]);
14031 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014032
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014033 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014034 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014035 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14036 {
14037 list_append_string(rettv->vval.v_list,
14038 syn_id2name(m->hlg_id), -1);
14039 list_append_string(rettv->vval.v_list, m->pattern, -1);
14040 }
14041 else
14042 {
14043 list_append_string(rettv->vval.v_list, NUL, -1);
14044 list_append_string(rettv->vval.v_list, NUL, -1);
14045 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014046 }
14047#endif
14048 }
14049}
14050
14051/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014052 * "matchdelete()" function
14053 */
14054 static void
14055f_matchdelete(argvars, rettv)
14056 typval_T *argvars;
14057 typval_T *rettv;
14058{
14059#ifdef FEAT_SEARCH_EXTRA
14060 rettv->vval.v_number = match_delete(curwin,
14061 (int)get_tv_number(&argvars[0]), TRUE);
14062#endif
14063}
14064
14065/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066 * "matchend()" function
14067 */
14068 static void
14069f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014070 typval_T *argvars;
14071 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014072{
14073 find_some_match(argvars, rettv, 0);
14074}
14075
14076/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014077 * "matchlist()" function
14078 */
14079 static void
14080f_matchlist(argvars, rettv)
14081 typval_T *argvars;
14082 typval_T *rettv;
14083{
14084 find_some_match(argvars, rettv, 3);
14085}
14086
14087/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014088 * "matchstr()" function
14089 */
14090 static void
14091f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014092 typval_T *argvars;
14093 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014094{
14095 find_some_match(argvars, rettv, 2);
14096}
14097
Bram Moolenaar33570922005-01-25 22:26:29 +000014098static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014099
14100 static void
14101max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014102 typval_T *argvars;
14103 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014104 int domax;
14105{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014106 long n = 0;
14107 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014108 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014109
14110 if (argvars[0].v_type == VAR_LIST)
14111 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014112 list_T *l;
14113 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014114
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014115 l = argvars[0].vval.v_list;
14116 if (l != NULL)
14117 {
14118 li = l->lv_first;
14119 if (li != NULL)
14120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014121 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014122 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014123 {
14124 li = li->li_next;
14125 if (li == NULL)
14126 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014127 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014128 if (domax ? i > n : i < n)
14129 n = i;
14130 }
14131 }
14132 }
14133 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014134 else if (argvars[0].v_type == VAR_DICT)
14135 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014136 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014137 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014138 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014139 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014140
14141 d = argvars[0].vval.v_dict;
14142 if (d != NULL)
14143 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014144 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014145 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014147 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014148 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014149 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014150 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014151 if (first)
14152 {
14153 n = i;
14154 first = FALSE;
14155 }
14156 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014157 n = i;
14158 }
14159 }
14160 }
14161 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014162 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014163 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014165}
14166
14167/*
14168 * "max()" function
14169 */
14170 static void
14171f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014172 typval_T *argvars;
14173 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014174{
14175 max_min(argvars, rettv, TRUE);
14176}
14177
14178/*
14179 * "min()" function
14180 */
14181 static void
14182f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014183 typval_T *argvars;
14184 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014185{
14186 max_min(argvars, rettv, FALSE);
14187}
14188
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014189static int mkdir_recurse __ARGS((char_u *dir, int prot));
14190
14191/*
14192 * Create the directory in which "dir" is located, and higher levels when
14193 * needed.
14194 */
14195 static int
14196mkdir_recurse(dir, prot)
14197 char_u *dir;
14198 int prot;
14199{
14200 char_u *p;
14201 char_u *updir;
14202 int r = FAIL;
14203
14204 /* Get end of directory name in "dir".
14205 * We're done when it's "/" or "c:/". */
14206 p = gettail_sep(dir);
14207 if (p <= get_past_head(dir))
14208 return OK;
14209
14210 /* If the directory exists we're done. Otherwise: create it.*/
14211 updir = vim_strnsave(dir, (int)(p - dir));
14212 if (updir == NULL)
14213 return FAIL;
14214 if (mch_isdir(updir))
14215 r = OK;
14216 else if (mkdir_recurse(updir, prot) == OK)
14217 r = vim_mkdir_emsg(updir, prot);
14218 vim_free(updir);
14219 return r;
14220}
14221
14222#ifdef vim_mkdir
14223/*
14224 * "mkdir()" function
14225 */
14226 static void
14227f_mkdir(argvars, rettv)
14228 typval_T *argvars;
14229 typval_T *rettv;
14230{
14231 char_u *dir;
14232 char_u buf[NUMBUFLEN];
14233 int prot = 0755;
14234
14235 rettv->vval.v_number = FAIL;
14236 if (check_restricted() || check_secure())
14237 return;
14238
14239 dir = get_tv_string_buf(&argvars[0], buf);
14240 if (argvars[1].v_type != VAR_UNKNOWN)
14241 {
14242 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 prot = get_tv_number_chk(&argvars[2], NULL);
14244 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014245 mkdir_recurse(dir, prot);
14246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014247 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014248}
14249#endif
14250
Bram Moolenaar0d660222005-01-07 21:51:51 +000014251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252 * "mode()" function
14253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014255f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014256 typval_T *argvars;
14257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014259 char_u buf[3];
14260
14261 buf[1] = NUL;
14262 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263
14264#ifdef FEAT_VISUAL
14265 if (VIsual_active)
14266 {
14267 if (VIsual_select)
14268 buf[0] = VIsual_mode + 's' - 'v';
14269 else
14270 buf[0] = VIsual_mode;
14271 }
14272 else
14273#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014274 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14275 || State == CONFIRM)
14276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014278 if (State == ASKMORE)
14279 buf[1] = 'm';
14280 else if (State == CONFIRM)
14281 buf[1] = '?';
14282 }
14283 else if (State == EXTERNCMD)
14284 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 else if (State & INSERT)
14286 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014287#ifdef FEAT_VREPLACE
14288 if (State & VREPLACE_FLAG)
14289 {
14290 buf[0] = 'R';
14291 buf[1] = 'v';
14292 }
14293 else
14294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 if (State & REPLACE_FLAG)
14296 buf[0] = 'R';
14297 else
14298 buf[0] = 'i';
14299 }
14300 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014303 if (exmode_active)
14304 buf[1] = 'v';
14305 }
14306 else if (exmode_active)
14307 {
14308 buf[0] = 'c';
14309 buf[1] = 'e';
14310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014314 if (finish_op)
14315 buf[1] = 'o';
14316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014318 /* Clear out the minor mode when the argument is not a non-zero number or
14319 * non-empty string. */
14320 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014321 buf[1] = NUL;
14322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014323 rettv->vval.v_string = vim_strsave(buf);
14324 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325}
14326
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014327#ifdef FEAT_MZSCHEME
14328/*
14329 * "mzeval()" function
14330 */
14331 static void
14332f_mzeval(argvars, rettv)
14333 typval_T *argvars;
14334 typval_T *rettv;
14335{
14336 char_u *str;
14337 char_u buf[NUMBUFLEN];
14338
14339 str = get_tv_string_buf(&argvars[0], buf);
14340 do_mzeval(str, rettv);
14341}
Bram Moolenaar75676462013-01-30 14:55:42 +010014342
14343 void
14344mzscheme_call_vim(name, args, rettv)
14345 char_u *name;
14346 typval_T *args;
14347 typval_T *rettv;
14348{
14349 typval_T argvars[3];
14350
14351 argvars[0].v_type = VAR_STRING;
14352 argvars[0].vval.v_string = name;
14353 copy_tv(args, &argvars[1]);
14354 argvars[2].v_type = VAR_UNKNOWN;
14355 f_call(argvars, rettv);
14356 clear_tv(&argvars[1]);
14357}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014358#endif
14359
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014361 * "nextnonblank()" function
14362 */
14363 static void
14364f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014365 typval_T *argvars;
14366 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014367{
14368 linenr_T lnum;
14369
14370 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14371 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014372 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014373 {
14374 lnum = 0;
14375 break;
14376 }
14377 if (*skipwhite(ml_get(lnum)) != NUL)
14378 break;
14379 }
14380 rettv->vval.v_number = lnum;
14381}
14382
14383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 * "nr2char()" function
14385 */
14386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014388 typval_T *argvars;
14389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390{
14391 char_u buf[NUMBUFLEN];
14392
14393#ifdef FEAT_MBYTE
14394 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014395 {
14396 int utf8 = 0;
14397
14398 if (argvars[1].v_type != VAR_UNKNOWN)
14399 utf8 = get_tv_number_chk(&argvars[1], NULL);
14400 if (utf8)
14401 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14402 else
14403 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 else
14406#endif
14407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014408 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 buf[1] = NUL;
14410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 rettv->v_type = VAR_STRING;
14412 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014413}
14414
14415/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014416 * "or(expr, expr)" function
14417 */
14418 static void
14419f_or(argvars, rettv)
14420 typval_T *argvars;
14421 typval_T *rettv;
14422{
14423 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14424 | get_tv_number_chk(&argvars[1], NULL);
14425}
14426
14427/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014428 * "pathshorten()" function
14429 */
14430 static void
14431f_pathshorten(argvars, rettv)
14432 typval_T *argvars;
14433 typval_T *rettv;
14434{
14435 char_u *p;
14436
14437 rettv->v_type = VAR_STRING;
14438 p = get_tv_string_chk(&argvars[0]);
14439 if (p == NULL)
14440 rettv->vval.v_string = NULL;
14441 else
14442 {
14443 p = vim_strsave(p);
14444 rettv->vval.v_string = p;
14445 if (p != NULL)
14446 shorten_dir(p);
14447 }
14448}
14449
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014450#ifdef FEAT_FLOAT
14451/*
14452 * "pow()" function
14453 */
14454 static void
14455f_pow(argvars, rettv)
14456 typval_T *argvars;
14457 typval_T *rettv;
14458{
14459 float_T fx, fy;
14460
14461 rettv->v_type = VAR_FLOAT;
14462 if (get_float_arg(argvars, &fx) == OK
14463 && get_float_arg(&argvars[1], &fy) == OK)
14464 rettv->vval.v_float = pow(fx, fy);
14465 else
14466 rettv->vval.v_float = 0.0;
14467}
14468#endif
14469
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014470/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014471 * "prevnonblank()" function
14472 */
14473 static void
14474f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014475 typval_T *argvars;
14476 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014477{
14478 linenr_T lnum;
14479
14480 lnum = get_tv_lnum(argvars);
14481 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14482 lnum = 0;
14483 else
14484 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14485 --lnum;
14486 rettv->vval.v_number = lnum;
14487}
14488
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014489#ifdef HAVE_STDARG_H
14490/* This dummy va_list is here because:
14491 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14492 * - locally in the function results in a "used before set" warning
14493 * - using va_start() to initialize it gives "function with fixed args" error */
14494static va_list ap;
14495#endif
14496
Bram Moolenaar8c711452005-01-14 21:53:12 +000014497/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014498 * "printf()" function
14499 */
14500 static void
14501f_printf(argvars, rettv)
14502 typval_T *argvars;
14503 typval_T *rettv;
14504{
14505 rettv->v_type = VAR_STRING;
14506 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014507#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014508 {
14509 char_u buf[NUMBUFLEN];
14510 int len;
14511 char_u *s;
14512 int saved_did_emsg = did_emsg;
14513 char *fmt;
14514
14515 /* Get the required length, allocate the buffer and do it for real. */
14516 did_emsg = FALSE;
14517 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014518 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014519 if (!did_emsg)
14520 {
14521 s = alloc(len + 1);
14522 if (s != NULL)
14523 {
14524 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014525 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014526 }
14527 }
14528 did_emsg |= saved_did_emsg;
14529 }
14530#endif
14531}
14532
14533/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014534 * "pumvisible()" function
14535 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014536 static void
14537f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014538 typval_T *argvars UNUSED;
14539 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014540{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014541#ifdef FEAT_INS_EXPAND
14542 if (pum_visible())
14543 rettv->vval.v_number = 1;
14544#endif
14545}
14546
Bram Moolenaardb913952012-06-29 12:54:53 +020014547#ifdef FEAT_PYTHON3
14548/*
14549 * "py3eval()" function
14550 */
14551 static void
14552f_py3eval(argvars, rettv)
14553 typval_T *argvars;
14554 typval_T *rettv;
14555{
14556 char_u *str;
14557 char_u buf[NUMBUFLEN];
14558
14559 str = get_tv_string_buf(&argvars[0], buf);
14560 do_py3eval(str, rettv);
14561}
14562#endif
14563
14564#ifdef FEAT_PYTHON
14565/*
14566 * "pyeval()" function
14567 */
14568 static void
14569f_pyeval(argvars, rettv)
14570 typval_T *argvars;
14571 typval_T *rettv;
14572{
14573 char_u *str;
14574 char_u buf[NUMBUFLEN];
14575
14576 str = get_tv_string_buf(&argvars[0], buf);
14577 do_pyeval(str, rettv);
14578}
14579#endif
14580
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014581/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014582 * "range()" function
14583 */
14584 static void
14585f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014586 typval_T *argvars;
14587 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014588{
14589 long start;
14590 long end;
14591 long stride = 1;
14592 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014593 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014595 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014596 if (argvars[1].v_type == VAR_UNKNOWN)
14597 {
14598 end = start - 1;
14599 start = 0;
14600 }
14601 else
14602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014603 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014604 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014605 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014606 }
14607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014608 if (error)
14609 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014610 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014611 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014612 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014613 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014614 else
14615 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014616 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014617 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014618 if (list_append_number(rettv->vval.v_list,
14619 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014620 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014621 }
14622}
14623
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014624/*
14625 * "readfile()" function
14626 */
14627 static void
14628f_readfile(argvars, rettv)
14629 typval_T *argvars;
14630 typval_T *rettv;
14631{
14632 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014633 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014634 char_u *fname;
14635 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014636 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14637 int io_size = sizeof(buf);
14638 int readlen; /* size of last fread() */
14639 char_u *prev = NULL; /* previously read bytes, if any */
14640 long prevlen = 0; /* length of data in prev */
14641 long prevsize = 0; /* size of prev buffer */
14642 long maxline = MAXLNUM;
14643 long cnt = 0;
14644 char_u *p; /* position in buf */
14645 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014646
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014647 if (argvars[1].v_type != VAR_UNKNOWN)
14648 {
14649 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14650 binary = TRUE;
14651 if (argvars[2].v_type != VAR_UNKNOWN)
14652 maxline = get_tv_number(&argvars[2]);
14653 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014654
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014655 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014656 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014657
14658 /* Always open the file in binary mode, library functions have a mind of
14659 * their own about CR-LF conversion. */
14660 fname = get_tv_string(&argvars[0]);
14661 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14662 {
14663 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14664 return;
14665 }
14666
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014667 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014668 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014669 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014670
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014671 /* This for loop processes what was read, but is also entered at end
14672 * of file so that either:
14673 * - an incomplete line gets written
14674 * - a "binary" file gets an empty line at the end if it ends in a
14675 * newline. */
14676 for (p = buf, start = buf;
14677 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14678 ++p)
14679 {
14680 if (*p == '\n' || readlen <= 0)
14681 {
14682 listitem_T *li;
14683 char_u *s = NULL;
14684 long_u len = p - start;
14685
14686 /* Finished a line. Remove CRs before NL. */
14687 if (readlen > 0 && !binary)
14688 {
14689 while (len > 0 && start[len - 1] == '\r')
14690 --len;
14691 /* removal may cross back to the "prev" string */
14692 if (len == 0)
14693 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14694 --prevlen;
14695 }
14696 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014697 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014698 else
14699 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014700 /* Change "prev" buffer to be the right size. This way
14701 * the bytes are only copied once, and very long lines are
14702 * allocated only once. */
14703 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014704 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014705 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014706 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014707 prev = NULL; /* the list will own the string */
14708 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014709 }
14710 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014711 if (s == NULL)
14712 {
14713 do_outofmem_msg((long_u) prevlen + len + 1);
14714 failed = TRUE;
14715 break;
14716 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014717
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014718 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014719 {
14720 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014721 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014722 break;
14723 }
14724 li->li_tv.v_type = VAR_STRING;
14725 li->li_tv.v_lock = 0;
14726 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014727 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014728
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014729 start = p + 1; /* step over newline */
14730 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014731 break;
14732 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014733 else if (*p == NUL)
14734 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014735#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014736 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14737 * when finding the BF and check the previous two bytes. */
14738 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014739 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014740 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14741 * + 1, these may be in the "prev" string. */
14742 char_u back1 = p >= buf + 1 ? p[-1]
14743 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14744 char_u back2 = p >= buf + 2 ? p[-2]
14745 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14746 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014747
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014748 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014749 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014750 char_u *dest = p - 2;
14751
14752 /* Usually a BOM is at the beginning of a file, and so at
14753 * the beginning of a line; then we can just step over it.
14754 */
14755 if (start == dest)
14756 start = p + 1;
14757 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014758 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014759 /* have to shuffle buf to close gap */
14760 int adjust_prevlen = 0;
14761
14762 if (dest < buf)
14763 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014764 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014765 dest = buf;
14766 }
14767 if (readlen > p - buf + 1)
14768 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14769 readlen -= 3 - adjust_prevlen;
14770 prevlen -= adjust_prevlen;
14771 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014772 }
14773 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014774 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014775#endif
14776 } /* for */
14777
14778 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14779 break;
14780 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014781 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014782 /* There's part of a line in buf, store it in "prev". */
14783 if (p - start + prevlen >= prevsize)
14784 {
14785 /* need bigger "prev" buffer */
14786 char_u *newprev;
14787
14788 /* A common use case is ordinary text files and "prev" gets a
14789 * fragment of a line, so the first allocation is made
14790 * small, to avoid repeatedly 'allocing' large and
14791 * 'reallocing' small. */
14792 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014793 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014794 else
14795 {
14796 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014797 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014798 prevsize = grow50pc > growmin ? grow50pc : growmin;
14799 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014800 newprev = prev == NULL ? alloc(prevsize)
14801 : vim_realloc(prev, prevsize);
14802 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014803 {
14804 do_outofmem_msg((long_u)prevsize);
14805 failed = TRUE;
14806 break;
14807 }
14808 prev = newprev;
14809 }
14810 /* Add the line part to end of "prev". */
14811 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014812 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014813 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014814 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014815
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014816 /*
14817 * For a negative line count use only the lines at the end of the file,
14818 * free the rest.
14819 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014820 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014821 while (cnt > -maxline)
14822 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014823 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014824 --cnt;
14825 }
14826
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014827 if (failed)
14828 {
14829 list_free(rettv->vval.v_list, TRUE);
14830 /* readfile doc says an empty list is returned on error */
14831 rettv->vval.v_list = list_alloc();
14832 }
14833
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014834 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014835 fclose(fd);
14836}
14837
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014838#if defined(FEAT_RELTIME)
14839static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14840
14841/*
14842 * Convert a List to proftime_T.
14843 * Return FAIL when there is something wrong.
14844 */
14845 static int
14846list2proftime(arg, tm)
14847 typval_T *arg;
14848 proftime_T *tm;
14849{
14850 long n1, n2;
14851 int error = FALSE;
14852
14853 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14854 || arg->vval.v_list->lv_len != 2)
14855 return FAIL;
14856 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14857 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14858# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014859 tm->HighPart = n1;
14860 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014861# else
14862 tm->tv_sec = n1;
14863 tm->tv_usec = n2;
14864# endif
14865 return error ? FAIL : OK;
14866}
14867#endif /* FEAT_RELTIME */
14868
14869/*
14870 * "reltime()" function
14871 */
14872 static void
14873f_reltime(argvars, rettv)
14874 typval_T *argvars;
14875 typval_T *rettv;
14876{
14877#ifdef FEAT_RELTIME
14878 proftime_T res;
14879 proftime_T start;
14880
14881 if (argvars[0].v_type == VAR_UNKNOWN)
14882 {
14883 /* No arguments: get current time. */
14884 profile_start(&res);
14885 }
14886 else if (argvars[1].v_type == VAR_UNKNOWN)
14887 {
14888 if (list2proftime(&argvars[0], &res) == FAIL)
14889 return;
14890 profile_end(&res);
14891 }
14892 else
14893 {
14894 /* Two arguments: compute the difference. */
14895 if (list2proftime(&argvars[0], &start) == FAIL
14896 || list2proftime(&argvars[1], &res) == FAIL)
14897 return;
14898 profile_sub(&res, &start);
14899 }
14900
14901 if (rettv_list_alloc(rettv) == OK)
14902 {
14903 long n1, n2;
14904
14905# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014906 n1 = res.HighPart;
14907 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014908# else
14909 n1 = res.tv_sec;
14910 n2 = res.tv_usec;
14911# endif
14912 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14913 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14914 }
14915#endif
14916}
14917
14918/*
14919 * "reltimestr()" function
14920 */
14921 static void
14922f_reltimestr(argvars, rettv)
14923 typval_T *argvars;
14924 typval_T *rettv;
14925{
14926#ifdef FEAT_RELTIME
14927 proftime_T tm;
14928#endif
14929
14930 rettv->v_type = VAR_STRING;
14931 rettv->vval.v_string = NULL;
14932#ifdef FEAT_RELTIME
14933 if (list2proftime(&argvars[0], &tm) == OK)
14934 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14935#endif
14936}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014937
Bram Moolenaar0d660222005-01-07 21:51:51 +000014938#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14939static void make_connection __ARGS((void));
14940static int check_connection __ARGS((void));
14941
14942 static void
14943make_connection()
14944{
14945 if (X_DISPLAY == NULL
14946# ifdef FEAT_GUI
14947 && !gui.in_use
14948# endif
14949 )
14950 {
14951 x_force_connect = TRUE;
14952 setup_term_clip();
14953 x_force_connect = FALSE;
14954 }
14955}
14956
14957 static int
14958check_connection()
14959{
14960 make_connection();
14961 if (X_DISPLAY == NULL)
14962 {
14963 EMSG(_("E240: No connection to Vim server"));
14964 return FAIL;
14965 }
14966 return OK;
14967}
14968#endif
14969
14970#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014971static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014972
14973 static void
14974remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014975 typval_T *argvars;
14976 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014977 int expr;
14978{
14979 char_u *server_name;
14980 char_u *keys;
14981 char_u *r = NULL;
14982 char_u buf[NUMBUFLEN];
14983# ifdef WIN32
14984 HWND w;
14985# else
14986 Window w;
14987# endif
14988
14989 if (check_restricted() || check_secure())
14990 return;
14991
14992# ifdef FEAT_X11
14993 if (check_connection() == FAIL)
14994 return;
14995# endif
14996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014997 server_name = get_tv_string_chk(&argvars[0]);
14998 if (server_name == NULL)
14999 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015000 keys = get_tv_string_buf(&argvars[1], buf);
15001# ifdef WIN32
15002 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15003# else
15004 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15005 < 0)
15006# endif
15007 {
15008 if (r != NULL)
15009 EMSG(r); /* sending worked but evaluation failed */
15010 else
15011 EMSG2(_("E241: Unable to send to %s"), server_name);
15012 return;
15013 }
15014
15015 rettv->vval.v_string = r;
15016
15017 if (argvars[2].v_type != VAR_UNKNOWN)
15018 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015019 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015020 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015021 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015022
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015023 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015024 v.di_tv.v_type = VAR_STRING;
15025 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015026 idvar = get_tv_string_chk(&argvars[2]);
15027 if (idvar != NULL)
15028 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015029 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015030 }
15031}
15032#endif
15033
15034/*
15035 * "remote_expr()" function
15036 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015037 static void
15038f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015039 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015040 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015041{
15042 rettv->v_type = VAR_STRING;
15043 rettv->vval.v_string = NULL;
15044#ifdef FEAT_CLIENTSERVER
15045 remote_common(argvars, rettv, TRUE);
15046#endif
15047}
15048
15049/*
15050 * "remote_foreground()" function
15051 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015052 static void
15053f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015054 typval_T *argvars UNUSED;
15055 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015056{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015057#ifdef FEAT_CLIENTSERVER
15058# ifdef WIN32
15059 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015060 {
15061 char_u *server_name = get_tv_string_chk(&argvars[0]);
15062
15063 if (server_name != NULL)
15064 serverForeground(server_name);
15065 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015066# else
15067 /* Send a foreground() expression to the server. */
15068 argvars[1].v_type = VAR_STRING;
15069 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15070 argvars[2].v_type = VAR_UNKNOWN;
15071 remote_common(argvars, rettv, TRUE);
15072 vim_free(argvars[1].vval.v_string);
15073# endif
15074#endif
15075}
15076
Bram Moolenaar0d660222005-01-07 21:51:51 +000015077 static void
15078f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015079 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015080 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015081{
15082#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015083 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084 char_u *s = NULL;
15085# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015086 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015088 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089
15090 if (check_restricted() || check_secure())
15091 {
15092 rettv->vval.v_number = -1;
15093 return;
15094 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015095 serverid = get_tv_string_chk(&argvars[0]);
15096 if (serverid == NULL)
15097 {
15098 rettv->vval.v_number = -1;
15099 return; /* type error; errmsg already given */
15100 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015101# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015102 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015103 if (n == 0)
15104 rettv->vval.v_number = -1;
15105 else
15106 {
15107 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15108 rettv->vval.v_number = (s != NULL);
15109 }
15110# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111 if (check_connection() == FAIL)
15112 return;
15113
15114 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015115 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015116# endif
15117
15118 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015120 char_u *retvar;
15121
Bram Moolenaar33570922005-01-25 22:26:29 +000015122 v.di_tv.v_type = VAR_STRING;
15123 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015124 retvar = get_tv_string_chk(&argvars[1]);
15125 if (retvar != NULL)
15126 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015127 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015128 }
15129#else
15130 rettv->vval.v_number = -1;
15131#endif
15132}
15133
Bram Moolenaar0d660222005-01-07 21:51:51 +000015134 static void
15135f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015136 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015137 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015138{
15139 char_u *r = NULL;
15140
15141#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015142 char_u *serverid = get_tv_string_chk(&argvars[0]);
15143
15144 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015145 {
15146# ifdef WIN32
15147 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015148 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015149
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015150 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015151 if (n != 0)
15152 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15153 if (r == NULL)
15154# else
15155 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015156 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015157# endif
15158 EMSG(_("E277: Unable to read a server reply"));
15159 }
15160#endif
15161 rettv->v_type = VAR_STRING;
15162 rettv->vval.v_string = r;
15163}
15164
15165/*
15166 * "remote_send()" function
15167 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015168 static void
15169f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015170 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015171 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015172{
15173 rettv->v_type = VAR_STRING;
15174 rettv->vval.v_string = NULL;
15175#ifdef FEAT_CLIENTSERVER
15176 remote_common(argvars, rettv, FALSE);
15177#endif
15178}
15179
15180/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015181 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015182 */
15183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015184f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015185 typval_T *argvars;
15186 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015187{
Bram Moolenaar33570922005-01-25 22:26:29 +000015188 list_T *l;
15189 listitem_T *item, *item2;
15190 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015191 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015192 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015193 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015194 dict_T *d;
15195 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015196 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015197
Bram Moolenaar8c711452005-01-14 21:53:12 +000015198 if (argvars[0].v_type == VAR_DICT)
15199 {
15200 if (argvars[2].v_type != VAR_UNKNOWN)
15201 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015202 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015203 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015205 key = get_tv_string_chk(&argvars[1]);
15206 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015208 di = dict_find(d, key, -1);
15209 if (di == NULL)
15210 EMSG2(_(e_dictkey), key);
15211 else
15212 {
15213 *rettv = di->di_tv;
15214 init_tv(&di->di_tv);
15215 dictitem_remove(d, di);
15216 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015217 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015218 }
15219 }
15220 else if (argvars[0].v_type != VAR_LIST)
15221 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015222 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015223 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015225 int error = FALSE;
15226
15227 idx = get_tv_number_chk(&argvars[1], &error);
15228 if (error)
15229 ; /* type error: do nothing, errmsg already given */
15230 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015231 EMSGN(_(e_listidx), idx);
15232 else
15233 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015234 if (argvars[2].v_type == VAR_UNKNOWN)
15235 {
15236 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015237 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015238 *rettv = item->li_tv;
15239 vim_free(item);
15240 }
15241 else
15242 {
15243 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015244 end = get_tv_number_chk(&argvars[2], &error);
15245 if (error)
15246 ; /* type error: do nothing */
15247 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015248 EMSGN(_(e_listidx), end);
15249 else
15250 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015251 int cnt = 0;
15252
15253 for (li = item; li != NULL; li = li->li_next)
15254 {
15255 ++cnt;
15256 if (li == item2)
15257 break;
15258 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015259 if (li == NULL) /* didn't find "item2" after "item" */
15260 EMSG(_(e_invrange));
15261 else
15262 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015263 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015264 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015265 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015266 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015267 l->lv_first = item;
15268 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015269 item->li_prev = NULL;
15270 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015271 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015272 }
15273 }
15274 }
15275 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015276 }
15277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278}
15279
15280/*
15281 * "rename({from}, {to})" function
15282 */
15283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015284f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015285 typval_T *argvars;
15286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287{
15288 char_u buf[NUMBUFLEN];
15289
15290 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015291 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015293 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15294 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295}
15296
15297/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015298 * "repeat()" function
15299 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015300 static void
15301f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015302 typval_T *argvars;
15303 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015304{
15305 char_u *p;
15306 int n;
15307 int slen;
15308 int len;
15309 char_u *r;
15310 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015311
15312 n = get_tv_number(&argvars[1]);
15313 if (argvars[0].v_type == VAR_LIST)
15314 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015315 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015316 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015317 if (list_extend(rettv->vval.v_list,
15318 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015319 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015320 }
15321 else
15322 {
15323 p = get_tv_string(&argvars[0]);
15324 rettv->v_type = VAR_STRING;
15325 rettv->vval.v_string = NULL;
15326
15327 slen = (int)STRLEN(p);
15328 len = slen * n;
15329 if (len <= 0)
15330 return;
15331
15332 r = alloc(len + 1);
15333 if (r != NULL)
15334 {
15335 for (i = 0; i < n; i++)
15336 mch_memmove(r + i * slen, p, (size_t)slen);
15337 r[len] = NUL;
15338 }
15339
15340 rettv->vval.v_string = r;
15341 }
15342}
15343
15344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 * "resolve()" function
15346 */
15347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015348f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015349 typval_T *argvars;
15350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351{
15352 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015353#ifdef HAVE_READLINK
15354 char_u *buf = NULL;
15355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358#ifdef FEAT_SHORTCUT
15359 {
15360 char_u *v = NULL;
15361
15362 v = mch_resolve_shortcut(p);
15363 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015364 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367 }
15368#else
15369# ifdef HAVE_READLINK
15370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 char_u *cpy;
15372 int len;
15373 char_u *remain = NULL;
15374 char_u *q;
15375 int is_relative_to_current = FALSE;
15376 int has_trailing_pathsep = FALSE;
15377 int limit = 100;
15378
15379 p = vim_strsave(p);
15380
15381 if (p[0] == '.' && (vim_ispathsep(p[1])
15382 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15383 is_relative_to_current = TRUE;
15384
15385 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015386 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015389 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391
15392 q = getnextcomp(p);
15393 if (*q != NUL)
15394 {
15395 /* Separate the first path component in "p", and keep the
15396 * remainder (beginning with the path separator). */
15397 remain = vim_strsave(q - 1);
15398 q[-1] = NUL;
15399 }
15400
Bram Moolenaard9462e32011-04-11 21:35:11 +020015401 buf = alloc(MAXPATHL + 1);
15402 if (buf == NULL)
15403 goto fail;
15404
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 for (;;)
15406 {
15407 for (;;)
15408 {
15409 len = readlink((char *)p, (char *)buf, MAXPATHL);
15410 if (len <= 0)
15411 break;
15412 buf[len] = NUL;
15413
15414 if (limit-- == 0)
15415 {
15416 vim_free(p);
15417 vim_free(remain);
15418 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015419 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 goto fail;
15421 }
15422
15423 /* Ensure that the result will have a trailing path separator
15424 * if the argument has one. */
15425 if (remain == NULL && has_trailing_pathsep)
15426 add_pathsep(buf);
15427
15428 /* Separate the first path component in the link value and
15429 * concatenate the remainders. */
15430 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15431 if (*q != NUL)
15432 {
15433 if (remain == NULL)
15434 remain = vim_strsave(q - 1);
15435 else
15436 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015437 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438 if (cpy != NULL)
15439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 vim_free(remain);
15441 remain = cpy;
15442 }
15443 }
15444 q[-1] = NUL;
15445 }
15446
15447 q = gettail(p);
15448 if (q > p && *q == NUL)
15449 {
15450 /* Ignore trailing path separator. */
15451 q[-1] = NUL;
15452 q = gettail(p);
15453 }
15454 if (q > p && !mch_isFullName(buf))
15455 {
15456 /* symlink is relative to directory of argument */
15457 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15458 if (cpy != NULL)
15459 {
15460 STRCPY(cpy, p);
15461 STRCPY(gettail(cpy), buf);
15462 vim_free(p);
15463 p = cpy;
15464 }
15465 }
15466 else
15467 {
15468 vim_free(p);
15469 p = vim_strsave(buf);
15470 }
15471 }
15472
15473 if (remain == NULL)
15474 break;
15475
15476 /* Append the first path component of "remain" to "p". */
15477 q = getnextcomp(remain + 1);
15478 len = q - remain - (*q != NUL);
15479 cpy = vim_strnsave(p, STRLEN(p) + len);
15480 if (cpy != NULL)
15481 {
15482 STRNCAT(cpy, remain, len);
15483 vim_free(p);
15484 p = cpy;
15485 }
15486 /* Shorten "remain". */
15487 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015488 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489 else
15490 {
15491 vim_free(remain);
15492 remain = NULL;
15493 }
15494 }
15495
15496 /* If the result is a relative path name, make it explicitly relative to
15497 * the current directory if and only if the argument had this form. */
15498 if (!vim_ispathsep(*p))
15499 {
15500 if (is_relative_to_current
15501 && *p != NUL
15502 && !(p[0] == '.'
15503 && (p[1] == NUL
15504 || vim_ispathsep(p[1])
15505 || (p[1] == '.'
15506 && (p[2] == NUL
15507 || vim_ispathsep(p[2]))))))
15508 {
15509 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015510 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 if (cpy != NULL)
15512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 vim_free(p);
15514 p = cpy;
15515 }
15516 }
15517 else if (!is_relative_to_current)
15518 {
15519 /* Strip leading "./". */
15520 q = p;
15521 while (q[0] == '.' && vim_ispathsep(q[1]))
15522 q += 2;
15523 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015524 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 }
15526 }
15527
15528 /* Ensure that the result will have no trailing path separator
15529 * if the argument had none. But keep "/" or "//". */
15530 if (!has_trailing_pathsep)
15531 {
15532 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015533 if (after_pathsep(p, q))
15534 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 }
15536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015537 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 }
15539# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015540 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541# endif
15542#endif
15543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015544 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545
15546#ifdef HAVE_READLINK
15547fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015548 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015550 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551}
15552
15553/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015554 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 */
15556 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015557f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015558 typval_T *argvars;
15559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560{
Bram Moolenaar33570922005-01-25 22:26:29 +000015561 list_T *l;
15562 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563
Bram Moolenaar0d660222005-01-07 21:51:51 +000015564 if (argvars[0].v_type != VAR_LIST)
15565 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015566 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015567 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015568 {
15569 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015570 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015571 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015572 while (li != NULL)
15573 {
15574 ni = li->li_prev;
15575 list_append(l, li);
15576 li = ni;
15577 }
15578 rettv->vval.v_list = l;
15579 rettv->v_type = VAR_LIST;
15580 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015581 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583}
15584
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015585#define SP_NOMOVE 0x01 /* don't move cursor */
15586#define SP_REPEAT 0x02 /* repeat to find outer pair */
15587#define SP_RETCOUNT 0x04 /* return matchcount */
15588#define SP_SETPCMARK 0x08 /* set previous context mark */
15589#define SP_START 0x10 /* accept match at start position */
15590#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15591#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015592
Bram Moolenaar33570922005-01-25 22:26:29 +000015593static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015594
15595/*
15596 * Get flags for a search function.
15597 * Possibly sets "p_ws".
15598 * Returns BACKWARD, FORWARD or zero (for an error).
15599 */
15600 static int
15601get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015602 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015603 int *flagsp;
15604{
15605 int dir = FORWARD;
15606 char_u *flags;
15607 char_u nbuf[NUMBUFLEN];
15608 int mask;
15609
15610 if (varp->v_type != VAR_UNKNOWN)
15611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015612 flags = get_tv_string_buf_chk(varp, nbuf);
15613 if (flags == NULL)
15614 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015615 while (*flags != NUL)
15616 {
15617 switch (*flags)
15618 {
15619 case 'b': dir = BACKWARD; break;
15620 case 'w': p_ws = TRUE; break;
15621 case 'W': p_ws = FALSE; break;
15622 default: mask = 0;
15623 if (flagsp != NULL)
15624 switch (*flags)
15625 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015626 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015627 case 'e': mask = SP_END; break;
15628 case 'm': mask = SP_RETCOUNT; break;
15629 case 'n': mask = SP_NOMOVE; break;
15630 case 'p': mask = SP_SUBPAT; break;
15631 case 'r': mask = SP_REPEAT; break;
15632 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015633 }
15634 if (mask == 0)
15635 {
15636 EMSG2(_(e_invarg2), flags);
15637 dir = 0;
15638 }
15639 else
15640 *flagsp |= mask;
15641 }
15642 if (dir == 0)
15643 break;
15644 ++flags;
15645 }
15646 }
15647 return dir;
15648}
15649
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015651 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015653 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015654search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015655 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015656 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015657 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015659 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 char_u *pat;
15661 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015662 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 int save_p_ws = p_ws;
15664 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015665 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015666 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015667 proftime_T tm;
15668#ifdef FEAT_RELTIME
15669 long time_limit = 0;
15670#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015671 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015672 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015674 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015675 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015676 if (dir == 0)
15677 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015678 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015679 if (flags & SP_START)
15680 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015681 if (flags & SP_END)
15682 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015683
Bram Moolenaar76929292008-01-06 19:07:36 +000015684 /* Optional arguments: line number to stop searching and timeout. */
15685 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015686 {
15687 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15688 if (lnum_stop < 0)
15689 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015690#ifdef FEAT_RELTIME
15691 if (argvars[3].v_type != VAR_UNKNOWN)
15692 {
15693 time_limit = get_tv_number_chk(&argvars[3], NULL);
15694 if (time_limit < 0)
15695 goto theend;
15696 }
15697#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015698 }
15699
Bram Moolenaar76929292008-01-06 19:07:36 +000015700#ifdef FEAT_RELTIME
15701 /* Set the time limit, if there is one. */
15702 profile_setlimit(time_limit, &tm);
15703#endif
15704
Bram Moolenaar231334e2005-07-25 20:46:57 +000015705 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015706 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015707 * Check to make sure only those flags are set.
15708 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15709 * flags cannot be set. Check for that condition also.
15710 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015711 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015712 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015714 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015715 goto theend;
15716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015718 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015719 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015720 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015721 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015723 if (flags & SP_SUBPAT)
15724 retval = subpatnum;
15725 else
15726 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015727 if (flags & SP_SETPCMARK)
15728 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015730 if (match_pos != NULL)
15731 {
15732 /* Store the match cursor position */
15733 match_pos->lnum = pos.lnum;
15734 match_pos->col = pos.col + 1;
15735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 /* "/$" will put the cursor after the end of the line, may need to
15737 * correct that here */
15738 check_cursor();
15739 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015740
15741 /* If 'n' flag is used: restore cursor position. */
15742 if (flags & SP_NOMOVE)
15743 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015744 else
15745 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015746theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015748
15749 return retval;
15750}
15751
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015752#ifdef FEAT_FLOAT
15753/*
15754 * "round({float})" function
15755 */
15756 static void
15757f_round(argvars, rettv)
15758 typval_T *argvars;
15759 typval_T *rettv;
15760{
15761 float_T f;
15762
15763 rettv->v_type = VAR_FLOAT;
15764 if (get_float_arg(argvars, &f) == OK)
15765 /* round() is not in C90, use ceil() or floor() instead. */
15766 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15767 else
15768 rettv->vval.v_float = 0.0;
15769}
15770#endif
15771
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015772/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015773 * "screencol()" function
15774 *
15775 * First column is 1 to be consistent with virtcol().
15776 */
15777 static void
15778f_screencol(argvars, rettv)
15779 typval_T *argvars UNUSED;
15780 typval_T *rettv;
15781{
15782 rettv->vval.v_number = screen_screencol() + 1;
15783}
15784
15785/*
15786 * "screenrow()" function
15787 */
15788 static void
15789f_screenrow(argvars, rettv)
15790 typval_T *argvars UNUSED;
15791 typval_T *rettv;
15792{
15793 rettv->vval.v_number = screen_screenrow() + 1;
15794}
15795
15796/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015797 * "search()" function
15798 */
15799 static void
15800f_search(argvars, rettv)
15801 typval_T *argvars;
15802 typval_T *rettv;
15803{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015804 int flags = 0;
15805
15806 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015807}
15808
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015810 * "searchdecl()" function
15811 */
15812 static void
15813f_searchdecl(argvars, rettv)
15814 typval_T *argvars;
15815 typval_T *rettv;
15816{
15817 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015818 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015819 int error = FALSE;
15820 char_u *name;
15821
15822 rettv->vval.v_number = 1; /* default: FAIL */
15823
15824 name = get_tv_string_chk(&argvars[0]);
15825 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015826 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015827 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015828 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15829 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15830 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015831 if (!error && name != NULL)
15832 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015833 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015834}
15835
15836/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015837 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015839 static int
15840searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015841 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015842 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843{
15844 char_u *spat, *mpat, *epat;
15845 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847 int dir;
15848 int flags = 0;
15849 char_u nbuf1[NUMBUFLEN];
15850 char_u nbuf2[NUMBUFLEN];
15851 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015852 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015853 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015854 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015857 spat = get_tv_string_chk(&argvars[0]);
15858 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15859 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15860 if (spat == NULL || mpat == NULL || epat == NULL)
15861 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863 /* Handle the optional fourth argument: flags */
15864 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015865 if (dir == 0)
15866 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015867
15868 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015869 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15870 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015871 if ((flags & (SP_END | SP_SUBPAT)) != 0
15872 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015873 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015874 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015875 goto theend;
15876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015878 /* Using 'r' implies 'W', otherwise it doesn't work. */
15879 if (flags & SP_REPEAT)
15880 p_ws = FALSE;
15881
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015882 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015883 if (argvars[3].v_type == VAR_UNKNOWN
15884 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885 skip = (char_u *)"";
15886 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015888 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015889 if (argvars[5].v_type != VAR_UNKNOWN)
15890 {
15891 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15892 if (lnum_stop < 0)
15893 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015894#ifdef FEAT_RELTIME
15895 if (argvars[6].v_type != VAR_UNKNOWN)
15896 {
15897 time_limit = get_tv_number_chk(&argvars[6], NULL);
15898 if (time_limit < 0)
15899 goto theend;
15900 }
15901#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015902 }
15903 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015904 if (skip == NULL)
15905 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015907 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015908 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015909
15910theend:
15911 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015912
15913 return retval;
15914}
15915
15916/*
15917 * "searchpair()" function
15918 */
15919 static void
15920f_searchpair(argvars, rettv)
15921 typval_T *argvars;
15922 typval_T *rettv;
15923{
15924 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15925}
15926
15927/*
15928 * "searchpairpos()" function
15929 */
15930 static void
15931f_searchpairpos(argvars, rettv)
15932 typval_T *argvars;
15933 typval_T *rettv;
15934{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015935 pos_T match_pos;
15936 int lnum = 0;
15937 int col = 0;
15938
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015939 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015940 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015941
15942 if (searchpair_cmn(argvars, &match_pos) > 0)
15943 {
15944 lnum = match_pos.lnum;
15945 col = match_pos.col;
15946 }
15947
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015948 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15949 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015950}
15951
15952/*
15953 * Search for a start/middle/end thing.
15954 * Used by searchpair(), see its documentation for the details.
15955 * Returns 0 or -1 for no match,
15956 */
15957 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015958do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15959 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015960 char_u *spat; /* start pattern */
15961 char_u *mpat; /* middle pattern */
15962 char_u *epat; /* end pattern */
15963 int dir; /* BACKWARD or FORWARD */
15964 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015965 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015966 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015967 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015968 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015969{
15970 char_u *save_cpo;
15971 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15972 long retval = 0;
15973 pos_T pos;
15974 pos_T firstpos;
15975 pos_T foundpos;
15976 pos_T save_cursor;
15977 pos_T save_pos;
15978 int n;
15979 int r;
15980 int nest = 1;
15981 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015982 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015983 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015984
15985 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15986 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015987 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015988
Bram Moolenaar76929292008-01-06 19:07:36 +000015989#ifdef FEAT_RELTIME
15990 /* Set the time limit, if there is one. */
15991 profile_setlimit(time_limit, &tm);
15992#endif
15993
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015994 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15995 * start/middle/end (pat3, for the top pair). */
15996 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15997 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15998 if (pat2 == NULL || pat3 == NULL)
15999 goto theend;
16000 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16001 if (*mpat == NUL)
16002 STRCPY(pat3, pat2);
16003 else
16004 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16005 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016006 if (flags & SP_START)
16007 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016008
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009 save_cursor = curwin->w_cursor;
16010 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016011 clearpos(&firstpos);
16012 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 pat = pat3;
16014 for (;;)
16015 {
16016 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016017 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16019 /* didn't find it or found the first match again: FAIL */
16020 break;
16021
16022 if (firstpos.lnum == 0)
16023 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016024 if (equalpos(pos, foundpos))
16025 {
16026 /* Found the same position again. Can happen with a pattern that
16027 * has "\zs" at the end and searching backwards. Advance one
16028 * character and try again. */
16029 if (dir == BACKWARD)
16030 decl(&pos);
16031 else
16032 incl(&pos);
16033 }
16034 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016036 /* clear the start flag to avoid getting stuck here */
16037 options &= ~SEARCH_START;
16038
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 /* If the skip pattern matches, ignore this match. */
16040 if (*skip != NUL)
16041 {
16042 save_pos = curwin->w_cursor;
16043 curwin->w_cursor = pos;
16044 r = eval_to_bool(skip, &err, NULL, FALSE);
16045 curwin->w_cursor = save_pos;
16046 if (err)
16047 {
16048 /* Evaluating {skip} caused an error, break here. */
16049 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016050 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 break;
16052 }
16053 if (r)
16054 continue;
16055 }
16056
16057 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16058 {
16059 /* Found end when searching backwards or start when searching
16060 * forward: nested pair. */
16061 ++nest;
16062 pat = pat2; /* nested, don't search for middle */
16063 }
16064 else
16065 {
16066 /* Found end when searching forward or start when searching
16067 * backward: end of (nested) pair; or found middle in outer pair. */
16068 if (--nest == 1)
16069 pat = pat3; /* outer level, search for middle */
16070 }
16071
16072 if (nest == 0)
16073 {
16074 /* Found the match: return matchcount or line number. */
16075 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016076 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016078 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016079 if (flags & SP_SETPCMARK)
16080 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 curwin->w_cursor = pos;
16082 if (!(flags & SP_REPEAT))
16083 break;
16084 nest = 1; /* search for next unmatched */
16085 }
16086 }
16087
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016088 if (match_pos != NULL)
16089 {
16090 /* Store the match cursor position */
16091 match_pos->lnum = curwin->w_cursor.lnum;
16092 match_pos->col = curwin->w_cursor.col + 1;
16093 }
16094
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016096 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097 curwin->w_cursor = save_cursor;
16098
16099theend:
16100 vim_free(pat2);
16101 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016102 if (p_cpo == empty_option)
16103 p_cpo = save_cpo;
16104 else
16105 /* Darn, evaluating the {skip} expression changed the value. */
16106 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016107
16108 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109}
16110
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016111/*
16112 * "searchpos()" function
16113 */
16114 static void
16115f_searchpos(argvars, rettv)
16116 typval_T *argvars;
16117 typval_T *rettv;
16118{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016119 pos_T match_pos;
16120 int lnum = 0;
16121 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016122 int n;
16123 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016124
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016125 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016126 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016127
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016128 n = search_cmn(argvars, &match_pos, &flags);
16129 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016130 {
16131 lnum = match_pos.lnum;
16132 col = match_pos.col;
16133 }
16134
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016135 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16136 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016137 if (flags & SP_SUBPAT)
16138 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016139}
16140
16141
Bram Moolenaar0d660222005-01-07 21:51:51 +000016142 static void
16143f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016144 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016147#ifdef FEAT_CLIENTSERVER
16148 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016149 char_u *server = get_tv_string_chk(&argvars[0]);
16150 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151
Bram Moolenaar0d660222005-01-07 21:51:51 +000016152 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016153 if (server == NULL || reply == NULL)
16154 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016155 if (check_restricted() || check_secure())
16156 return;
16157# ifdef FEAT_X11
16158 if (check_connection() == FAIL)
16159 return;
16160# endif
16161
16162 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016164 EMSG(_("E258: Unable to send to client"));
16165 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016167 rettv->vval.v_number = 0;
16168#else
16169 rettv->vval.v_number = -1;
16170#endif
16171}
16172
Bram Moolenaar0d660222005-01-07 21:51:51 +000016173 static void
16174f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016175 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016177{
16178 char_u *r = NULL;
16179
16180#ifdef FEAT_CLIENTSERVER
16181# ifdef WIN32
16182 r = serverGetVimNames();
16183# else
16184 make_connection();
16185 if (X_DISPLAY != NULL)
16186 r = serverGetVimNames(X_DISPLAY);
16187# endif
16188#endif
16189 rettv->v_type = VAR_STRING;
16190 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191}
16192
16193/*
16194 * "setbufvar()" function
16195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016197f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016198 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016199 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200{
16201 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016204 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205 char_u nbuf[NUMBUFLEN];
16206
16207 if (check_restricted() || check_secure())
16208 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016209 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16210 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016211 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 varp = &argvars[2];
16213
16214 if (buf != NULL && varname != NULL && varp != NULL)
16215 {
16216 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218
16219 if (*varname == '&')
16220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016221 long numval;
16222 char_u *strval;
16223 int error = FALSE;
16224
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016226 numval = get_tv_number_chk(varp, &error);
16227 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016228 if (!error && strval != NULL)
16229 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 }
16231 else
16232 {
16233 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16234 if (bufvarname != NULL)
16235 {
16236 STRCPY(bufvarname, "b:");
16237 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016238 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 vim_free(bufvarname);
16240 }
16241 }
16242
16243 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246}
16247
16248/*
16249 * "setcmdpos()" function
16250 */
16251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016252f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016253 typval_T *argvars;
16254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016256 int pos = (int)get_tv_number(&argvars[0]) - 1;
16257
16258 if (pos >= 0)
16259 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260}
16261
16262/*
16263 * "setline()" function
16264 */
16265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016266f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 typval_T *argvars;
16268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269{
16270 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016271 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016272 list_T *l = NULL;
16273 listitem_T *li = NULL;
16274 long added = 0;
16275 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016277 lnum = get_tv_lnum(&argvars[0]);
16278 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016280 l = argvars[1].vval.v_list;
16281 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016283 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016284 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016285
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016286 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016287 for (;;)
16288 {
16289 if (l != NULL)
16290 {
16291 /* list argument, get next string */
16292 if (li == NULL)
16293 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016294 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016295 li = li->li_next;
16296 }
16297
16298 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016299 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016300 break;
16301 if (lnum <= curbuf->b_ml.ml_line_count)
16302 {
16303 /* existing line, replace it */
16304 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16305 {
16306 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016307 if (lnum == curwin->w_cursor.lnum)
16308 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016309 rettv->vval.v_number = 0; /* OK */
16310 }
16311 }
16312 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16313 {
16314 /* lnum is one past the last line, append the line */
16315 ++added;
16316 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16317 rettv->vval.v_number = 0; /* OK */
16318 }
16319
16320 if (l == NULL) /* only one string argument */
16321 break;
16322 ++lnum;
16323 }
16324
16325 if (added > 0)
16326 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327}
16328
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016329static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16330
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016332 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016333 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016334 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016335set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016336 win_T *wp UNUSED;
16337 typval_T *list_arg UNUSED;
16338 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016339 typval_T *rettv;
16340{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016341#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016342 char_u *act;
16343 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016344#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016345
Bram Moolenaar2641f772005-03-25 21:58:17 +000016346 rettv->vval.v_number = -1;
16347
16348#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016349 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016350 EMSG(_(e_listreq));
16351 else
16352 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016353 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016354
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016355 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016356 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016357 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016358 if (act == NULL)
16359 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016360 if (*act == 'a' || *act == 'r')
16361 action = *act;
16362 }
16363
Bram Moolenaar81484f42012-12-05 15:16:47 +010016364 if (l != NULL && set_errorlist(wp, l, action,
16365 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016366 rettv->vval.v_number = 0;
16367 }
16368#endif
16369}
16370
16371/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016372 * "setloclist()" function
16373 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016374 static void
16375f_setloclist(argvars, rettv)
16376 typval_T *argvars;
16377 typval_T *rettv;
16378{
16379 win_T *win;
16380
16381 rettv->vval.v_number = -1;
16382
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016383 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016384 if (win != NULL)
16385 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16386}
16387
16388/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016389 * "setmatches()" function
16390 */
16391 static void
16392f_setmatches(argvars, rettv)
16393 typval_T *argvars;
16394 typval_T *rettv;
16395{
16396#ifdef FEAT_SEARCH_EXTRA
16397 list_T *l;
16398 listitem_T *li;
16399 dict_T *d;
16400
16401 rettv->vval.v_number = -1;
16402 if (argvars[0].v_type != VAR_LIST)
16403 {
16404 EMSG(_(e_listreq));
16405 return;
16406 }
16407 if ((l = argvars[0].vval.v_list) != NULL)
16408 {
16409
16410 /* To some extent make sure that we are dealing with a list from
16411 * "getmatches()". */
16412 li = l->lv_first;
16413 while (li != NULL)
16414 {
16415 if (li->li_tv.v_type != VAR_DICT
16416 || (d = li->li_tv.vval.v_dict) == NULL)
16417 {
16418 EMSG(_(e_invarg));
16419 return;
16420 }
16421 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16422 && dict_find(d, (char_u *)"pattern", -1) != NULL
16423 && dict_find(d, (char_u *)"priority", -1) != NULL
16424 && dict_find(d, (char_u *)"id", -1) != NULL))
16425 {
16426 EMSG(_(e_invarg));
16427 return;
16428 }
16429 li = li->li_next;
16430 }
16431
16432 clear_matches(curwin);
16433 li = l->lv_first;
16434 while (li != NULL)
16435 {
16436 d = li->li_tv.vval.v_dict;
16437 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16438 get_dict_string(d, (char_u *)"pattern", FALSE),
16439 (int)get_dict_number(d, (char_u *)"priority"),
16440 (int)get_dict_number(d, (char_u *)"id"));
16441 li = li->li_next;
16442 }
16443 rettv->vval.v_number = 0;
16444 }
16445#endif
16446}
16447
16448/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016449 * "setpos()" function
16450 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016451 static void
16452f_setpos(argvars, rettv)
16453 typval_T *argvars;
16454 typval_T *rettv;
16455{
16456 pos_T pos;
16457 int fnum;
16458 char_u *name;
16459
Bram Moolenaar08250432008-02-13 11:42:46 +000016460 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016461 name = get_tv_string_chk(argvars);
16462 if (name != NULL)
16463 {
16464 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16465 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016466 if (--pos.col < 0)
16467 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016468 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016469 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016470 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016471 if (fnum == curbuf->b_fnum)
16472 {
16473 curwin->w_cursor = pos;
16474 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016475 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016476 }
16477 else
16478 EMSG(_(e_invarg));
16479 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016480 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16481 {
16482 /* set mark */
16483 if (setmark_pos(name[1], &pos, fnum) == OK)
16484 rettv->vval.v_number = 0;
16485 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016486 else
16487 EMSG(_(e_invarg));
16488 }
16489 }
16490}
16491
16492/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016493 * "setqflist()" function
16494 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016495 static void
16496f_setqflist(argvars, rettv)
16497 typval_T *argvars;
16498 typval_T *rettv;
16499{
16500 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16501}
16502
16503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016504 * "setreg()" function
16505 */
16506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016507f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016508 typval_T *argvars;
16509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510{
16511 int regname;
16512 char_u *strregname;
16513 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016514 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515 int append;
16516 char_u yank_type;
16517 long block_len;
16518
16519 block_len = -1;
16520 yank_type = MAUTO;
16521 append = FALSE;
16522
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016523 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016524 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016526 if (strregname == NULL)
16527 return; /* type error; errmsg already given */
16528 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 if (regname == 0 || regname == '@')
16530 regname = '"';
16531 else if (regname == '=')
16532 return;
16533
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016534 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016536 stropt = get_tv_string_chk(&argvars[2]);
16537 if (stropt == NULL)
16538 return; /* type error */
16539 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 switch (*stropt)
16541 {
16542 case 'a': case 'A': /* append */
16543 append = TRUE;
16544 break;
16545 case 'v': case 'c': /* character-wise selection */
16546 yank_type = MCHAR;
16547 break;
16548 case 'V': case 'l': /* line-wise selection */
16549 yank_type = MLINE;
16550 break;
16551#ifdef FEAT_VISUAL
16552 case 'b': case Ctrl_V: /* block-wise selection */
16553 yank_type = MBLOCK;
16554 if (VIM_ISDIGIT(stropt[1]))
16555 {
16556 ++stropt;
16557 block_len = getdigits(&stropt) - 1;
16558 --stropt;
16559 }
16560 break;
16561#endif
16562 }
16563 }
16564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016565 strval = get_tv_string_chk(&argvars[1]);
16566 if (strval != NULL)
16567 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016569 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570}
16571
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016572/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016573 * "settabvar()" function
16574 */
16575 static void
16576f_settabvar(argvars, rettv)
16577 typval_T *argvars;
16578 typval_T *rettv;
16579{
16580 tabpage_T *save_curtab;
16581 char_u *varname, *tabvarname;
16582 typval_T *varp;
16583 tabpage_T *tp;
16584
16585 rettv->vval.v_number = 0;
16586
16587 if (check_restricted() || check_secure())
16588 return;
16589
16590 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16591 varname = get_tv_string_chk(&argvars[1]);
16592 varp = &argvars[2];
16593
16594 if (tp != NULL && varname != NULL && varp != NULL)
16595 {
16596 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016597 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016598
16599 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16600 if (tabvarname != NULL)
16601 {
16602 STRCPY(tabvarname, "t:");
16603 STRCPY(tabvarname + 2, varname);
16604 set_var(tabvarname, varp, TRUE);
16605 vim_free(tabvarname);
16606 }
16607
16608 /* Restore current tabpage */
16609 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016610 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016611 }
16612}
16613
16614/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016615 * "settabwinvar()" function
16616 */
16617 static void
16618f_settabwinvar(argvars, rettv)
16619 typval_T *argvars;
16620 typval_T *rettv;
16621{
16622 setwinvar(argvars, rettv, 1);
16623}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624
16625/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016626 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016629f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016630 typval_T *argvars;
16631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016633 setwinvar(argvars, rettv, 0);
16634}
16635
16636/*
16637 * "setwinvar()" and "settabwinvar()" functions
16638 */
16639 static void
16640setwinvar(argvars, rettv, off)
16641 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016642 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016643 int off;
16644{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 win_T *win;
16646#ifdef FEAT_WINDOWS
16647 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016648 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649#endif
16650 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016651 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016653 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654
16655 if (check_restricted() || check_secure())
16656 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016657
16658#ifdef FEAT_WINDOWS
16659 if (off == 1)
16660 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16661 else
16662 tp = curtab;
16663#endif
16664 win = find_win_by_nr(&argvars[off], tp);
16665 varname = get_tv_string_chk(&argvars[off + 1]);
16666 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667
16668 if (win != NULL && varname != NULL && varp != NULL)
16669 {
16670#ifdef FEAT_WINDOWS
16671 /* set curwin to be our win, temporarily */
16672 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016673 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016674 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016675 if (!win_valid(win))
16676 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 curwin = win;
16678 curbuf = curwin->w_buffer;
16679#endif
16680
16681 if (*varname == '&')
16682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016683 long numval;
16684 char_u *strval;
16685 int error = FALSE;
16686
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016688 numval = get_tv_number_chk(varp, &error);
16689 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016690 if (!error && strval != NULL)
16691 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 }
16693 else
16694 {
16695 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16696 if (winvarname != NULL)
16697 {
16698 STRCPY(winvarname, "w:");
16699 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016700 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701 vim_free(winvarname);
16702 }
16703 }
16704
16705#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016706 /* Restore current tabpage and window, if still valid (autocomands can
16707 * make them invalid). */
16708 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016709 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 if (win_valid(save_curwin))
16711 {
16712 curwin = save_curwin;
16713 curbuf = curwin->w_buffer;
16714 }
16715#endif
16716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717}
16718
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016719#ifdef FEAT_CRYPT
16720/*
16721 * "sha256({string})" function
16722 */
16723 static void
16724f_sha256(argvars, rettv)
16725 typval_T *argvars;
16726 typval_T *rettv;
16727{
16728 char_u *p;
16729
16730 p = get_tv_string(&argvars[0]);
16731 rettv->vval.v_string = vim_strsave(
16732 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16733 rettv->v_type = VAR_STRING;
16734}
16735#endif /* FEAT_CRYPT */
16736
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016738 * "shellescape({string})" function
16739 */
16740 static void
16741f_shellescape(argvars, rettv)
16742 typval_T *argvars;
16743 typval_T *rettv;
16744{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016745 rettv->vval.v_string = vim_strsave_shellescape(
16746 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016747 rettv->v_type = VAR_STRING;
16748}
16749
16750/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016751 * shiftwidth() function
16752 */
16753 static void
16754f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016755 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016756 typval_T *rettv;
16757{
16758 rettv->vval.v_number = get_sw_value();
16759}
16760
16761/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016762 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763 */
16764 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016765f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016766 typval_T *argvars;
16767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016769 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770
Bram Moolenaar0d660222005-01-07 21:51:51 +000016771 p = get_tv_string(&argvars[0]);
16772 rettv->vval.v_string = vim_strsave(p);
16773 simplify_filename(rettv->vval.v_string); /* simplify in place */
16774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775}
16776
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016777#ifdef FEAT_FLOAT
16778/*
16779 * "sin()" function
16780 */
16781 static void
16782f_sin(argvars, rettv)
16783 typval_T *argvars;
16784 typval_T *rettv;
16785{
16786 float_T f;
16787
16788 rettv->v_type = VAR_FLOAT;
16789 if (get_float_arg(argvars, &f) == OK)
16790 rettv->vval.v_float = sin(f);
16791 else
16792 rettv->vval.v_float = 0.0;
16793}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016794
16795/*
16796 * "sinh()" function
16797 */
16798 static void
16799f_sinh(argvars, rettv)
16800 typval_T *argvars;
16801 typval_T *rettv;
16802{
16803 float_T f;
16804
16805 rettv->v_type = VAR_FLOAT;
16806 if (get_float_arg(argvars, &f) == OK)
16807 rettv->vval.v_float = sinh(f);
16808 else
16809 rettv->vval.v_float = 0.0;
16810}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016811#endif
16812
Bram Moolenaar0d660222005-01-07 21:51:51 +000016813static int
16814#ifdef __BORLANDC__
16815 _RTLENTRYF
16816#endif
16817 item_compare __ARGS((const void *s1, const void *s2));
16818static int
16819#ifdef __BORLANDC__
16820 _RTLENTRYF
16821#endif
16822 item_compare2 __ARGS((const void *s1, const void *s2));
16823
16824static int item_compare_ic;
16825static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016826static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016827static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016828#define ITEM_COMPARE_FAIL 999
16829
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016831 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016833 static int
16834#ifdef __BORLANDC__
16835_RTLENTRYF
16836#endif
16837item_compare(s1, s2)
16838 const void *s1;
16839 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016841 char_u *p1, *p2;
16842 char_u *tofree1, *tofree2;
16843 int res;
16844 char_u numbuf1[NUMBUFLEN];
16845 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016847 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16848 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016849 if (p1 == NULL)
16850 p1 = (char_u *)"";
16851 if (p2 == NULL)
16852 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016853 if (item_compare_ic)
16854 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016856 res = STRCMP(p1, p2);
16857 vim_free(tofree1);
16858 vim_free(tofree2);
16859 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860}
16861
16862 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016863#ifdef __BORLANDC__
16864_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016866item_compare2(s1, s2)
16867 const void *s1;
16868 const void *s2;
16869{
16870 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016871 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016872 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016875 /* shortcut after failure in previous call; compare all items equal */
16876 if (item_compare_func_err)
16877 return 0;
16878
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016879 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16880 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016881 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16882 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016883
16884 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016885 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016886 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16887 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016888 clear_tv(&argv[0]);
16889 clear_tv(&argv[1]);
16890
16891 if (res == FAIL)
16892 res = ITEM_COMPARE_FAIL;
16893 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016894 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16895 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016896 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016897 clear_tv(&rettv);
16898 return res;
16899}
16900
16901/*
16902 * "sort({list})" function
16903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016905f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016906 typval_T *argvars;
16907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908{
Bram Moolenaar33570922005-01-25 22:26:29 +000016909 list_T *l;
16910 listitem_T *li;
16911 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016912 long len;
16913 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914
Bram Moolenaar0d660222005-01-07 21:51:51 +000016915 if (argvars[0].v_type != VAR_LIST)
16916 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 else
16918 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016919 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016920 if (l == NULL || tv_check_lock(l->lv_lock,
16921 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922 return;
16923 rettv->vval.v_list = l;
16924 rettv->v_type = VAR_LIST;
16925 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926
Bram Moolenaar0d660222005-01-07 21:51:51 +000016927 len = list_len(l);
16928 if (len <= 1)
16929 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930
Bram Moolenaar0d660222005-01-07 21:51:51 +000016931 item_compare_ic = FALSE;
16932 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016933 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016934 if (argvars[1].v_type != VAR_UNKNOWN)
16935 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016936 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016937 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016938 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939 else
16940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016941 int error = FALSE;
16942
16943 i = get_tv_number_chk(&argvars[1], &error);
16944 if (error)
16945 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946 if (i == 1)
16947 item_compare_ic = TRUE;
16948 else
16949 item_compare_func = get_tv_string(&argvars[1]);
16950 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016951
16952 if (argvars[2].v_type != VAR_UNKNOWN)
16953 {
16954 /* optional third argument: {dict} */
16955 if (argvars[2].v_type != VAR_DICT)
16956 {
16957 EMSG(_(e_dictreq));
16958 return;
16959 }
16960 item_compare_selfdict = argvars[2].vval.v_dict;
16961 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016965 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966 if (ptrs == NULL)
16967 return;
16968 i = 0;
16969 for (li = l->lv_first; li != NULL; li = li->li_next)
16970 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016972 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973 /* test the compare function */
16974 if (item_compare_func != NULL
16975 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16976 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016977 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979 {
16980 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016981 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 item_compare_func == NULL ? item_compare : item_compare2);
16983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 if (!item_compare_func_err)
16985 {
16986 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016987 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016988 l->lv_len = 0;
16989 for (i = 0; i < len; ++i)
16990 list_append(l, ptrs[i]);
16991 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 }
16993
16994 vim_free(ptrs);
16995 }
16996}
16997
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016998/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016999 * "soundfold({word})" function
17000 */
17001 static void
17002f_soundfold(argvars, rettv)
17003 typval_T *argvars;
17004 typval_T *rettv;
17005{
17006 char_u *s;
17007
17008 rettv->v_type = VAR_STRING;
17009 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017010#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017011 rettv->vval.v_string = eval_soundfold(s);
17012#else
17013 rettv->vval.v_string = vim_strsave(s);
17014#endif
17015}
17016
17017/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017018 * "spellbadword()" function
17019 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017020 static void
17021f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017022 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017023 typval_T *rettv;
17024{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017025 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017026 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017027 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017028
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017029 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017030 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017031
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017032#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017033 if (argvars[0].v_type == VAR_UNKNOWN)
17034 {
17035 /* Find the start and length of the badly spelled word. */
17036 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17037 if (len != 0)
17038 word = ml_get_cursor();
17039 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017040 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017041 {
17042 char_u *str = get_tv_string_chk(&argvars[0]);
17043 int capcol = -1;
17044
17045 if (str != NULL)
17046 {
17047 /* Check the argument for spelling. */
17048 while (*str != NUL)
17049 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017050 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017051 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017052 {
17053 word = str;
17054 break;
17055 }
17056 str += len;
17057 }
17058 }
17059 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017060#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017061
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017062 list_append_string(rettv->vval.v_list, word, len);
17063 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017064 attr == HLF_SPB ? "bad" :
17065 attr == HLF_SPR ? "rare" :
17066 attr == HLF_SPL ? "local" :
17067 attr == HLF_SPC ? "caps" :
17068 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017069}
17070
17071/*
17072 * "spellsuggest()" function
17073 */
17074 static void
17075f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017076 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017077 typval_T *rettv;
17078{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017079#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017080 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017081 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017082 int maxcount;
17083 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017084 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017085 listitem_T *li;
17086 int need_capital = FALSE;
17087#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017088
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017089 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017090 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017091
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017092#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017093 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017094 {
17095 str = get_tv_string(&argvars[0]);
17096 if (argvars[1].v_type != VAR_UNKNOWN)
17097 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017098 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017099 if (maxcount <= 0)
17100 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017101 if (argvars[2].v_type != VAR_UNKNOWN)
17102 {
17103 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17104 if (typeerr)
17105 return;
17106 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017107 }
17108 else
17109 maxcount = 25;
17110
Bram Moolenaar4770d092006-01-12 23:22:24 +000017111 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017112
17113 for (i = 0; i < ga.ga_len; ++i)
17114 {
17115 str = ((char_u **)ga.ga_data)[i];
17116
17117 li = listitem_alloc();
17118 if (li == NULL)
17119 vim_free(str);
17120 else
17121 {
17122 li->li_tv.v_type = VAR_STRING;
17123 li->li_tv.v_lock = 0;
17124 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017125 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017126 }
17127 }
17128 ga_clear(&ga);
17129 }
17130#endif
17131}
17132
Bram Moolenaar0d660222005-01-07 21:51:51 +000017133 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017134f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017135 typval_T *argvars;
17136 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017137{
17138 char_u *str;
17139 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017140 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017141 regmatch_T regmatch;
17142 char_u patbuf[NUMBUFLEN];
17143 char_u *save_cpo;
17144 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017145 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017146 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017147 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017148
17149 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17150 save_cpo = p_cpo;
17151 p_cpo = (char_u *)"";
17152
17153 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017154 if (argvars[1].v_type != VAR_UNKNOWN)
17155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017156 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17157 if (pat == NULL)
17158 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017159 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017160 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017161 }
17162 if (pat == NULL || *pat == NUL)
17163 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017164
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017165 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017167 if (typeerr)
17168 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169
Bram Moolenaar0d660222005-01-07 21:51:51 +000017170 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17171 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017173 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017174 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017175 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017176 if (*str == NUL)
17177 match = FALSE; /* empty item at the end */
17178 else
17179 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180 if (match)
17181 end = regmatch.startp[0];
17182 else
17183 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017184 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17185 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017186 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017187 if (list_append_string(rettv->vval.v_list, str,
17188 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017189 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017190 }
17191 if (!match)
17192 break;
17193 /* Advance to just after the match. */
17194 if (regmatch.endp[0] > str)
17195 col = 0;
17196 else
17197 {
17198 /* Don't get stuck at the same match. */
17199#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017200 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201#else
17202 col = 1;
17203#endif
17204 }
17205 str = regmatch.endp[0];
17206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207
Bram Moolenaar0d660222005-01-07 21:51:51 +000017208 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210
Bram Moolenaar0d660222005-01-07 21:51:51 +000017211 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212}
17213
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017214#ifdef FEAT_FLOAT
17215/*
17216 * "sqrt()" function
17217 */
17218 static void
17219f_sqrt(argvars, rettv)
17220 typval_T *argvars;
17221 typval_T *rettv;
17222{
17223 float_T f;
17224
17225 rettv->v_type = VAR_FLOAT;
17226 if (get_float_arg(argvars, &f) == OK)
17227 rettv->vval.v_float = sqrt(f);
17228 else
17229 rettv->vval.v_float = 0.0;
17230}
17231
17232/*
17233 * "str2float()" function
17234 */
17235 static void
17236f_str2float(argvars, rettv)
17237 typval_T *argvars;
17238 typval_T *rettv;
17239{
17240 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17241
17242 if (*p == '+')
17243 p = skipwhite(p + 1);
17244 (void)string2float(p, &rettv->vval.v_float);
17245 rettv->v_type = VAR_FLOAT;
17246}
17247#endif
17248
Bram Moolenaar2c932302006-03-18 21:42:09 +000017249/*
17250 * "str2nr()" function
17251 */
17252 static void
17253f_str2nr(argvars, rettv)
17254 typval_T *argvars;
17255 typval_T *rettv;
17256{
17257 int base = 10;
17258 char_u *p;
17259 long n;
17260
17261 if (argvars[1].v_type != VAR_UNKNOWN)
17262 {
17263 base = get_tv_number(&argvars[1]);
17264 if (base != 8 && base != 10 && base != 16)
17265 {
17266 EMSG(_(e_invarg));
17267 return;
17268 }
17269 }
17270
17271 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017272 if (*p == '+')
17273 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017274 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17275 rettv->vval.v_number = n;
17276}
17277
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278#ifdef HAVE_STRFTIME
17279/*
17280 * "strftime({format}[, {time}])" function
17281 */
17282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017283f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017284 typval_T *argvars;
17285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286{
17287 char_u result_buf[256];
17288 struct tm *curtime;
17289 time_t seconds;
17290 char_u *p;
17291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017292 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017294 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017295 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296 seconds = time(NULL);
17297 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017298 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299 curtime = localtime(&seconds);
17300 /* MSVC returns NULL for an invalid value of seconds. */
17301 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017302 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303 else
17304 {
17305# ifdef FEAT_MBYTE
17306 vimconv_T conv;
17307 char_u *enc;
17308
17309 conv.vc_type = CONV_NONE;
17310 enc = enc_locale();
17311 convert_setup(&conv, p_enc, enc);
17312 if (conv.vc_type != CONV_NONE)
17313 p = string_convert(&conv, p, NULL);
17314# endif
17315 if (p != NULL)
17316 (void)strftime((char *)result_buf, sizeof(result_buf),
17317 (char *)p, curtime);
17318 else
17319 result_buf[0] = NUL;
17320
17321# ifdef FEAT_MBYTE
17322 if (conv.vc_type != CONV_NONE)
17323 vim_free(p);
17324 convert_setup(&conv, enc, p_enc);
17325 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017326 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 else
17328# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017329 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330
17331# ifdef FEAT_MBYTE
17332 /* Release conversion descriptors */
17333 convert_setup(&conv, NULL, NULL);
17334 vim_free(enc);
17335# endif
17336 }
17337}
17338#endif
17339
17340/*
17341 * "stridx()" function
17342 */
17343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017344f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017345 typval_T *argvars;
17346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347{
17348 char_u buf[NUMBUFLEN];
17349 char_u *needle;
17350 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017351 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017353 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017355 needle = get_tv_string_chk(&argvars[1]);
17356 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017357 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017358 if (needle == NULL || haystack == NULL)
17359 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360
Bram Moolenaar33570922005-01-25 22:26:29 +000017361 if (argvars[2].v_type != VAR_UNKNOWN)
17362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017363 int error = FALSE;
17364
17365 start_idx = get_tv_number_chk(&argvars[2], &error);
17366 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017367 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017368 if (start_idx >= 0)
17369 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017370 }
17371
17372 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17373 if (pos != NULL)
17374 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375}
17376
17377/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017378 * "string()" function
17379 */
17380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017381f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017382 typval_T *argvars;
17383 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017384{
17385 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017386 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017389 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017390 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017391 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017392 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393}
17394
17395/*
17396 * "strlen()" function
17397 */
17398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017399f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017400 typval_T *argvars;
17401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017403 rettv->vval.v_number = (varnumber_T)(STRLEN(
17404 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405}
17406
17407/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017408 * "strchars()" function
17409 */
17410 static void
17411f_strchars(argvars, rettv)
17412 typval_T *argvars;
17413 typval_T *rettv;
17414{
17415 char_u *s = get_tv_string(&argvars[0]);
17416#ifdef FEAT_MBYTE
17417 varnumber_T len = 0;
17418
17419 while (*s != NUL)
17420 {
17421 mb_cptr2char_adv(&s);
17422 ++len;
17423 }
17424 rettv->vval.v_number = len;
17425#else
17426 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17427#endif
17428}
17429
17430/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017431 * "strdisplaywidth()" function
17432 */
17433 static void
17434f_strdisplaywidth(argvars, rettv)
17435 typval_T *argvars;
17436 typval_T *rettv;
17437{
17438 char_u *s = get_tv_string(&argvars[0]);
17439 int col = 0;
17440
17441 if (argvars[1].v_type != VAR_UNKNOWN)
17442 col = get_tv_number(&argvars[1]);
17443
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017444 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017445}
17446
17447/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017448 * "strwidth()" function
17449 */
17450 static void
17451f_strwidth(argvars, rettv)
17452 typval_T *argvars;
17453 typval_T *rettv;
17454{
17455 char_u *s = get_tv_string(&argvars[0]);
17456
17457 rettv->vval.v_number = (varnumber_T)(
17458#ifdef FEAT_MBYTE
17459 mb_string2cells(s, -1)
17460#else
17461 STRLEN(s)
17462#endif
17463 );
17464}
17465
17466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 * "strpart()" function
17468 */
17469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017470f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017471 typval_T *argvars;
17472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473{
17474 char_u *p;
17475 int n;
17476 int len;
17477 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017478 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017480 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 slen = (int)STRLEN(p);
17482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017483 n = get_tv_number_chk(&argvars[1], &error);
17484 if (error)
17485 len = 0;
17486 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017487 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 else
17489 len = slen - n; /* default len: all bytes that are available. */
17490
17491 /*
17492 * Only return the overlap between the specified part and the actual
17493 * string.
17494 */
17495 if (n < 0)
17496 {
17497 len += n;
17498 n = 0;
17499 }
17500 else if (n > slen)
17501 n = slen;
17502 if (len < 0)
17503 len = 0;
17504 else if (n + len > slen)
17505 len = slen - n;
17506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017507 rettv->v_type = VAR_STRING;
17508 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509}
17510
17511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017512 * "strridx()" function
17513 */
17514 static void
17515f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017516 typval_T *argvars;
17517 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017518{
17519 char_u buf[NUMBUFLEN];
17520 char_u *needle;
17521 char_u *haystack;
17522 char_u *rest;
17523 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017524 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017526 needle = get_tv_string_chk(&argvars[1]);
17527 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017528
17529 rettv->vval.v_number = -1;
17530 if (needle == NULL || haystack == NULL)
17531 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017532
17533 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017534 if (argvars[2].v_type != VAR_UNKNOWN)
17535 {
17536 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017537 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017538 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017539 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017540 }
17541 else
17542 end_idx = haystack_len;
17543
Bram Moolenaar0d660222005-01-07 21:51:51 +000017544 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017545 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017546 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017547 lastmatch = haystack + end_idx;
17548 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017549 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017550 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017551 for (rest = haystack; *rest != '\0'; ++rest)
17552 {
17553 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017554 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017555 break;
17556 lastmatch = rest;
17557 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017558 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017559
17560 if (lastmatch == NULL)
17561 rettv->vval.v_number = -1;
17562 else
17563 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17564}
17565
17566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 * "strtrans()" function
17568 */
17569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017570f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017571 typval_T *argvars;
17572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017574 rettv->v_type = VAR_STRING;
17575 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576}
17577
17578/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017579 * "submatch()" function
17580 */
17581 static void
17582f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017583 typval_T *argvars;
17584 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017585{
17586 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017587 rettv->vval.v_string =
17588 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017589}
17590
17591/*
17592 * "substitute()" function
17593 */
17594 static void
17595f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017596 typval_T *argvars;
17597 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017598{
17599 char_u patbuf[NUMBUFLEN];
17600 char_u subbuf[NUMBUFLEN];
17601 char_u flagsbuf[NUMBUFLEN];
17602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017603 char_u *str = get_tv_string_chk(&argvars[0]);
17604 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17605 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17606 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17607
Bram Moolenaar0d660222005-01-07 21:51:51 +000017608 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017609 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17610 rettv->vval.v_string = NULL;
17611 else
17612 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017613}
17614
17615/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017616 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017619f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017620 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622{
17623 int id = 0;
17624#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017625 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 long col;
17627 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017628 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017630 lnum = get_tv_lnum(argvars); /* -1 on type error */
17631 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17632 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017634 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017635 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017636 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637#endif
17638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017639 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640}
17641
17642/*
17643 * "synIDattr(id, what [, mode])" function
17644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017646f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017647 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649{
17650 char_u *p = NULL;
17651#ifdef FEAT_SYN_HL
17652 int id;
17653 char_u *what;
17654 char_u *mode;
17655 char_u modebuf[NUMBUFLEN];
17656 int modec;
17657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017658 id = get_tv_number(&argvars[0]);
17659 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017660 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017662 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017664 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 modec = 0; /* replace invalid with current */
17666 }
17667 else
17668 {
17669#ifdef FEAT_GUI
17670 if (gui.in_use)
17671 modec = 'g';
17672 else
17673#endif
17674 if (t_colors > 1)
17675 modec = 'c';
17676 else
17677 modec = 't';
17678 }
17679
17680
17681 switch (TOLOWER_ASC(what[0]))
17682 {
17683 case 'b':
17684 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17685 p = highlight_color(id, what, modec);
17686 else /* bold */
17687 p = highlight_has_attr(id, HL_BOLD, modec);
17688 break;
17689
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017690 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 p = highlight_color(id, what, modec);
17692 break;
17693
17694 case 'i':
17695 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17696 p = highlight_has_attr(id, HL_INVERSE, modec);
17697 else /* italic */
17698 p = highlight_has_attr(id, HL_ITALIC, modec);
17699 break;
17700
17701 case 'n': /* name */
17702 p = get_highlight_name(NULL, id - 1);
17703 break;
17704
17705 case 'r': /* reverse */
17706 p = highlight_has_attr(id, HL_INVERSE, modec);
17707 break;
17708
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017709 case 's':
17710 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17711 p = highlight_color(id, what, modec);
17712 else /* standout */
17713 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 break;
17715
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017716 case 'u':
17717 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17718 /* underline */
17719 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17720 else
17721 /* undercurl */
17722 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 break;
17724 }
17725
17726 if (p != NULL)
17727 p = vim_strsave(p);
17728#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017729 rettv->v_type = VAR_STRING;
17730 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731}
17732
17733/*
17734 * "synIDtrans(id)" function
17735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017737f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017738 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740{
17741 int id;
17742
17743#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017744 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745
17746 if (id > 0)
17747 id = syn_get_final_id(id);
17748 else
17749#endif
17750 id = 0;
17751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017752 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753}
17754
17755/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017756 * "synconcealed(lnum, col)" function
17757 */
17758 static void
17759f_synconcealed(argvars, rettv)
17760 typval_T *argvars UNUSED;
17761 typval_T *rettv;
17762{
17763#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17764 long lnum;
17765 long col;
17766 int syntax_flags = 0;
17767 int cchar;
17768 int matchid = 0;
17769 char_u str[NUMBUFLEN];
17770#endif
17771
17772 rettv->v_type = VAR_LIST;
17773 rettv->vval.v_list = NULL;
17774
17775#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17776 lnum = get_tv_lnum(argvars); /* -1 on type error */
17777 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17778
17779 vim_memset(str, NUL, sizeof(str));
17780
17781 if (rettv_list_alloc(rettv) != FAIL)
17782 {
17783 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17784 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17785 && curwin->w_p_cole > 0)
17786 {
17787 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17788 syntax_flags = get_syntax_info(&matchid);
17789
17790 /* get the conceal character */
17791 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17792 {
17793 cchar = syn_get_sub_char();
17794 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17795 cchar = lcs_conceal;
17796 if (cchar != NUL)
17797 {
17798# ifdef FEAT_MBYTE
17799 if (has_mbyte)
17800 (*mb_char2bytes)(cchar, str);
17801 else
17802# endif
17803 str[0] = cchar;
17804 }
17805 }
17806 }
17807
17808 list_append_number(rettv->vval.v_list,
17809 (syntax_flags & HL_CONCEAL) != 0);
17810 /* -1 to auto-determine strlen */
17811 list_append_string(rettv->vval.v_list, str, -1);
17812 list_append_number(rettv->vval.v_list, matchid);
17813 }
17814#endif
17815}
17816
17817/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017818 * "synstack(lnum, col)" function
17819 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017820 static void
17821f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017822 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017823 typval_T *rettv;
17824{
17825#ifdef FEAT_SYN_HL
17826 long lnum;
17827 long col;
17828 int i;
17829 int id;
17830#endif
17831
17832 rettv->v_type = VAR_LIST;
17833 rettv->vval.v_list = NULL;
17834
17835#ifdef FEAT_SYN_HL
17836 lnum = get_tv_lnum(argvars); /* -1 on type error */
17837 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17838
17839 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017840 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017841 && rettv_list_alloc(rettv) != FAIL)
17842 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017843 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017844 for (i = 0; ; ++i)
17845 {
17846 id = syn_get_stack_item(i);
17847 if (id < 0)
17848 break;
17849 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17850 break;
17851 }
17852 }
17853#endif
17854}
17855
17856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857 * "system()" function
17858 */
17859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017860f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017861 typval_T *argvars;
17862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017864 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017866 char_u *infile = NULL;
17867 char_u buf[NUMBUFLEN];
17868 int err = FALSE;
17869 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017871 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017872 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017873
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017874 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017875 {
17876 /*
17877 * Write the string to a temp file, to be used for input of the shell
17878 * command.
17879 */
17880 if ((infile = vim_tempname('i')) == NULL)
17881 {
17882 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017883 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017884 }
17885
17886 fd = mch_fopen((char *)infile, WRITEBIN);
17887 if (fd == NULL)
17888 {
17889 EMSG2(_(e_notopen), infile);
17890 goto done;
17891 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017892 p = get_tv_string_buf_chk(&argvars[1], buf);
17893 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017894 {
17895 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017896 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017897 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017898 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17899 err = TRUE;
17900 if (fclose(fd) != 0)
17901 err = TRUE;
17902 if (err)
17903 {
17904 EMSG(_("E677: Error writing temp file"));
17905 goto done;
17906 }
17907 }
17908
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017909 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17910 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017911
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912#ifdef USE_CR
17913 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017914 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915 {
17916 char_u *s;
17917
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017918 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 {
17920 if (*s == CAR)
17921 *s = NL;
17922 }
17923 }
17924#else
17925# ifdef USE_CRNL
17926 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017927 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 {
17929 char_u *s, *d;
17930
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017931 d = res;
17932 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933 {
17934 if (s[0] == CAR && s[1] == NL)
17935 ++s;
17936 *d++ = *s;
17937 }
17938 *d = NUL;
17939 }
17940# endif
17941#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017942
17943done:
17944 if (infile != NULL)
17945 {
17946 mch_remove(infile);
17947 vim_free(infile);
17948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017949 rettv->v_type = VAR_STRING;
17950 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951}
17952
17953/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017954 * "tabpagebuflist()" function
17955 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017956 static void
17957f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017958 typval_T *argvars UNUSED;
17959 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017960{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017961#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017962 tabpage_T *tp;
17963 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017964
17965 if (argvars[0].v_type == VAR_UNKNOWN)
17966 wp = firstwin;
17967 else
17968 {
17969 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17970 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017971 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017972 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017973 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017974 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017975 for (; wp != NULL; wp = wp->w_next)
17976 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017977 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017978 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017979 }
17980#endif
17981}
17982
17983
17984/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017985 * "tabpagenr()" function
17986 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017987 static void
17988f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017989 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017990 typval_T *rettv;
17991{
17992 int nr = 1;
17993#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017994 char_u *arg;
17995
17996 if (argvars[0].v_type != VAR_UNKNOWN)
17997 {
17998 arg = get_tv_string_chk(&argvars[0]);
17999 nr = 0;
18000 if (arg != NULL)
18001 {
18002 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018003 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018004 else
18005 EMSG2(_(e_invexpr2), arg);
18006 }
18007 }
18008 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018009 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018010#endif
18011 rettv->vval.v_number = nr;
18012}
18013
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018014
18015#ifdef FEAT_WINDOWS
18016static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18017
18018/*
18019 * Common code for tabpagewinnr() and winnr().
18020 */
18021 static int
18022get_winnr(tp, argvar)
18023 tabpage_T *tp;
18024 typval_T *argvar;
18025{
18026 win_T *twin;
18027 int nr = 1;
18028 win_T *wp;
18029 char_u *arg;
18030
18031 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18032 if (argvar->v_type != VAR_UNKNOWN)
18033 {
18034 arg = get_tv_string_chk(argvar);
18035 if (arg == NULL)
18036 nr = 0; /* type error; errmsg already given */
18037 else if (STRCMP(arg, "$") == 0)
18038 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18039 else if (STRCMP(arg, "#") == 0)
18040 {
18041 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18042 if (twin == NULL)
18043 nr = 0;
18044 }
18045 else
18046 {
18047 EMSG2(_(e_invexpr2), arg);
18048 nr = 0;
18049 }
18050 }
18051
18052 if (nr > 0)
18053 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18054 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018055 {
18056 if (wp == NULL)
18057 {
18058 /* didn't find it in this tabpage */
18059 nr = 0;
18060 break;
18061 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018062 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018063 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018064 return nr;
18065}
18066#endif
18067
18068/*
18069 * "tabpagewinnr()" function
18070 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018071 static void
18072f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018073 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018074 typval_T *rettv;
18075{
18076 int nr = 1;
18077#ifdef FEAT_WINDOWS
18078 tabpage_T *tp;
18079
18080 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18081 if (tp == NULL)
18082 nr = 0;
18083 else
18084 nr = get_winnr(tp, &argvars[1]);
18085#endif
18086 rettv->vval.v_number = nr;
18087}
18088
18089
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018090/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018091 * "tagfiles()" function
18092 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018093 static void
18094f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018095 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018096 typval_T *rettv;
18097{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018098 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018099 tagname_T tn;
18100 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018101
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018102 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018103 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018104 fname = alloc(MAXPATHL);
18105 if (fname == NULL)
18106 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018107
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018108 for (first = TRUE; ; first = FALSE)
18109 if (get_tagfname(&tn, first, fname) == FAIL
18110 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018111 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018112 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018113 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018114}
18115
18116/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018117 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018118 */
18119 static void
18120f_taglist(argvars, rettv)
18121 typval_T *argvars;
18122 typval_T *rettv;
18123{
18124 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018125
18126 tag_pattern = get_tv_string(&argvars[0]);
18127
18128 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018129 if (*tag_pattern == NUL)
18130 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018131
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018132 if (rettv_list_alloc(rettv) == OK)
18133 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018134}
18135
18136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137 * "tempname()" function
18138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018140f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018141 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143{
18144 static int x = 'A';
18145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018146 rettv->v_type = VAR_STRING;
18147 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148
18149 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18150 * names. Skip 'I' and 'O', they are used for shell redirection. */
18151 do
18152 {
18153 if (x == 'Z')
18154 x = '0';
18155 else if (x == '9')
18156 x = 'A';
18157 else
18158 {
18159#ifdef EBCDIC
18160 if (x == 'I')
18161 x = 'J';
18162 else if (x == 'R')
18163 x = 'S';
18164 else
18165#endif
18166 ++x;
18167 }
18168 } while (x == 'I' || x == 'O');
18169}
18170
18171/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018172 * "test(list)" function: Just checking the walls...
18173 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018174 static void
18175f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018176 typval_T *argvars UNUSED;
18177 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018178{
18179 /* Used for unit testing. Change the code below to your liking. */
18180#if 0
18181 listitem_T *li;
18182 list_T *l;
18183 char_u *bad, *good;
18184
18185 if (argvars[0].v_type != VAR_LIST)
18186 return;
18187 l = argvars[0].vval.v_list;
18188 if (l == NULL)
18189 return;
18190 li = l->lv_first;
18191 if (li == NULL)
18192 return;
18193 bad = get_tv_string(&li->li_tv);
18194 li = li->li_next;
18195 if (li == NULL)
18196 return;
18197 good = get_tv_string(&li->li_tv);
18198 rettv->vval.v_number = test_edit_score(bad, good);
18199#endif
18200}
18201
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018202#ifdef FEAT_FLOAT
18203/*
18204 * "tan()" function
18205 */
18206 static void
18207f_tan(argvars, rettv)
18208 typval_T *argvars;
18209 typval_T *rettv;
18210{
18211 float_T f;
18212
18213 rettv->v_type = VAR_FLOAT;
18214 if (get_float_arg(argvars, &f) == OK)
18215 rettv->vval.v_float = tan(f);
18216 else
18217 rettv->vval.v_float = 0.0;
18218}
18219
18220/*
18221 * "tanh()" function
18222 */
18223 static void
18224f_tanh(argvars, rettv)
18225 typval_T *argvars;
18226 typval_T *rettv;
18227{
18228 float_T f;
18229
18230 rettv->v_type = VAR_FLOAT;
18231 if (get_float_arg(argvars, &f) == OK)
18232 rettv->vval.v_float = tanh(f);
18233 else
18234 rettv->vval.v_float = 0.0;
18235}
18236#endif
18237
Bram Moolenaard52d9742005-08-21 22:20:28 +000018238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239 * "tolower(string)" function
18240 */
18241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018242f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018243 typval_T *argvars;
18244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245{
18246 char_u *p;
18247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018248 p = vim_strsave(get_tv_string(&argvars[0]));
18249 rettv->v_type = VAR_STRING;
18250 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251
18252 if (p != NULL)
18253 while (*p != NUL)
18254 {
18255#ifdef FEAT_MBYTE
18256 int l;
18257
18258 if (enc_utf8)
18259 {
18260 int c, lc;
18261
18262 c = utf_ptr2char(p);
18263 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018264 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 /* TODO: reallocate string when byte count changes. */
18266 if (utf_char2len(lc) == l)
18267 utf_char2bytes(lc, p);
18268 p += l;
18269 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018270 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 p += l; /* skip multi-byte character */
18272 else
18273#endif
18274 {
18275 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18276 ++p;
18277 }
18278 }
18279}
18280
18281/*
18282 * "toupper(string)" function
18283 */
18284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018285f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018286 typval_T *argvars;
18287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018289 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018290 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291}
18292
18293/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018294 * "tr(string, fromstr, tostr)" function
18295 */
18296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018297f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018298 typval_T *argvars;
18299 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018300{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018301 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018302 char_u *fromstr;
18303 char_u *tostr;
18304 char_u *p;
18305#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018306 int inlen;
18307 int fromlen;
18308 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018309 int idx;
18310 char_u *cpstr;
18311 int cplen;
18312 int first = TRUE;
18313#endif
18314 char_u buf[NUMBUFLEN];
18315 char_u buf2[NUMBUFLEN];
18316 garray_T ga;
18317
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018318 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018319 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18320 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018321
18322 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018323 rettv->v_type = VAR_STRING;
18324 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018325 if (fromstr == NULL || tostr == NULL)
18326 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018327 ga_init2(&ga, (int)sizeof(char), 80);
18328
18329#ifdef FEAT_MBYTE
18330 if (!has_mbyte)
18331#endif
18332 /* not multi-byte: fromstr and tostr must be the same length */
18333 if (STRLEN(fromstr) != STRLEN(tostr))
18334 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018335#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018336error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018337#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018338 EMSG2(_(e_invarg2), fromstr);
18339 ga_clear(&ga);
18340 return;
18341 }
18342
18343 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018344 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018345 {
18346#ifdef FEAT_MBYTE
18347 if (has_mbyte)
18348 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018349 inlen = (*mb_ptr2len)(in_str);
18350 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018351 cplen = inlen;
18352 idx = 0;
18353 for (p = fromstr; *p != NUL; p += fromlen)
18354 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018355 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018356 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018357 {
18358 for (p = tostr; *p != NUL; p += tolen)
18359 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018360 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018361 if (idx-- == 0)
18362 {
18363 cplen = tolen;
18364 cpstr = p;
18365 break;
18366 }
18367 }
18368 if (*p == NUL) /* tostr is shorter than fromstr */
18369 goto error;
18370 break;
18371 }
18372 ++idx;
18373 }
18374
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018375 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018376 {
18377 /* Check that fromstr and tostr have the same number of
18378 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018379 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018380 first = FALSE;
18381 for (p = tostr; *p != NUL; p += tolen)
18382 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018383 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018384 --idx;
18385 }
18386 if (idx != 0)
18387 goto error;
18388 }
18389
18390 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018391 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018392 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018393
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018394 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018395 }
18396 else
18397#endif
18398 {
18399 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018400 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018401 if (p != NULL)
18402 ga_append(&ga, tostr[p - fromstr]);
18403 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018404 ga_append(&ga, *in_str);
18405 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018406 }
18407 }
18408
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018409 /* add a terminating NUL */
18410 ga_grow(&ga, 1);
18411 ga_append(&ga, NUL);
18412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018413 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018414}
18415
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018416#ifdef FEAT_FLOAT
18417/*
18418 * "trunc({float})" function
18419 */
18420 static void
18421f_trunc(argvars, rettv)
18422 typval_T *argvars;
18423 typval_T *rettv;
18424{
18425 float_T f;
18426
18427 rettv->v_type = VAR_FLOAT;
18428 if (get_float_arg(argvars, &f) == OK)
18429 /* trunc() is not in C90, use floor() or ceil() instead. */
18430 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18431 else
18432 rettv->vval.v_float = 0.0;
18433}
18434#endif
18435
Bram Moolenaar8299df92004-07-10 09:47:34 +000018436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437 * "type(expr)" function
18438 */
18439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018440f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018441 typval_T *argvars;
18442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018444 int n;
18445
18446 switch (argvars[0].v_type)
18447 {
18448 case VAR_NUMBER: n = 0; break;
18449 case VAR_STRING: n = 1; break;
18450 case VAR_FUNC: n = 2; break;
18451 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018452 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018453#ifdef FEAT_FLOAT
18454 case VAR_FLOAT: n = 5; break;
18455#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018456 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18457 }
18458 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459}
18460
18461/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018462 * "undofile(name)" function
18463 */
18464 static void
18465f_undofile(argvars, rettv)
18466 typval_T *argvars;
18467 typval_T *rettv;
18468{
18469 rettv->v_type = VAR_STRING;
18470#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018471 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018472 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018473
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018474 if (*fname == NUL)
18475 {
18476 /* If there is no file name there will be no undo file. */
18477 rettv->vval.v_string = NULL;
18478 }
18479 else
18480 {
18481 char_u *ffname = FullName_save(fname, FALSE);
18482
18483 if (ffname != NULL)
18484 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18485 vim_free(ffname);
18486 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018487 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018488#else
18489 rettv->vval.v_string = NULL;
18490#endif
18491}
18492
18493/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018494 * "undotree()" function
18495 */
18496 static void
18497f_undotree(argvars, rettv)
18498 typval_T *argvars UNUSED;
18499 typval_T *rettv;
18500{
18501 if (rettv_dict_alloc(rettv) == OK)
18502 {
18503 dict_T *dict = rettv->vval.v_dict;
18504 list_T *list;
18505
Bram Moolenaar730cde92010-06-27 05:18:54 +020018506 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018507 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018508 dict_add_nr_str(dict, "save_last",
18509 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018510 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18511 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018512 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018513
18514 list = list_alloc();
18515 if (list != NULL)
18516 {
18517 u_eval_tree(curbuf->b_u_oldhead, list);
18518 dict_add_list(dict, "entries", list);
18519 }
18520 }
18521}
18522
18523/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018524 * "values(dict)" function
18525 */
18526 static void
18527f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018528 typval_T *argvars;
18529 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018530{
18531 dict_list(argvars, rettv, 1);
18532}
18533
18534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535 * "virtcol(string)" function
18536 */
18537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018538f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018539 typval_T *argvars;
18540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541{
18542 colnr_T vcol = 0;
18543 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018544 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018546 fp = var2fpos(&argvars[0], FALSE, &fnum);
18547 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18548 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 {
18550 getvvcol(curwin, fp, NULL, NULL, &vcol);
18551 ++vcol;
18552 }
18553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018554 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555}
18556
18557/*
18558 * "visualmode()" function
18559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018561f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018562 typval_T *argvars UNUSED;
18563 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564{
18565#ifdef FEAT_VISUAL
18566 char_u str[2];
18567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018568 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 str[0] = curbuf->b_visual_mode_eval;
18570 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018571 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572
18573 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018574 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576#endif
18577}
18578
18579/*
18580 * "winbufnr(nr)" function
18581 */
18582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018583f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018584 typval_T *argvars;
18585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586{
18587 win_T *wp;
18588
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018589 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018591 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018593 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594}
18595
18596/*
18597 * "wincol()" function
18598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018600f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018601 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603{
18604 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018605 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606}
18607
18608/*
18609 * "winheight(nr)" function
18610 */
18611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018612f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018613 typval_T *argvars;
18614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615{
18616 win_T *wp;
18617
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018618 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018620 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018622 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623}
18624
18625/*
18626 * "winline()" function
18627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018629f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018630 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632{
18633 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018634 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635}
18636
18637/*
18638 * "winnr()" function
18639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018641f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018642 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644{
18645 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018646
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018648 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018650 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651}
18652
18653/*
18654 * "winrestcmd()" function
18655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018657f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018658 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660{
18661#ifdef FEAT_WINDOWS
18662 win_T *wp;
18663 int winnr = 1;
18664 garray_T ga;
18665 char_u buf[50];
18666
18667 ga_init2(&ga, (int)sizeof(char), 70);
18668 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18669 {
18670 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18671 ga_concat(&ga, buf);
18672# ifdef FEAT_VERTSPLIT
18673 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18674 ga_concat(&ga, buf);
18675# endif
18676 ++winnr;
18677 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018678 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018680 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018682 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685}
18686
18687/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018688 * "winrestview()" function
18689 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018690 static void
18691f_winrestview(argvars, rettv)
18692 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018693 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018694{
18695 dict_T *dict;
18696
18697 if (argvars[0].v_type != VAR_DICT
18698 || (dict = argvars[0].vval.v_dict) == NULL)
18699 EMSG(_(e_invarg));
18700 else
18701 {
18702 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18703 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18704#ifdef FEAT_VIRTUALEDIT
18705 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18706#endif
18707 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018708 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018709
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018710 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018711#ifdef FEAT_DIFF
18712 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18713#endif
18714 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18715 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18716
18717 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018718 win_new_height(curwin, curwin->w_height);
18719# ifdef FEAT_VERTSPLIT
18720 win_new_width(curwin, W_WIDTH(curwin));
18721# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018722 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018723
18724 if (curwin->w_topline == 0)
18725 curwin->w_topline = 1;
18726 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18727 curwin->w_topline = curbuf->b_ml.ml_line_count;
18728#ifdef FEAT_DIFF
18729 check_topfill(curwin, TRUE);
18730#endif
18731 }
18732}
18733
18734/*
18735 * "winsaveview()" function
18736 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018737 static void
18738f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018739 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018740 typval_T *rettv;
18741{
18742 dict_T *dict;
18743
Bram Moolenaara800b422010-06-27 01:15:55 +020018744 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018745 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018746 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018747
18748 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18749 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18750#ifdef FEAT_VIRTUALEDIT
18751 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18752#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018753 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018754 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18755
18756 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18757#ifdef FEAT_DIFF
18758 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18759#endif
18760 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18761 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18762}
18763
18764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 * "winwidth(nr)" function
18766 */
18767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018768f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018769 typval_T *argvars;
18770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771{
18772 win_T *wp;
18773
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018774 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018776 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 else
18778#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018779 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018781 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782#endif
18783}
18784
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018786 * "writefile()" function
18787 */
18788 static void
18789f_writefile(argvars, rettv)
18790 typval_T *argvars;
18791 typval_T *rettv;
18792{
18793 int binary = FALSE;
18794 char_u *fname;
18795 FILE *fd;
18796 listitem_T *li;
18797 char_u *s;
18798 int ret = 0;
18799 int c;
18800
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018801 if (check_restricted() || check_secure())
18802 return;
18803
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018804 if (argvars[0].v_type != VAR_LIST)
18805 {
18806 EMSG2(_(e_listarg), "writefile()");
18807 return;
18808 }
18809 if (argvars[0].vval.v_list == NULL)
18810 return;
18811
18812 if (argvars[2].v_type != VAR_UNKNOWN
18813 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18814 binary = TRUE;
18815
18816 /* Always open the file in binary mode, library functions have a mind of
18817 * their own about CR-LF conversion. */
18818 fname = get_tv_string(&argvars[1]);
18819 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18820 {
18821 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18822 ret = -1;
18823 }
18824 else
18825 {
18826 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18827 li = li->li_next)
18828 {
18829 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18830 {
18831 if (*s == '\n')
18832 c = putc(NUL, fd);
18833 else
18834 c = putc(*s, fd);
18835 if (c == EOF)
18836 {
18837 ret = -1;
18838 break;
18839 }
18840 }
18841 if (!binary || li->li_next != NULL)
18842 if (putc('\n', fd) == EOF)
18843 {
18844 ret = -1;
18845 break;
18846 }
18847 if (ret < 0)
18848 {
18849 EMSG(_(e_write));
18850 break;
18851 }
18852 }
18853 fclose(fd);
18854 }
18855
18856 rettv->vval.v_number = ret;
18857}
18858
18859/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018860 * "xor(expr, expr)" function
18861 */
18862 static void
18863f_xor(argvars, rettv)
18864 typval_T *argvars;
18865 typval_T *rettv;
18866{
18867 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18868 ^ get_tv_number_chk(&argvars[1], NULL);
18869}
18870
18871
18872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018873 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018874 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 */
18876 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018877var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018878 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018879 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018880 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018882 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018884 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885
Bram Moolenaara5525202006-03-02 22:52:09 +000018886 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018887 if (varp->v_type == VAR_LIST)
18888 {
18889 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018890 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018891 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018892 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018893
18894 l = varp->vval.v_list;
18895 if (l == NULL)
18896 return NULL;
18897
18898 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018899 pos.lnum = list_find_nr(l, 0L, &error);
18900 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018901 return NULL; /* invalid line number */
18902
18903 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018904 pos.col = list_find_nr(l, 1L, &error);
18905 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018906 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018907 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018908
18909 /* We accept "$" for the column number: last column. */
18910 li = list_find(l, 1L);
18911 if (li != NULL && li->li_tv.v_type == VAR_STRING
18912 && li->li_tv.vval.v_string != NULL
18913 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18914 pos.col = len + 1;
18915
Bram Moolenaara5525202006-03-02 22:52:09 +000018916 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018917 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018918 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018919 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018920
Bram Moolenaara5525202006-03-02 22:52:09 +000018921#ifdef FEAT_VIRTUALEDIT
18922 /* Get the virtual offset. Defaults to zero. */
18923 pos.coladd = list_find_nr(l, 2L, &error);
18924 if (error)
18925 pos.coladd = 0;
18926#endif
18927
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018928 return &pos;
18929 }
18930
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018931 name = get_tv_string_chk(varp);
18932 if (name == NULL)
18933 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018934 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018936#ifdef FEAT_VISUAL
18937 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18938 {
18939 if (VIsual_active)
18940 return &VIsual;
18941 return &curwin->w_cursor;
18942 }
18943#endif
18944 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018946 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18948 return NULL;
18949 return pp;
18950 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018951
18952#ifdef FEAT_VIRTUALEDIT
18953 pos.coladd = 0;
18954#endif
18955
Bram Moolenaar477933c2007-07-17 14:32:23 +000018956 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018957 {
18958 pos.col = 0;
18959 if (name[1] == '0') /* "w0": first visible line */
18960 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018961 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018962 pos.lnum = curwin->w_topline;
18963 return &pos;
18964 }
18965 else if (name[1] == '$') /* "w$": last visible line */
18966 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018967 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018968 pos.lnum = curwin->w_botline - 1;
18969 return &pos;
18970 }
18971 }
18972 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018974 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 {
18976 pos.lnum = curbuf->b_ml.ml_line_count;
18977 pos.col = 0;
18978 }
18979 else
18980 {
18981 pos.lnum = curwin->w_cursor.lnum;
18982 pos.col = (colnr_T)STRLEN(ml_get_curline());
18983 }
18984 return &pos;
18985 }
18986 return NULL;
18987}
18988
18989/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018990 * Convert list in "arg" into a position and optional file number.
18991 * When "fnump" is NULL there is no file number, only 3 items.
18992 * Note that the column is passed on as-is, the caller may want to decrement
18993 * it to use 1 for the first column.
18994 * Return FAIL when conversion is not possible, doesn't check the position for
18995 * validity.
18996 */
18997 static int
18998list2fpos(arg, posp, fnump)
18999 typval_T *arg;
19000 pos_T *posp;
19001 int *fnump;
19002{
19003 list_T *l = arg->vval.v_list;
19004 long i = 0;
19005 long n;
19006
Bram Moolenaarbde35262006-07-23 20:12:24 +000019007 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19008 * when "fnump" isn't NULL and "coladd" is optional. */
19009 if (arg->v_type != VAR_LIST
19010 || l == NULL
19011 || l->lv_len < (fnump == NULL ? 2 : 3)
19012 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019013 return FAIL;
19014
19015 if (fnump != NULL)
19016 {
19017 n = list_find_nr(l, i++, NULL); /* fnum */
19018 if (n < 0)
19019 return FAIL;
19020 if (n == 0)
19021 n = curbuf->b_fnum; /* current buffer */
19022 *fnump = n;
19023 }
19024
19025 n = list_find_nr(l, i++, NULL); /* lnum */
19026 if (n < 0)
19027 return FAIL;
19028 posp->lnum = n;
19029
19030 n = list_find_nr(l, i++, NULL); /* col */
19031 if (n < 0)
19032 return FAIL;
19033 posp->col = n;
19034
19035#ifdef FEAT_VIRTUALEDIT
19036 n = list_find_nr(l, i, NULL);
19037 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019038 posp->coladd = 0;
19039 else
19040 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019041#endif
19042
19043 return OK;
19044}
19045
19046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 * Get the length of an environment variable name.
19048 * Advance "arg" to the first character after the name.
19049 * Return 0 for error.
19050 */
19051 static int
19052get_env_len(arg)
19053 char_u **arg;
19054{
19055 char_u *p;
19056 int len;
19057
19058 for (p = *arg; vim_isIDc(*p); ++p)
19059 ;
19060 if (p == *arg) /* no name found */
19061 return 0;
19062
19063 len = (int)(p - *arg);
19064 *arg = p;
19065 return len;
19066}
19067
19068/*
19069 * Get the length of the name of a function or internal variable.
19070 * "arg" is advanced to the first non-white character after the name.
19071 * Return 0 if something is wrong.
19072 */
19073 static int
19074get_id_len(arg)
19075 char_u **arg;
19076{
19077 char_u *p;
19078 int len;
19079
19080 /* Find the end of the name. */
19081 for (p = *arg; eval_isnamec(*p); ++p)
19082 ;
19083 if (p == *arg) /* no name found */
19084 return 0;
19085
19086 len = (int)(p - *arg);
19087 *arg = skipwhite(p);
19088
19089 return len;
19090}
19091
19092/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019093 * Get the length of the name of a variable or function.
19094 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019096 * Return -1 if curly braces expansion failed.
19097 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 * If the name contains 'magic' {}'s, expand them and return the
19099 * expanded name in an allocated string via 'alias' - caller must free.
19100 */
19101 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019102get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 char_u **arg;
19104 char_u **alias;
19105 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019106 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107{
19108 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109 char_u *p;
19110 char_u *expr_start;
19111 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112
19113 *alias = NULL; /* default to no alias */
19114
19115 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19116 && (*arg)[2] == (int)KE_SNR)
19117 {
19118 /* hard coded <SNR>, already translated */
19119 *arg += 3;
19120 return get_id_len(arg) + 3;
19121 }
19122 len = eval_fname_script(*arg);
19123 if (len > 0)
19124 {
19125 /* literal "<SID>", "s:" or "<SNR>" */
19126 *arg += len;
19127 }
19128
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019130 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019132 p = find_name_end(*arg, &expr_start, &expr_end,
19133 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 if (expr_start != NULL)
19135 {
19136 char_u *temp_string;
19137
19138 if (!evaluate)
19139 {
19140 len += (int)(p - *arg);
19141 *arg = skipwhite(p);
19142 return len;
19143 }
19144
19145 /*
19146 * Include any <SID> etc in the expanded string:
19147 * Thus the -len here.
19148 */
19149 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19150 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019151 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152 *alias = temp_string;
19153 *arg = skipwhite(p);
19154 return (int)STRLEN(temp_string);
19155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156
19157 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019158 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 EMSG2(_(e_invexpr2), *arg);
19160
19161 return len;
19162}
19163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019164/*
19165 * Find the end of a variable or function name, taking care of magic braces.
19166 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19167 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019168 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019169 * Return a pointer to just after the name. Equal to "arg" if there is no
19170 * valid name.
19171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019173find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174 char_u *arg;
19175 char_u **expr_start;
19176 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019177 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019179 int mb_nest = 0;
19180 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181 char_u *p;
19182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019183 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019185 *expr_start = NULL;
19186 *expr_end = NULL;
19187 }
19188
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019189 /* Quick check for valid starting character. */
19190 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19191 return arg;
19192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019193 for (p = arg; *p != NUL
19194 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019195 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019196 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019197 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019198 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019199 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019200 if (*p == '\'')
19201 {
19202 /* skip over 'string' to avoid counting [ and ] inside it. */
19203 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19204 ;
19205 if (*p == NUL)
19206 break;
19207 }
19208 else if (*p == '"')
19209 {
19210 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19211 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19212 if (*p == '\\' && p[1] != NUL)
19213 ++p;
19214 if (*p == NUL)
19215 break;
19216 }
19217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019218 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019220 if (*p == '[')
19221 ++br_nest;
19222 else if (*p == ']')
19223 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019226 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019228 if (*p == '{')
19229 {
19230 mb_nest++;
19231 if (expr_start != NULL && *expr_start == NULL)
19232 *expr_start = p;
19233 }
19234 else if (*p == '}')
19235 {
19236 mb_nest--;
19237 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19238 *expr_end = p;
19239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241 }
19242
19243 return p;
19244}
19245
19246/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019247 * Expands out the 'magic' {}'s in a variable/function name.
19248 * Note that this can call itself recursively, to deal with
19249 * constructs like foo{bar}{baz}{bam}
19250 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19251 * "in_start" ^
19252 * "expr_start" ^
19253 * "expr_end" ^
19254 * "in_end" ^
19255 *
19256 * Returns a new allocated string, which the caller must free.
19257 * Returns NULL for failure.
19258 */
19259 static char_u *
19260make_expanded_name(in_start, expr_start, expr_end, in_end)
19261 char_u *in_start;
19262 char_u *expr_start;
19263 char_u *expr_end;
19264 char_u *in_end;
19265{
19266 char_u c1;
19267 char_u *retval = NULL;
19268 char_u *temp_result;
19269 char_u *nextcmd = NULL;
19270
19271 if (expr_end == NULL || in_end == NULL)
19272 return NULL;
19273 *expr_start = NUL;
19274 *expr_end = NUL;
19275 c1 = *in_end;
19276 *in_end = NUL;
19277
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019278 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019279 if (temp_result != NULL && nextcmd == NULL)
19280 {
19281 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19282 + (in_end - expr_end) + 1));
19283 if (retval != NULL)
19284 {
19285 STRCPY(retval, in_start);
19286 STRCAT(retval, temp_result);
19287 STRCAT(retval, expr_end + 1);
19288 }
19289 }
19290 vim_free(temp_result);
19291
19292 *in_end = c1; /* put char back for error messages */
19293 *expr_start = '{';
19294 *expr_end = '}';
19295
19296 if (retval != NULL)
19297 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019298 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019299 if (expr_start != NULL)
19300 {
19301 /* Further expansion! */
19302 temp_result = make_expanded_name(retval, expr_start,
19303 expr_end, temp_result);
19304 vim_free(retval);
19305 retval = temp_result;
19306 }
19307 }
19308
19309 return retval;
19310}
19311
19312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019314 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315 */
19316 static int
19317eval_isnamec(c)
19318 int c;
19319{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019320 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19321}
19322
19323/*
19324 * Return TRUE if character "c" can be used as the first character in a
19325 * variable or function name (excluding '{' and '}').
19326 */
19327 static int
19328eval_isnamec1(c)
19329 int c;
19330{
19331 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332}
19333
19334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 * Set number v: variable to "val".
19336 */
19337 void
19338set_vim_var_nr(idx, val)
19339 int idx;
19340 long val;
19341{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019342 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343}
19344
19345/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019346 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 */
19348 long
19349get_vim_var_nr(idx)
19350 int idx;
19351{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019352 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353}
19354
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019355/*
19356 * Get string v: variable value. Uses a static buffer, can only be used once.
19357 */
19358 char_u *
19359get_vim_var_str(idx)
19360 int idx;
19361{
19362 return get_tv_string(&vimvars[idx].vv_tv);
19363}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019364
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019366 * Get List v: variable value. Caller must take care of reference count when
19367 * needed.
19368 */
19369 list_T *
19370get_vim_var_list(idx)
19371 int idx;
19372{
19373 return vimvars[idx].vv_list;
19374}
19375
19376/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019377 * Set v:char to character "c".
19378 */
19379 void
19380set_vim_var_char(c)
19381 int c;
19382{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019383 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019384
19385#ifdef FEAT_MBYTE
19386 if (has_mbyte)
19387 buf[(*mb_char2bytes)(c, buf)] = NUL;
19388 else
19389#endif
19390 {
19391 buf[0] = c;
19392 buf[1] = NUL;
19393 }
19394 set_vim_var_string(VV_CHAR, buf, -1);
19395}
19396
19397/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019398 * Set v:count to "count" and v:count1 to "count1".
19399 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 */
19401 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019402set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403 long count;
19404 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019405 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019407 if (set_prevcount)
19408 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019409 vimvars[VV_COUNT].vv_nr = count;
19410 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411}
19412
19413/*
19414 * Set string v: variable to a copy of "val".
19415 */
19416 void
19417set_vim_var_string(idx, val, len)
19418 int idx;
19419 char_u *val;
19420 int len; /* length of "val" to use or -1 (whole string) */
19421{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019422 /* Need to do this (at least) once, since we can't initialize a union.
19423 * Will always be invoked when "v:progname" is set. */
19424 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19425
Bram Moolenaare9a41262005-01-15 22:18:47 +000019426 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019428 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019430 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019432 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433}
19434
19435/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019436 * Set List v: variable to "val".
19437 */
19438 void
19439set_vim_var_list(idx, val)
19440 int idx;
19441 list_T *val;
19442{
19443 list_unref(vimvars[idx].vv_list);
19444 vimvars[idx].vv_list = val;
19445 if (val != NULL)
19446 ++val->lv_refcount;
19447}
19448
19449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450 * Set v:register if needed.
19451 */
19452 void
19453set_reg_var(c)
19454 int c;
19455{
19456 char_u regname;
19457
19458 if (c == 0 || c == ' ')
19459 regname = '"';
19460 else
19461 regname = c;
19462 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019463 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464 set_vim_var_string(VV_REG, &regname, 1);
19465}
19466
19467/*
19468 * Get or set v:exception. If "oldval" == NULL, return the current value.
19469 * Otherwise, restore the value to "oldval" and return NULL.
19470 * Must always be called in pairs to save and restore v:exception! Does not
19471 * take care of memory allocations.
19472 */
19473 char_u *
19474v_exception(oldval)
19475 char_u *oldval;
19476{
19477 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019478 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479
Bram Moolenaare9a41262005-01-15 22:18:47 +000019480 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 return NULL;
19482}
19483
19484/*
19485 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19486 * Otherwise, restore the value to "oldval" and return NULL.
19487 * Must always be called in pairs to save and restore v:throwpoint! Does not
19488 * take care of memory allocations.
19489 */
19490 char_u *
19491v_throwpoint(oldval)
19492 char_u *oldval;
19493{
19494 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019495 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496
Bram Moolenaare9a41262005-01-15 22:18:47 +000019497 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498 return NULL;
19499}
19500
19501#if defined(FEAT_AUTOCMD) || defined(PROTO)
19502/*
19503 * Set v:cmdarg.
19504 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19505 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19506 * Must always be called in pairs!
19507 */
19508 char_u *
19509set_cmdarg(eap, oldarg)
19510 exarg_T *eap;
19511 char_u *oldarg;
19512{
19513 char_u *oldval;
19514 char_u *newval;
19515 unsigned len;
19516
Bram Moolenaare9a41262005-01-15 22:18:47 +000019517 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019518 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019520 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019521 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019522 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 }
19524
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019525 if (eap->force_bin == FORCE_BIN)
19526 len = 6;
19527 else if (eap->force_bin == FORCE_NOBIN)
19528 len = 8;
19529 else
19530 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019531
19532 if (eap->read_edit)
19533 len += 7;
19534
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019535 if (eap->force_ff != 0)
19536 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19537# ifdef FEAT_MBYTE
19538 if (eap->force_enc != 0)
19539 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019540 if (eap->bad_char != 0)
19541 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019542# endif
19543
19544 newval = alloc(len + 1);
19545 if (newval == NULL)
19546 return NULL;
19547
19548 if (eap->force_bin == FORCE_BIN)
19549 sprintf((char *)newval, " ++bin");
19550 else if (eap->force_bin == FORCE_NOBIN)
19551 sprintf((char *)newval, " ++nobin");
19552 else
19553 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019554
19555 if (eap->read_edit)
19556 STRCAT(newval, " ++edit");
19557
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019558 if (eap->force_ff != 0)
19559 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19560 eap->cmd + eap->force_ff);
19561# ifdef FEAT_MBYTE
19562 if (eap->force_enc != 0)
19563 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19564 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019565 if (eap->bad_char == BAD_KEEP)
19566 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19567 else if (eap->bad_char == BAD_DROP)
19568 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19569 else if (eap->bad_char != 0)
19570 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019571# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019572 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019573 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574}
19575#endif
19576
19577/*
19578 * Get the value of internal variable "name".
19579 * Return OK or FAIL.
19580 */
19581 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019582get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583 char_u *name;
19584 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019585 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019586 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587{
19588 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019589 typval_T *tv = NULL;
19590 typval_T atv;
19591 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593
19594 /* truncate the name, so that we can use strcmp() */
19595 cc = name[len];
19596 name[len] = NUL;
19597
19598 /*
19599 * Check for "b:changedtick".
19600 */
19601 if (STRCMP(name, "b:changedtick") == 0)
19602 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019603 atv.v_type = VAR_NUMBER;
19604 atv.vval.v_number = curbuf->b_changedtick;
19605 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 }
19607
19608 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 * Check for user-defined variables.
19610 */
19611 else
19612 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019613 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019615 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 }
19617
Bram Moolenaare9a41262005-01-15 22:18:47 +000019618 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019620 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019621 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 ret = FAIL;
19623 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019624 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019625 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626
19627 name[len] = cc;
19628
19629 return ret;
19630}
19631
19632/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019633 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19634 * Also handle function call with Funcref variable: func(expr)
19635 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19636 */
19637 static int
19638handle_subscript(arg, rettv, evaluate, verbose)
19639 char_u **arg;
19640 typval_T *rettv;
19641 int evaluate; /* do more than finding the end */
19642 int verbose; /* give error messages */
19643{
19644 int ret = OK;
19645 dict_T *selfdict = NULL;
19646 char_u *s;
19647 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019648 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019649
19650 while (ret == OK
19651 && (**arg == '['
19652 || (**arg == '.' && rettv->v_type == VAR_DICT)
19653 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19654 && !vim_iswhite(*(*arg - 1)))
19655 {
19656 if (**arg == '(')
19657 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019658 /* need to copy the funcref so that we can clear rettv */
19659 functv = *rettv;
19660 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019661
19662 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019663 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019664 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019665 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19666 &len, evaluate, selfdict);
19667
19668 /* Clear the funcref afterwards, so that deleting it while
19669 * evaluating the arguments is possible (see test55). */
19670 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019671
19672 /* Stop the expression evaluation when immediately aborting on
19673 * error, or when an interrupt occurred or an exception was thrown
19674 * but not caught. */
19675 if (aborting())
19676 {
19677 if (ret == OK)
19678 clear_tv(rettv);
19679 ret = FAIL;
19680 }
19681 dict_unref(selfdict);
19682 selfdict = NULL;
19683 }
19684 else /* **arg == '[' || **arg == '.' */
19685 {
19686 dict_unref(selfdict);
19687 if (rettv->v_type == VAR_DICT)
19688 {
19689 selfdict = rettv->vval.v_dict;
19690 if (selfdict != NULL)
19691 ++selfdict->dv_refcount;
19692 }
19693 else
19694 selfdict = NULL;
19695 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19696 {
19697 clear_tv(rettv);
19698 ret = FAIL;
19699 }
19700 }
19701 }
19702 dict_unref(selfdict);
19703 return ret;
19704}
19705
19706/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019707 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019708 * value).
19709 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019710 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019711alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019712{
Bram Moolenaar33570922005-01-25 22:26:29 +000019713 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019714}
19715
19716/*
19717 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 * The string "s" must have been allocated, it is consumed.
19719 * Return NULL for out of memory, the variable otherwise.
19720 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019721 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019722alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723 char_u *s;
19724{
Bram Moolenaar33570922005-01-25 22:26:29 +000019725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019727 rettv = alloc_tv();
19728 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019730 rettv->v_type = VAR_STRING;
19731 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 }
19733 else
19734 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019735 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736}
19737
19738/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019739 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019741 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019742free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019743 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744{
19745 if (varp != NULL)
19746 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019747 switch (varp->v_type)
19748 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019749 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019750 func_unref(varp->vval.v_string);
19751 /*FALLTHROUGH*/
19752 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019753 vim_free(varp->vval.v_string);
19754 break;
19755 case VAR_LIST:
19756 list_unref(varp->vval.v_list);
19757 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019758 case VAR_DICT:
19759 dict_unref(varp->vval.v_dict);
19760 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019761 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019762#ifdef FEAT_FLOAT
19763 case VAR_FLOAT:
19764#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019765 case VAR_UNKNOWN:
19766 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019767 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019768 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019769 break;
19770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 vim_free(varp);
19772 }
19773}
19774
19775/*
19776 * Free the memory for a variable value and set the value to NULL or 0.
19777 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019778 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019779clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019780 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781{
19782 if (varp != NULL)
19783 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019784 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019786 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019787 func_unref(varp->vval.v_string);
19788 /*FALLTHROUGH*/
19789 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019790 vim_free(varp->vval.v_string);
19791 varp->vval.v_string = NULL;
19792 break;
19793 case VAR_LIST:
19794 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019795 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019796 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019797 case VAR_DICT:
19798 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019799 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019800 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019801 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019802 varp->vval.v_number = 0;
19803 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019804#ifdef FEAT_FLOAT
19805 case VAR_FLOAT:
19806 varp->vval.v_float = 0.0;
19807 break;
19808#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019809 case VAR_UNKNOWN:
19810 break;
19811 default:
19812 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019814 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 }
19816}
19817
19818/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019819 * Set the value of a variable to NULL without freeing items.
19820 */
19821 static void
19822init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019823 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019824{
19825 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019826 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019827}
19828
19829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830 * Get the number value of a variable.
19831 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019832 * For incompatible types, return 0.
19833 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19834 * caller of incompatible types: it sets *denote to TRUE if "denote"
19835 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 */
19837 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019839 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019841 int error = FALSE;
19842
19843 return get_tv_number_chk(varp, &error); /* return 0L on error */
19844}
19845
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019846 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019847get_tv_number_chk(varp, denote)
19848 typval_T *varp;
19849 int *denote;
19850{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019851 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019853 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019855 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019856 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019857#ifdef FEAT_FLOAT
19858 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019859 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019860 break;
19861#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019862 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019863 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019864 break;
19865 case VAR_STRING:
19866 if (varp->vval.v_string != NULL)
19867 vim_str2nr(varp->vval.v_string, NULL, NULL,
19868 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019869 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019870 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019871 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019872 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019873 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019874 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019875 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019876 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019877 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019878 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019880 if (denote == NULL) /* useful for values that must be unsigned */
19881 n = -1;
19882 else
19883 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019884 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885}
19886
19887/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019888 * Get the lnum from the first argument.
19889 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019890 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891 */
19892 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019893get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019894 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895{
Bram Moolenaar33570922005-01-25 22:26:29 +000019896 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 linenr_T lnum;
19898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019899 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019900 if (lnum == 0) /* no valid number, try using line() */
19901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019902 rettv.v_type = VAR_NUMBER;
19903 f_line(argvars, &rettv);
19904 lnum = rettv.vval.v_number;
19905 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019906 }
19907 return lnum;
19908}
19909
19910/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019911 * Get the lnum from the first argument.
19912 * Also accepts "$", then "buf" is used.
19913 * Returns 0 on error.
19914 */
19915 static linenr_T
19916get_tv_lnum_buf(argvars, buf)
19917 typval_T *argvars;
19918 buf_T *buf;
19919{
19920 if (argvars[0].v_type == VAR_STRING
19921 && argvars[0].vval.v_string != NULL
19922 && argvars[0].vval.v_string[0] == '$'
19923 && buf != NULL)
19924 return buf->b_ml.ml_line_count;
19925 return get_tv_number_chk(&argvars[0], NULL);
19926}
19927
19928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 * Get the string value of a variable.
19930 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019931 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19932 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 * If the String variable has never been set, return an empty string.
19934 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019935 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19936 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 */
19938 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019939get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019940 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941{
19942 static char_u mybuf[NUMBUFLEN];
19943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019944 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945}
19946
19947 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019948get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019949 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019950 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019952 char_u *res = get_tv_string_buf_chk(varp, buf);
19953
19954 return res != NULL ? res : (char_u *)"";
19955}
19956
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019957 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019958get_tv_string_chk(varp)
19959 typval_T *varp;
19960{
19961 static char_u mybuf[NUMBUFLEN];
19962
19963 return get_tv_string_buf_chk(varp, mybuf);
19964}
19965
19966 static char_u *
19967get_tv_string_buf_chk(varp, buf)
19968 typval_T *varp;
19969 char_u *buf;
19970{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019971 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019973 case VAR_NUMBER:
19974 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19975 return buf;
19976 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019977 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019978 break;
19979 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019980 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019981 break;
19982 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019983 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019984 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019985#ifdef FEAT_FLOAT
19986 case VAR_FLOAT:
19987 EMSG(_("E806: using Float as a String"));
19988 break;
19989#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019990 case VAR_STRING:
19991 if (varp->vval.v_string != NULL)
19992 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019993 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019994 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019995 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019996 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019998 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999}
20000
20001/*
20002 * Find variable "name" in the list of variables.
20003 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020004 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020005 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020006 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020008 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020009find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020011 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020014 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015
Bram Moolenaara7043832005-01-21 11:56:39 +000020016 ht = find_var_ht(name, &varname);
20017 if (htp != NULL)
20018 *htp = ht;
20019 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020021 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022}
20023
20024/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020025 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000020026 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020028 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020029find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020030 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000020031 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020032 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020033{
Bram Moolenaar33570922005-01-25 22:26:29 +000020034 hashitem_T *hi;
20035
20036 if (*varname == NUL)
20037 {
20038 /* Must be something like "s:", otherwise "ht" would be NULL. */
20039 switch (varname[-2])
20040 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020041 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020042 case 'g': return &globvars_var;
20043 case 'v': return &vimvars_var;
20044 case 'b': return &curbuf->b_bufvar;
20045 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020046#ifdef FEAT_WINDOWS
20047 case 't': return &curtab->tp_winvar;
20048#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020049 case 'l': return current_funccal == NULL
20050 ? NULL : &current_funccal->l_vars_var;
20051 case 'a': return current_funccal == NULL
20052 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020053 }
20054 return NULL;
20055 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020056
20057 hi = hash_find(ht, varname);
20058 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020059 {
20060 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020061 * worked find the variable again. Don't auto-load a script if it was
20062 * loaded already, otherwise it would be loaded every time when
20063 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020064 if (ht == &globvarht && !writing)
20065 {
20066 /* Note: script_autoload() may make "hi" invalid. It must either
20067 * be obtained again or not used. */
20068 if (!script_autoload(varname, FALSE) || aborting())
20069 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020070 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020071 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020072 if (HASHITEM_EMPTY(hi))
20073 return NULL;
20074 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020075 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020076}
20077
20078/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020079 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020080 * Set "varname" to the start of name without ':'.
20081 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020082 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020083find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 char_u *name;
20085 char_u **varname;
20086{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020087 hashitem_T *hi;
20088
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 if (name[1] != ':')
20090 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020091 /* The name must not start with a colon or #. */
20092 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 return NULL;
20094 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020095
20096 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020097 hi = hash_find(&compat_hashtab, name);
20098 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020099 return &compat_hashtab;
20100
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020102 return &globvarht; /* global variable */
20103 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 }
20105 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020106 if (*name == 'g') /* global variable */
20107 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020108 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20109 */
20110 if (vim_strchr(name + 2, ':') != NULL
20111 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020112 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020114 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020116 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020117#ifdef FEAT_WINDOWS
20118 if (*name == 't') /* tab page variable */
20119 return &curtab->tp_vars.dv_hashtab;
20120#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020121 if (*name == 'v') /* v: variable */
20122 return &vimvarht;
20123 if (*name == 'a' && current_funccal != NULL) /* function argument */
20124 return &current_funccal->l_avars.dv_hashtab;
20125 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20126 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 if (*name == 's' /* script variable */
20128 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20129 return &SCRIPT_VARS(current_SID);
20130 return NULL;
20131}
20132
20133/*
20134 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020135 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 * Returns NULL when it doesn't exist.
20137 */
20138 char_u *
20139get_var_value(name)
20140 char_u *name;
20141{
Bram Moolenaar33570922005-01-25 22:26:29 +000020142 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143
Bram Moolenaara7043832005-01-21 11:56:39 +000020144 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145 if (v == NULL)
20146 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020147 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148}
20149
20150/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020151 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 * sourcing this script and when executing functions defined in the script.
20153 */
20154 void
20155new_script_vars(id)
20156 scid_T id;
20157{
Bram Moolenaara7043832005-01-21 11:56:39 +000020158 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020159 hashtab_T *ht;
20160 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020161
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20163 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020164 /* Re-allocating ga_data means that an ht_array pointing to
20165 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020166 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020167 for (i = 1; i <= ga_scripts.ga_len; ++i)
20168 {
20169 ht = &SCRIPT_VARS(i);
20170 if (ht->ht_mask == HT_INIT_SIZE - 1)
20171 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020172 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020173 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020174 }
20175
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 while (ga_scripts.ga_len < id)
20177 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020178 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020179 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020180 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 }
20183 }
20184}
20185
20186/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020187 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20188 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 */
20190 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020191init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020192 dict_T *dict;
20193 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020194 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195{
Bram Moolenaar33570922005-01-25 22:26:29 +000020196 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020197 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020198 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020199 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020200 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020201 dict_var->di_tv.vval.v_dict = dict;
20202 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020203 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020204 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20205 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206}
20207
20208/*
20209 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020210 * Frees all allocated variables and the value they contain.
20211 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 */
20213 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020214vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020215 hashtab_T *ht;
20216{
20217 vars_clear_ext(ht, TRUE);
20218}
20219
20220/*
20221 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20222 */
20223 static void
20224vars_clear_ext(ht, free_val)
20225 hashtab_T *ht;
20226 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227{
Bram Moolenaara7043832005-01-21 11:56:39 +000020228 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020229 hashitem_T *hi;
20230 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231
Bram Moolenaar33570922005-01-25 22:26:29 +000020232 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020233 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020234 for (hi = ht->ht_array; todo > 0; ++hi)
20235 {
20236 if (!HASHITEM_EMPTY(hi))
20237 {
20238 --todo;
20239
Bram Moolenaar33570922005-01-25 22:26:29 +000020240 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020241 * ht_array might change then. hash_clear() takes care of it
20242 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020243 v = HI2DI(hi);
20244 if (free_val)
20245 clear_tv(&v->di_tv);
20246 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20247 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020248 }
20249 }
20250 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020251 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252}
20253
Bram Moolenaara7043832005-01-21 11:56:39 +000020254/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020255 * Delete a variable from hashtab "ht" at item "hi".
20256 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020259delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020260 hashtab_T *ht;
20261 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262{
Bram Moolenaar33570922005-01-25 22:26:29 +000020263 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020264
20265 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020266 clear_tv(&di->di_tv);
20267 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268}
20269
20270/*
20271 * List the value of one internal variable.
20272 */
20273 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020274list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020275 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020277 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020279 char_u *tofree;
20280 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020281 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020282
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020283 current_copyID += COPYID_INC;
20284 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020285 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020286 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020287 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288}
20289
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020291list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 char_u *prefix;
20293 char_u *name;
20294 int type;
20295 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020296 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297{
Bram Moolenaar31859182007-08-14 20:41:13 +000020298 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20299 msg_start();
20300 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301 if (name != NULL) /* "a:" vars don't have a name stored */
20302 msg_puts(name);
20303 msg_putchar(' ');
20304 msg_advance(22);
20305 if (type == VAR_NUMBER)
20306 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020307 else if (type == VAR_FUNC)
20308 msg_putchar('*');
20309 else if (type == VAR_LIST)
20310 {
20311 msg_putchar('[');
20312 if (*string == '[')
20313 ++string;
20314 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020315 else if (type == VAR_DICT)
20316 {
20317 msg_putchar('{');
20318 if (*string == '{')
20319 ++string;
20320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 else
20322 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020323
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020325
20326 if (type == VAR_FUNC)
20327 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020328 if (*first)
20329 {
20330 msg_clr_eos();
20331 *first = FALSE;
20332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333}
20334
20335/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020336 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337 * If the variable already exists, the value is updated.
20338 * Otherwise the variable is created.
20339 */
20340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020341set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020343 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020344 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345{
Bram Moolenaar33570922005-01-25 22:26:29 +000020346 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020348 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020350 ht = find_var_ht(name, &varname);
20351 if (ht == NULL || *varname == NUL)
20352 {
20353 EMSG2(_(e_illvar), name);
20354 return;
20355 }
20356 v = find_var_in_ht(ht, varname, TRUE);
20357
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020358 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20359 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020360
Bram Moolenaar33570922005-01-25 22:26:29 +000020361 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020363 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020364 if (var_check_ro(v->di_flags, name)
20365 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020366 return;
20367 if (v->di_tv.v_type != tv->v_type
20368 && !((v->di_tv.v_type == VAR_STRING
20369 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020370 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020371 || tv->v_type == VAR_NUMBER))
20372#ifdef FEAT_FLOAT
20373 && !((v->di_tv.v_type == VAR_NUMBER
20374 || v->di_tv.v_type == VAR_FLOAT)
20375 && (tv->v_type == VAR_NUMBER
20376 || tv->v_type == VAR_FLOAT))
20377#endif
20378 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020379 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020380 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020381 return;
20382 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020383
20384 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020385 * Handle setting internal v: variables separately: we don't change
20386 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020387 */
20388 if (ht == &vimvarht)
20389 {
20390 if (v->di_tv.v_type == VAR_STRING)
20391 {
20392 vim_free(v->di_tv.vval.v_string);
20393 if (copy || tv->v_type != VAR_STRING)
20394 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20395 else
20396 {
20397 /* Take over the string to avoid an extra alloc/free. */
20398 v->di_tv.vval.v_string = tv->vval.v_string;
20399 tv->vval.v_string = NULL;
20400 }
20401 }
20402 else if (v->di_tv.v_type != VAR_NUMBER)
20403 EMSG2(_(e_intern2), "set_var()");
20404 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020405 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020406 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020407 if (STRCMP(varname, "searchforward") == 0)
20408 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20409 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020410 return;
20411 }
20412
20413 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414 }
20415 else /* add a new variable */
20416 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020417 /* Can't add "v:" variable. */
20418 if (ht == &vimvarht)
20419 {
20420 EMSG2(_(e_illvar), name);
20421 return;
20422 }
20423
Bram Moolenaar92124a32005-06-17 22:03:40 +000020424 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020425 if (!valid_varname(varname))
20426 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020427
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020428 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20429 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020430 if (v == NULL)
20431 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020432 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020433 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020435 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436 return;
20437 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020438 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020440
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020441 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020442 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020443 else
20444 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020445 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020446 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020447 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449}
20450
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020451/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020452 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020453 * Also give an error message.
20454 */
20455 static int
20456var_check_ro(flags, name)
20457 int flags;
20458 char_u *name;
20459{
20460 if (flags & DI_FLAGS_RO)
20461 {
20462 EMSG2(_(e_readonlyvar), name);
20463 return TRUE;
20464 }
20465 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20466 {
20467 EMSG2(_(e_readonlysbx), name);
20468 return TRUE;
20469 }
20470 return FALSE;
20471}
20472
20473/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020474 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20475 * Also give an error message.
20476 */
20477 static int
20478var_check_fixed(flags, name)
20479 int flags;
20480 char_u *name;
20481{
20482 if (flags & DI_FLAGS_FIX)
20483 {
20484 EMSG2(_("E795: Cannot delete variable %s"), name);
20485 return TRUE;
20486 }
20487 return FALSE;
20488}
20489
20490/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020491 * Check if a funcref is assigned to a valid variable name.
20492 * Return TRUE and give an error if not.
20493 */
20494 static int
20495var_check_func_name(name, new_var)
20496 char_u *name; /* points to start of variable name */
20497 int new_var; /* TRUE when creating the variable */
20498{
20499 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20500 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20501 ? name[2] : name[0]))
20502 {
20503 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20504 name);
20505 return TRUE;
20506 }
20507 /* Don't allow hiding a function. When "v" is not NULL we might be
20508 * assigning another function to the same var, the type is checked
20509 * below. */
20510 if (new_var && function_exists(name))
20511 {
20512 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20513 name);
20514 return TRUE;
20515 }
20516 return FALSE;
20517}
20518
20519/*
20520 * Check if a variable name is valid.
20521 * Return FALSE and give an error if not.
20522 */
20523 static int
20524valid_varname(varname)
20525 char_u *varname;
20526{
20527 char_u *p;
20528
20529 for (p = varname; *p != NUL; ++p)
20530 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20531 && *p != AUTOLOAD_CHAR)
20532 {
20533 EMSG2(_(e_illvar), varname);
20534 return FALSE;
20535 }
20536 return TRUE;
20537}
20538
20539/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020540 * Return TRUE if typeval "tv" is set to be locked (immutable).
20541 * Also give an error message, using "name".
20542 */
20543 static int
20544tv_check_lock(lock, name)
20545 int lock;
20546 char_u *name;
20547{
20548 if (lock & VAR_LOCKED)
20549 {
20550 EMSG2(_("E741: Value is locked: %s"),
20551 name == NULL ? (char_u *)_("Unknown") : name);
20552 return TRUE;
20553 }
20554 if (lock & VAR_FIXED)
20555 {
20556 EMSG2(_("E742: Cannot change value of %s"),
20557 name == NULL ? (char_u *)_("Unknown") : name);
20558 return TRUE;
20559 }
20560 return FALSE;
20561}
20562
20563/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020564 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020565 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020566 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020567 * It is OK for "from" and "to" to point to the same item. This is used to
20568 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020569 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020570 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020571copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020572 typval_T *from;
20573 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020575 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020576 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020577 switch (from->v_type)
20578 {
20579 case VAR_NUMBER:
20580 to->vval.v_number = from->vval.v_number;
20581 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020582#ifdef FEAT_FLOAT
20583 case VAR_FLOAT:
20584 to->vval.v_float = from->vval.v_float;
20585 break;
20586#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020587 case VAR_STRING:
20588 case VAR_FUNC:
20589 if (from->vval.v_string == NULL)
20590 to->vval.v_string = NULL;
20591 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020593 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020594 if (from->v_type == VAR_FUNC)
20595 func_ref(to->vval.v_string);
20596 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020597 break;
20598 case VAR_LIST:
20599 if (from->vval.v_list == NULL)
20600 to->vval.v_list = NULL;
20601 else
20602 {
20603 to->vval.v_list = from->vval.v_list;
20604 ++to->vval.v_list->lv_refcount;
20605 }
20606 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020607 case VAR_DICT:
20608 if (from->vval.v_dict == NULL)
20609 to->vval.v_dict = NULL;
20610 else
20611 {
20612 to->vval.v_dict = from->vval.v_dict;
20613 ++to->vval.v_dict->dv_refcount;
20614 }
20615 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020616 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020617 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020618 break;
20619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620}
20621
20622/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020623 * Make a copy of an item.
20624 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020625 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20626 * reference to an already copied list/dict can be used.
20627 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020628 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020629 static int
20630item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020631 typval_T *from;
20632 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020633 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020634 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020635{
20636 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020637 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020638
Bram Moolenaar33570922005-01-25 22:26:29 +000020639 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020640 {
20641 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020642 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020643 }
20644 ++recurse;
20645
20646 switch (from->v_type)
20647 {
20648 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020649#ifdef FEAT_FLOAT
20650 case VAR_FLOAT:
20651#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020652 case VAR_STRING:
20653 case VAR_FUNC:
20654 copy_tv(from, to);
20655 break;
20656 case VAR_LIST:
20657 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020658 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020659 if (from->vval.v_list == NULL)
20660 to->vval.v_list = NULL;
20661 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20662 {
20663 /* use the copy made earlier */
20664 to->vval.v_list = from->vval.v_list->lv_copylist;
20665 ++to->vval.v_list->lv_refcount;
20666 }
20667 else
20668 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20669 if (to->vval.v_list == NULL)
20670 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020671 break;
20672 case VAR_DICT:
20673 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020674 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020675 if (from->vval.v_dict == NULL)
20676 to->vval.v_dict = NULL;
20677 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20678 {
20679 /* use the copy made earlier */
20680 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20681 ++to->vval.v_dict->dv_refcount;
20682 }
20683 else
20684 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20685 if (to->vval.v_dict == NULL)
20686 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020687 break;
20688 default:
20689 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020690 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020691 }
20692 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020693 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020694}
20695
20696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 * ":echo expr1 ..." print each argument separated with a space, add a
20698 * newline at the end.
20699 * ":echon expr1 ..." print each argument plain.
20700 */
20701 void
20702ex_echo(eap)
20703 exarg_T *eap;
20704{
20705 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020706 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020707 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 char_u *p;
20709 int needclr = TRUE;
20710 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020711 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712
20713 if (eap->skip)
20714 ++emsg_skip;
20715 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20716 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020717 /* If eval1() causes an error message the text from the command may
20718 * still need to be cleared. E.g., "echo 22,44". */
20719 need_clr_eos = needclr;
20720
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020722 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 {
20724 /*
20725 * Report the invalid expression unless the expression evaluation
20726 * has been cancelled due to an aborting error, an interrupt, or an
20727 * exception.
20728 */
20729 if (!aborting())
20730 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020731 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732 break;
20733 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020734 need_clr_eos = FALSE;
20735
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 if (!eap->skip)
20737 {
20738 if (atstart)
20739 {
20740 atstart = FALSE;
20741 /* Call msg_start() after eval1(), evaluating the expression
20742 * may cause a message to appear. */
20743 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020744 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020745 /* Mark the saved text as finishing the line, so that what
20746 * follows is displayed on a new line when scrolling back
20747 * at the more prompt. */
20748 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751 }
20752 else if (eap->cmdidx == CMD_echo)
20753 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020754 current_copyID += COPYID_INC;
20755 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020756 if (p != NULL)
20757 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020759 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020761 if (*p != TAB && needclr)
20762 {
20763 /* remove any text still there from the command */
20764 msg_clr_eos();
20765 needclr = FALSE;
20766 }
20767 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768 }
20769 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020770 {
20771#ifdef FEAT_MBYTE
20772 if (has_mbyte)
20773 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020774 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020775
20776 (void)msg_outtrans_len_attr(p, i, echo_attr);
20777 p += i - 1;
20778 }
20779 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020781 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020784 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020786 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 arg = skipwhite(arg);
20788 }
20789 eap->nextcmd = check_nextcmd(arg);
20790
20791 if (eap->skip)
20792 --emsg_skip;
20793 else
20794 {
20795 /* remove text that may still be there from the command */
20796 if (needclr)
20797 msg_clr_eos();
20798 if (eap->cmdidx == CMD_echo)
20799 msg_end();
20800 }
20801}
20802
20803/*
20804 * ":echohl {name}".
20805 */
20806 void
20807ex_echohl(eap)
20808 exarg_T *eap;
20809{
20810 int id;
20811
20812 id = syn_name2id(eap->arg);
20813 if (id == 0)
20814 echo_attr = 0;
20815 else
20816 echo_attr = syn_id2attr(id);
20817}
20818
20819/*
20820 * ":execute expr1 ..." execute the result of an expression.
20821 * ":echomsg expr1 ..." Print a message
20822 * ":echoerr expr1 ..." Print an error
20823 * Each gets spaces around each argument and a newline at the end for
20824 * echo commands
20825 */
20826 void
20827ex_execute(eap)
20828 exarg_T *eap;
20829{
20830 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020831 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 int ret = OK;
20833 char_u *p;
20834 garray_T ga;
20835 int len;
20836 int save_did_emsg;
20837
20838 ga_init2(&ga, 1, 80);
20839
20840 if (eap->skip)
20841 ++emsg_skip;
20842 while (*arg != NUL && *arg != '|' && *arg != '\n')
20843 {
20844 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020845 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 {
20847 /*
20848 * Report the invalid expression unless the expression evaluation
20849 * has been cancelled due to an aborting error, an interrupt, or an
20850 * exception.
20851 */
20852 if (!aborting())
20853 EMSG2(_(e_invexpr2), p);
20854 ret = FAIL;
20855 break;
20856 }
20857
20858 if (!eap->skip)
20859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020860 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861 len = (int)STRLEN(p);
20862 if (ga_grow(&ga, len + 2) == FAIL)
20863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020864 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865 ret = FAIL;
20866 break;
20867 }
20868 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871 ga.ga_len += len;
20872 }
20873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020874 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 arg = skipwhite(arg);
20876 }
20877
20878 if (ret != FAIL && ga.ga_data != NULL)
20879 {
20880 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020883 out_flush();
20884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 else if (eap->cmdidx == CMD_echoerr)
20886 {
20887 /* We don't want to abort following commands, restore did_emsg. */
20888 save_did_emsg = did_emsg;
20889 EMSG((char_u *)ga.ga_data);
20890 if (!force_abort)
20891 did_emsg = save_did_emsg;
20892 }
20893 else if (eap->cmdidx == CMD_execute)
20894 do_cmdline((char_u *)ga.ga_data,
20895 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20896 }
20897
20898 ga_clear(&ga);
20899
20900 if (eap->skip)
20901 --emsg_skip;
20902
20903 eap->nextcmd = check_nextcmd(arg);
20904}
20905
20906/*
20907 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20908 * "arg" points to the "&" or '+' when called, to "option" when returning.
20909 * Returns NULL when no option name found. Otherwise pointer to the char
20910 * after the option name.
20911 */
20912 static char_u *
20913find_option_end(arg, opt_flags)
20914 char_u **arg;
20915 int *opt_flags;
20916{
20917 char_u *p = *arg;
20918
20919 ++p;
20920 if (*p == 'g' && p[1] == ':')
20921 {
20922 *opt_flags = OPT_GLOBAL;
20923 p += 2;
20924 }
20925 else if (*p == 'l' && p[1] == ':')
20926 {
20927 *opt_flags = OPT_LOCAL;
20928 p += 2;
20929 }
20930 else
20931 *opt_flags = 0;
20932
20933 if (!ASCII_ISALPHA(*p))
20934 return NULL;
20935 *arg = p;
20936
20937 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20938 p += 4; /* termcap option */
20939 else
20940 while (ASCII_ISALPHA(*p))
20941 ++p;
20942 return p;
20943}
20944
20945/*
20946 * ":function"
20947 */
20948 void
20949ex_function(eap)
20950 exarg_T *eap;
20951{
20952 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020953 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 int j;
20955 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 char_u *name = NULL;
20958 char_u *p;
20959 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020960 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961 garray_T newargs;
20962 garray_T newlines;
20963 int varargs = FALSE;
20964 int mustend = FALSE;
20965 int flags = 0;
20966 ufunc_T *fp;
20967 int indent;
20968 int nesting;
20969 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020970 dictitem_T *v;
20971 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020972 static int func_nr = 0; /* number for nameless function */
20973 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020974 hashtab_T *ht;
20975 int todo;
20976 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020977 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978
20979 /*
20980 * ":function" without argument: list functions.
20981 */
20982 if (ends_excmd(*eap->arg))
20983 {
20984 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020985 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020986 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020987 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020988 {
20989 if (!HASHITEM_EMPTY(hi))
20990 {
20991 --todo;
20992 fp = HI2UF(hi);
20993 if (!isdigit(*fp->uf_name))
20994 list_func_head(fp, FALSE);
20995 }
20996 }
20997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998 eap->nextcmd = check_nextcmd(eap->arg);
20999 return;
21000 }
21001
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021002 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021003 * ":function /pat": list functions matching pattern.
21004 */
21005 if (*eap->arg == '/')
21006 {
21007 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21008 if (!eap->skip)
21009 {
21010 regmatch_T regmatch;
21011
21012 c = *p;
21013 *p = NUL;
21014 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21015 *p = c;
21016 if (regmatch.regprog != NULL)
21017 {
21018 regmatch.rm_ic = p_ic;
21019
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021020 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021021 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21022 {
21023 if (!HASHITEM_EMPTY(hi))
21024 {
21025 --todo;
21026 fp = HI2UF(hi);
21027 if (!isdigit(*fp->uf_name)
21028 && vim_regexec(&regmatch, fp->uf_name, 0))
21029 list_func_head(fp, FALSE);
21030 }
21031 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000021032 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021033 }
21034 }
21035 if (*p == '/')
21036 ++p;
21037 eap->nextcmd = check_nextcmd(p);
21038 return;
21039 }
21040
21041 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021042 * Get the function name. There are these situations:
21043 * func normal function name
21044 * "name" == func, "fudi.fd_dict" == NULL
21045 * dict.func new dictionary entry
21046 * "name" == NULL, "fudi.fd_dict" set,
21047 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21048 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021049 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021050 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21051 * dict.func existing dict entry that's not a Funcref
21052 * "name" == NULL, "fudi.fd_dict" set,
21053 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021056 name = trans_function_name(&p, eap->skip, 0, &fudi);
21057 paren = (vim_strchr(p, '(') != NULL);
21058 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 {
21060 /*
21061 * Return on an invalid expression in braces, unless the expression
21062 * evaluation has been cancelled due to an aborting error, an
21063 * interrupt, or an exception.
21064 */
21065 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021066 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021067 if (!eap->skip && fudi.fd_newkey != NULL)
21068 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021069 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072 else
21073 eap->skip = TRUE;
21074 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021075
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 /* An error in a function call during evaluation of an expression in magic
21077 * braces should not cause the function not to be defined. */
21078 saved_did_emsg = did_emsg;
21079 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080
21081 /*
21082 * ":function func" with only function name: list function.
21083 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021084 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 {
21086 if (!ends_excmd(*skipwhite(p)))
21087 {
21088 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021089 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 }
21091 eap->nextcmd = check_nextcmd(p);
21092 if (eap->nextcmd != NULL)
21093 *p = NUL;
21094 if (!eap->skip && !got_int)
21095 {
21096 fp = find_func(name);
21097 if (fp != NULL)
21098 {
21099 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021100 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021102 if (FUNCLINE(fp, j) == NULL)
21103 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 msg_putchar('\n');
21105 msg_outnum((long)(j + 1));
21106 if (j < 9)
21107 msg_putchar(' ');
21108 if (j < 99)
21109 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021110 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 out_flush(); /* show a line at a time */
21112 ui_breakcheck();
21113 }
21114 if (!got_int)
21115 {
21116 msg_putchar('\n');
21117 msg_puts((char_u *)" endfunction");
21118 }
21119 }
21120 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021121 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021123 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124 }
21125
21126 /*
21127 * ":function name(arg1, arg2)" Define function.
21128 */
21129 p = skipwhite(p);
21130 if (*p != '(')
21131 {
21132 if (!eap->skip)
21133 {
21134 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021135 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 }
21137 /* attempt to continue by skipping some text */
21138 if (vim_strchr(p, '(') != NULL)
21139 p = vim_strchr(p, '(');
21140 }
21141 p = skipwhite(p + 1);
21142
21143 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21144 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21145
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021146 if (!eap->skip)
21147 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021148 /* Check the name of the function. Unless it's a dictionary function
21149 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021150 if (name != NULL)
21151 arg = name;
21152 else
21153 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021154 if (arg != NULL && (fudi.fd_di == NULL
21155 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021156 {
21157 if (*arg == K_SPECIAL)
21158 j = 3;
21159 else
21160 j = 0;
21161 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21162 : eval_isnamec(arg[j])))
21163 ++j;
21164 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021165 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021166 }
21167 }
21168
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169 /*
21170 * Isolate the arguments: "arg1, arg2, ...)"
21171 */
21172 while (*p != ')')
21173 {
21174 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21175 {
21176 varargs = TRUE;
21177 p += 3;
21178 mustend = TRUE;
21179 }
21180 else
21181 {
21182 arg = p;
21183 while (ASCII_ISALNUM(*p) || *p == '_')
21184 ++p;
21185 if (arg == p || isdigit(*arg)
21186 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21187 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21188 {
21189 if (!eap->skip)
21190 EMSG2(_("E125: Illegal argument: %s"), arg);
21191 break;
21192 }
21193 if (ga_grow(&newargs, 1) == FAIL)
21194 goto erret;
21195 c = *p;
21196 *p = NUL;
21197 arg = vim_strsave(arg);
21198 if (arg == NULL)
21199 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021200
21201 /* Check for duplicate argument name. */
21202 for (i = 0; i < newargs.ga_len; ++i)
21203 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21204 {
21205 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21206 goto erret;
21207 }
21208
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21210 *p = c;
21211 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 if (*p == ',')
21213 ++p;
21214 else
21215 mustend = TRUE;
21216 }
21217 p = skipwhite(p);
21218 if (mustend && *p != ')')
21219 {
21220 if (!eap->skip)
21221 EMSG2(_(e_invarg2), eap->arg);
21222 break;
21223 }
21224 }
21225 ++p; /* skip the ')' */
21226
Bram Moolenaare9a41262005-01-15 22:18:47 +000021227 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 for (;;)
21229 {
21230 p = skipwhite(p);
21231 if (STRNCMP(p, "range", 5) == 0)
21232 {
21233 flags |= FC_RANGE;
21234 p += 5;
21235 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021236 else if (STRNCMP(p, "dict", 4) == 0)
21237 {
21238 flags |= FC_DICT;
21239 p += 4;
21240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 else if (STRNCMP(p, "abort", 5) == 0)
21242 {
21243 flags |= FC_ABORT;
21244 p += 5;
21245 }
21246 else
21247 break;
21248 }
21249
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021250 /* When there is a line break use what follows for the function body.
21251 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21252 if (*p == '\n')
21253 line_arg = p + 1;
21254 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255 EMSG(_(e_trailing));
21256
21257 /*
21258 * Read the body of the function, until ":endfunction" is found.
21259 */
21260 if (KeyTyped)
21261 {
21262 /* Check if the function already exists, don't let the user type the
21263 * whole function before telling him it doesn't work! For a script we
21264 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021265 if (!eap->skip && !eap->forceit)
21266 {
21267 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21268 EMSG(_(e_funcdict));
21269 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021270 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021273 if (!eap->skip && did_emsg)
21274 goto erret;
21275
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 msg_putchar('\n'); /* don't overwrite the function name */
21277 cmdline_row = msg_row;
21278 }
21279
21280 indent = 2;
21281 nesting = 0;
21282 for (;;)
21283 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021284 if (KeyTyped)
21285 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021287 sourcing_lnum_off = sourcing_lnum;
21288
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021289 if (line_arg != NULL)
21290 {
21291 /* Use eap->arg, split up in parts by line breaks. */
21292 theline = line_arg;
21293 p = vim_strchr(theline, '\n');
21294 if (p == NULL)
21295 line_arg += STRLEN(line_arg);
21296 else
21297 {
21298 *p = NUL;
21299 line_arg = p + 1;
21300 }
21301 }
21302 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303 theline = getcmdline(':', 0L, indent);
21304 else
21305 theline = eap->getline(':', eap->cookie, indent);
21306 if (KeyTyped)
21307 lines_left = Rows - 1;
21308 if (theline == NULL)
21309 {
21310 EMSG(_("E126: Missing :endfunction"));
21311 goto erret;
21312 }
21313
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021314 /* Detect line continuation: sourcing_lnum increased more than one. */
21315 if (sourcing_lnum > sourcing_lnum_off + 1)
21316 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21317 else
21318 sourcing_lnum_off = 0;
21319
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320 if (skip_until != NULL)
21321 {
21322 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21323 * don't check for ":endfunc". */
21324 if (STRCMP(theline, skip_until) == 0)
21325 {
21326 vim_free(skip_until);
21327 skip_until = NULL;
21328 }
21329 }
21330 else
21331 {
21332 /* skip ':' and blanks*/
21333 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21334 ;
21335
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021336 /* Check for "endfunction". */
21337 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021339 if (line_arg == NULL)
21340 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 break;
21342 }
21343
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021344 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 * at "end". */
21346 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21347 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021348 else if (STRNCMP(p, "if", 2) == 0
21349 || STRNCMP(p, "wh", 2) == 0
21350 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 || STRNCMP(p, "try", 3) == 0)
21352 indent += 2;
21353
21354 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021355 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021357 if (*p == '!')
21358 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 p += eval_fname_script(p);
21360 if (ASCII_ISALPHA(*p))
21361 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021362 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 if (*skipwhite(p) == '(')
21364 {
21365 ++nesting;
21366 indent += 2;
21367 }
21368 }
21369 }
21370
21371 /* Check for ":append" or ":insert". */
21372 p = skip_range(p, NULL);
21373 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21374 || (p[0] == 'i'
21375 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21376 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21377 skip_until = vim_strsave((char_u *)".");
21378
21379 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21380 arg = skipwhite(skiptowhite(p));
21381 if (arg[0] == '<' && arg[1] =='<'
21382 && ((p[0] == 'p' && p[1] == 'y'
21383 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21384 || (p[0] == 'p' && p[1] == 'e'
21385 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21386 || (p[0] == 't' && p[1] == 'c'
21387 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021388 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21389 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21391 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021392 || (p[0] == 'm' && p[1] == 'z'
21393 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 ))
21395 {
21396 /* ":python <<" continues until a dot, like ":append" */
21397 p = skipwhite(arg + 2);
21398 if (*p == NUL)
21399 skip_until = vim_strsave((char_u *)".");
21400 else
21401 skip_until = vim_strsave(p);
21402 }
21403 }
21404
21405 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021406 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021407 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021408 if (line_arg == NULL)
21409 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021411 }
21412
21413 /* Copy the line to newly allocated memory. get_one_sourceline()
21414 * allocates 250 bytes per line, this saves 80% on average. The cost
21415 * is an extra alloc/free. */
21416 p = vim_strsave(theline);
21417 if (p != NULL)
21418 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021419 if (line_arg == NULL)
21420 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021421 theline = p;
21422 }
21423
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021424 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21425
21426 /* Add NULL lines for continuation lines, so that the line count is
21427 * equal to the index in the growarray. */
21428 while (sourcing_lnum_off-- > 0)
21429 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021430
21431 /* Check for end of eap->arg. */
21432 if (line_arg != NULL && *line_arg == NUL)
21433 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 }
21435
21436 /* Don't define the function when skipping commands or when an error was
21437 * detected. */
21438 if (eap->skip || did_emsg)
21439 goto erret;
21440
21441 /*
21442 * If there are no errors, add the function
21443 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021444 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021445 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021446 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021447 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021448 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021449 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021450 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021451 goto erret;
21452 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021453
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021454 fp = find_func(name);
21455 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021457 if (!eap->forceit)
21458 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021459 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021460 goto erret;
21461 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021462 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021463 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021464 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021465 name);
21466 goto erret;
21467 }
21468 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021469 ga_clear_strings(&(fp->uf_args));
21470 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021471 vim_free(name);
21472 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 }
21475 else
21476 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021477 char numbuf[20];
21478
21479 fp = NULL;
21480 if (fudi.fd_newkey == NULL && !eap->forceit)
21481 {
21482 EMSG(_(e_funcdict));
21483 goto erret;
21484 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021485 if (fudi.fd_di == NULL)
21486 {
21487 /* Can't add a function to a locked dictionary */
21488 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21489 goto erret;
21490 }
21491 /* Can't change an existing function if it is locked */
21492 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21493 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021494
21495 /* Give the function a sequential number. Can only be used with a
21496 * Funcref! */
21497 vim_free(name);
21498 sprintf(numbuf, "%d", ++func_nr);
21499 name = vim_strsave((char_u *)numbuf);
21500 if (name == NULL)
21501 goto erret;
21502 }
21503
21504 if (fp == NULL)
21505 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021506 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021507 {
21508 int slen, plen;
21509 char_u *scriptname;
21510
21511 /* Check that the autoload name matches the script name. */
21512 j = FAIL;
21513 if (sourcing_name != NULL)
21514 {
21515 scriptname = autoload_name(name);
21516 if (scriptname != NULL)
21517 {
21518 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021519 plen = (int)STRLEN(p);
21520 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021521 if (slen > plen && fnamecmp(p,
21522 sourcing_name + slen - plen) == 0)
21523 j = OK;
21524 vim_free(scriptname);
21525 }
21526 }
21527 if (j == FAIL)
21528 {
21529 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21530 goto erret;
21531 }
21532 }
21533
21534 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 if (fp == NULL)
21536 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021537
21538 if (fudi.fd_dict != NULL)
21539 {
21540 if (fudi.fd_di == NULL)
21541 {
21542 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021543 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021544 if (fudi.fd_di == NULL)
21545 {
21546 vim_free(fp);
21547 goto erret;
21548 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021549 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21550 {
21551 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021552 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021553 goto erret;
21554 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021555 }
21556 else
21557 /* overwrite existing dict entry */
21558 clear_tv(&fudi.fd_di->di_tv);
21559 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021560 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021561 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021562 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021563
21564 /* behave like "dict" was used */
21565 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021566 }
21567
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021569 STRCPY(fp->uf_name, name);
21570 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021571 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021572 fp->uf_args = newargs;
21573 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021574#ifdef FEAT_PROFILE
21575 fp->uf_tml_count = NULL;
21576 fp->uf_tml_total = NULL;
21577 fp->uf_tml_self = NULL;
21578 fp->uf_profiling = FALSE;
21579 if (prof_def_func())
21580 func_do_profile(fp);
21581#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021582 fp->uf_varargs = varargs;
21583 fp->uf_flags = flags;
21584 fp->uf_calls = 0;
21585 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021586 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587
21588erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 ga_clear_strings(&newargs);
21590 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021591ret_free:
21592 vim_free(skip_until);
21593 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596}
21597
21598/*
21599 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021600 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021602 * flags:
21603 * TFN_INT: internal function name OK
21604 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 * Advances "pp" to just after the function name (if no error).
21606 */
21607 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021608trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 char_u **pp;
21610 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021611 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021612 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021614 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 char_u *start;
21616 char_u *end;
21617 int lead;
21618 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021620 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021621
21622 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021623 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021625
21626 /* Check for hard coded <SNR>: already translated function ID (from a user
21627 * command). */
21628 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21629 && (*pp)[2] == (int)KE_SNR)
21630 {
21631 *pp += 3;
21632 len = get_id_len(pp) + 3;
21633 return vim_strnsave(start, len);
21634 }
21635
21636 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21637 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021639 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021641
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021642 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21643 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021644 if (end == start)
21645 {
21646 if (!skip)
21647 EMSG(_("E129: Function name required"));
21648 goto theend;
21649 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021650 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021651 {
21652 /*
21653 * Report an invalid expression in braces, unless the expression
21654 * evaluation has been cancelled due to an aborting error, an
21655 * interrupt, or an exception.
21656 */
21657 if (!aborting())
21658 {
21659 if (end != NULL)
21660 EMSG2(_(e_invarg2), start);
21661 }
21662 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021663 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021664 goto theend;
21665 }
21666
21667 if (lv.ll_tv != NULL)
21668 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021669 if (fdp != NULL)
21670 {
21671 fdp->fd_dict = lv.ll_dict;
21672 fdp->fd_newkey = lv.ll_newkey;
21673 lv.ll_newkey = NULL;
21674 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021675 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021676 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21677 {
21678 name = vim_strsave(lv.ll_tv->vval.v_string);
21679 *pp = end;
21680 }
21681 else
21682 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021683 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21684 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021685 EMSG(_(e_funcref));
21686 else
21687 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021688 name = NULL;
21689 }
21690 goto theend;
21691 }
21692
21693 if (lv.ll_name == NULL)
21694 {
21695 /* Error found, but continue after the function name. */
21696 *pp = end;
21697 goto theend;
21698 }
21699
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021700 /* Check if the name is a Funcref. If so, use the value. */
21701 if (lv.ll_exp_name != NULL)
21702 {
21703 len = (int)STRLEN(lv.ll_exp_name);
21704 name = deref_func_name(lv.ll_exp_name, &len);
21705 if (name == lv.ll_exp_name)
21706 name = NULL;
21707 }
21708 else
21709 {
21710 len = (int)(end - *pp);
21711 name = deref_func_name(*pp, &len);
21712 if (name == *pp)
21713 name = NULL;
21714 }
21715 if (name != NULL)
21716 {
21717 name = vim_strsave(name);
21718 *pp = end;
21719 goto theend;
21720 }
21721
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021722 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021723 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021724 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021725 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21726 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21727 {
21728 /* When there was "s:" already or the name expanded to get a
21729 * leading "s:" then remove it. */
21730 lv.ll_name += 2;
21731 len -= 2;
21732 lead = 2;
21733 }
21734 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021735 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021736 {
21737 if (lead == 2) /* skip over "s:" */
21738 lv.ll_name += 2;
21739 len = (int)(end - lv.ll_name);
21740 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021741
21742 /*
21743 * Copy the function name to allocated memory.
21744 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21745 * Accept <SNR>123_name() outside a script.
21746 */
21747 if (skip)
21748 lead = 0; /* do nothing */
21749 else if (lead > 0)
21750 {
21751 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021752 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21753 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021754 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021755 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021756 if (current_SID <= 0)
21757 {
21758 EMSG(_(e_usingsid));
21759 goto theend;
21760 }
21761 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21762 lead += (int)STRLEN(sid_buf);
21763 }
21764 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021765 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021766 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021767 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021768 goto theend;
21769 }
21770 name = alloc((unsigned)(len + lead + 1));
21771 if (name != NULL)
21772 {
21773 if (lead > 0)
21774 {
21775 name[0] = K_SPECIAL;
21776 name[1] = KS_EXTRA;
21777 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021778 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021779 STRCPY(name + 3, sid_buf);
21780 }
21781 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21782 name[len + lead] = NUL;
21783 }
21784 *pp = end;
21785
21786theend:
21787 clear_lval(&lv);
21788 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789}
21790
21791/*
21792 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21793 * Return 2 if "p" starts with "s:".
21794 * Return 0 otherwise.
21795 */
21796 static int
21797eval_fname_script(p)
21798 char_u *p;
21799{
21800 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21801 || STRNICMP(p + 1, "SNR>", 4) == 0))
21802 return 5;
21803 if (p[0] == 's' && p[1] == ':')
21804 return 2;
21805 return 0;
21806}
21807
21808/*
21809 * Return TRUE if "p" starts with "<SID>" or "s:".
21810 * Only works if eval_fname_script() returned non-zero for "p"!
21811 */
21812 static int
21813eval_fname_sid(p)
21814 char_u *p;
21815{
21816 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21817}
21818
21819/*
21820 * List the head of the function: "name(arg1, arg2)".
21821 */
21822 static void
21823list_func_head(fp, indent)
21824 ufunc_T *fp;
21825 int indent;
21826{
21827 int j;
21828
21829 msg_start();
21830 if (indent)
21831 MSG_PUTS(" ");
21832 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021833 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834 {
21835 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021836 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837 }
21838 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021839 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021841 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 {
21843 if (j)
21844 MSG_PUTS(", ");
21845 msg_puts(FUNCARG(fp, j));
21846 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021847 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848 {
21849 if (j)
21850 MSG_PUTS(", ");
21851 MSG_PUTS("...");
21852 }
21853 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021854 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021855 if (p_verbose > 0)
21856 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857}
21858
21859/*
21860 * Find a function by name, return pointer to it in ufuncs.
21861 * Return NULL for unknown function.
21862 */
21863 static ufunc_T *
21864find_func(name)
21865 char_u *name;
21866{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021867 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021869 hi = hash_find(&func_hashtab, name);
21870 if (!HASHITEM_EMPTY(hi))
21871 return HI2UF(hi);
21872 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873}
21874
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021875#if defined(EXITFREE) || defined(PROTO)
21876 void
21877free_all_functions()
21878{
21879 hashitem_T *hi;
21880
21881 /* Need to start all over every time, because func_free() may change the
21882 * hash table. */
21883 while (func_hashtab.ht_used > 0)
21884 for (hi = func_hashtab.ht_array; ; ++hi)
21885 if (!HASHITEM_EMPTY(hi))
21886 {
21887 func_free(HI2UF(hi));
21888 break;
21889 }
21890}
21891#endif
21892
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021893/*
21894 * Return TRUE if a function "name" exists.
21895 */
21896 static int
21897function_exists(name)
21898 char_u *name;
21899{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021900 char_u *nm = name;
21901 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021902 int n = FALSE;
21903
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021904 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021905 nm = skipwhite(nm);
21906
21907 /* Only accept "funcname", "funcname ", "funcname (..." and
21908 * "funcname(...", not "funcname!...". */
21909 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021910 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021911 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021912 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021913 else
21914 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021915 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021916 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021917 return n;
21918}
21919
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021920/*
21921 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021922 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021923 */
21924 static int
21925builtin_function(name)
21926 char_u *name;
21927{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021928 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21929 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021930}
21931
Bram Moolenaar05159a02005-02-26 23:04:13 +000021932#if defined(FEAT_PROFILE) || defined(PROTO)
21933/*
21934 * Start profiling function "fp".
21935 */
21936 static void
21937func_do_profile(fp)
21938 ufunc_T *fp;
21939{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021940 int len = fp->uf_lines.ga_len;
21941
21942 if (len == 0)
21943 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021944 fp->uf_tm_count = 0;
21945 profile_zero(&fp->uf_tm_self);
21946 profile_zero(&fp->uf_tm_total);
21947 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021948 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021949 if (fp->uf_tml_total == NULL)
21950 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021951 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021952 if (fp->uf_tml_self == NULL)
21953 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021954 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021955 fp->uf_tml_idx = -1;
21956 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21957 || fp->uf_tml_self == NULL)
21958 return; /* out of memory */
21959
21960 fp->uf_profiling = TRUE;
21961}
21962
21963/*
21964 * Dump the profiling results for all functions in file "fd".
21965 */
21966 void
21967func_dump_profile(fd)
21968 FILE *fd;
21969{
21970 hashitem_T *hi;
21971 int todo;
21972 ufunc_T *fp;
21973 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021974 ufunc_T **sorttab;
21975 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021976
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021977 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021978 if (todo == 0)
21979 return; /* nothing to dump */
21980
Bram Moolenaar73830342005-02-28 22:48:19 +000021981 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21982
Bram Moolenaar05159a02005-02-26 23:04:13 +000021983 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21984 {
21985 if (!HASHITEM_EMPTY(hi))
21986 {
21987 --todo;
21988 fp = HI2UF(hi);
21989 if (fp->uf_profiling)
21990 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021991 if (sorttab != NULL)
21992 sorttab[st_len++] = fp;
21993
Bram Moolenaar05159a02005-02-26 23:04:13 +000021994 if (fp->uf_name[0] == K_SPECIAL)
21995 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21996 else
21997 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21998 if (fp->uf_tm_count == 1)
21999 fprintf(fd, "Called 1 time\n");
22000 else
22001 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22002 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22003 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22004 fprintf(fd, "\n");
22005 fprintf(fd, "count total (s) self (s)\n");
22006
22007 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22008 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022009 if (FUNCLINE(fp, i) == NULL)
22010 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022011 prof_func_line(fd, fp->uf_tml_count[i],
22012 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022013 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22014 }
22015 fprintf(fd, "\n");
22016 }
22017 }
22018 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022019
22020 if (sorttab != NULL && st_len > 0)
22021 {
22022 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22023 prof_total_cmp);
22024 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22025 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22026 prof_self_cmp);
22027 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22028 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022029
22030 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022031}
Bram Moolenaar73830342005-02-28 22:48:19 +000022032
22033 static void
22034prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22035 FILE *fd;
22036 ufunc_T **sorttab;
22037 int st_len;
22038 char *title;
22039 int prefer_self; /* when equal print only self time */
22040{
22041 int i;
22042 ufunc_T *fp;
22043
22044 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22045 fprintf(fd, "count total (s) self (s) function\n");
22046 for (i = 0; i < 20 && i < st_len; ++i)
22047 {
22048 fp = sorttab[i];
22049 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22050 prefer_self);
22051 if (fp->uf_name[0] == K_SPECIAL)
22052 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22053 else
22054 fprintf(fd, " %s()\n", fp->uf_name);
22055 }
22056 fprintf(fd, "\n");
22057}
22058
22059/*
22060 * Print the count and times for one function or function line.
22061 */
22062 static void
22063prof_func_line(fd, count, total, self, prefer_self)
22064 FILE *fd;
22065 int count;
22066 proftime_T *total;
22067 proftime_T *self;
22068 int prefer_self; /* when equal print only self time */
22069{
22070 if (count > 0)
22071 {
22072 fprintf(fd, "%5d ", count);
22073 if (prefer_self && profile_equal(total, self))
22074 fprintf(fd, " ");
22075 else
22076 fprintf(fd, "%s ", profile_msg(total));
22077 if (!prefer_self && profile_equal(total, self))
22078 fprintf(fd, " ");
22079 else
22080 fprintf(fd, "%s ", profile_msg(self));
22081 }
22082 else
22083 fprintf(fd, " ");
22084}
22085
22086/*
22087 * Compare function for total time sorting.
22088 */
22089 static int
22090#ifdef __BORLANDC__
22091_RTLENTRYF
22092#endif
22093prof_total_cmp(s1, s2)
22094 const void *s1;
22095 const void *s2;
22096{
22097 ufunc_T *p1, *p2;
22098
22099 p1 = *(ufunc_T **)s1;
22100 p2 = *(ufunc_T **)s2;
22101 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22102}
22103
22104/*
22105 * Compare function for self time sorting.
22106 */
22107 static int
22108#ifdef __BORLANDC__
22109_RTLENTRYF
22110#endif
22111prof_self_cmp(s1, s2)
22112 const void *s1;
22113 const void *s2;
22114{
22115 ufunc_T *p1, *p2;
22116
22117 p1 = *(ufunc_T **)s1;
22118 p2 = *(ufunc_T **)s2;
22119 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22120}
22121
Bram Moolenaar05159a02005-02-26 23:04:13 +000022122#endif
22123
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022124/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022125 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022126 * Return TRUE if a package was loaded.
22127 */
22128 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022129script_autoload(name, reload)
22130 char_u *name;
22131 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022132{
22133 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022134 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022135 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022136 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022137
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022138 /* Return quickly when autoload disabled. */
22139 if (no_autoload)
22140 return FALSE;
22141
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022142 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022143 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022144 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022145 return FALSE;
22146
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022147 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022148
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022149 /* Find the name in the list of previously loaded package names. Skip
22150 * "autoload/", it's always the same. */
22151 for (i = 0; i < ga_loaded.ga_len; ++i)
22152 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22153 break;
22154 if (!reload && i < ga_loaded.ga_len)
22155 ret = FALSE; /* was loaded already */
22156 else
22157 {
22158 /* Remember the name if it wasn't loaded already. */
22159 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22160 {
22161 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22162 tofree = NULL;
22163 }
22164
22165 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022166 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022167 ret = TRUE;
22168 }
22169
22170 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022171 return ret;
22172}
22173
22174/*
22175 * Return the autoload script name for a function or variable name.
22176 * Returns NULL when out of memory.
22177 */
22178 static char_u *
22179autoload_name(name)
22180 char_u *name;
22181{
22182 char_u *p;
22183 char_u *scriptname;
22184
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022185 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022186 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22187 if (scriptname == NULL)
22188 return FALSE;
22189 STRCPY(scriptname, "autoload/");
22190 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022191 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022192 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022193 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022194 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022195 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022196}
22197
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22199
22200/*
22201 * Function given to ExpandGeneric() to obtain the list of user defined
22202 * function names.
22203 */
22204 char_u *
22205get_user_func_name(xp, idx)
22206 expand_T *xp;
22207 int idx;
22208{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022209 static long_u done;
22210 static hashitem_T *hi;
22211 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212
22213 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022215 done = 0;
22216 hi = func_hashtab.ht_array;
22217 }
22218 if (done < func_hashtab.ht_used)
22219 {
22220 if (done++ > 0)
22221 ++hi;
22222 while (HASHITEM_EMPTY(hi))
22223 ++hi;
22224 fp = HI2UF(hi);
22225
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022226 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022227 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022228
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022229 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22230 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022231
22232 cat_func_name(IObuff, fp);
22233 if (xp->xp_context != EXPAND_USER_FUNC)
22234 {
22235 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022236 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 STRCAT(IObuff, ")");
22238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239 return IObuff;
22240 }
22241 return NULL;
22242}
22243
22244#endif /* FEAT_CMDL_COMPL */
22245
22246/*
22247 * Copy the function name of "fp" to buffer "buf".
22248 * "buf" must be able to hold the function name plus three bytes.
22249 * Takes care of script-local function names.
22250 */
22251 static void
22252cat_func_name(buf, fp)
22253 char_u *buf;
22254 ufunc_T *fp;
22255{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022256 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022257 {
22258 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022259 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 }
22261 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022262 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263}
22264
22265/*
22266 * ":delfunction {name}"
22267 */
22268 void
22269ex_delfunction(eap)
22270 exarg_T *eap;
22271{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022272 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022273 char_u *p;
22274 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022275 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276
22277 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022278 name = trans_function_name(&p, eap->skip, 0, &fudi);
22279 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022281 {
22282 if (fudi.fd_dict != NULL && !eap->skip)
22283 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 if (!ends_excmd(*skipwhite(p)))
22287 {
22288 vim_free(name);
22289 EMSG(_(e_trailing));
22290 return;
22291 }
22292 eap->nextcmd = check_nextcmd(p);
22293 if (eap->nextcmd != NULL)
22294 *p = NUL;
22295
22296 if (!eap->skip)
22297 fp = find_func(name);
22298 vim_free(name);
22299
22300 if (!eap->skip)
22301 {
22302 if (fp == NULL)
22303 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022304 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305 return;
22306 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022307 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308 {
22309 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22310 return;
22311 }
22312
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022313 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022315 /* Delete the dict item that refers to the function, it will
22316 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022317 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022319 else
22320 func_free(fp);
22321 }
22322}
22323
22324/*
22325 * Free a function and remove it from the list of functions.
22326 */
22327 static void
22328func_free(fp)
22329 ufunc_T *fp;
22330{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022331 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022332
22333 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022334 ga_clear_strings(&(fp->uf_args));
22335 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022336#ifdef FEAT_PROFILE
22337 vim_free(fp->uf_tml_count);
22338 vim_free(fp->uf_tml_total);
22339 vim_free(fp->uf_tml_self);
22340#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022341
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022342 /* remove the function from the function hashtable */
22343 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22344 if (HASHITEM_EMPTY(hi))
22345 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022346 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022347 hash_remove(&func_hashtab, hi);
22348
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022349 vim_free(fp);
22350}
22351
22352/*
22353 * Unreference a Function: decrement the reference count and free it when it
22354 * becomes zero. Only for numbered functions.
22355 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022356 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022357func_unref(name)
22358 char_u *name;
22359{
22360 ufunc_T *fp;
22361
22362 if (name != NULL && isdigit(*name))
22363 {
22364 fp = find_func(name);
22365 if (fp == NULL)
22366 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022367 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022368 {
22369 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022370 * when "uf_calls" becomes zero. */
22371 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022372 func_free(fp);
22373 }
22374 }
22375}
22376
22377/*
22378 * Count a reference to a Function.
22379 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022380 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022381func_ref(name)
22382 char_u *name;
22383{
22384 ufunc_T *fp;
22385
22386 if (name != NULL && isdigit(*name))
22387 {
22388 fp = find_func(name);
22389 if (fp == NULL)
22390 EMSG2(_(e_intern2), "func_ref()");
22391 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022392 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393 }
22394}
22395
22396/*
22397 * Call a user function.
22398 */
22399 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022400call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 ufunc_T *fp; /* pointer to function */
22402 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022403 typval_T *argvars; /* arguments */
22404 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 linenr_T firstline; /* first line of range */
22406 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022407 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408{
Bram Moolenaar33570922005-01-25 22:26:29 +000022409 char_u *save_sourcing_name;
22410 linenr_T save_sourcing_lnum;
22411 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022412 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022413 int save_did_emsg;
22414 static int depth = 0;
22415 dictitem_T *v;
22416 int fixvar_idx = 0; /* index in fixvar[] */
22417 int i;
22418 int ai;
22419 char_u numbuf[NUMBUFLEN];
22420 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022421#ifdef FEAT_PROFILE
22422 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022423 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425
22426 /* If depth of calling is getting too high, don't execute the function */
22427 if (depth >= p_mfd)
22428 {
22429 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022430 rettv->v_type = VAR_NUMBER;
22431 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 return;
22433 }
22434 ++depth;
22435
22436 line_breakcheck(); /* check for CTRL-C hit */
22437
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022438 fc = (funccall_T *)alloc(sizeof(funccall_T));
22439 fc->caller = current_funccal;
22440 current_funccal = fc;
22441 fc->func = fp;
22442 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022443 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022444 fc->linenr = 0;
22445 fc->returned = FALSE;
22446 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022448 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22449 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450
Bram Moolenaar33570922005-01-25 22:26:29 +000022451 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022452 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022453 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22454 * each argument variable and saves a lot of time.
22455 */
22456 /*
22457 * Init l: variables.
22458 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022459 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022460 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022461 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022462 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22463 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022464 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022465 name = v->di_key;
22466 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022467 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022468 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022469 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022470 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022471 v->di_tv.vval.v_dict = selfdict;
22472 ++selfdict->dv_refcount;
22473 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022474
Bram Moolenaar33570922005-01-25 22:26:29 +000022475 /*
22476 * Init a: variables.
22477 * Set a:0 to "argcount".
22478 * Set a:000 to a list with room for the "..." arguments.
22479 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022480 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022481 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022482 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022483 /* Use "name" to avoid a warning from some compiler that checks the
22484 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022485 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022486 name = v->di_key;
22487 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022488 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022489 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022490 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022491 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022492 v->di_tv.vval.v_list = &fc->l_varlist;
22493 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22494 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22495 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022496
22497 /*
22498 * Set a:firstline to "firstline" and a:lastline to "lastline".
22499 * Set a:name to named arguments.
22500 * Set a:N to the "..." arguments.
22501 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022502 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022503 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022504 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022505 (varnumber_T)lastline);
22506 for (i = 0; i < argcount; ++i)
22507 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022508 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022509 if (ai < 0)
22510 /* named argument a:name */
22511 name = FUNCARG(fp, i);
22512 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022513 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022514 /* "..." argument a:1, a:2, etc. */
22515 sprintf((char *)numbuf, "%d", ai + 1);
22516 name = numbuf;
22517 }
22518 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22519 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022520 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022521 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22522 }
22523 else
22524 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022525 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22526 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022527 if (v == NULL)
22528 break;
22529 v->di_flags = DI_FLAGS_RO;
22530 }
22531 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022532 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022533
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022534 /* Note: the values are copied directly to avoid alloc/free.
22535 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022536 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022537 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022538
22539 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22540 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022541 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22542 fc->l_listitems[ai].li_tv = argvars[i];
22543 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022544 }
22545 }
22546
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 /* Don't redraw while executing the function. */
22548 ++RedrawingDisabled;
22549 save_sourcing_name = sourcing_name;
22550 save_sourcing_lnum = sourcing_lnum;
22551 sourcing_lnum = 1;
22552 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022553 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554 if (sourcing_name != NULL)
22555 {
22556 if (save_sourcing_name != NULL
22557 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22558 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22559 else
22560 STRCPY(sourcing_name, "function ");
22561 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22562
22563 if (p_verbose >= 12)
22564 {
22565 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022566 verbose_enter_scroll();
22567
Bram Moolenaar555b2802005-05-19 21:08:39 +000022568 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569 if (p_verbose >= 14)
22570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022572 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022573 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022574 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575
22576 msg_puts((char_u *)"(");
22577 for (i = 0; i < argcount; ++i)
22578 {
22579 if (i > 0)
22580 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022581 if (argvars[i].v_type == VAR_NUMBER)
22582 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583 else
22584 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022585 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22586 if (s != NULL)
22587 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022588 if (vim_strsize(s) > MSG_BUF_CLEN)
22589 {
22590 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22591 s = buf;
22592 }
22593 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022594 vim_free(tofree);
22595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 }
22597 }
22598 msg_puts((char_u *)")");
22599 }
22600 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022601
22602 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603 --no_wait_return;
22604 }
22605 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022606#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022607 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022608 {
22609 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22610 func_do_profile(fp);
22611 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022612 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022613 {
22614 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022615 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022616 profile_zero(&fp->uf_tm_children);
22617 }
22618 script_prof_save(&wait_start);
22619 }
22620#endif
22621
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022623 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 save_did_emsg = did_emsg;
22625 did_emsg = FALSE;
22626
22627 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022628 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22630
22631 --RedrawingDisabled;
22632
22633 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022634 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022636 clear_tv(rettv);
22637 rettv->v_type = VAR_NUMBER;
22638 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 }
22640
Bram Moolenaar05159a02005-02-26 23:04:13 +000022641#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022642 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022643 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022644 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022645 profile_end(&call_start);
22646 profile_sub_wait(&wait_start, &call_start);
22647 profile_add(&fp->uf_tm_total, &call_start);
22648 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022649 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022650 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022651 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22652 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022653 }
22654 }
22655#endif
22656
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657 /* when being verbose, mention the return value */
22658 if (p_verbose >= 12)
22659 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022661 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022664 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022665 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022666 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022667 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022668 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022670 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022671 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022672 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022673 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022674
Bram Moolenaar555b2802005-05-19 21:08:39 +000022675 /* The value may be very long. Skip the middle part, so that we
22676 * have some idea how it starts and ends. smsg() would always
22677 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022678 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022679 if (s != NULL)
22680 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022681 if (vim_strsize(s) > MSG_BUF_CLEN)
22682 {
22683 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22684 s = buf;
22685 }
22686 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022687 vim_free(tofree);
22688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 }
22690 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022691
22692 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693 --no_wait_return;
22694 }
22695
22696 vim_free(sourcing_name);
22697 sourcing_name = save_sourcing_name;
22698 sourcing_lnum = save_sourcing_lnum;
22699 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022700#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022701 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022702 script_prof_restore(&wait_start);
22703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704
22705 if (p_verbose >= 12 && sourcing_name != NULL)
22706 {
22707 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022708 verbose_enter_scroll();
22709
Bram Moolenaar555b2802005-05-19 21:08:39 +000022710 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022712
22713 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 --no_wait_return;
22715 }
22716
22717 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022718 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022720
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022721 /* If the a:000 list and the l: and a: dicts are not referenced we can
22722 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022723 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22724 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22725 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22726 {
22727 free_funccal(fc, FALSE);
22728 }
22729 else
22730 {
22731 hashitem_T *hi;
22732 listitem_T *li;
22733 int todo;
22734
22735 /* "fc" is still in use. This can happen when returning "a:000" or
22736 * assigning "l:" to a global variable.
22737 * Link "fc" in the list for garbage collection later. */
22738 fc->caller = previous_funccal;
22739 previous_funccal = fc;
22740
22741 /* Make a copy of the a: variables, since we didn't do that above. */
22742 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22743 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22744 {
22745 if (!HASHITEM_EMPTY(hi))
22746 {
22747 --todo;
22748 v = HI2DI(hi);
22749 copy_tv(&v->di_tv, &v->di_tv);
22750 }
22751 }
22752
22753 /* Make a copy of the a:000 items, since we didn't do that above. */
22754 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22755 copy_tv(&li->li_tv, &li->li_tv);
22756 }
22757}
22758
22759/*
22760 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022761 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022762 */
22763 static int
22764can_free_funccal(fc, copyID)
22765 funccall_T *fc;
22766 int copyID;
22767{
22768 return (fc->l_varlist.lv_copyID != copyID
22769 && fc->l_vars.dv_copyID != copyID
22770 && fc->l_avars.dv_copyID != copyID);
22771}
22772
22773/*
22774 * Free "fc" and what it contains.
22775 */
22776 static void
22777free_funccal(fc, free_val)
22778 funccall_T *fc;
22779 int free_val; /* a: vars were allocated */
22780{
22781 listitem_T *li;
22782
22783 /* The a: variables typevals may not have been allocated, only free the
22784 * allocated variables. */
22785 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22786
22787 /* free all l: variables */
22788 vars_clear(&fc->l_vars.dv_hashtab);
22789
22790 /* Free the a:000 variables if they were allocated. */
22791 if (free_val)
22792 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22793 clear_tv(&li->li_tv);
22794
22795 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796}
22797
22798/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022799 * Add a number variable "name" to dict "dp" with value "nr".
22800 */
22801 static void
22802add_nr_var(dp, v, name, nr)
22803 dict_T *dp;
22804 dictitem_T *v;
22805 char *name;
22806 varnumber_T nr;
22807{
22808 STRCPY(v->di_key, name);
22809 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22810 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22811 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022812 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022813 v->di_tv.vval.v_number = nr;
22814}
22815
22816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817 * ":return [expr]"
22818 */
22819 void
22820ex_return(eap)
22821 exarg_T *eap;
22822{
22823 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022824 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 int returning = FALSE;
22826
22827 if (current_funccal == NULL)
22828 {
22829 EMSG(_("E133: :return not inside a function"));
22830 return;
22831 }
22832
22833 if (eap->skip)
22834 ++emsg_skip;
22835
22836 eap->nextcmd = NULL;
22837 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022838 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839 {
22840 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022841 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022843 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844 }
22845 /* It's safer to return also on error. */
22846 else if (!eap->skip)
22847 {
22848 /*
22849 * Return unless the expression evaluation has been cancelled due to an
22850 * aborting error, an interrupt, or an exception.
22851 */
22852 if (!aborting())
22853 returning = do_return(eap, FALSE, TRUE, NULL);
22854 }
22855
22856 /* When skipping or the return gets pending, advance to the next command
22857 * in this line (!returning). Otherwise, ignore the rest of the line.
22858 * Following lines will be ignored by get_func_line(). */
22859 if (returning)
22860 eap->nextcmd = NULL;
22861 else if (eap->nextcmd == NULL) /* no argument */
22862 eap->nextcmd = check_nextcmd(arg);
22863
22864 if (eap->skip)
22865 --emsg_skip;
22866}
22867
22868/*
22869 * Return from a function. Possibly makes the return pending. Also called
22870 * for a pending return at the ":endtry" or after returning from an extra
22871 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022872 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022873 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 * FALSE when the return gets pending.
22875 */
22876 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022877do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 exarg_T *eap;
22879 int reanimate;
22880 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022881 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882{
22883 int idx;
22884 struct condstack *cstack = eap->cstack;
22885
22886 if (reanimate)
22887 /* Undo the return. */
22888 current_funccal->returned = FALSE;
22889
22890 /*
22891 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22892 * not in its finally clause (which then is to be executed next) is found.
22893 * In this case, make the ":return" pending for execution at the ":endtry".
22894 * Otherwise, return normally.
22895 */
22896 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22897 if (idx >= 0)
22898 {
22899 cstack->cs_pending[idx] = CSTP_RETURN;
22900
22901 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022902 /* A pending return again gets pending. "rettv" points to an
22903 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022905 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 else
22907 {
22908 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022909 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022911 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022913 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 {
22915 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022916 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022917 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 else
22919 EMSG(_(e_outofmem));
22920 }
22921 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022922 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923
22924 if (reanimate)
22925 {
22926 /* The pending return value could be overwritten by a ":return"
22927 * without argument in a finally clause; reset the default
22928 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022929 current_funccal->rettv->v_type = VAR_NUMBER;
22930 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 }
22932 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022933 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934 }
22935 else
22936 {
22937 current_funccal->returned = TRUE;
22938
22939 /* If the return is carried out now, store the return value. For
22940 * a return immediately after reanimation, the value is already
22941 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022942 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022944 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022945 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022947 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 }
22949 }
22950
22951 return idx < 0;
22952}
22953
22954/*
22955 * Free the variable with a pending return value.
22956 */
22957 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022958discard_pending_return(rettv)
22959 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022960{
Bram Moolenaar33570922005-01-25 22:26:29 +000022961 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962}
22963
22964/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022965 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 * is an allocated string. Used by report_pending() for verbose messages.
22967 */
22968 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022969get_return_cmd(rettv)
22970 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022972 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022973 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022974 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022976 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022977 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022978 if (s == NULL)
22979 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022980
22981 STRCPY(IObuff, ":return ");
22982 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22983 if (STRLEN(s) + 8 >= IOSIZE)
22984 STRCPY(IObuff + IOSIZE - 4, "...");
22985 vim_free(tofree);
22986 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987}
22988
22989/*
22990 * Get next function line.
22991 * Called by do_cmdline() to get the next line.
22992 * Returns allocated string, or NULL for end of function.
22993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994 char_u *
22995get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022996 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022998 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999{
Bram Moolenaar33570922005-01-25 22:26:29 +000023000 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023001 ufunc_T *fp = fcp->func;
23002 char_u *retval;
23003 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004
23005 /* If breakpoints have been added/deleted need to check for it. */
23006 if (fcp->dbg_tick != debug_tick)
23007 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023008 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009 sourcing_lnum);
23010 fcp->dbg_tick = debug_tick;
23011 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023012#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023013 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023014 func_line_end(cookie);
23015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016
Bram Moolenaar05159a02005-02-26 23:04:13 +000023017 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023018 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23019 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020 retval = NULL;
23021 else
23022 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023023 /* Skip NULL lines (continuation lines). */
23024 while (fcp->linenr < gap->ga_len
23025 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23026 ++fcp->linenr;
23027 if (fcp->linenr >= gap->ga_len)
23028 retval = NULL;
23029 else
23030 {
23031 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23032 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023033#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023034 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023035 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023036#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 }
23039
23040 /* Did we encounter a breakpoint? */
23041 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23042 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023043 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023044 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023045 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 sourcing_lnum);
23047 fcp->dbg_tick = debug_tick;
23048 }
23049
23050 return retval;
23051}
23052
Bram Moolenaar05159a02005-02-26 23:04:13 +000023053#if defined(FEAT_PROFILE) || defined(PROTO)
23054/*
23055 * Called when starting to read a function line.
23056 * "sourcing_lnum" must be correct!
23057 * When skipping lines it may not actually be executed, but we won't find out
23058 * until later and we need to store the time now.
23059 */
23060 void
23061func_line_start(cookie)
23062 void *cookie;
23063{
23064 funccall_T *fcp = (funccall_T *)cookie;
23065 ufunc_T *fp = fcp->func;
23066
23067 if (fp->uf_profiling && sourcing_lnum >= 1
23068 && sourcing_lnum <= fp->uf_lines.ga_len)
23069 {
23070 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023071 /* Skip continuation lines. */
23072 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23073 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023074 fp->uf_tml_execed = FALSE;
23075 profile_start(&fp->uf_tml_start);
23076 profile_zero(&fp->uf_tml_children);
23077 profile_get_wait(&fp->uf_tml_wait);
23078 }
23079}
23080
23081/*
23082 * Called when actually executing a function line.
23083 */
23084 void
23085func_line_exec(cookie)
23086 void *cookie;
23087{
23088 funccall_T *fcp = (funccall_T *)cookie;
23089 ufunc_T *fp = fcp->func;
23090
23091 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23092 fp->uf_tml_execed = TRUE;
23093}
23094
23095/*
23096 * Called when done with a function line.
23097 */
23098 void
23099func_line_end(cookie)
23100 void *cookie;
23101{
23102 funccall_T *fcp = (funccall_T *)cookie;
23103 ufunc_T *fp = fcp->func;
23104
23105 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23106 {
23107 if (fp->uf_tml_execed)
23108 {
23109 ++fp->uf_tml_count[fp->uf_tml_idx];
23110 profile_end(&fp->uf_tml_start);
23111 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023112 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023113 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23114 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023115 }
23116 fp->uf_tml_idx = -1;
23117 }
23118}
23119#endif
23120
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121/*
23122 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023123 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 */
23125 int
23126func_has_ended(cookie)
23127 void *cookie;
23128{
Bram Moolenaar33570922005-01-25 22:26:29 +000023129 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023130
23131 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23132 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023133 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 || fcp->returned);
23135}
23136
23137/*
23138 * return TRUE if cookie indicates a function which "abort"s on errors.
23139 */
23140 int
23141func_has_abort(cookie)
23142 void *cookie;
23143{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023144 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145}
23146
23147#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23148typedef enum
23149{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023150 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23151 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23152 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153} var_flavour_T;
23154
23155static var_flavour_T var_flavour __ARGS((char_u *varname));
23156
23157 static var_flavour_T
23158var_flavour(varname)
23159 char_u *varname;
23160{
23161 char_u *p = varname;
23162
23163 if (ASCII_ISUPPER(*p))
23164 {
23165 while (*(++p))
23166 if (ASCII_ISLOWER(*p))
23167 return VAR_FLAVOUR_SESSION;
23168 return VAR_FLAVOUR_VIMINFO;
23169 }
23170 else
23171 return VAR_FLAVOUR_DEFAULT;
23172}
23173#endif
23174
23175#if defined(FEAT_VIMINFO) || defined(PROTO)
23176/*
23177 * Restore global vars that start with a capital from the viminfo file
23178 */
23179 int
23180read_viminfo_varlist(virp, writing)
23181 vir_T *virp;
23182 int writing;
23183{
23184 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023185 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023186 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187
23188 if (!writing && (find_viminfo_parameter('!') != NULL))
23189 {
23190 tab = vim_strchr(virp->vir_line + 1, '\t');
23191 if (tab != NULL)
23192 {
23193 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023194 switch (*tab)
23195 {
23196 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023197#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023198 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023199#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023200 case 'D': type = VAR_DICT; break;
23201 case 'L': type = VAR_LIST; break;
23202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203
23204 tab = vim_strchr(tab, '\t');
23205 if (tab != NULL)
23206 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023207 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023208 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023209 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023211#ifdef FEAT_FLOAT
23212 else if (type == VAR_FLOAT)
23213 (void)string2float(tab + 1, &tv.vval.v_float);
23214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023216 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023217 if (type == VAR_DICT || type == VAR_LIST)
23218 {
23219 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23220
23221 if (etv == NULL)
23222 /* Failed to parse back the dict or list, use it as a
23223 * string. */
23224 tv.v_type = VAR_STRING;
23225 else
23226 {
23227 vim_free(tv.vval.v_string);
23228 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023229 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023230 }
23231 }
23232
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023233 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023234
23235 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023236 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023237 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23238 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023239 }
23240 }
23241 }
23242
23243 return viminfo_readline(virp);
23244}
23245
23246/*
23247 * Write global vars that start with a capital to the viminfo file
23248 */
23249 void
23250write_viminfo_varlist(fp)
23251 FILE *fp;
23252{
Bram Moolenaar33570922005-01-25 22:26:29 +000023253 hashitem_T *hi;
23254 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023255 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023256 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023257 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023258 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023259 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260
23261 if (find_viminfo_parameter('!') == NULL)
23262 return;
23263
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023264 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023265
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023266 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023267 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023269 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023271 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023272 this_var = HI2DI(hi);
23273 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023274 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023275 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023276 {
23277 case VAR_STRING: s = "STR"; break;
23278 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023279#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023280 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023281#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023282 case VAR_DICT: s = "DIC"; break;
23283 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023284 default: continue;
23285 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023286 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023287 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023288 if (p != NULL)
23289 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023290 vim_free(tofree);
23291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 }
23293 }
23294}
23295#endif
23296
23297#if defined(FEAT_SESSION) || defined(PROTO)
23298 int
23299store_session_globals(fd)
23300 FILE *fd;
23301{
Bram Moolenaar33570922005-01-25 22:26:29 +000023302 hashitem_T *hi;
23303 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023304 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 char_u *p, *t;
23306
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023307 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023308 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023310 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023312 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023313 this_var = HI2DI(hi);
23314 if ((this_var->di_tv.v_type == VAR_NUMBER
23315 || this_var->di_tv.v_type == VAR_STRING)
23316 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023317 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023318 /* Escape special characters with a backslash. Turn a LF and
23319 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023320 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023321 (char_u *)"\\\"\n\r");
23322 if (p == NULL) /* out of memory */
23323 break;
23324 for (t = p; *t != NUL; ++t)
23325 if (*t == '\n')
23326 *t = 'n';
23327 else if (*t == '\r')
23328 *t = 'r';
23329 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023330 this_var->di_key,
23331 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23332 : ' ',
23333 p,
23334 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23335 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023336 || put_eol(fd) == FAIL)
23337 {
23338 vim_free(p);
23339 return FAIL;
23340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341 vim_free(p);
23342 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023343#ifdef FEAT_FLOAT
23344 else if (this_var->di_tv.v_type == VAR_FLOAT
23345 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23346 {
23347 float_T f = this_var->di_tv.vval.v_float;
23348 int sign = ' ';
23349
23350 if (f < 0)
23351 {
23352 f = -f;
23353 sign = '-';
23354 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023355 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023356 this_var->di_key, sign, f) < 0)
23357 || put_eol(fd) == FAIL)
23358 return FAIL;
23359 }
23360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 }
23362 }
23363 return OK;
23364}
23365#endif
23366
Bram Moolenaar661b1822005-07-28 22:36:45 +000023367/*
23368 * Display script name where an item was last set.
23369 * Should only be invoked when 'verbose' is non-zero.
23370 */
23371 void
23372last_set_msg(scriptID)
23373 scid_T scriptID;
23374{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023375 char_u *p;
23376
Bram Moolenaar661b1822005-07-28 22:36:45 +000023377 if (scriptID != 0)
23378 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023379 p = home_replace_save(NULL, get_scriptname(scriptID));
23380 if (p != NULL)
23381 {
23382 verbose_enter();
23383 MSG_PUTS(_("\n\tLast set from "));
23384 MSG_PUTS(p);
23385 vim_free(p);
23386 verbose_leave();
23387 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023388 }
23389}
23390
Bram Moolenaard812df62008-11-09 12:46:09 +000023391/*
23392 * List v:oldfiles in a nice way.
23393 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023394 void
23395ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023396 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023397{
23398 list_T *l = vimvars[VV_OLDFILES].vv_list;
23399 listitem_T *li;
23400 int nr = 0;
23401
23402 if (l == NULL)
23403 msg((char_u *)_("No old files"));
23404 else
23405 {
23406 msg_start();
23407 msg_scroll = TRUE;
23408 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23409 {
23410 msg_outnum((long)++nr);
23411 MSG_PUTS(": ");
23412 msg_outtrans(get_tv_string(&li->li_tv));
23413 msg_putchar('\n');
23414 out_flush(); /* output one line at a time */
23415 ui_breakcheck();
23416 }
23417 /* Assume "got_int" was set to truncate the listing. */
23418 got_int = FALSE;
23419
23420#ifdef FEAT_BROWSE_CMD
23421 if (cmdmod.browse)
23422 {
23423 quit_more = FALSE;
23424 nr = prompt_for_number(FALSE);
23425 msg_starthere();
23426 if (nr > 0)
23427 {
23428 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23429 (long)nr);
23430
23431 if (p != NULL)
23432 {
23433 p = expand_env_save(p);
23434 eap->arg = p;
23435 eap->cmdidx = CMD_edit;
23436 cmdmod.browse = FALSE;
23437 do_exedit(eap, NULL);
23438 vim_free(p);
23439 }
23440 }
23441 }
23442#endif
23443 }
23444}
23445
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446#endif /* FEAT_EVAL */
23447
Bram Moolenaar071d4272004-06-13 20:20:40 +000023448
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023449#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450
23451#ifdef WIN3264
23452/*
23453 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23454 */
23455static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23456static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23457static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23458
23459/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023460 * Get the short path (8.3) for the filename in "fnamep".
23461 * Only works for a valid file name.
23462 * When the path gets longer "fnamep" is changed and the allocated buffer
23463 * is put in "bufp".
23464 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23465 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023466 */
23467 static int
23468get_short_pathname(fnamep, bufp, fnamelen)
23469 char_u **fnamep;
23470 char_u **bufp;
23471 int *fnamelen;
23472{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023473 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 char_u *newbuf;
23475
23476 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 l = GetShortPathName(*fnamep, *fnamep, len);
23478 if (l > len - 1)
23479 {
23480 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023481 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 newbuf = vim_strnsave(*fnamep, l);
23483 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023484 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485
23486 vim_free(*bufp);
23487 *fnamep = *bufp = newbuf;
23488
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023489 /* Really should always succeed, as the buffer is big enough. */
23490 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491 }
23492
23493 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023494 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495}
23496
23497/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023498 * Get the short path (8.3) for the filename in "fname". The converted
23499 * path is returned in "bufp".
23500 *
23501 * Some of the directories specified in "fname" may not exist. This function
23502 * will shorten the existing directories at the beginning of the path and then
23503 * append the remaining non-existing path.
23504 *
23505 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023506 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023507 * bufp - Pointer to an allocated buffer for the filename.
23508 * fnamelen - Length of the filename pointed to by fname
23509 *
23510 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511 */
23512 static int
23513shortpath_for_invalid_fname(fname, bufp, fnamelen)
23514 char_u **fname;
23515 char_u **bufp;
23516 int *fnamelen;
23517{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023518 char_u *short_fname, *save_fname, *pbuf_unused;
23519 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023520 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023521 int old_len, len;
23522 int new_len, sfx_len;
23523 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524
23525 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023526 old_len = *fnamelen;
23527 save_fname = vim_strnsave(*fname, old_len);
23528 pbuf_unused = NULL;
23529 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023531 endp = save_fname + old_len - 1; /* Find the end of the copy */
23532 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023534 /*
23535 * Try shortening the supplied path till it succeeds by removing one
23536 * directory at a time from the tail of the path.
23537 */
23538 len = 0;
23539 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023541 /* go back one path-separator */
23542 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23543 --endp;
23544 if (endp <= save_fname)
23545 break; /* processed the complete path */
23546
23547 /*
23548 * Replace the path separator with a NUL and try to shorten the
23549 * resulting path.
23550 */
23551 ch = *endp;
23552 *endp = 0;
23553 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023554 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023555 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23556 {
23557 retval = FAIL;
23558 goto theend;
23559 }
23560 *endp = ch; /* preserve the string */
23561
23562 if (len > 0)
23563 break; /* successfully shortened the path */
23564
23565 /* failed to shorten the path. Skip the path separator */
23566 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 }
23568
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023569 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023570 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023571 /*
23572 * Succeeded in shortening the path. Now concatenate the shortened
23573 * path with the remaining path at the tail.
23574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023576 /* Compute the length of the new path. */
23577 sfx_len = (int)(save_endp - endp) + 1;
23578 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023579
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023580 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023582 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023584 /* There is not enough space in the currently allocated string,
23585 * copy it to a buffer big enough. */
23586 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023588 {
23589 retval = FAIL;
23590 goto theend;
23591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592 }
23593 else
23594 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023595 /* Transfer short_fname to the main buffer (it's big enough),
23596 * unless get_short_pathname() did its work in-place. */
23597 *fname = *bufp = save_fname;
23598 if (short_fname != save_fname)
23599 vim_strncpy(save_fname, short_fname, len);
23600 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023602
23603 /* concat the not-shortened part of the path */
23604 vim_strncpy(*fname + len, endp, sfx_len);
23605 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023607
23608theend:
23609 vim_free(pbuf_unused);
23610 vim_free(save_fname);
23611
23612 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613}
23614
23615/*
23616 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023617 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 */
23619 static int
23620shortpath_for_partial(fnamep, bufp, fnamelen)
23621 char_u **fnamep;
23622 char_u **bufp;
23623 int *fnamelen;
23624{
23625 int sepcount, len, tflen;
23626 char_u *p;
23627 char_u *pbuf, *tfname;
23628 int hasTilde;
23629
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023630 /* Count up the path separators from the RHS.. so we know which part
23631 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023633 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 if (vim_ispathsep(*p))
23635 ++sepcount;
23636
23637 /* Need full path first (use expand_env() to remove a "~/") */
23638 hasTilde = (**fnamep == '~');
23639 if (hasTilde)
23640 pbuf = tfname = expand_env_save(*fnamep);
23641 else
23642 pbuf = tfname = FullName_save(*fnamep, FALSE);
23643
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023644 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023646 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23647 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648
23649 if (len == 0)
23650 {
23651 /* Don't have a valid filename, so shorten the rest of the
23652 * path if we can. This CAN give us invalid 8.3 filenames, but
23653 * there's not a lot of point in guessing what it might be.
23654 */
23655 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023656 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23657 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 }
23659
23660 /* Count the paths backward to find the beginning of the desired string. */
23661 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023662 {
23663#ifdef FEAT_MBYTE
23664 if (has_mbyte)
23665 p -= mb_head_off(tfname, p);
23666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 if (vim_ispathsep(*p))
23668 {
23669 if (sepcount == 0 || (hasTilde && sepcount == 1))
23670 break;
23671 else
23672 sepcount --;
23673 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 if (hasTilde)
23676 {
23677 --p;
23678 if (p >= tfname)
23679 *p = '~';
23680 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023681 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023682 }
23683 else
23684 ++p;
23685
23686 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23687 vim_free(*bufp);
23688 *fnamelen = (int)STRLEN(p);
23689 *bufp = pbuf;
23690 *fnamep = p;
23691
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023692 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693}
23694#endif /* WIN3264 */
23695
23696/*
23697 * Adjust a filename, according to a string of modifiers.
23698 * *fnamep must be NUL terminated when called. When returning, the length is
23699 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023700 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023701 * When there is an error, *fnamep is set to NULL.
23702 */
23703 int
23704modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23705 char_u *src; /* string with modifiers */
23706 int *usedlen; /* characters after src that are used */
23707 char_u **fnamep; /* file name so far */
23708 char_u **bufp; /* buffer for allocated file name or NULL */
23709 int *fnamelen; /* length of fnamep */
23710{
23711 int valid = 0;
23712 char_u *tail;
23713 char_u *s, *p, *pbuf;
23714 char_u dirname[MAXPATHL];
23715 int c;
23716 int has_fullname = 0;
23717#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023718 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719 int has_shortname = 0;
23720#endif
23721
23722repeat:
23723 /* ":p" - full path/file_name */
23724 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23725 {
23726 has_fullname = 1;
23727
23728 valid |= VALID_PATH;
23729 *usedlen += 2;
23730
23731 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23732 if ((*fnamep)[0] == '~'
23733#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23734 && ((*fnamep)[1] == '/'
23735# ifdef BACKSLASH_IN_FILENAME
23736 || (*fnamep)[1] == '\\'
23737# endif
23738 || (*fnamep)[1] == NUL)
23739
23740#endif
23741 )
23742 {
23743 *fnamep = expand_env_save(*fnamep);
23744 vim_free(*bufp); /* free any allocated file name */
23745 *bufp = *fnamep;
23746 if (*fnamep == NULL)
23747 return -1;
23748 }
23749
23750 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023751 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023752 {
23753 if (vim_ispathsep(*p)
23754 && p[1] == '.'
23755 && (p[2] == NUL
23756 || vim_ispathsep(p[2])
23757 || (p[2] == '.'
23758 && (p[3] == NUL || vim_ispathsep(p[3])))))
23759 break;
23760 }
23761
23762 /* FullName_save() is slow, don't use it when not needed. */
23763 if (*p != NUL || !vim_isAbsName(*fnamep))
23764 {
23765 *fnamep = FullName_save(*fnamep, *p != NUL);
23766 vim_free(*bufp); /* free any allocated file name */
23767 *bufp = *fnamep;
23768 if (*fnamep == NULL)
23769 return -1;
23770 }
23771
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023772#ifdef WIN3264
23773# if _WIN32_WINNT >= 0x0500
23774 if (vim_strchr(*fnamep, '~') != NULL)
23775 {
23776 /* Expand 8.3 filename to full path. Needed to make sure the same
23777 * file does not have two different names.
23778 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23779 p = alloc(_MAX_PATH + 1);
23780 if (p != NULL)
23781 {
23782 if (GetLongPathName(*fnamep, p, MAXPATHL))
23783 {
23784 vim_free(*bufp);
23785 *bufp = *fnamep = p;
23786 }
23787 else
23788 vim_free(p);
23789 }
23790 }
23791# endif
23792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793 /* Append a path separator to a directory. */
23794 if (mch_isdir(*fnamep))
23795 {
23796 /* Make room for one or two extra characters. */
23797 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23798 vim_free(*bufp); /* free any allocated file name */
23799 *bufp = *fnamep;
23800 if (*fnamep == NULL)
23801 return -1;
23802 add_pathsep(*fnamep);
23803 }
23804 }
23805
23806 /* ":." - path relative to the current directory */
23807 /* ":~" - path relative to the home directory */
23808 /* ":8" - shortname path - postponed till after */
23809 while (src[*usedlen] == ':'
23810 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23811 {
23812 *usedlen += 2;
23813 if (c == '8')
23814 {
23815#ifdef WIN3264
23816 has_shortname = 1; /* Postpone this. */
23817#endif
23818 continue;
23819 }
23820 pbuf = NULL;
23821 /* Need full path first (use expand_env() to remove a "~/") */
23822 if (!has_fullname)
23823 {
23824 if (c == '.' && **fnamep == '~')
23825 p = pbuf = expand_env_save(*fnamep);
23826 else
23827 p = pbuf = FullName_save(*fnamep, FALSE);
23828 }
23829 else
23830 p = *fnamep;
23831
23832 has_fullname = 0;
23833
23834 if (p != NULL)
23835 {
23836 if (c == '.')
23837 {
23838 mch_dirname(dirname, MAXPATHL);
23839 s = shorten_fname(p, dirname);
23840 if (s != NULL)
23841 {
23842 *fnamep = s;
23843 if (pbuf != NULL)
23844 {
23845 vim_free(*bufp); /* free any allocated file name */
23846 *bufp = pbuf;
23847 pbuf = NULL;
23848 }
23849 }
23850 }
23851 else
23852 {
23853 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23854 /* Only replace it when it starts with '~' */
23855 if (*dirname == '~')
23856 {
23857 s = vim_strsave(dirname);
23858 if (s != NULL)
23859 {
23860 *fnamep = s;
23861 vim_free(*bufp);
23862 *bufp = s;
23863 }
23864 }
23865 }
23866 vim_free(pbuf);
23867 }
23868 }
23869
23870 tail = gettail(*fnamep);
23871 *fnamelen = (int)STRLEN(*fnamep);
23872
23873 /* ":h" - head, remove "/file_name", can be repeated */
23874 /* Don't remove the first "/" or "c:\" */
23875 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23876 {
23877 valid |= VALID_HEAD;
23878 *usedlen += 2;
23879 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023880 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023881 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882 *fnamelen = (int)(tail - *fnamep);
23883#ifdef VMS
23884 if (*fnamelen > 0)
23885 *fnamelen += 1; /* the path separator is part of the path */
23886#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023887 if (*fnamelen == 0)
23888 {
23889 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23890 p = vim_strsave((char_u *)".");
23891 if (p == NULL)
23892 return -1;
23893 vim_free(*bufp);
23894 *bufp = *fnamep = tail = p;
23895 *fnamelen = 1;
23896 }
23897 else
23898 {
23899 while (tail > s && !after_pathsep(s, tail))
23900 mb_ptr_back(*fnamep, tail);
23901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902 }
23903
23904 /* ":8" - shortname */
23905 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23906 {
23907 *usedlen += 2;
23908#ifdef WIN3264
23909 has_shortname = 1;
23910#endif
23911 }
23912
23913#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023914 /*
23915 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916 */
23917 if (has_shortname)
23918 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023919 /* Copy the string if it is shortened by :h and when it wasn't copied
23920 * yet, because we are going to change it in place. Avoids changing
23921 * the buffer name for "%:8". */
23922 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 {
23924 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023925 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 return -1;
23927 vim_free(*bufp);
23928 *bufp = *fnamep = p;
23929 }
23930
23931 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023932 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933 if (!has_fullname && !vim_isAbsName(*fnamep))
23934 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023935 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023936 return -1;
23937 }
23938 else
23939 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023940 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941
Bram Moolenaardc935552011-08-17 15:23:23 +020023942 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023944 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023945 return -1;
23946
23947 if (l == 0)
23948 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023949 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023950 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023951 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 return -1;
23953 }
23954 *fnamelen = l;
23955 }
23956 }
23957#endif /* WIN3264 */
23958
23959 /* ":t" - tail, just the basename */
23960 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23961 {
23962 *usedlen += 2;
23963 *fnamelen -= (int)(tail - *fnamep);
23964 *fnamep = tail;
23965 }
23966
23967 /* ":e" - extension, can be repeated */
23968 /* ":r" - root, without extension, can be repeated */
23969 while (src[*usedlen] == ':'
23970 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23971 {
23972 /* find a '.' in the tail:
23973 * - for second :e: before the current fname
23974 * - otherwise: The last '.'
23975 */
23976 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23977 s = *fnamep - 2;
23978 else
23979 s = *fnamep + *fnamelen - 1;
23980 for ( ; s > tail; --s)
23981 if (s[0] == '.')
23982 break;
23983 if (src[*usedlen + 1] == 'e') /* :e */
23984 {
23985 if (s > tail)
23986 {
23987 *fnamelen += (int)(*fnamep - (s + 1));
23988 *fnamep = s + 1;
23989#ifdef VMS
23990 /* cut version from the extension */
23991 s = *fnamep + *fnamelen - 1;
23992 for ( ; s > *fnamep; --s)
23993 if (s[0] == ';')
23994 break;
23995 if (s > *fnamep)
23996 *fnamelen = s - *fnamep;
23997#endif
23998 }
23999 else if (*fnamep <= tail)
24000 *fnamelen = 0;
24001 }
24002 else /* :r */
24003 {
24004 if (s > tail) /* remove one extension */
24005 *fnamelen = (int)(s - *fnamep);
24006 }
24007 *usedlen += 2;
24008 }
24009
24010 /* ":s?pat?foo?" - substitute */
24011 /* ":gs?pat?foo?" - global substitute */
24012 if (src[*usedlen] == ':'
24013 && (src[*usedlen + 1] == 's'
24014 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24015 {
24016 char_u *str;
24017 char_u *pat;
24018 char_u *sub;
24019 int sep;
24020 char_u *flags;
24021 int didit = FALSE;
24022
24023 flags = (char_u *)"";
24024 s = src + *usedlen + 2;
24025 if (src[*usedlen + 1] == 'g')
24026 {
24027 flags = (char_u *)"g";
24028 ++s;
24029 }
24030
24031 sep = *s++;
24032 if (sep)
24033 {
24034 /* find end of pattern */
24035 p = vim_strchr(s, sep);
24036 if (p != NULL)
24037 {
24038 pat = vim_strnsave(s, (int)(p - s));
24039 if (pat != NULL)
24040 {
24041 s = p + 1;
24042 /* find end of substitution */
24043 p = vim_strchr(s, sep);
24044 if (p != NULL)
24045 {
24046 sub = vim_strnsave(s, (int)(p - s));
24047 str = vim_strnsave(*fnamep, *fnamelen);
24048 if (sub != NULL && str != NULL)
24049 {
24050 *usedlen = (int)(p + 1 - src);
24051 s = do_string_sub(str, pat, sub, flags);
24052 if (s != NULL)
24053 {
24054 *fnamep = s;
24055 *fnamelen = (int)STRLEN(s);
24056 vim_free(*bufp);
24057 *bufp = s;
24058 didit = TRUE;
24059 }
24060 }
24061 vim_free(sub);
24062 vim_free(str);
24063 }
24064 vim_free(pat);
24065 }
24066 }
24067 /* after using ":s", repeat all the modifiers */
24068 if (didit)
24069 goto repeat;
24070 }
24071 }
24072
24073 return valid;
24074}
24075
24076/*
24077 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24078 * "flags" can be "g" to do a global substitute.
24079 * Returns an allocated string, NULL for error.
24080 */
24081 char_u *
24082do_string_sub(str, pat, sub, flags)
24083 char_u *str;
24084 char_u *pat;
24085 char_u *sub;
24086 char_u *flags;
24087{
24088 int sublen;
24089 regmatch_T regmatch;
24090 int i;
24091 int do_all;
24092 char_u *tail;
24093 garray_T ga;
24094 char_u *ret;
24095 char_u *save_cpo;
24096
24097 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24098 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024099 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024100
24101 ga_init2(&ga, 1, 200);
24102
24103 do_all = (flags[0] == 'g');
24104
24105 regmatch.rm_ic = p_ic;
24106 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24107 if (regmatch.regprog != NULL)
24108 {
24109 tail = str;
24110 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24111 {
24112 /*
24113 * Get some space for a temporary buffer to do the substitution
24114 * into. It will contain:
24115 * - The text up to where the match is.
24116 * - The substituted text.
24117 * - The text after the match.
24118 */
24119 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24120 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24121 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24122 {
24123 ga_clear(&ga);
24124 break;
24125 }
24126
24127 /* copy the text up to where the match is */
24128 i = (int)(regmatch.startp[0] - tail);
24129 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24130 /* add the substituted text */
24131 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24132 + ga.ga_len + i, TRUE, TRUE, FALSE);
24133 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134 /* avoid getting stuck on a match with an empty string */
24135 if (tail == regmatch.endp[0])
24136 {
24137 if (*tail == NUL)
24138 break;
24139 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24140 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141 }
24142 else
24143 {
24144 tail = regmatch.endp[0];
24145 if (*tail == NUL)
24146 break;
24147 }
24148 if (!do_all)
24149 break;
24150 }
24151
24152 if (ga.ga_data != NULL)
24153 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24154
24155 vim_free(regmatch.regprog);
24156 }
24157
24158 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24159 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024160 if (p_cpo == empty_option)
24161 p_cpo = save_cpo;
24162 else
24163 /* Darn, evaluating {sub} expression changed the value. */
24164 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165
24166 return ret;
24167}
24168
24169#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */